-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrepnet.js
More file actions
516 lines (479 loc) · 16.5 KB
/
Copy pathrepnet.js
File metadata and controls
516 lines (479 loc) · 16.5 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
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
// repflow node.js implementation
// a wrapper of the net module
var net = require('net');
var events = require('events');
var stream = require('stream');
var timers = require('timers');
var util = require('util');
// Usage: run with NODE_DEBUG=repnet node XXXX.js
var debug;
if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
var pid = process.pid;
debug = function(x) {
// if console is not set up yet, then skip this.
if (!console.error)
return;
console.error('NET: %d', pid,
util.format.apply(util, arguments).slice(0, 500));
};
} else {
debug = function() { };
}
// repsocket state names
var ONE_CONN = 0;
var DUP_CONN = 1;
var CHOSEN = 2;
var ENDED = 3;
// local port range
var LOCAL_PORT_LOW = 32768;
var LOCAL_PORT_HIGH = 61000;
// Flag to enable RepSYN instead of RepFlow.
// Usage: net.flag_repsyn = true
var flag_repsyn = false;
function createTCP() {
var TCP = process.binding('tcp_wrap').TCP;
return new TCP;
}
//******* Constructor of an Queue Item
//****** Parameter: a ONE_CONN repnet.socket object
//******* (if omitted) set port as undefined
function QItem(socket) {
if (!(this instanceof QItem)) return new QItem(socket);
if (typeof socket !== "undefined") {
this.port = socket.conn1.remotePort;
this.ip = socket.conn1.remoteAddress;
this.handle = socket;
socket.queueItem_handle = this;
}
else {
// if undefined, this item is used in the queue for helping update timing
this.port = undefined;
}
};
//****** Function: Try to match new conn in the queue
//****** Parameter: <netsocket> the handle of new net.socket connection
//****** Return Value: if found return the repnet.socket handle; else undefinded
function findItem(self, netsocket) {
var i = 1;
while (typeof self.queue[i] !== "undefined") {
// whether the <port:ip_addr> tuple is matched
if ( self.queue[i].port == netsocket.remotePort &&
self.queue[i].ip == netsocket.remoteAddress) {
var item = self.queue[i];
self.queue.splice(i,1);
return item;
}
else i++;
}
return undefined;
}
//****** Function: The callback of 'connect' evemt (in a net.socket object)
//****** Parameter: <conn> the handle of new net.socket connection, emited by 'connect' event
//****** Process: 1. search the item in the waiting list;
//****** 2. new a repnet.socket or insert this new conn as the second member of an existing repnet.socket
function getconnection(self, conn) {
item = findItem(self, conn);
//not matched, emit a new repnet.socket connection
if (!item) {
debug("not matched from port", conn.remotePort);
s = new Socket();
s.conn1 = conn;
s.state = ONE_CONN;
// Add event listener
s.conn1.on('data', function(data) {getdata(s, data, true)});
s.conn1.on('end', function() {getend(s, true)});
s.conn1.on('error', function() {
if (s.state == ONE_CONN || s.state == CHOSEN) s.emit('error');
getend(s, true);
})
// push into the queue
self.queue.push(QItem(s));
debug("Push New Item. Queue length:", self.queue.length);
// if there is a connection listener as a callback of listen
if (typeof self.connectListener === 'function') {
self.connectListener.call(self, s);
debug("connectListener called: (local:remote)", conn.localPort, conn.remotePort);
}
// if not, emit the "connection" event
else self.emit('connection', s);
}
// matched. then insert new net.socket to the repnet as a conn2
else {
s = item.handle;
s.conn2 = conn;
s.state = DUP_CONN;
debug("Matched! Queue length:", self.queue.length, "repnet.socket state", s.state);
//Add appropriate event listeners
s.conn2.on('data', function(data) {getdata(s, data, false)});
s.conn2.on('end', function() {getend(s, false)});
s.conn1.on('error', function() {
if (s.state == ONE_CONN || s.state == CHOSEN) s.emit('error');
getend(s, true);
})
s.conn2.on('error', function() {getend(s, false);});
debug("Two port numbers", s.conn1.remotePort, s.conn2.remotePort);
//execute the archived commands on the slower conn
for (var i = 0; i < s.archive_write.length; i++) {
conn.write.apply(conn, s.archive_write[i]);
};
}
}
//****** Function: The callback of 'data' evemt (in a net.socket object)
//****** Parameter: <data> newly emitted data
//****** <index> to differentiate which conn has got data. true for conn1, false for conn2.
//****** Process: check the data count.
//****** if the new chunk is detected, it will be emited along with the 'data' event in a net.repsockt object.
function getdata(self, data, index) {
if (index) {
// if conn1 gets new data
self.readcount[0] += data.length;
newcount = self.readcount[0] - self.readcount[1];
if (newcount > 0) {
debug("conn1 is reporting ", newcount, "byte(s) of new chunk");
newchunk = data.slice(data.length - newcount);
self.emit('data', newchunk);
}
}
else {
// if conn2 gets new data
self.readcount[1] += data.length;
newcount = self.readcount[1] - self.readcount[0];
if (newcount > 0) {
debug("conn2 is reporting ", newcount, "byte(s) of new chunk");
newchunk = data.slice(data.length - newcount);
self.emit('data', newchunk);
}
}
}
//****** Function: The callback of 'end' evemt (in a net.socket object)
//****** Parameter: <index> to differentiate which conn has got data. true for conn1, false for conn2.
//****** Process: check the state of current net.repsocket object.
//****** do decisions based on which sub-socket is going to be ended
function getend(self, index){
debug("state before ened, index", self.state, index);
if (index) { // conn1 is ended
switch (self.state) {
case ONE_CONN: // ending the only connection
self.state = ENDED;
self.emit('end');
break;
case DUP_CONN: // ending one of the two connections, convert to CHOSEN
self.state = CHOSEN;
// in chosen mode, the working socket is always conn1
self.conn1 = self.conn2;
self.conn2 = undefined;
break;
case CHOSEN: // ending the only connection
self.state = ENDED;
self.emit('end');
break;
default:
break;
}
}
else { // conn2 is ended
switch (self.state) {
case DUP_CONN: // convert to CHOSEN
self.state = CHOSEN;
self.conn2 = undefined;
break;
case CHOSEN: // ending the only connection
self.state = ENDED;
self.emit('end');
break;
default: // in CHOSEN state, conn2 is always unvalid
break;
}
}
}
//****** The repnet.server class
function Server() {
// Adapt to parameter variations, become a constructor
if (!(this instanceof Server)) {
if (arguments.length == 2) return new Server(arguments[0], arguments[1]);
if (arguments.length == 1) return new Server(arguments[0]);
if (arguments.length == 0) return new Server();
}
events.EventEmitter.call(this);
// member variables
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
this.connectListener = args.pop();
if (typeof this.connectListener !== 'function') args.push(this.connectListener);
this.server1 = net.createServer(args);
this.server2 = net.createServer(args);
this.queue = [QItem(), QItem(), QItem(), QItem(), QItem()];
var self = this; // use self to pass server handle to the member functions
// update the queue per 200ms
setInterval(function(){
do {
var trash = self.queue.shift();
if (trash.port) trash.handle.state = CHOSEN;
} while(typeof self.queue[0].port !== 'undefined');
self.queue.push(QItem());
//debug("Updated Queue Length:", self.queue.length);
}, 200);
// listen function
this.listen = function() {
// normalize parameters
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
callback = args.pop();
// if there is no callback function, the last parameter should be pushed back
if (typeof callback !== 'function') args.push(callback);
// two port numbers
port = args.shift();
port2 = port + 1;
if (args.length > 0) host = args.shift(); else host = 'localhost';
if (args.length > 0) backlog = args.shift(); else backlog = null;
var flag_listen = 0; // flag to make sure when to call the callback function
self.server1.listen(port, host, backlog, function() {
flag_listen += 1;
if (flag_listen == 2) callback.apply(this);
});
self.server2.listen(port2, host, backlog, function() {
flag_listen += 1;
if (flag_listen == 2) callback.apply(this);
});
};
// got a connection
this.server1.on('connection', function(s) {
getconnection(self, s);
})
this.server2.on('connection', function(s) {
getconnection(self, s);
})
};
util.inherits(Server, events.EventEmitter);
// exports as an API
exports.Server = Server;
//****** The repnet.socket class
function Socket() {
//********** The constructor
if (!(this instanceof Socket)) return new Socket(options);
events.EventEmitter.call(this);
this.conn1 = undefined;
this.conn2 = undefined;
this.state = ENDED;
this.archive_write = [];
this.readcount = [0, 0];
this.queueItem_handle = undefined;
var self = this; // use self to pass server handle to the member functions
//********* Connect Function
//********* Accept 1 port arguments
this.connect = function() {
if (!flag_repsyn) { // not a repsyn conn
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
var callback = args[args.length-1];
if (typeof callback !== 'function') { // don't have a listener
var flag_connect = false;
var ifconnect = function() {
if (!flag_connect) {
flag_connect = true;
self.emit('connect');
}
else {
if (flag_repsyn) {
this.end();
self.state = CHOSEN;
}
}
}
args.push(ifconnect);
// Generate a random port number
var localport = Math.floor(Math.random() * (LOCAL_PORT_HIGH - LOCAL_PORT_LOW)) + LOCAL_PORT_LOW;
console.log("Random Port:", localport);
// bind to the same local port, using a little trick.
self.state = DUP_CONN;
var _handle1 = createTCP();
_handle1.bind('0.0.0.0', localport);
var _handle2 = createTCP();
_handle2.bind('0.0.0.0', localport);
self.conn1 = new net.Socket({ handle: _handle1});
self.conn2 = new net.Socket({ handle: _handle2});
// connect!
self.conn1.connect.apply(self.conn1, args);
args[0] += 1;
self.conn2.connect.apply(self.conn2, args);
}
else { // callback is the 'connect' listener
var flag_connect = false;
var ifconnect = function() {
if (!flag_connect) {
flag_connect = true;
callback.call(self);
}
}
args.push(ifconnect);
self.state = DUP_CONN;
// Generate a random port number
var localport = Math.floor(Math.random() * (LOCAL_PORT_HIGH - LOCAL_PORT_LOW)) + LOCAL_PORT_LOW;
debug("Random Port:", localport);
// bind to the same local port, using a little trick.
var _handle1 = createTCP();
_handle1.bind('0.0.0.0', localport);
var _handle2 = createTCP();
_handle2.bind('0.0.0.0', localport);
self.conn1 = new net.Socket({ handle: _handle1});
self.conn2 = new net.Socket({ handle: _handle2});
// connect!
self.conn1.connect.apply(self.conn1, args);
args[0] += 1;
self.conn2.connect.apply(self.conn2, args);
}
// register the event listeners
this.conn1.on('data', function(data) {getdata(self, data, true)});
this.conn2.on('data', function(data) {getdata(self, data, false)});
this.conn1.on('end', function() {getend(self, true)});
this.conn2.on('end', function() {getend(self, false)});
this.conn1.on('error', function() {
if (this.state == ONE_CONN || this.state == CHOSEN) this.emit('error');
getend(this, true);
})
this.conn2.on('error', function() {getend(this, false);});
}
// is a repsyn conn
else {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
var callback = args[args.length-1];
if (typeof callback !== 'funcion') {
// don't have a listener, so we have to emit the connect event
var flag_connect = false;
var ifconnect = function() {
if (!flag_connect) {
flag_connect = true;
self.conn1 = this;
self.state = CHOSEN
self.emit('connect');
}
else {
if (flag_repsyn) {
this.end();
}
}
}
}
else {
// have a listener, so we have to call the connect event right after the first connection is connected
var flag_connect = false;
var ifconnect = function() {
if (!flag_connect) {
flag_connect = true;
self.conn1 = this;
self.state = CHOSEN
callback.call(self.conn1)
}
else {
if (flag_repsyn) {
this.end();
}
}
}
}
// make up the new callback function
args.push(ifconnect);
var localport = Math.floor(Math.random() * (LOCAL_PORT_HIGH - LOCAL_PORT_LOW)) + LOCAL_PORT_LOW;
debug("Random Port:", localport);
// bind to the same local port, using a little trick.
var _handle1 = createTCP();
_handle1.bind('0.0.0.0', localport);
var _handle2 = createTCP();
_handle2.bind('0.0.0.0', localport);
self.conn1 = new net.Socket({ handle: _handle1});
self.conn2 = new net.Socket({ handle: _handle2});
// connect!
self.conn1.connect.apply(self.conn1, args);
args[0] += 1;
self.conn2.connect.apply(self.conn2, args);
// register the event listeners
this.conn1.on('data', function(data) {getdata(self, data, true)});
this.conn1.on('end', function() {getend(self, true)});
this.conn1.on('error', function() {
if (this.state == ONE_CONN || this.state == CHOSEN) this.emit('error');
getend(this, true);
})
}
}
//********** Write Function
//********** Depend on state while take care of possible callback parameter
this.write = function() {
var data = arguments[0];
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
switch (self.state) {
case ONE_CONN: // should write on conn1, and then archive the commands
self.conn1.write.apply(self.conn1, args);
callback = args.pop();
if (typeof callback !== 'function') {
args.push(callback);
}
self.archive_write.push(args);
break;
case DUP_CONN: // should write on both conncetions
callback = args.pop();
if (typeof callback !== 'funcion') {
args.push(callback);
self.conn1.write.apply(self.conn1, args);
self.conn2.write.apply(self.conn2, args);
}
else {
var write_status = false;
var setstatus = function() {
if (write_status == false) {
write_status = true;
callback.apply(self);
}
};
args.push(setstatus);
self.conn1.write.apply(self.conn1, args);
self.conn2.write.apply(self.conn2, args);
}
break;
case CHOSEN: // should write on conn1
self.conn1.write.apply(self.conn1, args);
break;
default:
self.emit('error', "ENDED");
break;
}
}
this.end = function(){
// depend on which one is ended.
switch (self.state){
case ONE_CONN:
self.conn1.end.apply(self.conn1, arguments);
break;
case DUP_CONN:
self.conn1.end.apply(self.conn1, arguments);
self.conn2.end.apply(self.conn2, arguments);
break;
case CHOSEN:
self.conn1.end.apply(self.conn1, arguments);
break;
default:
break;
}
}
};
util.inherits(Socket, events.EventEmitter);
exports.Socket = Socket;
exports.createServer = function() {
return Server.apply(this, arguments);
};
exports.connect = function() {
conn = new Socket();
conn.connect.apply(conn, arguments);
return conn;
}