-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathws_server.js
More file actions
424 lines (348 loc) · 13.4 KB
/
Copy pathws_server.js
File metadata and controls
424 lines (348 loc) · 13.4 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env node
const fs = require('fs');
const WebSocket = require('ws');
var https = require('https');
// setup websocket object
let WS = {}
WS.args = process.argv
WS.is_subprocess = true
WS.config_file = null
WS.show_help = false
WS.config = {
server_port: 25444,
server_ip: "0.0.0.0",
server_https: false,
https_cert: "/path/to/cert.pem",
https_key: "/path/to/key.pem",
remove_cert_key: false, // see start_as_root.sh for more info
message_json: true,
auth_attempt_limit: 5,
ban_interval:1800000,
max_rate_per_sec: 20,
ban_on_rate_limit: true
}
WS.clients = {}
WS.new_client_id = 0
WS.banned = { list:[], info:{} }
WS.server = null
WS.loadComfig = function (){
if (fs.existsSync(WS.config_file)) {
console.log('WS: Loading config file.');
WS.config = JSON.parse( fs.readFileSync(WS.config_file , 'utf8') )
}
}
WS.showHelp = function () {
console.table(WS.help.cli_options);
//console.table(WS.help.config);
for (let item in WS.help.config) {
console.log(item );
console.log(WS.help.config[item]);
console.log("\n");
}
process.exit(0)
}
WS.init = function(){
// check if run as a sub-process
if (!process.send) {
WS.is_subprocess = false
// check for commandline args then start the server
WS.args.forEach((item, i) => {
// show help for command line
if (item === "--help" ) {
WS.show_help = true
}
if (item === "--config" ) {
WS.config_file = WS.args[i+1]
}
// custom port
if (item === "--port" ) {
WS.config.server_port = WS.args[i+1]
}
// show the config window
if (item === "--restrict" ) {
WS.config.server_ip = "127.0.0.1"
}
if (item === "--https" ) {
WS.config.server_https = true
}
});
if (WS.config_file !== null){
WS.loadComfig()
}
if (WS.show_help === true ) {
WS.showHelp()
} else {
WS.startServer()
}
} else {
// if run as a subprocess setup messaging and request config
process.on('message', (msg) => {
handle.parentMessage(msg)
});
// request the config from parent before starting ws server
process.send({type:"request_config"})
}
}
WS.help = {
config:{
server_port: "(Integer) defaults to 25444",
server_ip: "(string) defaults to 0.0.0.0 for network accesable,\n use 127.0.0.1 for localhost only",
server_https: "(booleen) defaults to false. if true the server will use https mode",
https_cert: "(string) absolute path to https certificate file",
https_key: "(string) absolute path to https key file",
remove_cert_key: "(booleen) defaults to false. \n If true the server(in https mode) will delete the certificate and key file after reading them in.\n see start_as_root.sh for more info",
message_json: "(booleen) defaults to true which will JSON.parse() each incoming message.\n Set this to false to leave the messages as the are",
auth_attempt_limit: "(Integer) defaults to 5,\n number of failed auth attempts before an ip address is added to banned list",
ban_interval:"(Integer) defaults to 1800000,\n length of time in milliseconds that an ip address will stay on the banned list",
max_rate_per_sec: "(Integer) defaults to 20,\n maximum messages per second from a clinet",
ban_on_rate_limit: "(booleen) defaults to true,\n ban clients who go over rate limit, if false just disconnect them"
},
cli_options:{
"--help":"Show this help",
"--config":"Specify a config file to use",
"--port":"Specify a port to use",
"--restrict":"Restrict connections to localhost only",
"--https":"Start the server in https mode",
}
}
WS.makeBannedId = function (ip) {
return ip.replace(/\./g ,"_").replace(/:/g ,"_")
}
WS.startServer = function () {
console.log("WS: server is starting", WS.config);
// determine type of server to start http/https
if (WS.config.server_https === true) {
console.log("WS: Server in https mode");
// https
const httpsServer = https.createServer({
cert: fs.readFileSync(WS.config.https_cert),
key: fs.readFileSync(WS.config.https_key)
});
httpsServer.listen({ port:WS.config.server_port , host:WS.config.server_ip }, function() {
console.log(`WS: Server is listening on wss://${WS.config.server_ip}:${WS.config.server_port}`);
});
WS.server = new WebSocket.Server({ server:httpsServer , maxPayload:128 * 1024 /* 128 KB*/ });
// remove the cert and key file if needed
if (WS.config.remove_cert_key === true){ // see start_as_root.sh for more info
if ( fs.existsSync( WS.config.https_cert ) ) {
console.log("deleting cert.pem");
fs.unlinkSync(WS.config.https_cert)
}
if ( fs.existsSync( WS.config.https_key ) ) {
console.log("deleting key.pem");
fs.unlinkSync( WS.config.https_key )
}
}
} else {
// http
console.log("WS: Server in http mode");
WS.server = new WebSocket.Server({ port:WS.config.server_port , host:WS.config.server_ip , maxPayload:128 * 1024 });
console.log(`WS: Server is listening on ws://${WS.config.server_ip}:${WS.config.server_port}` );
}
function noop() {}
function heartbeat() {
this.isAlive = true;
}
const zombieInterval = setInterval(function ping() {
if (WS.server !== null){
// check for no reponsive clients
WS.server.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
console.log("no heartbeat", ws.client_id)
ws.close();
} else {
ws.isAlive = false;
ws.ping(noop);
}
});
// check for banned list removals
let timenow = Date.now()
WS.banned.list.forEach((item, i) => {
let id = WS.makeBannedId(item)
if (WS.banned.info[id].bantime + WS.config.ban_interval < timenow && WS.banned.info[id].static === false) {
// remove from banlist
WS.banned.list.splice(i,1)
delete WS.banned.info[id]
}
});
}
}, 30000);
WS.server.shouldHandle = function(req) {
//console.log("shouldHandle CALLED", req.connection.remoteAddress);
if (WS.banned.list.includes(req.connection.remoteAddress)) {
console.log("WS: not handling banned client");
return false
}
if (this.options.path) {
const index = req.url.indexOf('?');
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
if (pathname !== this.options.path) return false;
}
return true;
}
// handle websocket server errors
WS.server.on('error', function error(err) {
clearInterval(zombieInterval);
handle.wsServerError(err)
});
// handle websocket server closing
WS.server.on('close', function close() {
clearInterval(zombieInterval);
handle.wsServerClose()
});
WS.server.on('connection', function connection(ws, req) {
ws.isAlive = true;
ws.connnectTime = Date.now()
ws.originIP = req.connection.remoteAddress
ws.rlBucket = { time:ws.connnectTime,count:0 }
ws.isAuthed = false
// give an id and setup client in WS.clients
ws.client_id = WS.new_client_id;
WS.clients[WS.new_client_id] = { ws_ref: ws , id : WS.new_client_id }
WS.new_client_id += 1;
ws.on('pong', heartbeat);
ws.on('error', function error(err) {
handle.wsClientError(ws.client_id, err)
});
ws.on('close', function close() {
// remove the client from ws
delete WS.clients[ws.client_id];
handle.wsClientClose(ws.client_id)
})
ws.on('message', function incoming(message) {
let auth_id
let packet = message
// check for rate limit
ws.rlBucket.count += 1
if (ws.rlBucket.count > WS.config.max_rate_per_sec) {
let timenow = Date.now()
let secs = Math.floor( (timenow - ws.rlBucket.time) / 1000 )
let avg = Math.floor( ws.rlBucket.count / secs )
console.log("rlout", timenow,secs,avg);
if ( avg > WS.config.max_rate_per_sec ) {
// limit exceded
console.log( "WS-SECURITY: Client has exceded rate limit");
if (WS.config.ban_on_rate_limit === true) {
auth_id = WS.makeBannedId(ws.originIP)
if (!WS.banned.info[auth_id]) { WS.banned.info[auth_id] = { bantime:null, attempts:[] , static:false} }
WS.banned.list.push(ws.originIP)
WS.banned.info[auth_id].bantime = Date.now()
}
ws.close()
} else {
//empty bucket
ws.rlBucket.time = timenow
ws.rlBucket.count = 0
}
}
try {
if (WS.config.message_json === true){
packet = JSON.parse( message )
}
// check for auth needed
if (ws.isAuthed === true){
handle.wsClientMessage(ws.client_id, packet)
} else {
// until a new client isAuthed all messages will go to clientAuthorize
auth_id = WS.makeBannedId(ws.originIP)
ws.isAuthed = handle.clientAuthorize(ws.client_id, packet)
if (ws.isAuthed !== true){
// failed auth attempt
if (!WS.banned.info[auth_id]) { WS.banned.info[auth_id] = { bantime:null, attempts:[] , static:false} }
WS.banned.info[auth_id].attempts.push({time:Date.now(), data:packet})
// check if time to ban
if ( WS.banned.info[auth_id].attempts.length > WS.config.auth_attempt_limit ) {
console.log( "WS-SECURITY: Client has been banned");
WS.banned.list.push(ws.originIP)
WS.banned.info[auth_id].bantime = Date.now()
ws.close()
}
} else {
console.log("WS client auth good");
}
}
} catch (e) {
console.log( "WS-Error: Client message invalid\n",e);
ws.close()
} finally {
// nothing to do here
}
})
})
// the server is ready for clients
if (WS.is_subprocess){
process.send({type:"websocket_ready"})
}
}
WS.stopServer = function () {
console.log("'WS: server is stopping", WS.config);
setTimeout(function(){
WS.server.close()
//process.exit();
},1000)
}
WS.sendToClient = function (id, packet) {
if ( WS.clients[id] ) {
WS.clients[id].ws_ref.send(JSON.stringify(packet))
}
}
WS.sendToAllClients = function (packet, checkAuth = true) {
for (let id in WS.clients) {
if (checkAuth === false || WS.clients[id].ws_ref.isAuthed === true) {
WS.clients[id].ws_ref.send(JSON.stringify(packet))
}
}
}
// all the functions in handle are intended to modified for your specific use case
let handle = {}
handle.wsServerError = function (err){
console.log("WS: Server error has occured",err);
process.exit()
}
handle.wsServerClose = function (){
console.log("WS: server has closed");
process.exit()
}
handle.wsClientMessage = function (client_id, packet){
console.log(`WS: Message from client_id ${ client_id }`, packet );
}
handle.wsClientClose = function (client_id){
console.log(`WS: Client disconnected: id ${ client_id } `);
}
handle.wsClientError = function (client_id, err){
console.log(`WS: Client id ${ client_id } error`, err);
}
handle.clientAuthorize = function (client_id, packet) {
console.log("WS: Verify client auth");
// verify auth acording to your own logic
// this function must return a booleen and should probobly be syncronous
// simple example
let key = "someSecretKey"
if (packet.key && packet.key === key) {
return true
} else {
return false
}
// to bypass auth just return true
//return true
}
handle.parentMessage = function (msg){
console.log('WS: Message from parent', msg);
if (msg.type === "config_info") {
//WS.config = msg.config
for (let item in msg.config ){
WS.config[item] = msg.config[item]
}
WS.startServer()
}
if (msg.type === "shutdown_server") {
WS.stopServer()
}
}
// call this when your code is ready for the server to start up
WS.init()
// below here just for testing
setTimeout(function(){
//WS.stopServer()
//process.exit();
},10000)