/
net_connection.js
executable file
·379 lines (346 loc) · 9.61 KB
/
net_connection.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
/**
* NetConnection
*
* Copyright 2011 OpenRTMFP
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License received along this program for more
* details (or else see http://www.gnu.org/licenses/).
*
* This file is a part of ArcusNode.
*/
var EventEmitter = require('events').EventEmitter;
var ke = require('../build/Release/keyexchange.node');
var Packet = require('./packet.js');
/**
* Represents a connection to ArcusNode
* TODO: Make group join/leave more usable for events
* TODO: provide NetConnection.call('commandName', callback, args...);
* TODO: provide NetConnection.forward(endpoint);
* TODO: provide NetConnection.connect() like in client
* TODO: provide farId
* TODO: make sure KeyExchange instance is destroyed on close to get rid of DH instance
* TODO: document NetConnection API
*/
var NetConnection = module.exports = function(arcus, id, settings){
//Check ArcusNode instance
if(typeof arcus !== 'object'){
throw new Error('A NetConnection depends on an ArcusNode instance that has to be given in the constuctor');
}
//Check id
if(typeof id !== 'number'){
throw new Error('A NetConnection need a numeric id that has to be given in the constuctor');
}
//PRIVATE ATTRIBUTES
this.__p = {
keyExchange: new ke.KeyExchange(),
keyPair: null,
sharedSecret: null,
certificate: null,
state: NetConnection.HANDSHAKE,
id: id,
peerId: null,
latency: 999999,
lastMessage: null,
groups: {},
touchTime: Date.now(),
keepalives: 0,
//TODO: Cleanup Iterators in manage cycle
addressIterators: {},
authenticated: false,
decryptKey: null,
decryptKey: null,
lastPacketTime: 0,
cookie: null,
clientConnectionId: 0,
waiting: false,
arcus: arcus
};
this.endpoints = [];
};
//Inherit from EventEmitter
NetConnection.prototype.__proto__ = EventEmitter.prototype;
/**
* Get the certificate for that connection, if not yet existent, generate it
*/
NetConnection.prototype.__defineGetter__('certificate', function(){
if(!this.__p.certificate){
this.__p.certificate = Packet.randomBytes(64, new Buffer(64), 0);
}
return this.__p.certificate;
});
/**
* Get the public key for DH key exchange, if not yet generated it will be
*
* @return {Buffer}
*/
NetConnection.prototype.__defineGetter__('publicKey', function(){
if(!this.__p.keyPair){
this.generateKeyPair();
}
return this.__p.keyPair[1];
});
/**
* Get the private key for DH key exchange, if not yet generated it will be
*
* @return {Buffer}
*/
NetConnection.prototype.__defineGetter__('privateKey', function(){
if(!this.__p.keyPair){
this.__p.keyPair = _keyExchange.generateKeyPair();
}
return this.__p.keyPair[0];
});
/**
* Generates a new private/public keypair for DH key Exchange and returns it
*
* @return {Array} [privateKey, publicKey]
*/
NetConnection.prototype.generateKeyPair = function(){
return this.__p.keyPair = this.__p.keyExchange.generateKeyPair();
};
/**
* Computes the shared secret for a public key from the other side stores and returns it
*
* @param {Buffer} publicKey
* @return {Buffer} The shared secret
*/
NetConnection.prototype.computeSharedSecret = function(publicKey){
if(!publicKey){
throw new Error('Need public key to generate shared secret');
}
if(!this.__p.keyPair){
this.generateKeyPair();
}
return this.__p.sharedSecret = this.__p.keyExchange.computeSharedSecret(publicKey);
};
/**
* Computes the asymetric encryption/decryption keys, stores and returns them
*
* @param {Buffer} initiatorNonce
* @param {Buffer} responderNonce
* @return {Array} [decryptKey, encryptKey]
*/
NetConnection.prototype.computeAsymetricKeys = function(initiatorNonce, responderNonce){
if(!this.__p.sharedSecret){
throw new Error('Cannot compute keys, need public key from other side first (computeSharedSecret)');
}
if(!initiatorNonce || !responderNonce){
throw new Error('Cannot compute keys without nonces');
}
var keys = this.__p.keyExchange.computeAsymetricKeys(this.__p.sharedSecret, initiatorNonce, responderNonce);
this.__p.decryptKey = keys[0];
this.__p.encryptKey = keys[1];
return keys;
};
/**
* The unique identifier within an ArcusNode instance
*
* @return {integer}
*/
NetConnection.prototype.__defineGetter__('id', function() {
return this.__p.id;
});
/**
* Keep the connection alive
*/
NetConnection.prototype.keepalive = function() {
//TODO: Send server keepalive to client
this.touch();
return ++this.__p.keepalives;
};
/**
* Gives the next address in from all endpoints, given a specific iterator.
* Used to walk through endpoints on a rendezvouz message.
* TODO: cleanup iterators
* TODO: make private, used by arcus internally only
*/
NetConnection.prototype.nextAddress = function(tag){
if(!this.__p.addressIterators[tag])
this.__p.addressIterators[tag] = 0;
if(this.__p.addressIterators[tag] == this.endpoints.length)
{
this.__p.addressIterators[tag] = 1;
} else {
this.__p.addressIterators[tag]++;
}
return this.endpoints[this.__p.addressIterators[tag] - 1];
};
/**
* Gets latency of this specific connection
*
* @return {Integer} milliseconds
*/
NetConnection.prototype.__defineGetter__('latency', function(){
return this.__p.latency;
});
/**
* Gets the peer id for this connection, computed in the handshake
*
* @return {Buffer}
*/
NetConnection.prototype.__defineGetter__('peerId', function(){
return this.__p.peerId;
});
/**
* Gets the current connection state
*
* @return {Integer}
*/
NetConnection.prototype.__defineGetter__('state', function(){
return this.__p.state;
});
/**
* Set the current state of this connection,
* checking the given state for correctness
* and emitting the STATE_CHANGE event
*/
NetConnection.prototype.__defineSetter__('state', function(v){
switch(v){
case 0x00:
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
this.__p.state = v;
break;
default:
throw new Error('Unsupported state for NetConnection');
}
this.emit('state', v);
return this.__p.state;
});
/**
* Reset the timeout
*/
NetConnection.prototype.touch = function() {
this.__p.touchTime = Date.now();
};
/**
* When was the connection last touched?
*
* @return {Integer} Timestamp
*/
NetConnection.prototype.__defineGetter__('touched', function() {
return this.__p.touchTime;
});
/**
* Have this connection join a group, to keep track in which groups this connection is
* TODO: make publicly usable without flow
*/
NetConnection.prototype.join = function(flow, group) {
this.__p.groups[flow] = group;
};
/**
* Remove a group
* TODO: make publicly usable without flow
*/
NetConnection.prototype.leave = function(flow) {
var group = this.__p.groups[flow];
delete this.__p.groups[flow];
return group;
};
/**
* Close this connection
*/
NetConnection.prototype.close = function() {
if(this.state != NetConnection.CLOSING) {
this.state = NetConnection.CLOSING;
this.emit('close');
for(k in this.__p.groups){
this.__p.groups[k].remove(this.id);
}
console.log('nc close');
this.__p.arcus.closeConnection(this);
}
};
/**
* Let the connection fail and send the fail message to the client
* Emits the fail event.
*/
NetConnection.prototype.fail = function(){
this.state = NetConnection.FAILED;
this.proceed();
this.__p.arcus.failConnection(this, this.__p.connectionMessage.flow);
this.emit('fail');
};
/**
* Accept the connection and trigger NetConnection.Connect.Success on the client.
* Emits the accept event
*/
NetConnection.prototype.accept = function(description){
this.state = NetConnection.ACCEPTED;
this.proceed();
this.__p.arcus.acceptConnection(this, description);
this.emit('accept');
};
/**
* Reject the connection and trigger NetConnection.Connect.Rejected on the client
*/
NetConnection.prototype.reject = function(description){
this.state = NetConnection.REJECTED;
this.proceed();
this.__p.arcus.rejectConnection(this, description);
this.emit('reject');
};
/**
* Wait and do not handle anything until proceeding
*/
NetConnection.prototype.wait = function(){
this.__p.waiting = true;
};
/**
* Proceed with normal operation
*/
NetConnection.prototype.proceed = function(){
this.__p.waiting = false;
};
/**
* Is this connection waiting to be processed?
*
* @return {Boolean}
*/
NetConnection.prototype.__defineGetter__('waiting', function() {
return this.__p.waiting;
});
/**
* Set if this connection has been authenticated
*
* @param {Boolean} v
*/
NetConnection.prototype.__defineSetter__('authenticated', function(v) {
return this.__p.authenticated = (v === true) ? true : false;
});
/**
* Has this connection been authenticated?
*
* @return {Boolean}
*/
NetConnection.prototype.__defineGetter__('authenticated', function() {
return this.__p.authenticated;
});
/**
* Set if this connection has been authenticated
*
* @param {Boolean} v
*/
NetConnection.prototype.__defineSetter__('authenticated', function(v) {
return this.__p.authenticated = (v === true) ? true : false;
});
//States
NetConnection.CLOSING = 0x00;
NetConnection.HANDSHAKE = 0x01;
NetConnection.CONNECTING = 0x02;
NetConnection.CONNECTED = 0x03;
NetConnection.FAILED = 0x04;
NetConnection.REJECTED = 0x05;
NetConnection.ACCEPTED = 0x06;