-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtestWebsocket.js
More file actions
109 lines (98 loc) · 3.49 KB
/
testWebsocket.js
File metadata and controls
109 lines (98 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const WebSocket = require('ws');
const axios = require('axios');
const testWebsocket = async ({enable_ssl, backend_host, frontend_host, login, password}, callback) => {
let userId = 3;
let spySocket = null;
const ws_proto = enable_ssl ? 'wss' : 'ws';
const web_proto = enable_ssl ? 'https' : 'http';
const backend_url = `${ws_proto}://${backend_host}`;
const frontend_url = frontend_host ? `${web_proto}://${frontend_host}` : null;
const options = {
headers: {
'Origin': frontend_url || `${web_proto}://${backend_host}`,
}
};
// if login and password, try to get token from post to backend_url/auth/login
let token = null;
if (login && password) {
try {
const response = await axios.post(`${web_proto}://${backend_host}/auth/login`, {
email: login,
password: password
}, options);
token = response.data.token;
console.log("Token received:", token);
} catch (error) {
console.error("Error getting token:", error);
callback({ type: "log", message: "Error getting token" });
return;
}
}
/* */
try {
if (token) {
spySocket = new WebSocket(`${backend_url}/socket.io/?token=${token}&EIO=4&transport=websocket`, options);
} else {
spySocket = new WebSocket(`${backend_url}/socket.io/?userId=${userId}&EIO=4&transport=websocket`, options);
}
} catch (error) {
console.error("Error creating WebSocket:", error);
callback({ type: "log", message: "Error creating WebSocket" });
return;
}
spySocket.onerror = function(event) {
console.error("WebSocket error observed:", event);
callback({ type: "log", message: `WebSocket error: ${event.message}` });
};
spySocket.onclose = function(event) {
console.error("WebSocket connection closed:", event);
callback({ type: "log", message: "WebSocket connection closed" });
};
callback("Initialized");
spySocket.onmessage = function(event) {
callback({ type: "rawevent", event: event.data });
if (event.data.startsWith("0{")) {
setTimeout(() => {
callback({ type: "log", message: "Sending handshake"});
spySocket.send("40");
callback({ type: "log", message: "Connecting to namespaces from 1 to 1024"});
for (let n = 1; n <= 1024; n++) {
spySocket.send(`40/${n},`);
}
}, 1000);
} else if (event.data === "2") {
spySocket.send("3");
} else if (event.data.startsWith("40{")) {
callback({ type: "log", message: "Received handshake"});
setTimeout(() => {
callback({ type: "log", message: "Joining notifications channel"});
spySocket.send('42["joinNotification"]');
}, 2000);
} else if (event.data.startsWith("42")) {
console.log(event.data);
const rawData = event.data.substr(event.data.indexOf('[')).replace("\n","");
let data = JSON.parse(rawData);
console.log(data);
if (data[0].endsWith('appMessage')) {
if (data[1]?.message?.body) {
callback({
type: "message",
from: data[1].message.fromMe ? "Me" : data[1].message.remoteJid,
body: data[1].message.body
});
}
} else if (data[0].endsWith('contact')) {
if (data[1]?.contact?.name) {
callback({
type: "contact",
name: data[1].contact.name,
number: data[1].contact.number
});
}
} else {
callback({ type: "rawdata", data });
}
}
}
}
exports.testWebsocket = testWebsocket;