-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.js
336 lines (296 loc) · 8.78 KB
/
util.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
/* exported Util, Params, DummyRateLimit */
/* global Config */
/*
A JavaScript WebRTC snowflake proxy
Contains helpers for parsing query strings and other utilities.
*/
class Util {
static genSnowflakeID() {
return Math.random().toString(36).substring(2);
}
static hasWebRTC() {
return typeof RTCPeerConnection === 'function';
}
static hasCookies() {
return navigator.cookieEnabled;
}
// returns a promise that resolves to "restricted" if we
// fail to make a test connection to a known restricted
// NAT, "unrestricted" if the test connection succeeds, and
// "unknown" if we fail to reach the probe test server
static checkNATType(timeout) {
let pc = new RTCPeerConnection({iceServers: [
{urls: 'stun:stun1.l.google.com:19302'}
]});
let channel = pc.createDataChannel("NAT test");
return (new Promise((fulfill, reject) => {
let open = false;
channel.onopen = function() {
open = true;
fulfill("unrestricted");
};
pc.onicecandidate = (evt) => {
if (evt.candidate == null) {
//ice gathering is finished
Util.sendOffer(pc.localDescription)
.then((answer) => {
setTimeout(() => {
if(!open) {
fulfill("restricted");
}
}, timeout);
pc.setRemoteDescription(JSON.parse(answer));
}).catch((e) => {
console.log(e);
reject("Error receiving probetest answer");
});
}
};
pc.createOffer()
.then((offer) => pc.setLocalDescription(offer))
.catch((e) => {
console.log(e);
reject("Error creating offer for probetest");
});
}).finally(() => {
channel.close();
pc.close();
}));
}
// Assumes getClientOffer happened, and a WebRTC SDP answer has been generated.
// Sends it back to the broker, which passes it back to the original client.
static sendOffer(offer) {
return new Promise((fulfill, reject) => {
var xhr;
xhr = new XMLHttpRequest();
xhr.timeout = 30 * 1000;
xhr.onreadystatechange = function() {
if (xhr.DONE !== xhr.readyState) {
return;
}
switch (xhr.status) {
case 200:
var response = JSON.parse(xhr.responseText);
return fulfill(response.Answer); // Should contain offer.
default:
console.log('Probe ERROR: Unexpected ' + xhr.status + ' - ' + xhr.statusText);
return reject('Failed to get answer from probe service');
}
};
var data = {"Status": "client match", "Offer": JSON.stringify(offer)};
try {
xhr.open('POST', Config.PROBEURL);
} catch (error) {
console.log('Signaling Server: exception while connecting: ' + error.message);
return reject('unable to connect to signaling server');
}
return xhr.send(JSON.stringify(data));
});
}
}
class Parse {
// Parse a cookie data string (usually document.cookie). The return type is an
// object mapping cookies names to values. Returns null on error.
// http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038
static cookie(cookies) {
var i, j, len, name, result, string, strings, value;
result = {};
strings = [];
if (cookies) {
strings = cookies.split(';');
}
for (i = 0, len = strings.length; i < len; i++) {
string = strings[i];
j = string.indexOf('=');
if (-1 === j) {
return null;
}
name = decodeURIComponent(string.substr(0, j).trim());
value = decodeURIComponent(string.substr(j + 1).trim());
if (!(name in result)) {
result[name] = value;
}
}
return result;
}
// Parse an address in the form 'host:port'. Returns an Object with keys 'host'
// (String) and 'port' (int). Returns null on error.
static address(spec) {
var host, m, port;
m = null;
if (!m) {
// IPv6 syntax.
m = spec.match(/^\[([\0-9a-fA-F:.]+)\]:([0-9]+)$/);
}
if (!m) {
// IPv4 syntax.
m = spec.match(/^([0-9.]+):([0-9]+)$/);
}
if (!m) {
// TODO: Domain match
return null;
}
host = m[1];
port = parseInt(m[2], 10);
if (isNaN(port) || port < 0 || port > 65535) {
return null;
}
return {
host: host,
port: port
};
}
// Parse a count of bytes. A suffix of 'k', 'm', or 'g' (or uppercase)
// does what you would think. Returns null on error.
static byteCount(spec) {
let matches = spec.match(/^(\d+(?:\.\d*)?)(\w*)$/);
if (matches === null) {
return null;
}
let count = Number(matches[1]);
if (isNaN(count)) {
return null;
}
const UNITS = new Map([
['', 1],
['k', 1024],
['m', 1024*1024],
['g', 1024*1024*1024],
]);
let unit = matches[2].toLowerCase();
if (!UNITS.has(unit)) {
return null;
}
let multiplier = UNITS.get(unit);
return count * multiplier;
}
//Parse a remote connection-address out of the "c=" Connection Data field
// or the "a=" attribute fields of the session description.
// Return undefined if none is found.
// https://tools.ietf.org/html/rfc4566#section-5.7
// https://tools.ietf.org/html/rfc5245#section-15
static ipFromSDP(sdp) {
var i, len, m, pattern, ref;
console.log(sdp);
ref = [
/^a=candidate:[a-zA-Z0-9+/]+ \d+ udp \d+ ([\d.]+) /mg,
/^a=candidate:[a-zA-Z0-9+/]+ \d+ udp \d+ ([0-9A-Fa-f:.]+) /mg,
/^c=IN IP4 ([\d.]+)(?:(?:\/\d+)?\/\d+)?(:? |$)/mg,
/^c=IN IP6 ([0-9A-Fa-f:.]+)(?:\/\d+)?(:? |$)/mg
];
for (i = 0, len = ref.length; i < len; i++) {
pattern = ref[i];
m = pattern.exec(sdp);
while (m != null) {
if(Parse.isRemoteIP(m[1])) return m[1];
m = pattern.exec(sdp);
}
}
}
// Parse the mapped port out of an ice candidate returned from the
// onicecandidate callback
static portFromCandidate(c) {
var m, pattern;
pattern = /(?:[\d.]+|[0-9A-Fa-f:.]+) (\d+) typ srflx/m;
m = pattern.exec(c);
if (m != null) {
return m[1];
}
return null;
}
// Determine whether an IP address is a local, unspecified, or loopback address
static isRemoteIP(ip) {
if (ip.includes(":")) {
var ip6 = ip.split(':');
// Loopback address
var loopback = /^(?:0*:)*?:?0*1$/m;
// Unspecified address
var unspecified = /^(?:0*:)*?:?0*$/m;
// Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193
return !((loopback.exec(ip) != null) || (unspecified.exec(ip) != null) ||
(parseInt(ip6[0],16)&0xfe00) == 0xfc00);
}
// Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918
var ip4 = ip.split('.');
return !(ip4[0] == 10 || ip4[0] == 127 || ip == "0.0.0.0" ||
(ip4[0] == 172 && (ip4[1]&0xf0) == 16) ||
(ip4[0] == 192 && ip4[1] == 168) ||
// Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598
(ip4[0] == 100 && (ip4[1]&0xc0) == 64) ||
// Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927
(ip4[0] == 169 && ip4[1] == 254));
}
}
class Params {
static getBool(query, param, defaultValue) {
if (!query.has(param)) {
return defaultValue;
}
var val;
val = query.get(param);
if ('true' === val || '1' === val || '' === val) {
return true;
}
if ('false' === val || '0' === val) {
return false;
}
return null;
}
// Get an object value and parse it as a byte count. Example byte counts are
// '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null
// on a parsing error.
static getByteCount(query, param, defaultValue) {
if (!query.has(param)) {
return defaultValue;
}
return Parse.byteCount(query.get(param));
}
}
class BucketRateLimit {
constructor(capacity, time) {
this.capacity = capacity;
this.time = time;
}
age() {
var delta, now;
now = new Date();
delta = (now - this.lastUpdate) / 1000.0;
this.lastUpdate = now;
this.amount -= delta * this.capacity / this.time;
if (this.amount < 0.0) {
return this.amount = 0.0;
}
}
update(n) {
this.age();
this.amount += n;
return this.amount <= this.capacity;
}
// How many seconds in the future will the limit expire?
when() {
this.age();
return (this.amount - this.capacity) / (this.capacity / this.time);
}
isLimited() {
this.age();
return this.amount > this.capacity;
}
}
BucketRateLimit.prototype.amount = 0.0;
BucketRateLimit.prototype.lastUpdate = new Date();
// A rate limiter that never limits.
class DummyRateLimit {
constructor(capacity, time) {
this.capacity = capacity;
this.time = time;
}
update() {
return true;
}
when() {
return 0.0;
}
isLimited() {
return false;
}
}