-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpeer-manager.js
607 lines (531 loc) · 20.6 KB
/
peer-manager.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
'use strict'
console.log('Loading PeerManager')
const FINAL_ERRORS = ['browser-incompatible', 'invalid-id', 'invalid-key', 'ssl-unavailable', 'unavailable-id']
const DEFAULT_ICE_SERVERS = [{
"url": "stun:stun.l.google.com:19302",
"urls": "stun:stun.l.google.com:19302"
}, {
"url": "stun:global.stun.twilio.com:3478?transport=udp",
"urls": "stun:global.stun.twilio.com:3478?transport=udp"
}]
const RECONNECT_BACKOFF = 10
const RECONNECT_DELAY_START = 25
const MAX_RECONNECT_ATTEMPTS = 2
const PUBLISH_URL_BASE = 'https://pubsub.turbovpb.com/c/'
const SUBSCRIBE_URL_BASE = 'wss://pubsub.turbovpb.com/c/'
const WEBRTC_MODE = 'webrtc'
const WEBSOCKET_MODE = 'websocket'
const ENCRYPTION_KEY_LENGTH = 256
const ENCRYPTION_IV_BYTE_LENGTH = 12
const ENCRYPTION_ALGORITHM = 'AES-GCM'
const BASE64_URL_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
const BASE64_URL_LOOKUP = new Uint8Array(256);
for (let i = 0; i < BASE64_URL_CHARACTERS.length; i++) {
BASE64_URL_LOOKUP[BASE64_URL_CHARACTERS.charCodeAt(i)] = i;
}
const PUBSUB_STATE = {
CLOSED: 'CLOSED',
CONNECTING: 'CONNECTING',
OPEN: 'OPEN',
CONNECTED: 'CONNECTED'
}
class PeerManager {
constructor({ debugMode, remotePeerId, encryptionKey }) {
this.debugMode = debugMode
this.remotePeerId = remotePeerId
this.iceServers = null
this.active = false
this.peer = null
this.connection = null
this.onmessage = () => { }
this.onconnect = () => { }
this.onerror = () => { }
this.onreconnecting = () => { }
this.isConnecting = false
this.reconnectDelay = RECONNECT_DELAY_START
this.reconnectAttempts = 0
this.reconnectResolves = []
this.encryptionKey = encryptionKey
this.mode = this.encryptionKey ? WEBSOCKET_MODE : WEBRTC_MODE
this.fallbackMode = this.mode === WEBRTC_MODE ? WEBSOCKET_MODE : WEBRTC_MODE
this.ws = null
this.pubsubState = PUBSUB_STATE.CLOSED
}
static async from(opts) {
if (opts.encryptionKey) {
try {
opts.encryptionKey = await importKey(opts.encryptionKey)
} catch (err) {
console.error('Error importing key. WebSocket mode is disabled.', err)
opts.encryptionKey = null
}
return new PeerManager(opts)
}
}
async reconnect(err, immediate) {
if (this.active === false) {
console.warn('The PeerManager was already stopped, not reconnecting')
return
}
if (err && err.type && FINAL_ERRORS.includes(err.type)) {
console.warn('Not retrying final error:', err.type)
return this.onerror(err)
}
if (this.isConnecting) {
console.log('already reconnecting')
return new Promise((resolve) => {
this.reconnectResolves.push(resolve)
})
}
this.isConnecting = true
console.log('PeerManager reconnecting')
if (++this.reconnectAttempts > MAX_RECONNECT_ATTEMPTS) {
console.error('exceeded max number of reconnect attempts')
// Try swtiching to the fallback mode
if (this.mode !== this.fallbackMode) {
if (this.mode === WEBRTC_MODE) {
if (!this.encryptionKey) {
console.warn('cannot switch to websocket mode because the browser does not support SubtleCrypto')
} else {
console.log('switching to websocket mode')
this.mode = WEBSOCKET_MODE
this.reconnectDelay = RECONNECT_DELAY_START
this.reconnectAttempts = 0
return this.connect()
}
} else {
console.log('switching to webrtc mode')
this.mode = WEBRTC_MODE
this.reconnectDelay = RECONNECT_DELAY_START
this.reconnectAttempts = 0
return this.connect()
}
}
if (err) {
return this.onerror(err)
} else {
return this.onerror(new Error('Exceeded maximum number of reconnection attempts'))
}
}
if (!immediate) {
console.log(`waiting ${this.reconnectDelay}ms before reconnecting`)
await new Promise((resolve) => {
setTimeout(() => {
this.reconnectDelay = this.reconnectDelay * RECONNECT_BACKOFF
resolve()
}, this.reconnectDelay)
})
}
await this.connect()
this.reconnectDelay = RECONNECT_DELAY_START
this.reconnectAttempts = 0
}
async connect(websocketMode = false) {
this.active = true
this.isConnecting = true
// TODO maybe try racing both modes
if (websocketMode) {
if (!this.encryptionKey) {
throw new Error('Cannot use WebSocket mode because the browser\'s cryptography API is unavailable')
}
this.mode = WEBSOCKET_MODE
this.peer.onerror = () => { }
this.peer.onclose = () => { }
this.peer.destroy()
}
if (this.isConnected()) {
console.log('already connected')
} else {
console.log(`connecting (mode: ${this.mode})`)
// Use Sentry tracing to measure performance
let span
if (Sentry && typeof Sentry.startTransaction === 'function') {
span = Sentry.startTransaction({
name: `${this.mode}.connect`,
tags: {
connection_mode: this.mode
}
})
Sentry.configureScope((scope) => scope.setSpan(span))
}
try {
if (this.mode === WEBRTC_MODE) {
if (!this.iceServers) {
this.iceServers = await wrapWithTracingSpan(span, 'getIceServers', () => getIceServers())
}
await wrapWithTracingSpan(span, 'checkPeerConnected', () => this._checkPeerConnected())
await wrapWithTracingSpan(span, 'checkConnectionOpen', () => this._checkConnectionOpen())
} else {
await wrapWithTracingSpan(span, 'connectPubSub', () => this._connectPubSub())
}
this.isConnecting = false
await this.onconnect()
} catch (err) {
this.isConnecting = false
return wrapWithTracingSpan(span, 'reconnect', () => this.reconnect(err))
}
if (span) {
span.finish()
}
}
// Resolve all of the reconnect calls that were
// called while we were already reconnecting
let resolve
while (resolve = this.reconnectResolves.pop()) {
resolve()
}
}
async sendMessage(message) {
if (this.active === false) {
// TODO maybe it's better to throw an error here?
console.error('Not sending message because PeerManager has already been stopped')
return
}
console.log('sending message', message)
if (this.mode === WEBRTC_MODE) {
if (this.connection && this.connection.open) {
this.connection.send(message)
} else {
console.log('trying to send message but connection is not open, reconnecting first')
await this.reconnect()
this.connection.send(message)
}
} else {
await this._connectPubSub()
const encrypted = await encrypt(this.encryptionKey, message)
this.ws.send(encrypted)
}
}
stop() {
console.log('stopping peer manager')
this.active = false
if (this.peer) {
this.peer.onerror = () => { }
this.peer.onclose = () => { }
this.peer.destroy()
}
if (this.ws) {
this.ws.onerror = () => { }
this.ws.onclose = () => { }
this.ws.close()
}
}
isConnected() {
if (this.mode === WEBRTC_MODE) {
return this.active
&& !!this.peer && !this.peer.disconnected && !this.peer.destroyed
&& !!this.connection && this.connection.open
} else {
return !!this.ws && this.ws.readyState === WebSocket.OPEN
}
}
isStopped() {
return !this.active
}
isWebSocketMode() {
return this.mode === WEBSOCKET_MODE
}
async _checkPeerConnected() {
if (this.peer) {
if (!this.peer.destroyed && !this.peer.disconnected) {
console.log('peer is still connected')
return
}
console.log('peer was not connected, destroying the old one')
this.peer.destroy()
}
console.log('creating new peer')
await this.onreconnecting('Server')
const startTime = Date.now()
this.peer = await createPeer({
iceServers: this.iceServers,
debugMode: this.debugMode
})
console.log(`peer connected to server (took ${Date.now() - startTime}ms)`)
this.peer.on('error', async (err) => {
console.error('peer error', err)
if (this.mode === WEBRTC_MODE) {
this._closeConnection()
return this.reconnect(err)
}
})
this.peer.on('disconnect', async () => {
console.warn('peer disconnected')
if (this.mode === WEBRTC_MODE) {
this._closeConnection()
return this.reconnect()
}
})
}
_closeConnection() {
if (this.connection) {
this.connection.close()
this.connection = null
}
}
async _checkConnectionOpen() {
if (this.connection) {
if (this.connection.open) {
console.log('connection still open')
return
} else {
this.connection.close()
}
}
console.log('connecting to ', remotePeerId)
await this.onreconnecting('Extension')
const startTime = Date.now()
this.connection = await createConnection({
peer: this.peer,
remotePeerId: this.remotePeerId
})
console.log(`connected to extension (took ${Date.now() - startTime}ms)`)
this.connection.on('data', (data) => {
console.log('got data from peer', data)
this.onmessage(data)
})
this.connection.on('close', async () => {
console.warn('peer connection closed')
this.connection = null
if (this.mode === WEBRTC_MODE) {
return this.reconnect()
}
})
this.connection.on('error', async (err) => {
console.error('peer connection error', err)
this.connection = null
if (this.mode === WEBRTC_MODE) {
return this.reconnect(err)
}
})
}
/**
* Connect or reconnect to the WebSocket PubSub endpoint
* Note we only consider it to be "connected" after we
* have received a message (rather than just when the connection opens)
*/
async _connectPubSub() {
if (this.pubsubState !== PUBSUB_STATE.CLOSED) {
console.log(`websocket already ${this.pubsubState.toLowerCase()}`)
return
}
this.pubsubState = PUBSUB_STATE.CONNECTING
if (this.mode === WEBSOCKET_MODE) {
this.onreconnecting('Server')
}
// Close the old websocket
if (this.ws) {
// Note: we don't use the ws.reconnect method
// because it seems slower than just creating the ws again
console.log('closing old websocket and creating a new one')
this.ws.onclose = () => { }
this.ws.onerror = () => { }
this.ws.onopen = () => { }
this.ws.onmessage = () => { }
this.ws.close()
}
return new Promise((resolve, reject) => {
const url = `${SUBSCRIBE_URL_BASE}${this.remotePeerId}/browser`
console.log('connecting to:', url)
let startTime = Date.now()
let openTime
if (typeof ReconnectingWebSocket === 'function') {
console.log('Using ReconnectingWebSocket')
this.ws = new ReconnectingWebSocket(url, [], {
minReconnectionDelay: RECONNECT_BACKOFF,
maxRetries: MAX_RECONNECT_ATTEMPTS,
debug: true
})
} else {
console.warn('ReconnectingWebSocket not found, using normal WebSocket')
this.ws = new WebSocket(url)
}
this.ws.binaryType = 'arraybuffer'
this.ws.onopen = () => {
openTime = Date.now()
console.log(`websocket open (took ${Date.now() - startTime}ms)`)
this.pubsubState = PUBSUB_STATE.OPEN
// We only consider it connected when we get a message from the extension
if (this.mode === WEBSOCKET_MODE) {
this.onreconnecting('Extension')
}
this.sendMessage({ type: 'connect' })
}
this.ws.onclose = (event) => {
const reason = event ? event.reason : ''
if (openTime) {
console.warn(`websocket closed (after ${Date.now() - openTime}ms). reason: ${reason}`)
} else {
console.warn(`websocket closed. reason: ${reason}`)
}
startTime = Date.now()
this.pubsubState = PUBSUB_STATE.CLOSED
if (this.mode === WEBSOCKET_MODE) {
if (typeof ReconnectingWebSocket === 'function') {
this.onreconnecting('Server')
} else {
this.onerror(new Error(`WebSocket closed (reason: ${reason || 'unknown'})`))
}
}
}
this.ws.onerror = (event) => {
let err
if (event instanceof Error) {
err = event
} else if (typeof event === 'object') {
if (event.error) {
err = event.error
} else if (event.message) {
err = new Error(`WebSocket Error: ${event.message}`)
} else {
err = new Error('Websocket Error')
}
} else {
err = new Error('Websocket Error')
}
if (openTime) {
console.error(`websocket error (after ${Date.now() - openTime}ms)`, event)
} else {
console.error('websocket error', event)
}
this.pubsubState = PUBSUB_STATE.CLOSED
// Only call reconnect if ReconnectingWebSocket isn't already trying to reconnect
if (this.ws.readyState !== WebSocket.CONNECTING && this.mode === WEBSOCKET_MODE) {
this.reconnect(err)
}
reject(err)
}
this.ws.onmessage = async ({ data }) => {
if (this.mode !== WEBSOCKET_MODE) {
console.warn('got websocket message when not in websocket mode', data)
return
}
try {
const message = await decrypt(this.encryptionKey, data)
console.log('got data from pubsub', message)
if (this.pubsubState !== PUBSUB_STATE.CONNECTED) {
this.onconnect()
this.pubsubState = PUBSUB_STATE.CONNECTED
resolve()
}
this.onmessage(message)
} catch (err) {
console.error('got invalid message from pubsub', err)
}
}
})
}
}
async function getIceServers() {
try {
const response = await fetch('https://nts.turbovpb.com/ice')
// WebRTC is slow with more than 2 ICE servers
const iceServers = (await response.json()).slice(0, 2)
console.log('using ICE servers:', iceServers)
return iceServers
} catch (err) {
console.error('error getting ice servers', err)
return DEFAULT_ICE_SERVERS
}
}
async function createPeer({ iceServers, debugMode }) {
return new Promise((resolve, reject) => {
const peer = new Peer({
host: 'peerjs.turbovpb.com',
secure: true,
debug: debugMode ? 2 : 1,
config: {
iceServers
}
})
peer.on('error', (err) => console.error(`peer error (type: ${err.type})`, err))
peer.on('disconnect', () => console.warn('peer disconnected'))
peer.once('open', () => resolve(peer))
peer.once('error', reject)
peer.once('disconnect', () => reject(new Error('Peer disconnected')))
setTimeout(() => reject(new Error('Connection timed out while trying to reach peer server')), 10000)
})
}
async function createConnection({ peer, remotePeerId }) {
return new Promise((resolve, reject) => {
const conn = peer.connect(remotePeerId, {
serialization: 'json'
})
conn.on('error', (err) => console.error('connection error', err))
conn.on('close', () => console.warn('connection closed'))
conn.once('open', () => resolve(conn))
conn.once('error', reject)
conn.once('close', () => reject(new Error('Connection closed')))
setTimeout(() => reject(new Error('Connection timed out while trying to connect to extension')), 10000)
})
}
async function importKey(base64) {
if (!crypto || !crypto.subtle || typeof crypto.subtle.importKey !== 'function') {
throw new Error('SubtleCrypto API is not supported')
}
const buffer = decodeBase64Url(base64)
return crypto.subtle.importKey('raw', buffer, {
name: ENCRYPTION_ALGORITHM
}, true, ['encrypt', 'decrypt'])
}
async function encrypt(encryptionKey, message) {
if (typeof message === 'object') {
message = JSON.stringify(message)
}
const buffer = (new TextEncoder()).encode(message)
const iv = crypto.getRandomValues(new Uint8Array(ENCRYPTION_IV_BYTE_LENGTH))
const ciphertext = new Uint8Array(await crypto.subtle.encrypt({
name: 'AES-GCM',
iv
}, encryptionKey, buffer))
const payload = new Uint8Array(ciphertext.byteLength + ENCRYPTION_IV_BYTE_LENGTH)
payload.set(ciphertext, 0)
payload.set(iv, ciphertext.byteLength)
return payload
}
async function decrypt(encryptionKey, arrayBuffer) {
const payload = new Uint8Array(arrayBuffer)
const ciphertext = payload.slice(0, 0 - ENCRYPTION_IV_BYTE_LENGTH)
const iv = payload.slice(0 - ENCRYPTION_IV_BYTE_LENGTH)
const plaintext = await crypto.subtle.decrypt({
name: 'AES-GCM',
iv
}, encryptionKey, ciphertext)
const string = (new TextDecoder()).decode(plaintext)
return JSON.parse(string)
}
// Based on https://github.com/herrjemand/Base64URL-ArrayBuffer/blob/master/lib/base64url-arraybuffer.js
function decodeBase64Url(base64) {
base64 = base64.replace(/[=]+$/, '')
let bufferLength = base64.length * 0.75
const arraybuffer = new ArrayBuffer(bufferLength)
const bytes = new Uint8Array(arraybuffer)
let p = 0
let encoded1, encoded2, encoded3, encoded4
for (let i = 0; i < base64.length; i += 4) {
encoded1 = BASE64_URL_LOOKUP[base64.charCodeAt(i)]
encoded2 = BASE64_URL_LOOKUP[base64.charCodeAt(i + 1)]
encoded3 = BASE64_URL_LOOKUP[base64.charCodeAt(i + 2)]
encoded4 = BASE64_URL_LOOKUP[base64.charCodeAt(i + 3)]
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4)
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2)
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63)
}
return arraybuffer
}
async function wrapWithTracingSpan(span, spanName, promise) {
if (!span) {
if (typeof promise === 'function') {
return (promise)()
} else {
return promise
}
}
const child = span.startChild({ op: spanName })
if (typeof promise === 'function') {
await (promise)()
} else {
await promise
}
child.finish()
}