-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
198 lines (173 loc) · 6.44 KB
/
server.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
require('dotenv').config();
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: process.env.PORT || 8080,
});
const pingMessage = JSON.stringify({
type: 'ping',
});
const sendCommand = (sessionID, command) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === sessionID && (client.role === 'remote' || client.role === 'scanner')) {
client.send(JSON.stringify({
type: 'command',
data: command
}));
}
});
};
const sendMessage = (sessionID, type, data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === sessionID && (client.role === 'remote' || client.role === 'listener') ) {
client.send(JSON.stringify({
type: type,
data: data,
}));
}
});
};
const pingInterval = setInterval(() => {
console.log(`active clients: ${wss.clients.size}`);
wss.clients.forEach((client) => {
if (client.isAlive === false) {
console.log(`terminating ${client.sessionID}`);
return client.terminate();
}
client.isAlive = false;
client.send(pingMessage);
});
}, 30000);
wss.on('connection', (ws) => {
ws.isAlive = true;
ws.settings = {};
ws.on('message', (rawMessage) => {
const message = JSON.parse(rawMessage);
if(message.type === 'pong'){
ws.isAlive = true;
return true;
}
if(message?.type !== 'debug'){
console.log(message);
}
if(message.type === 'connect'){
ws.sessionID = message.sessionID;
if (typeof message.role !== 'undefined') {
ws.role = message.role;
} else {
ws.role = 'remote';
}
if (message.role === 'listener') {
let values = false;
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'scanner' ) {
values = client.settings;
}
});
if (values) {
ws.send(JSON.stringify({
type: 'scannerValues',
data: values
}));
}
sendCommand(message.sessionID, 'log-history');
}
return true;
}
if(message.type === 'command'){
if (ws.role !== 'remote' && message.password != process.env.WS_PASSWORD) {
sendMessage(ws.sessionID, 'debug', 'Access denied');
return false;
}
sendCommand(message.sessionID, message.data);
return true;
}
if(message.type === 'debug'){
sendMessage(message.sessionID, 'debug', message.data);
return true;
}
if (message.type === 'setValues') {
let values = false;
if (ws.role === 'scanner') {
ws.settings = {
...ws.settings,
...message.data
};
values = ws.settings;
} else {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'scanner' ) {
client.settings = {
...client.settings,
...message.data
};
values = client.settings;
}
});
}
if (!values) {
console.log(`Could not find ${message.sessionID} scanner to set values`);
return true;
}
console.log(`${ws.sessionID} (${ws.role}): set values ${JSON.stringify(message.data, null, 4)}`);
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'listener' ) {
client.send(JSON.stringify({
type: 'scannerValues',
data: values
}));
console.log(`sent scannerValues message to ${message.sessionID} listener`);
}
});
return true;
}
if (message.type === 'getValue') {
let theValue = false;
if (ws.role === 'scanner') {
theValue = ws.settings[message.data.name];
} else {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'scanner' ) {
theValue = client.settings[message.data.name];
}
});
}
ws.send(JSON.stringify({
type: 'scannerValue',
data: theValue,
}));
console.log(`${ws.sessionID} (${ws.role}): sent ${message.data.name} value ${theValue}`);
return true;
}
if (message.type === 'getValues') {
let theValues = false;
if (ws.role === 'scanner') {
theValues = ws.settings;
} else {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'scanner' ) {
theValues = client.settings;
}
});
}
ws.send(JSON.stringify({
type: 'scannerValues',
data: theValues,
}));
console.log(`${ws.sessionID} (${ws.role}): sent values ${theValues}`);
return true;
}
if (message.type === 'logHistory') {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN && client.sessionID === message.sessionID && client.role === 'listener' ) {
client.send(JSON.stringify({
type: 'logHistory',
data: message.data
}));
}
});
}
});
});
wss.on('close', () => {
clearInterval(pingInterval);
});