-
Notifications
You must be signed in to change notification settings - Fork 4
/
turn.js
651 lines (516 loc) · 23.5 KB
/
turn.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
// Copyright (c) 2012-present Tom Zhou<appnet.link@gmail.com>
//
// TURN establish the session between two clients by relay server
// 0. Relay server consists of three services: proxy server, agent server(used to punch hole) and agent client, that binds on same ip and different port
// - 0.a Proxy server accept connect from initiator client, then proxy request/response to responder client for agent client
// - 0.b Agent server accept connect from responder client, which used to punch hole from responder client to agent client
// - 0.c Aroxy server binds on proxy port, while both agent server and agent client binds on same ip, but another same agent port
//
// 1. One client acts as initiator, another client acts as responder and relay server as middle server
// 2. TURN has two mode: TURN_CS, TURN_PP, that's alike to STUN_CS, STUN_PP
// 3. TURN_CS means the connection setup in client to server mode
// 4. TURN_PP means the connection setup in rendezvous mode
// 5. TURN does need relay-server
//
// 6. TURN has three logical connections:
// - 6.a Responder client connect to relay-server's agent server, which used to punch hole to responder client for agent client
// - 6.b Initiator client connect to relay-server proxy server
// - 6.c Relay-server agent client connect to responder client
'use strict';
var debug = require('debug')('turn');
var eventEmitter = require('events').EventEmitter,
util = require('util');
var SdpDB = require('./db/sdp');
// security hash
// sipkey can be any user defined 4 integers, 3 sipkey generate 192bits secure number
var siphash = require('siphash'),
sipkey1 = [0x33331111, 0x66668888, 0x55556666, 0x33338888], // magic key1
sipkey2 = [0x38385886, 0x86866969, 0x56556686, 0x36839816]; // magic key2
// generate 128bits hashed uid
var genHuid = function(uuid){
var s1, s2;
s1 = siphash.hash_hex(sipkey1, uuid);
s2 = siphash.hash_hex(sipkey2, uuid+'-'+s1);
return s1+s2;
};
// generate gid for turn server
var genSrvid = function(srvinfo){
return genHuid(srvinfo.proto+'-srv-'+srvinfo.srvpublicIP+'-'+srvinfo.srvproxyPort+'-'+srvinfo.srvagentPort);
};
var genClntid = function(clntinfo){
return genHuid(clntinfo.proto+'-clnt-'+clntinfo.devkey+'-'+clntinfo.clntlocalIP+'-'+clntinfo.clntlocalPort);
};
var Turn = module.exports = function(session){
var self = this;
// super constructor
eventEmitter.call(self);
// Session info
self.sessionInfo = {
// protocol info
sid: session.sid,
proto: session.proto, // session's protocol: tcp/udp/sctp/rtp/rtcp, etc
mode: session.mode, // p2p or c/s
// initiator client's outer and public network address
mineid: session.mineid,
minenatype: session.minenatype,
mineIP: session.mineIP,
minePort: session.minePort, // meaningless in symmetric NAT/FW
// relay proxy/agent server outer and public network address
srvid: genSrvid(session),
srvDN: session.srvpublicDN, // domain name
srvIP: session.srvpublicIP,
proxyPort: session.srvproxyPort,
agentPort: session.srvagentPort,
// responder client's outer and public network address
peerid: session.peerid,
peernatype: session.peernatype,
peerIP: session.peerIP,
peerPort: session.peerPort, // meaningless in symmetric NAT/FW
// media,sensor parameters. video resolution/fps/bps, sensor resolution/sps, etc
// notes: parameters MUST be negotiated between peers
parameter: session.parameter || '',
// timestamp
start: session.start || Date.now()
};
// Clients info
// notes: store client's gid, NAT type, etc
// NAT type: symmetric, asymmetric, etc
self.clntsInfo = {
mine: {gid: session.mineid, natype: session.minenatype},
peer: {gid: session.peerid, natype: session.peernatype}
};
// Relay server info
self.srvInfo = {
dn: session.srvpublicDN, // domain name
ip: session.srvpublicIP, // outter/public ip
proxyport: session.srvproxyPort, // outter/public proxy port
agentport: session.srvagentPort, // outter/public agent port
gid: genSrvid(session), // Gid
localIP: session.srvlocalIP, // inner/local ip or interface
localproxyPort: session.srvlocalproxyPort, // inner/local proxy port
localagentPort: session.srvlocalagentPort // inner/local agent port
};
};
util.inherits(Turn, eventEmitter);
// db hook
Turn.db = SdpDB;
// instance methods
Turn.prototype.saveOupdate = function(fn){
var self = this;
// 1.
// query mine as client-node
SdpDB.getClient(self.clntsInfo.mine.gid, function(err, node){
if (err || !node) return fn(err+'query mine client node failure');
var mine = node;
// 2.
// update relay server-node
SdpDB.updateRelaysrv(self.srvInfo.gid, self.srvInfo, function(err, node){
if (err || !node) return fn(err+',update relay server node failure');
var server = node;
// 3.
// query peer as client-node
SdpDB.getClient(self.clntsInfo.peer.gid, function(err, node){
if (err || !node) return fn(err+'query peer client node failure');
var peer = node;
// 4.
// update turn session from mine to peer client
var sessinfo = {type: SdpDB.SESSION_TURN, data: self.sessionInfo};
SdpDB.updateSession(mine, peer, sessinfo, function(err, session){
if (err || !session) return fn(err+'update turn session failure');
var turn = session;
// 5. TBD...in case p2p session
// update turn proxy session from mine to relay proxy server
///sessinfo = {
/// type: SdpDB.SESSION_TURN_PROXY,
/// data: self.sessionInfo
///};
///SdpDB.updateSession(mine, server, sessinfo, function(err, session){
/// if (err || !session) return fn(err+'update turn proxy session failure');
/// var proxy = session;
// 6. TBD...in case p2p session
// update turn agent session from relay agent server to peer client
///sessinfo = {
/// type: SdpDB.SESSION_TURN_AGENT,
/// data: self.sessionInfo
///};
///SdpDB.updateSession(server, peer, sessinfo, function(err, session){
/// if (err || !session) return fn(err+'update turn agent session failure');
/// var agent = session;
// 7.
// emit update event
self.emit('update', {mine: mine, peer: peer, server: server, session: turn});
// 8.
// ...
// 9.
// pass TURN back
fn(null, {mine: mine, peer: peer, server: server, session: turn});
///});
///});
});
});
});
});
};
// class methods
// get turn session
exports.getTurnByClntFrom = function(clntid, fn){
SdpDB.getSessionByFromType({gid: clntid}, {type: SdpDB.SESSION_TURN}, fn);
};
exports.getTurnByClntTo = function(clntid, fn){
SdpDB.getSessionByToType({gid: clntid}, {type: SdpDB.SESSION_TURN}, fn);
};
exports.getAllTurn = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_TURN}, fn);
};
// export gid generator
Turn.genSrvid = genSrvid;
Turn.genClntid = genClntid;
// turn Punch session: client to agent server
var turnPunch = function(session){
var self = this;
var clntgidhex = '';
// super constructor
eventEmitter.call(self);
// calculate clntgid in case anonymous client
if (session.clntgid) {
clntgidhex = genHuid(session.clntgid);
} else {
clntgidhex = session.clntgid = genClntid(session);
}
// Session info
self.sessionInfo = {
sid: session.sid,
proto: session.proto, // session's protocol: tcp/udp/sctp/rtp/rtcp, etc
mode: session.mode, // p2p or c/s
// client's outer and public network address
clntid: session.clntgid,
clntIP: session.clntpublicIP,
clntPort: session.clntpublicPort,
// relay proxy/agent server outer and public network address
srvid: genSrvid(session),
srvDN: session.srvpublicDN, // domain name
srvIP: session.srvpublicIP,
proxyPort: session.srvproxyPort,
agentPort: session.srvagentPort,
// media,sensor parameters. video resolution/fps/bps, sensor resolution/sps, etc
// notes: parameters MUST be negotiated between peers
parameter: session.parameter || '',
// timestamp
start: session.start || Date.now()
};
// Client info
// notes: a client defined as a device with local proto/ip/port
self.clntInfo = {
// client's inner and local network address
// notes: user always binds on one ip/port in device to punch hole easily
localIP: session.clntlocalIP,
localPort: session.clntlocalPort,
// client's device info
devkey: session.devkey, // client device's serial number info: phone, pc, tablet, etc
devcap: session.devcap || '', // client sevice's capabilities: video/audio/stroage medias, sensors/ecg, etc
// NAT type behind client
// notes: assume asymmetric in default :)
// !!! NAT type can be determined in client side
///natype: session.natype || SdpDB.NAT_ASYM, // symmetric, asymmetric, etc
// client gid
gid: session.clntgid,
// client vURL vpath or vhost
// TBD... allocation schema
vpath: '/vurl/'+clntgidhex, // '/vurl/gid' in default by now
vhost: clntgidhex+'.vurl.', // 'gid.vurl.51dese.com' like append on 51dese.com
vmode: session.vmode, // vURL mode
vtoken: session.vtoken, // vURL secure token
// client security mode level-based
secmode: session.secmode
};
// Relay server info
self.srvInfo = {
dn: session.srvpublicDN, // domain name
ip: session.srvpublicIP, // outter/public ip
proxyport: session.srvproxyPort, // outter/public proxy port
agentport: session.srvagentPort, // outter/public agent port
gid: genSrvid(session), // Gid
localIP: session.srvlocalIP, // inner/local ip or interface
localproxyPort: session.srvlocalproxyPort, // inner/local proxy port
localagentPort: session.srvlocalagentPort // inner/local agent port
};
};
util.inherits(turnPunch, eventEmitter);
// instance methods
turnPunch.prototype.saveOupdate = function(fn){
var self = this;
// 1.
// update client-node
SdpDB.updateClient(self.clntInfo.gid, self.clntInfo, function(err, node){
if (err || !node) return fn(err+'update client node failure');
var client = node;
// 2.
// update relay server-node
SdpDB.updateRelaysrv(self.srvInfo.gid, self.srvInfo, function(err, node){
if (err || !node) return fn(err+',update relay server node failure');
var server = node;
// 3.
// update turn punch session from client to relay agent server
var sessinfo = {
type: SdpDB.SESSION_TURN_PUNCH,
data: self.sessionInfo
};
SdpDB.updateSession(client, server, sessinfo, function(err, session){
if (err || !session) return fn(err+'update turn punch session failure');
// 4.
// emit update event
self.emit('update', {client: client, server: server, session: session});
// 5.
// ...
// 6.
// pass TURN punch session back
fn(null, {client: client, server: server, session: session});
});
});
});
};
// class methods
// get turn punch session
exports.getTurnPunchByClnt = function(clntid, fn){
SdpDB.getSessionByFromType({gid: clntid}, {type: SdpDB.SESSION_TURN_PUNCH}, fn);
};
exports.getTurnPunchBySrv = function(srvid, fn){
SdpDB.getSessionByToType({gid: srvid}, {type: SdpDB.SESSION_TURN_PUNCH}, fn);
};
exports.getAllTurnPunch = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_TURN_PUNCH}, fn);
};
Turn.Punch = turnPunch;
// turn Proxy session: client to proxy server
var turnProxy = function(session){
var self = this;
var clntgidhex = '';
// super constructor
eventEmitter.call(self);
// calculate clntgid in case anonymous client
if (session.clntgid) {
clntgidhex = genHuid(session.clntgid);
} else {
clntgidhex = session.clntgid = genClntid(session);
}
// Session info
self.sessionInfo = {
// protocol info
sid: session.sid,
proto: session.proto, // session's protocol: tcp/udp/sctp/rtp/rtcp, etc
mode: session.mode, // p2p or c/s
// client's outer and public network address
clntid: session.clntgid,
clntIP: session.clntpublicIP,
clntPort: session.clntpublicPort,
// relay proxy/agent server outer and public network address
srvid: genSrvid(session),
srvDN: session.srvpublicDN, // domain name
srvIP: session.srvpublicIP,
proxyPort: session.srvproxyPort,
agentPort: session.srvagentPort,
// media,sensor parameters. video resolution/fps/bps, sensor resolution/sps, etc
// notes: parameters MUST be negotiated between peers
parameter: session.parameter || '',
// timestamp
start: session.start || Date.now()
};
// Client info
// notes: a client defined as a device with local proto/ip/port
self.clntInfo = {
// client's inner and local network address
// notes: user always binds on one ip/port in device to punch hole easily
localIP: session.clntlocalIP,
localPort: session.clntlocalPort,
// client's device info
devkey: session.devkey, // client device's serial number info: phone, pc, tablet, etc
devcap: session.devcap || '', // client sevice's capabilities: video/audio/stroage medias, sensors/ecg, etc
// NAT type behind client
// notes: assume asymmetric in default :)
// !!! NAT type can be determined in client side
///natype: session.devnat || SdpDB.NAT_ASYM, // symmetric, asymmetric, etc
// client gid
gid: session.clntgid,
// client vURL vpath or vhost
// TBD... allocation schema
vpath: '/vurl/'+clntgidhex, // '/vurl/gid' in default by now
vhost: clntgidhex+'.vurl.', // 'gid.vurl.51dese.com' like append on 51dese.com
vmode: session.vmode, // vURL mode
vtoken: session.vtoken, // vURL secure token
// client security mode level-based
secmode: session.secmode
};
// Relay server info
self.srvInfo = {
dn: session.srvpublicDN, // domain name
ip: session.srvpublicIP, // outter/public ip
proxyport: session.srvproxyPort, // outter/public proxy port
agentport: session.srvagentPort, // outter/public agent port
gid: genSrvid(session), // Gid
localIP: session.srvlocalIP, // inner/local ip or interface
localproxyPort: session.srvlocalproxyPort, // inner/local proxy port
localagentPort: session.srvlocalagentPort // inner/local agent port
};
};
util.inherits(turnProxy, eventEmitter);
// instance methods
turnProxy.prototype.saveOupdate = function(fn){
var self = this;
// 1.
// update client-node
SdpDB.updateClient(self.clntInfo.gid, self.clntInfo, function(err, node){
if (err || !node) return fn(err+'update client node failure');
var client = node;
// 2.
// update relay server-node
SdpDB.updateRelaysrv(self.srvInfo.gid, self.srvInfo, function(err, node){
if (err || !node) return fn(err+',update relay server node failure');
var server = node;
// 3.
// update turn proxy session from client to relay proxy server
var sessinfo = {
type: SdpDB.SESSION_TURN_PROXY,
data: self.sessionInfo
};
SdpDB.updateSession(client, server, sessinfo, function(err, session){
if (err || !session) return fn(err+'update turn proxy session failure');
// 4.
// emit update event
self.emit('update', {client: client, server: server, session: session});
// 5.
// ...
// 6.
// pass TURN proxy session back
fn(null, {client: client, server: server, session: session});
});
});
});
};
// class methods
// get turn proxy session
exports.getTurnProxyByClnt = function(clntid, fn){
SdpDB.getSessionByFromType({gid: clntid}, {type: SdpDB.SESSION_TURN_PROXY}, fn);
};
exports.getTurnProxyBySrv = function(srvid, fn){
SdpDB.getSessionByToType({gid: srvid}, {type: SdpDB.SESSION_TURN_PROXY}, fn);
};
exports.getAllTurnProxy = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_TURN_PROXY}, fn);
};
Turn.Proxy = turnProxy;
// turn Agent session: agent server to client
var turnAgent = function(session){
var self = this;
var clntgidhex = '';
// super constructor
eventEmitter.call(self);
// calculate clntgid in case anonymous client
if (session.clntgid) {
clntgidhex = genHuid(session.clntgid);
} else {
clntgidhex = session.clntgid = genClntid(session);
}
// Session info
self.sessionInfo = {
sid: session.sid,
proto: session.proto, // session's protocol: tcp/udp/sctp/rtp/rtcp, etc
mode: session.mode, // p2p or c/s
// client's outer and public network address
clntid: session.clntgid,
clntIP: session.clntpublicIP,
clntPort: session.clntpublicPort,
// relay proxy/agent server outer and public network address
srvid: genSrvid(session),
srvDN: session.srvpublicDN, // domain name
srvIP: session.srvpublicIP,
proxyPort: session.srvproxyPort,
agentPort: session.srvagentPort,
// media,sensor parameters. video resolution/fps/bps, sensor resolution/sps, etc
// notes: parameters MUST be negotiated between peers
parameter: session.parameter || '',
// timestamp
start: session.start || Date.now()
};
// Client info
// notes: a client defined as a device with local proto/ip/port
self.clntInfo = {
// client's inner and local network address
// notes: user always binds on one ip/port in device to punch hole easily
localIP: session.clntlocalIP,
localPort: session.clntlocalPort,
// client's device info
devkey: session.devkey, // client device's serial number info: phone, pc, tablet, etc
devcap: session.devcap || '', // client sevice's capabilities: video/audio/stroage medias, sensors/ecg, etc
// NAT type behind client
// notes: assume asymmetric in default :)
// !!! NAT type can be determined in client side
///natype: session.natype || SdpDB.NAT_ASYM, // symmetric, asymmetric, etc
// client gid
gid: session.clntgid,
// client vURL vpath or vhost
// TBD... allocation schema
vpath: '/vurl/'+clntgidhex, // '/vurl/gid' in default by now
vhost: clntgidhex+'.vurl.', // 'gid.vurl.51dese.com' like append on 51dese.com
vmode: session.vmode, // vURL mode
vtoken: session.vtoken, // vURL secure token
// client security mode level-based
secmode: session.secmode
};
// Relay server info
self.srvInfo = {
dn: session.srvpublicDN, // domain name
ip: session.srvpublicIP, // outter/public ip
proxyport: session.srvproxyPort, // outter/public proxy port
agentport: session.srvagentPort, // outter/public agent port
gid: genSrvid(session), // Gid
localIP: session.srvlocalIP, // inner/local ip or interface
localproxyPort: session.srvlocalproxyPort, // inner/local proxy port
localagentPort: session.srvlocalagentPort // inner/local agent port
};
};
util.inherits(turnAgent, eventEmitter);
// instance methods
turnAgent.prototype.saveOupdate = function(fn){
var self = this;
// 1.
// update client-node
SdpDB.updateClient(self.clntInfo.gid, self.clntInfo, function(err, node){
if (err || !node) return fn(err+'update client node failure');
var client = node;
// 2.
// update relay server-node
SdpDB.updateRelaysrv(self.srvInfo.gid, self.srvInfo, function(err, node){
if (err || !node) return fn(err+',update relay server node failure');
var server = node;
// 3.
// update turn agent session from relay agent server to client
var sessinfo = {
type: SdpDB.SESSION_TURN_AGENT,
data: self.sessionInfo
};
SdpDB.updateSession(server, client, sessinfo, function(err, session){
if (err || !session) return fn(err+'update turn agent session failure');
// 4.
// emit update event
self.emit('update', {client: client, server: server, session: session});
// 5.
// ...
// 6.
// pass TURN agent session back
fn(null, {client: client, server: server, session: session});
});
});
});
};
// class methods
// get turn agent session
exports.getTurnAgentByClnt = function(clntid, fn){
SdpDB.getSessionByToType({gid: clntid}, {type: SdpDB.SESSION_TURN_AGENT}, fn);
};
exports.getTurnAgentBySrv = function(srvid, fn){
SdpDB.getSessionByFromType({gid: srvid}, {type: SdpDB.SESSION_TURN_AGENT}, fn);
};
exports.getAllTurnAgent = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_TURN_AGENT}, fn);
};
Turn.Agent = turnAgent;