-
Notifications
You must be signed in to change notification settings - Fork 46
/
sockets.js
673 lines (598 loc) · 19.3 KB
/
sockets.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/**
* Connections
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Abstraction layer for multi-process SockJS connections.
*
* This file handles all the communications between the users'
* browsers, the networking processes, and users.js in the
* main process.
*
* @license MIT license
*/
'use strict';
const cluster = require('cluster');
const fs = require('fs');
if (cluster.isMaster) {
cluster.setupMaster({
exec: require('path').resolve(__dirname, 'sockets'),
});
/** @type {Map<number, cluster.Worker>} */
const workers = exports.workers = new Map();
const spawnWorker = exports.spawnWorker = function () {
let worker = cluster.fork({PSPORT: Config.port, PSBINDADDR: Config.bindaddress || '0.0.0.0', PSNOSSL: Config.ssl ? 0 : 1});
let id = worker.id;
workers.set(id, worker);
worker.on('message', data => {
// console.log('master received: ' + data);
switch (data.charAt(0)) {
case '*': {
// *socketid, ip, protocol
// connect
let nlPos = data.indexOf('\n');
let nlPos2 = data.indexOf('\n', nlPos + 1);
Users.socketConnect(worker, id, data.slice(1, nlPos), data.slice(nlPos + 1, nlPos2), data.slice(nlPos2 + 1));
break;
}
case '!': {
// !socketid
// disconnect
Users.socketDisconnect(worker, id, data.substr(1));
break;
}
case '<': {
// <socketid, message
// message
let nlPos = data.indexOf('\n');
Users.socketReceive(worker, id, data.substr(1, nlPos - 1), data.substr(nlPos + 1));
break;
}
default:
// unhandled
}
});
return worker;
};
cluster.on('exit', (worker, code, signal) => {
if (code === null && signal !== null) {
// Worker was killed by Sockets.killWorker or Sockets.killPid, probably.
console.log(`Worker ${worker.id} was forcibly killed with status ${signal}.`);
workers.delete(worker.id);
} else if (code === 0 && signal === null) {
// Happens when killing PS with ^C
console.log(`Worker ${worker.id} died, but returned a successful exit code.`);
workers.delete(worker.id);
} else if (code > 0) {
// Worker was killed abnormally, likely because of a crash.
require('./lib/crashlogger')(new Error(`Worker ${worker.id} abruptly died with code ${code} and signal ${signal}`), "The main process");
// Don't delete the worker so it can be inspected if need be.
}
if (worker.isConnected()) worker.disconnect();
// FIXME: this is a bad hack to get around a race condition in
// Connection#onDisconnect sending room deinit messages after already
// having removed the sockets from their channels.
// @ts-ignore
worker.send = () => {};
let count = 0;
Users.connections.forEach(connection => {
if (connection.worker === worker) {
Users.socketDisconnect(worker, worker.id, connection.socketid);
count++;
}
});
console.log(`${count} connections were lost.`);
// Attempt to recover.
spawnWorker();
});
/**
* @param {number} [port]
* @param {string} [bindAddress]
* @param {number} [workerCount]
*/
exports.listen = function (port, bindAddress, workerCount) {
if (port !== undefined && !isNaN(port)) {
Config.port = port;
Config.ssl = null;
} else {
port = Config.port;
// Autoconfigure when running in cloud environments.
try {
const cloudenv = require('cloud-env');
// @ts-ignore
bindAddress = cloudenv.get('IP', bindAddress);
// @ts-ignore
port = cloudenv.get('PORT', port);
} catch (e) {}
}
if (bindAddress !== undefined) {
Config.bindaddress = bindAddress;
}
if (port !== undefined) {
Config.port = port;
}
if (workerCount === undefined) {
workerCount = (Config.workers !== undefined ? Config.workers : 1);
}
// @ts-ignore - remove when Config is typed
for (let i = 0; i < workerCount; i++) {
spawnWorker();
}
};
/**
* @param {cluster.Worker} worker
*/
exports.killWorker = function (worker) {
let count = 0;
Users.connections.forEach(connection => {
if (connection.worker === worker) {
Users.socketDisconnect(worker, worker.id, connection.socketid);
count++;
}
});
console.log(`${count} connections were lost.`);
try {
worker.kill('SIGTERM');
} catch (e) {}
return count;
};
/**
* @param {number} pid
*/
exports.killPid = function (pid) {
for (const worker of workers.values()) {
if (pid === worker.process.pid) {
// @ts-ignore
return this.killWorker(worker);
}
}
return false;
};
/**
* @param {cluster.Worker} worker
* @param {number} socketid
* @param {string} message
*/
exports.socketSend = function (worker, socketid, message) {
worker.send(`>${socketid}\n${message}`);
};
/**
* @param {cluster.Worker} worker
* @param {number} socketid
*/
exports.socketDisconnect = function (worker, socketid) {
worker.send(`!${socketid}`);
};
/**
* @param {string} channelid
* @param {string} message
*/
exports.channelBroadcast = function (channelid, message) {
workers.forEach(worker => {
worker.send(`#${channelid}\n${message}`);
});
};
/**
* @param {cluster.Worker} worker
* @param {string} channelid
* @param {string} message
*/
exports.channelSend = function (worker, channelid, message) {
worker.send(`#${channelid}\n${message}`);
};
/**
* @param {cluster.Worker} worker
* @param {string} channelid
* @param {number} socketid
*/
exports.channelAdd = function (worker, channelid, socketid) {
worker.send(`+${channelid}\n${socketid}`);
};
/**
* @param {cluster.Worker} worker
* @param {string} channelid
* @param {number} socketid
*/
exports.channelRemove = function (worker, channelid, socketid) {
worker.send(`-${channelid}\n${socketid}`);
};
/**
* @param {string} channelid
* @param {string} message
*/
exports.subchannelBroadcast = function (channelid, message) {
workers.forEach(worker => {
worker.send(`:${channelid}\n${message}`);
});
};
/**
* @param {cluster.Worker} worker
* @param {string} channelid
* @param {string} subchannelid
* @param {number} socketid
*/
exports.subchannelMove = function (worker, channelid, subchannelid, socketid) {
worker.send(`.${channelid}\n${subchannelid}\n${socketid}`);
};
} else {
// is worker
// @ts-ignore This file doesn't exist on the repository, so Travis checks fail if this isn't ignored
global.Config = require('./config/config');
if (process.env.PSPORT) Config.port = +process.env.PSPORT;
if (process.env.PSBINDADDR) Config.bindaddress = process.env.PSBINDADDR;
if (process.env.PSNOSSL && parseInt(process.env.PSNOSSL)) Config.ssl = null;
if (Config.ofe) {
try {
require.resolve('node-oom-heapdump');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e; // should never happen
throw new Error(
'node-oom-heapdump is not installed, but it is a required dependency if Config.ofe is set to true! ' +
'Run npm install node-oom-heapdump and restart the server.'
);
}
// Create a heapdump if the process runs out of memory.
// @ts-ignore
require('node-oom-heapdump')({
addTimestamp: true,
});
}
// Static HTTP server
// This handles the custom CSS and custom avatar features, and also
// redirects yourserver:8001 to yourserver-8001.psim.us
// It's optional if you don't need these features.
global.Dnsbl = require('./dnsbl');
if (Config.crashguard) {
// graceful crash
process.on('uncaughtException', err => {
require('./lib/crashlogger')(err, `Socket process ${cluster.worker.id} (${process.pid})`, true);
});
}
let app = require('http').createServer();
/** @type {?import('https').Server} */
let appssl = null;
if (Config.ssl) {
let key;
try {
key = require('path').resolve(__dirname, Config.ssl.options.key);
if (!fs.lstatSync(key).isFile()) throw new Error();
try {
key = fs.readFileSync(key);
} catch (e) {
require('./lib/crashlogger')(new Error(`Failed to read the configured SSL private key PEM file:\n${e.stack}`), `Socket process ${cluster.worker.id} (${process.pid})`, true);
}
} catch (e) {
console.warn('SSL private key config values will not support HTTPS server option values in the future. Please set it to use the absolute path of its PEM file.');
key = Config.ssl.options.key;
}
let cert;
try {
cert = require('path').resolve(__dirname, Config.ssl.options.cert);
if (!fs.lstatSync(cert).isFile()) throw new Error();
try {
cert = fs.readFileSync(cert);
} catch (e) {
require('./lib/crashlogger')(new Error(`Failed to read the configured SSL certificate PEM file:\n${e.stack}`), `Socket process ${cluster.worker.id} (${process.pid})`, true);
}
} catch (e) {
console.warn('SSL certificate config values will not support HTTPS server option values in the future. Please set it to use the absolute path of its PEM file.');
cert = Config.ssl.options.cert;
}
if (key && cert) {
try {
// In case there are additional SSL config settings besides the key and cert...
appssl = require('https').createServer(Object.assign({}, Config.ssl.options, {key, cert}));
} catch (e) {
require('./lib/crashlogger')(`The SSL settings are misconfigured:\n${e.stack}`, `Socket process ${cluster.worker.id} (${process.pid})`, true);
}
}
}
// Static server
const StaticServer = require('node-static').Server;
const roomidRegex = /^\/(?:[A-Za-z0-9][A-Za-z0-9-]*)\/?$/;
const cssServer = new StaticServer('./config');
const avatarServer = new StaticServer('./config/avatars');
const staticServer = new StaticServer('./static');
/**
* @param {import('http').IncomingMessage} req
* @param {import('http').ServerResponse} res
*/
const staticRequestHandler = (req, res) => {
// console.log(`static rq: ${req.socket.remoteAddress}:${req.socket.remotePort} -> ${req.socket.localAddress}:${req.socket.localPort} - ${req.method} ${req.url} ${req.httpVersion} - ${req.rawHeaders.join('|')}`);
req.resume();
req.addListener('end', () => {
if (Config.customhttpresponse &&
Config.customhttpresponse(req, res)) {
return;
}
let server = staticServer;
if (req.url) {
if (req.url === '/custom.css') {
server = cssServer;
} else if (req.url.startsWith('/avatars/')) {
req.url = req.url.substr(8);
server = avatarServer;
} else if (roomidRegex.test(req.url)) {
req.url = '/';
}
}
server.serve(req, res, e => {
// @ts-ignore
if (e && e.status === 404) {
staticServer.serveFile('404.html', 404, {}, req, res);
}
});
});
};
app.on('request', staticRequestHandler);
if (appssl) appssl.on('request', staticRequestHandler);
// SockJS server
// This is the main server that handles users connecting to our server
// and doing things on our server.
const sockjs = require('sockjs');
const options = {
sockjs_url: "//play.pokemonshowdown.com/js/lib/sockjs-1.1.1-nwjsfix.min.js",
prefix: '/showdown',
/**
* @param {string} severity
* @param {string} message
*/
log(severity, message) {
if (severity === 'error') console.log(`ERROR: ${message}`);
},
};
if (Config.wsdeflate) {
try {
// @ts-ignore
const deflate = require('permessage-deflate').configure(Config.wsdeflate);
// @ts-ignore
options.faye_server_options = {extensions: [deflate]};
} catch (e) {
require('./lib/crashlogger')(new Error("Dependency permessage-deflate is not installed or is otherwise unaccessable. No message compression will take place until server restart."), "Sockets");
}
}
const server = sockjs.createServer(options);
/** @type {Map<string, import('sockjs').Connection>} */
const sockets = new Map();
/** @type {Map<string, Map<string, import('sockjs').Connection>>} */
const channels = new Map();
/** @type {Map<string, Map<string, string>>} */
const subchannels = new Map();
// Deal with phantom connections.
const sweepSocketInterval = setInterval(() => {
sockets.forEach(socket => {
// @ts-ignore
if (socket.protocol === 'xhr-streaming' && socket._session && socket._session.recv) {
// @ts-ignore
socket._session.recv.didClose();
}
// A ghost connection's `_session.to_tref._idlePrev` (and `_idleNext`) property is `null` while
// it is an object for normal users. Under normal circumstances, those properties should only be
// `null` when the timeout has already been called, but somehow it's not happening for some connections.
// Simply calling `_session.timeout_cb` (the function bound to the aformentioned timeout) manually
// on those connections kills those connections. For a bit of background, this timeout is the timeout
// that sockjs sets to wait for users to reconnect within that time to continue their session.
// @ts-ignore
if (socket._session && socket._session.to_tref && !socket._session.to_tref._idlePrev) {
// @ts-ignore
socket._session.timeout_cb();
}
});
}, 1000 * 60 * 10);
process.on('message', data => {
// console.log('worker received: ' + data);
/** @type {import('sockjs').Connection | undefined?} */
let socket = null;
let socketid = '';
/** @type {Map<string, import('sockjs').Connection> | undefined?} */
let channel = null;
let channelid = '';
/** @type {Map<string, string> | undefined?} */
let subchannel = null;
let subchannelid = '';
let nlLoc = -1;
let message = '';
switch (data.charAt(0)) {
case '$': // $code
eval(data.substr(1));
break;
case '!': // !socketid
// destroy
socketid = data.substr(1);
socket = sockets.get(socketid);
if (!socket) return;
socket.destroy();
sockets.delete(socketid);
channels.forEach(channel => channel.delete(socketid));
break;
case '>':
// >socketid, message
// message
nlLoc = data.indexOf('\n');
socketid = data.substr(1, nlLoc - 1);
socket = sockets.get(socketid);
if (!socket) return;
message = data.substr(nlLoc + 1);
socket.write(message);
break;
case '#':
// #channelid, message
// message to channel
nlLoc = data.indexOf('\n');
channelid = data.substr(1, nlLoc - 1);
channel = channels.get(channelid);
if (!channel) return;
message = data.substr(nlLoc + 1);
channel.forEach(socket => socket.write(message));
break;
case '+':
// +channelid, socketid
// add to channel
nlLoc = data.indexOf('\n');
socketid = data.substr(nlLoc + 1);
socket = sockets.get(socketid);
if (!socket) return;
channelid = data.substr(1, nlLoc - 1);
channel = channels.get(channelid);
if (!channel) {
channel = new Map();
channels.set(channelid, channel);
}
channel.set(socketid, socket);
break;
case '-':
// -channelid, socketid
// remove from channel
nlLoc = data.indexOf('\n');
channelid = data.slice(1, nlLoc);
channel = channels.get(channelid);
if (!channel) return;
socketid = data.slice(nlLoc + 1);
channel.delete(socketid);
subchannel = subchannels.get(channelid);
if (subchannel) subchannel.delete(socketid);
if (!channel.size) {
channels.delete(channelid);
if (subchannel) subchannels.delete(channelid);
}
break;
case '.':
// .channelid, subchannelid, socketid
// move subchannel
nlLoc = data.indexOf('\n');
channelid = data.slice(1, nlLoc);
let nlLoc2 = data.indexOf('\n', nlLoc + 1);
subchannelid = data.slice(nlLoc + 1, nlLoc2);
socketid = data.slice(nlLoc2 + 1);
subchannel = subchannels.get(channelid);
if (!subchannel) {
subchannel = new Map();
subchannels.set(channelid, subchannel);
}
if (subchannelid === '0') {
subchannel.delete(socketid);
} else {
subchannel.set(socketid, subchannelid);
}
break;
case ':':
// :channelid, message
// message to subchannel
nlLoc = data.indexOf('\n');
channelid = data.slice(1, nlLoc);
channel = channels.get(channelid);
if (!channel) return;
/** @type {[string?, string?, string?]} */
let messages = [null, null, null];
message = data.substr(nlLoc + 1);
subchannel = subchannels.get(channelid);
channel.forEach((socket, socketid) => {
switch (subchannel ? subchannel.get(socketid) : '0') {
case '1':
if (!messages[1]) {
messages[1] = message.replace(/\n\|split\n[^\n]*\n([^\n]*)\n[^\n]*\n[^\n]*/g, '\n$1').replace(/\n\n/g, '\n');
}
socket.write(messages[1]);
break;
case '2':
if (!messages[2]) {
messages[2] = message.replace(/\n\|split\n[^\n]*\n[^\n]*\n([^\n]*)\n[^\n]*/g, '\n$1').replace(/\n\n/g, '\n');
}
socket.write(messages[2]);
break;
default:
if (!messages[0]) {
messages[0] = message.replace(/\n\|split\n([^\n]*)\n[^\n]*\n[^\n]*\n[^\n]*/g, '\n$1').replace(/\n\n/g, '\n');
}
socket.write(messages[0]);
break;
}
});
break;
}
});
// Clean up any remaining connections on disconnect. If this isn't done,
// the process will not exit until any remaining connections have been destroyed.
// Afterwards, the worker process will die on its own.
process.once('disconnect', () => {
clearInterval(sweepSocketInterval);
sockets.forEach(socket => {
try {
socket.destroy();
} catch (e) {}
});
sockets.clear();
channels.clear();
subchannels.clear();
app.close();
if (appssl) appssl.close();
// Let the server(s) finish closing.
setImmediate(() => process.exit(0));
});
// this is global so it can be hotpatched if necessary
let isTrustedProxyIp = Dnsbl.checker(Config.proxyip);
let socketCounter = 0;
server.on('connection', socket => {
// For reasons that are not entirely clear, SockJS sometimes triggers
// this event with a null `socket` argument.
if (!socket) return;
if (!socket.remoteAddress) {
// SockJS sometimes fails to be able to cache the IP, port, and
// address from connection request headers.
try {
socket.destroy();
} catch (e) {}
return;
}
let socketid = '' + (++socketCounter);
sockets.set(socketid, socket);
let socketip = socket.remoteAddress;
if (isTrustedProxyIp(socketip)) {
let ips = (socket.headers['x-forwarded-for'] || '')
.split(',')
.reverse();
for (let ip of ips) {
let proxy = ip.trim();
if (!isTrustedProxyIp(proxy)) {
socketip = proxy;
break;
}
}
}
// @ts-ignore
process.send(`*${socketid}\n${socketip}\n${socket.protocol}`);
socket.on('data', message => {
// drop empty messages (DDoS?)
if (!message) return;
// drop messages over 100KB
if (message.length > (100 * 1024)) {
console.log(`Dropping client message ${message.length / 1024} KB...`);
console.log(message.slice(0, 160));
return;
}
// drop legacy JSON messages
if (typeof message !== 'string' || message.startsWith('{')) return;
// drop blank messages (DDoS?)
let pipeIndex = message.indexOf('|');
if (pipeIndex < 0 || pipeIndex === message.length - 1) return;
// @ts-ignore
process.send(`<${socketid}\n${message}`);
});
socket.once('close', () => {
// @ts-ignore
process.send(`!${socketid}`);
sockets.delete(socketid);
channels.forEach(channel => channel.delete(socketid));
});
});
server.installHandlers(app, {});
app.listen(Config.port, Config.bindaddress);
console.log(`Worker ${cluster.worker.id} now listening on ${Config.bindaddress}:${Config.port}`);
if (appssl) {
// @ts-ignore
server.installHandlers(appssl, {});
appssl.listen(Config.ssl.port, Config.bindaddress);
console.log(`Worker ${cluster.worker.id} now listening for SSL on port ${Config.ssl.port}`);
}
console.log(`Test your server at http://${Config.bindaddress === '0.0.0.0' ? 'localhost' : Config.bindaddress}:${Config.port}`);
require('./lib/repl').start(`sockets-${cluster.worker.id}-${process.pid}`, cmd => eval(cmd));
}