-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
675 lines (594 loc) · 16.4 KB
/
socket.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
674
675
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
var msgpack = require('msgpack5')();
var url = require('url');
var net = require('net');
var fs = require('fs');
var tls = require('tls');
var _ = require('lodash');
var forge = require('node-forge');
/**
* Errors to ignore.
*/
var ignore = [
'ECONNREFUSED',
'ECONNRESET',
'ETIMEDOUT',
'EHOSTUNREACH',
'ENETUNREACH',
'ENETDOWN',
'EPIPE',
'ENOENT'
];
/**
* Expose `Socket`.
*/
module.exports = function(debug) {
debug = debug || new Function();
/**
* Initialize a new `Socket`.
*
* A "Socket" encapsulates the ability of being
* the "client" or the "server" depending on
* whether `connect()` or `bind()` was called.
*
* @api private
*/
function Socket() {
var self = this;
this.server = null;
this.socks = [];
this.settings = {
"hwm": Infinity,
"identity": String(process.pid),
"retry timeout": 100,
"retry max timeout": 5000
};
this.callbacks = {};
this.identity = this.get('identity');
this.ids = 0;
this.queue = [];
this.on('connect', function(){
var prev = self.queue;
var len = prev.length;
self.queue = [];
debug('trace', 'flush ' + len + ' messages');
for (var i = 0; i < len; ++i) {
self.send.apply(this, prev[i]);
}
self.emit('flush', prev);
});
}
/**
* Inherit from `Emitter.prototype`.
*/
Socket.prototype.__proto__ = Emitter.prototype;
/**
* Make it configurable `.set()` etc.
*/
Socket.prototype.get = function(key) {
return this.settings[key];
};
Socket.prototype.set = function(key, value) {
this.settings[key] = value;
};
/**
* Provides an `.enqueue()` method to the `sock`. Messages
* passed to `enqueue` will be buffered until the next
* `connect` event is emitted.
*
* Emits:
*
* - `drop` (msg) when a message is dropped
* - `flush` (msgs) when the queue is flushed
*
* @param {Object} options
* @api private
*/
Socket.prototype.enqueue = function(msg){
var hwm = this.settings.hwm;
if (this.queue.length >= hwm) {
debug('trace', 'drop');
this.emit('drop', msg);
}
this.queue.push(msg);
};
/**
* Return a message id.
*
* @return {String}
* @api private
*/
Socket.prototype.id = function(){
var id = this.identity + ':' + this.ids++;
return id;
};
/**
* Use the given `plugin`.
*
* @param {Function} plugin
* @api private
*/
Socket.prototype.use = function(plugin){
plugin(this);
return this;
};
/**
* Creates a new `Message` and write the `args`.
*
* @param {Array} args
* @return {Buffer}
* @api private
*/
Socket.prototype.pack = function(args){
return msgpack.encode(args).slice();
};
/**
* Close all open underlying sockets.
*
* @api private
*/
Socket.prototype.closeSockets = function(fn){
debug('trace', this.type + ' closing ' + this.socks.length + ' connections');
var i = this.socks.length;
if (!i) {
return fn();
}
this.socks.forEach(function(sock){
if (fn) {
sock.on('end', function listener(){
sock.removeListener('close', listener);
--i || fn();
});
}
sock.end();
});
};
/**
* Close the socket.
*
* Delegates to the server or clients
* based on the socket `type`.
*
* @param {Function} [fn]
* @api public
*/
Socket.prototype.close = function(fn){
debug('trace', this.type + ' closing');
this.closing = true;
var i = this.server ? 2 : 1;
this.closeSockets(function(err) {
if (err) {
return fn && fn(err);
}
--i || fn && fn();
});
if (this.server) this.closeServer(function(err) {
if (err) {
return fn && fn(err);
}
--i || fn && fn();
});
};
/**
* Close the server.
*
* @param {Function} [fn]
* @api public
*/
Socket.prototype.closeServer = function(fn){
debug('trace', this.type + ' closing server');
this.server.on('close', this.emit.bind(this, 'close'));
this.server.close(fn);
};
/**
* Return the server address.
*
* @return {Object}
* @api public
*/
Socket.prototype.address = function(){
if (!this.server) return;
var addr = this.server.address();
addr.string = (this.get('tls') ? 'tls://' : 'tcp://') + addr.address + ':' + addr.port;
return addr;
};
/**
* Remove `sock`.
*
* @param {Socket} sock
* @api private
*/
Socket.prototype.removeSocket = function(sock){
var i = this.socks.indexOf(sock);
if (!~i) return;
debug('trace', this.type + ' remove socket ' + i);
this.socks.splice(i, 1);
};
/**
* Add `sock`.
*
* @param {Socket} sock
* @api private
*/
Socket.prototype.addSocket = function(sock){
var i = this.socks.push(sock) - 1;
debug('trace', this.type + ' add socket ' + i);
var decoder = msgpack.decoder();
sock.pipe(decoder);
decoder.on('data', this.onmessage(sock));
};
/**
* Handle `sock` errors.
*
* Emits:
*
* - `error` (err) when the error is not ignored
* - `ignored error` (err) when the error is ignored
* - `socket error` (err) regardless of ignoring
*
* @param {Socket} sock
* @api private
*/
Socket.prototype.handleErrors = function(sock){
var self = this;
sock.on('error', function(err){
debug('error', self.type + ' error ' + err.code || err.message);
self.emit('socket error', err);
self.removeSocket(sock);
if (!~ignore.indexOf(err.code)) {
return self.emit('error', err);
}
debug('trace', self.type + ' ignored ' + err.code);
self.emit('ignored error', err);
});
};
/**
* Handles framed messages emitted from the parser, by
* default it will go ahead and emit the "message" events on
* the socket. However, if the "higher level" socket needs
* to hook into the messages before they are emitted, it
* should override this method and take care of everything
* it self, including emitted the "message" event.
*
* @param {net.Socket} sock
* @return {Function} closure(msg, mulitpart)
* @api private
*/
Socket.prototype.onmessage = function(sock){
var self = this;
return function(args){
var id = args.pop();
var task = args[0];
var emitter = {
sock: sock,
emit: emit
};
if (task === 'register') {
if (sock.writable) {
sock.write(self.pack(['registered', id]));
}
var data = args[1];
return self.emit('register', data.identity, emitter);
}
if (task === 'registered') {
var fn = self.callbacks[id];
if (!fn) return debug('error', 'missing callback ' + id);
delete self.callbacks[id];
return fn(emitter);
}
args = _.cloneDeepWith(args, function(entry) {
if (_.isObject(entry) && entry.type === 'Buffer' && entry.data) {
return new Buffer(entry.data);
}
});
args.unshift('message');
args.push(emitter);
self.emit.apply(self, args);
function emit() {
var fn = function(){};
var id = self.id();
var args = Array.prototype.slice.call(arguments);
args[0] = args[0] || null;
var hasCallback = 'function' == typeof args[args.length - 1];
if (hasCallback) fn = args.pop();
args.push(id);
if (sock.writable) {
sock.write(self.pack(args), function(){ fn(true); });
return true;
} else {
debug('trace', 'peer went away');
process.nextTick(function(){ fn(false); });
return false;
}
}
};
};
Socket.prototype.checkCertificateValidity = function(certificate) {
certificate = forge.pki.certificateFromPem(certificate.toString());
if (!certificate) {
debug('error', this.type + ' no certificate');
return false;
}
var valid_from = new Date(certificate.validity.notBefore);
var valid_to = new Date(certificate.validity.notAfter);
var now = new Date();
if (now - valid_from < 0) {
debug('error', this.type + ' certificate not yet in effect');
debug('error', this.type + ' certificate will become valid the ' + valid_from.toJSON());
return false;
}
var dateDiff = now - valid_to;
if (dateDiff >= 0) {
debug('error', this.type + ' certificate expired the ' + valid_to.toJSON());
return false;
}
if (dateDiff / (1000 * 60 * 60 * 24) >= -30) {
debug('warn', this.type + ' certificate will soon expire : ' + valid_to.toJSON());
}
return true;
};
/**
* Connect to `port` at `host` and invoke `fn()`.
*
* Defaults `host` to localhost.
*
* TODO: needs big cleanup
*
* @param {Number|String} port
* @param {String} host
* @param {Function} fn
* @return {Socket}
* @api public
*/
Socket.prototype.connect = function(identity, port, host, fn){
var self = this;
if ('server' == this.type) throw new Error('cannot connect() after bind()');
if ('function' == typeof host) {
fn = host;
host = undefined;
}
if ('string' == typeof port) {
port = url.parse(port);
if (port.protocol == "unix:") {
host = fn;
fn = undefined;
port = port.pathname;
} else {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
}
} else {
host = host || '0.0.0.0';
}
var max = self.get('retry max timeout');
this.type = 'client';
var sock;
var tlsOpts = this.get('tls');
if (tlsOpts && tlsOpts.cert) {
if (!this.checkCertificateValidity(tlsOpts.cert)) {
throw new Error('Invalid certificate');
}
}
var onConnect = function() {
debug('trace', self.type + ' connect');
self.connected = true;
self.addSocket(sock);
self.retry = self.get('retry timeout');
self.emit('connect', sock);
fn && fn(null, sock);
};
if (tlsOpts) {
tlsOpts.host = host;
tlsOpts.port = port;
debug('trace', self.type + ' connect attempt ' + host + ':' + port);
sock = tls.connect(tlsOpts, onConnect);
} else {
sock = new net.Socket();
debug('trace', self.type + ' connect attempt ' + host + ':' + port);
sock.connect(port, host);
sock.on('connect', onConnect);
}
sock.identity = identity;
sock.setNoDelay();
this.handleErrors(sock);
sock.on('close', function() {
sock.removeAllListeners();
sock.destroy();
self.emit('socket close', sock.identity);
self.connected = false;
self.removeSocket(sock);
if (self.closing) {
return self.emit('close');
}
var retry = self.retry || self.get('retry timeout');
setTimeout(function(){
debug('trace', self.type + ' attempting reconnect');
self.emit('reconnect attempt');
self.connect(identity, port, host, fn);
self.retry = Math.round(Math.min(max, retry * 1.5));
}, retry);
});
return this;
};
Socket.prototype.register = function(identity, host, fn) {
var self = this;
var data = {identity: this.get('identity')};
if ('server' == this.type) throw new Error('cannot connect() after bind()');
var port;
if ('number' === typeof host) {
port = host;
host = '0.0.0.0';
} else if ('string' == typeof host) {
port = url.parse(host);
if (port.protocol == "unix:") {
host = fn;
port = port.pathname;
} else {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
}
} else {
throw new Error('invalid host parameter');
}
fn = fn || function(){};
this.connect(identity, port, host, function(err, sock) {
var args = ['register', data];
var id = self.id();
self.callbacks[id] = fn;
args.push(id);
if (sock) {
sock.write(self.pack(args));
} else {
debug('trace', 'no connected peers');
self.enqueue(args);
}
});
};
/**
* Handle connection.
*
* @param {Socket} sock
* @api private
*/
Socket.prototype.onconnect = function(sock){
var self = this;
var addr = sock.remoteAddress + ':' + sock.remotePort;
var tlsOptions = self.get('tls');
if (tlsOptions && !sock.authorized) {
debug('error', self.type + ' denied ' + addr + ' for authorizationError ' + sock.authorizationError);
}
debug('debug', self.type + ' accept ' + addr);
this.addSocket(sock);
this.handleErrors(sock);
this.emit('connect', sock);
sock.on('close', function() {
debug('debug', self.type + ' disconnect ' + addr);
self.emit('disconnect', sock.identity);
self.removeSocket(sock);
});
};
/**
* Bind to `port` at `host` and invoke `fn()`.
*
* Defaults `host` to INADDR_ANY.
*
* Emits:
*
* - `connection` when a client connects
* - `disconnect` when a client disconnects
* - `bind` when bound and listening
*
* @param {Number|String} port
* @param {Function} fn
* @return {Socket}
* @api public
*/
Socket.prototype.bind = function(port, host, fn){
var self = this;
if ('client' == this.type) throw new Error('cannot bind() after connect()');
if ('function' == typeof host) {
fn = host;
host = undefined;
}
var unixSocket = false;
if ('string' == typeof port) {
port = url.parse(port);
if ('unix:' == port.protocol) {
host = fn;
fn = undefined;
port = port.pathname;
unixSocket = true;
} else {
host = port.hostname || '0.0.0.0';
port = parseInt(port.port, 10);
}
} else {
host = host || '0.0.0.0';
}
this.type = 'server';
this.set('host', host);
this.set('port', port);
var tlsOptions = this.get('tls');
if (tlsOptions && tlsOptions.cert) {
if (!this.checkCertificateValidity(tlsOptions.cert)) {
throw new Error('Invalid certificate');
}
}
if (tlsOptions) {
tlsOptions.requestCert = tlsOptions.requestCert !== false;
tlsOptions.rejectUnauthorized = tlsOptions.rejectUnauthorized !== false;
this.server = tls.createServer(tlsOptions, this.onconnect.bind(this));
} else {
this.server = net.createServer(this.onconnect.bind(this));
}
debug('trace', this.type + ' bind ' + host + ':' + port);
this.server.on('listening', this.emit.bind(this, 'bind'));
if (unixSocket) {
// TODO: move out
this.server.on('error', function(e) {
if (e.code == 'EADDRINUSE') {
// Unix file socket and error EADDRINUSE is the case if
// the file socket exists. We check if other processes
// listen on file socket, otherwise it is a stale socket
// that we could reopen
// We try to connect to socket via plain network socket
var clientSocket = new net.Socket();
clientSocket.on('error', function(e2) {
if (e2.code == 'ECONNREFUSED') {
// No other server listening, so we can delete stale
// socket file and reopen server socket
fs.unlink(port);
self.server.listen(port, host, fn);
}
});
clientSocket.connect({path: port}, function() {
// Connection is possible, so other server is listening
// on this file socket
throw e;
});
}
});
}
this.server.listen(port, host, fn);
return this;
};
Socket.prototype.addCa = function(ca, callback){
var self = this;
var tlsOptions = this.get('tls');
if (!tlsOptions) {
throw new Error('no tls options found');
}
tlsOptions.ca = tls.ca || [];
tlsOptions.ca.push(ca);
if (_.isFunction(this.server.setSecureContext)) {
this.server.setSecureContext(tls);
} else {
this.set('tls', tlsOptions);
this.server.close(function(err) {
if (err) {
self.emit('error', err);
if (callback) {
callback(err);
}
return;
}
self.bind(self.get('port'), self.get('host'), function(err) {
if (err) {
self.emit('error', err);
if (callback) {
callback(err);
}
return;
}
if (callback) {
callback();
}
});
});
}
};
return Socket;
};