-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxypair.js
338 lines (305 loc) · 9.87 KB
/
proxypair.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
/* global snowflake, log, dbg, debug, Util, Parse, WS */
/**
Represents a single:
client <-- webrtc --> snowflake <-- websocket --> relay
Every ProxyPair has a Snowflake ID, which is necessary when responding to the
Broker with an WebRTC answer.
*/
class ProxyPair {
/**
* @param {DummyRateLimit | BucketRateLimit} rateLimit specifies a rate limit on traffic
* @param {Config} config
*/
constructor(rateLimit, config) {
this.prepareDataChannel = this.prepareDataChannel.bind(this);
this.connectRelay = this.connectRelay.bind(this);
this.onClientToRelayMessage = this.onClientToRelayMessage.bind(this);
this.onRelayToClientMessage = this.onRelayToClientMessage.bind(this);
this.onError = this.onError.bind(this);
this.flush = this.flush.bind(this);
/** @type {string | URL} */
this.relayURL = config.defaultRelayAddr;
this.rateLimit = rateLimit;
this.config = config;
this.id = Util.genSnowflakeID();
this.c2rSchedule = [];
this.r2cSchedule = [];
this.nowConnected = false;
}
/** Prepare a WebRTC PeerConnection and await for an SDP offer. */
begin() {
/** @private */
this.pc = new RTCPeerConnection(this.config.pcConfig);
// OnDataChannel triggered remotely from the client when connection succeeds.
this.pc.ondatachannel = ({ channel }) => {
dbg('Data Channel established...');
this.prepareDataChannel(channel);
/** @private */
this.client = channel;
};
}
/**
* @param {RTCSessionDescription} offer
* @param {(answer: RTCSessionDescription) => void} sendAnswer
* @returns {boolean} `true` on success, `false` on fail.
*/
receiveWebRTCOffer(offer, sendAnswer) {
if ('offer' !== offer.type) {
log('Invalid SDP received -- was not an offer.');
return false;
}
try {
this.pc.setRemoteDescription(offer);
} catch (error) {
log('Invalid SDP message.');
return false;
}
dbg('SDP ' + offer.type + ' successfully received.');
this.pc.createAnswer()
.then((sdp) => {
dbg('webrtc: Answer ready.');
return this.pc.setLocalDescription(sdp);
})
.catch(() => {
this.close();
dbg('webrtc: Failed to create or set Answer');
});
// Send the answer when ready.
const onceSendAnswer = () => {
sendAnswer(this.pc.localDescription);
this.pc.onicegatheringstatechange = null;
clearTimeout(this.answerTimeoutId);
};
this.pc.onicegatheringstatechange = () => {
if (this.pc.iceGatheringState === 'complete' && this.pc.connectionState !== 'closed') {
dbg('Finished gathering ICE candidates.');
onceSendAnswer();
}
};
if (this.pc.iceGatheringState === 'complete') {
// This probably never happens as we've `setRemoteDescription` just now,
// but let's play it safe.
onceSendAnswer();
} else {
this.answerTimeoutId = setTimeout(() => {
dbg('answerTimeout');
// ICE gathering is taking a while to complete - send what we got so far.
if (!this.pc.localDescription) {
// We don't have anything to send yet. Sigh. The client will probably timeout waiting
// for us, but let's not bail and just try to wait some more in hope that it won't.
// Worst case scenario - `datachannelTimeout` callback will run.
return;
}
onceSendAnswer();
}, this.config.answerTimeout);
}
return true;
}
/**
* Given a WebRTC DataChannel, prepare callbacks.
* @param {RTCDataChannel} channel
* @private
*/
prepareDataChannel(channel) {
channel.onopen = () => {
log('WebRTC DataChannel opened!');
snowflake.ui.increaseClients();
this.nowConnected = true;
// if we don't receive any keep-alive messages from the client, close the
// connection
const onStaleTimeout = () => {
console.log("Closing stale connection.");
this.flush();
this.close();
};
this.refreshStaleTimeout = () => {
clearTimeout(this.messageTimer);
this.messageTimer = setTimeout(onStaleTimeout, this.config.messageTimeout);
};
this.refreshStaleTimeout();
// This is the point when the WebRTC datachannel is done, so the next step
// is to establish websocket to the server.
this.connectRelay();
};
channel.onclose = () => {
log('WebRTC DataChannel closed.');
snowflake.ui.setStatus('disconnected by webrtc.');
if (this.nowConnected) {
snowflake.ui.decreaseClients();
this.nowConnected = false;
}
this.flush();
this.close();
};
channel.onerror = function () {
log('Data channel error!');
};
channel.binaryType = "arraybuffer";
channel.onmessage = this.onClientToRelayMessage;
}
/**
* Assumes WebRTC datachannel is connected.
* @private
*/
connectRelay() {
dbg('Connecting to relay...');
// Get a remote IP address from the PeerConnection, if possible. Add it to
// the WebSocket URL's query string if available.
// MDN marks remoteDescription as "experimental". However the other two
// options, currentRemoteDescription and pendingRemoteDescription, which
// are not marked experimental, were undefined when I tried them in Firefox
// 52.2.0.
// https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/remoteDescription
const desc = this.pc.remoteDescription;
const peer_ip = Parse.ipFromSDP(desc!= null ? desc.sdp : undefined);
const params = [];
if (peer_ip != null) {
params.push(["client_ip", peer_ip]);
}
const relay = this.relay = WS.makeWebsocket(this.relayURL, params);
relay.label = 'websocket-relay';
relay.onopen = () => {
clearTimeout(this.connectToRelayTimeoutId);
log(relay.label + ' connected!');
snowflake.ui.setStatus('connected');
};
relay.onclose = () => {
log(relay.label + ' closed.');
snowflake.ui.setStatus('disconnected.');
if (this.nowConnected) {
snowflake.ui.decreaseClients();
this.nowConnected = false;
}
this.flush();
this.close();
};
relay.onerror = this.onError;
relay.onmessage = this.onRelayToClientMessage;
// TODO: Better websocket timeout handling.
this.connectToRelayTimeoutId = setTimeout((() => {
log(relay.label + ' timed out connecting.');
relay.onclose();
}), 5000);
}
/**
* WebRTC --> websocket
* @param {MessageEvent} msg
* @private
*/
onClientToRelayMessage(msg) {
this.c2rSchedule.push(msg.data);
this.flush();
this.refreshStaleTimeout();
}
/**
* websocket --> WebRTC
* @param {MessageEvent} event
* @private
*/
onRelayToClientMessage(event) {
this.r2cSchedule.push(event.data);
this.flush();
}
/** @private */
onError(event) {
const ws = event.target;
log(ws.label + ' error.');
this.close();
}
/** Close both WebRTC and websocket. */
close() {
if (debug) {
this.pc.getStats().then(report => {
let transportStats;
for (const stat of report.values()) {
// Also consider 'data-channel'.
if (stat.type === 'transport') {
transportStats = stat;
break;
}
}
if (!transportStats) {
return;
}
function bytesToMBytesStr(numBytes) {
return (numBytes / 1024 / 1024).toFixed(3);
}
log(
`Connection closed. Traffic (up|down):`
+ ` ${bytesToMBytesStr(transportStats.bytesReceived)} MB|`
+ `${bytesToMBytesStr(transportStats.bytesSent)} MB`
+ `, packets: ${transportStats.packetsReceived}|`
+ `${transportStats.packetsSent}`
);
});
}
clearTimeout(this.connectToRelayTimeoutId);
clearTimeout(this.messageTimer);
clearTimeout(this.answerTimeoutId);
if (this.webrtcIsReady()) {
this.client.close();
}
if (this.peerConnOpen()) {
this.pc.close();
}
if (this.relayIsReady()) {
this.relay.close();
}
this.onCleanup();
}
/**
* Send as much data in both directions as the rate limit currently allows.
* @private
*/
flush() {
let busy = true;
while (busy && !this.rateLimit.isLimited()) {
busy = false;
// WebRTC --> websocket
if (this.c2rSchedule.length > 0 && this.relayIsReady() && this.relay.bufferedAmount < this.MAX_BUFFER) {
const chunk = this.c2rSchedule.shift();
this.relay.send(chunk);
this.rateLimit.update(chunk.byteLength);
busy = true;
}
// websocket --> WebRTC
if (this.r2cSchedule.length > 0 && this.webrtcIsReady() && this.client.bufferedAmount < this.MAX_BUFFER) {
const chunk = this.r2cSchedule.shift();
this.client.send(chunk);
this.rateLimit.update(chunk.byteLength);
busy = true;
}
}
if (this.flush_timeout_id) {
clearTimeout(this.flush_timeout_id);
this.flush_timeout_id = null;
}
if (this.r2cSchedule.length > 0 || this.c2rSchedule.length > 0) {
this.flush_timeout_id = setTimeout(this.flush, this.rateLimit.when() * 1000);
}
}
webrtcIsReady() {
return null !== this.client && 'open' === this.client.readyState;
}
relayIsReady() {
return (null !== this.relay) && (WebSocket.OPEN === this.relay.readyState);
}
peerConnOpen() {
return (null !== this.pc) && ('closed' !== this.pc.connectionState);
}
/**
* @param {URL | string} relayURL
*/
setRelayURL(relayURL) {
this.relayURL = relayURL;
}
}
ProxyPair.prototype.MAX_BUFFER = 10 * 1024 * 1024;
ProxyPair.prototype.pc = null;
ProxyPair.prototype.client = null; // WebRTC Data channel
ProxyPair.prototype.relay = null; // websocket
ProxyPair.prototype.connectToRelayTimeoutId = 0;
ProxyPair.prototype.messageTimer = 0;
ProxyPair.prototype.answerTimeoutId = 0;
ProxyPair.prototype.flush_timeout_id = null;
ProxyPair.prototype.onCleanup = null;