-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdp.js
292 lines (237 loc) · 10.9 KB
/
sdp.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
// Copyright (c) 2012-present Tom Zhou<[email protected]>
//
// Session Description protocol model:
// 1. when user login to nameserver, nameserver record user's connection info, session
// id and user identify, etc;
// 2. when user want to connect to peer, nameserver decide how to punch hole and setup
// p2p connection;
// 3. when user logout, nameserver update user's session to offline, and notify peers;
// 4. when user relogin, nameserver update user's session with new connection info and
// notify peers;
// 5. nameserver basically maintain a live connection state-machine and cooperate with peers;
// 6. to punch hole easily, the client as user always binds one same ip/port on the device
// 7. for details, please refer to AppNet_SW_Func_Spec.doc and Roadisys_Virtual_URL_SW_Func_Spec.doc
// ...
'use strict';
var debug = require('debug')('sdp');
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 SDP
var genUsrid = function(usrinfo){
return genHuid('usr-'+usrinfo.usrkey+'-'+usrinfo.domain);
};
var genClntid = function(clntinfo){
return genHuid(clntinfo.proto+'-clnt-'+clntinfo.devkey+'-'+clntinfo.clntlocalIP+'-'+clntinfo.clntlocalPort);
};
var genSrvid = function(srvinfo){
return genHuid(srvinfo.proto+'-srv-'+srvinfo.srvpublicIP+'-'+srvinfo.srvpublicPort);
};
// gid stands for nodeGID
var Sdp = module.exports = 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
// session id globally unique between nameserver and client
// notes: actually sid = nameserver info + session id within this nameserver
// for example, nameserver IP:port + sid
self.sessionInfo = {
sid: session.sid,
proto: session.proto, // session's protocol: tcp/udp/sctp/rtp/rtcp, etc
// set live flag as true
live: 1,
// client's outer and publick network address
// notes: public ip/port respects NAT info
clntid: session.clntgid,
clntIP: session.clntpublicIP,
clntPort: session.clntpublicPort,
// routerpath info from nameserver to client direction
// notes: routerpath info used to setup efficient p2p session traverse,
// that every element in array is a pair of (from, to) struct
routerpath: session.routerpath || '',
// timestamp
start: session.start || Date.now()
};
// Server info
// nameserver's public network address
// notes:
// 1. to detect client firewall type, client will bind at the same udp port
// and connect to two nameserver port at same time;
// 2. if client's two public IP/Port are same, then client is in non-symetric firewall
// 3. otherwise, client is behind symetric firewall
// 4. if both peer are behind symetric firewall, we need to TURN server to setup p2p connection
self.srvInfo = {
dn: session.srvpublicDN, // domain name
ip: session.srvpublicIP, // outter/public ip
port: session.srvpublicPort, // outter/public port. even port: 51868, odd port: 51869
gid: genSrvid(session), // Gid
localIP: session.srvlocalIP, // inner/local ip or interface
localPort: session.srvlocalPort // inner/local port
};
// Client info
// notes: a client defined as a device with local proto/ip/port
self.clntInfo = {
// set live flag as true
live: 1,
// client's Geo info, like below:
// {range: [ 3479299040, 3479299071 ], country: 'US', region: 'CA', city: 'San Francisco', ll: [37.7484, -122.4156] }
geoip: session.clntgeoip,
// 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
// TURN agent session state: true - ready, false - not ready
turnagentReady: session.turnagentReady || false,
// 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, it't querystring by now like ?vtoken=xxx
// client security mode level-based. default level is 1
// - 0: disable https/wss
// - 1: enable https/wss,
// allow turn-agent connection,
// allow host-only-based-token authentication with stun session
// - 2: enable https/wss,
// allow turn-agent connection,
// allow host-port-based-token authentication with stun session
secmode: session.secmode
};
// User info
self.usrInfo = {
// application specific identify for user
// notes: appkey used to identify a user on a live connection
// nameserver support multiple user login, that means a user
// can login from the different device at same time.
// !!! in a device, user can be only login uniquely.
// !!! devkey+(local IP/Port) can identify a client for user,
// !!! devkey+(local IP/Port)+(nameserver IP/Port) can identify one SDP record;
// !!! (public IP/Port) can identify a client for user as well, ???
// !!! (public IP/Port)+(nameserver IP/Port) can identify one SDP record as well. ???
domain: session.domain,
usrkey: session.usrkey,
gid: genUsrid(session),
// application info, optional
appkey: session.appkey || '', // app route path get/post ..., optional
grpkey: session.grpkey || '' // user group info, optional
};
};
util.inherits(Sdp, eventEmitter);
// db hook
Sdp.db = SdpDB;
// instance method
Sdp.prototype.saveOupdate = function(fn){
var self = this;
// 1.
// update user-node
SdpDB.updateUser(self.usrInfo.gid, self.usrInfo, function(err, node){
if (err || !node) return fn(err+',update user node failure');
var user = node;
// 2.
// 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;
// 3.
// update nameserver-node
SdpDB.updateNmsrv(self.srvInfo.gid, self.srvInfo, function(err, node){
if (err || !node) return fn(err+',update name server node failure');
var server = node;
// 4.
// update the session between user and client
// notes: sessionInfo is like {type: xxx, data: {xxx}}
var sessinfo = {type: SdpDB.SESSION_LOGIN, data: {start: self.sessionInfo.start}};
SdpDB.updateSession(user, client, sessinfo, function(err, session){
if (err || !session) return fn(err+',update login session failure');
var login = session;
// 5.
// update the session between client and server
sessinfo = {type: SdpDB.SESSION_SDP, data: self.sessionInfo};
SdpDB.updateSession(client, server, sessinfo, function(err, session){
if (err || !session) return fn(err+',update sdp session failure');
var sdp = session;
// 6.
// update the route session between client and server, in case tracerouter available
// TBD...
// 7.
// emit update event
self.emit('update', {user: user, client: client, server: server, login: login, sdp: sdp});
// 8.
// pass SDP back
fn(null, {user: user, client: client, server: server, login: login, sdp: sdp});
///console.log('update SDP session:'+JSON.stringify({user: user, client: client, server: server, login: login, sdp: sdp}));
});
});
});
});
});
};
// class method
// get login session
Sdp.getLoginByUsr = function(usrid, fn){
SdpDB.getSessionByFromType({gid: usrid}, {type: SdpDB.SESSION_LOGIN}, fn);
};
Sdp.getAllLogin = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_LOGIN}, fn);
};
// get sdp session
Sdp.getSdpByClnt = function(clntid, fn){
SdpDB.getSessionByFromType({gid: clntid}, {type: SdpDB.SESSION_SDP}, fn);
};
Sdp.getAllSdp = function(fn){
SdpDB.getSessionsByType({type: SdpDB.SESSION_SDP}, fn);
};
// update sdp session
Sdp.updateSdpByClnt = function(clntid, sesninfo, fn){
sesninfo.type = SdpDB.SESSION_SDP;
SdpDB.updateSessionsByFromType({gid: clntid}, sesninfo, fn);
};
// get user info
Sdp.getAllUsrs = function(fn){
SdpDB.getUsers(fn);
};
// update client info, used to update NAT type normally
Sdp.updateClntInfo = function(clntinfo, fn){
SdpDB.updateClient(clntinfo.gid, clntinfo, function(err, node){
if (err || !node) return fn(err+',update client info failed');
fn(null, node);
});
};
// test user existence
Sdp.testUser = function(usrinfo, fn){
SdpDB.testNodeById(genUsrid(usrinfo), fn);
};
// export gid generator
Sdp.genUsrid = genUsrid;
Sdp.genClntid = genClntid;
Sdp.genSrvid = genSrvid;