-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSocketPlugin.js
549 lines (481 loc) · 19.5 KB
/
SocketPlugin.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
/*
* This Socket plugin only fulfills http:/https: requests by intercepting them
* and sending as XMLHttpRequest. To make connections to servers without CORS,
* it uses the crossorigin.me proxy.
*/
function SocketPlugin() {
"use strict";
return {
getModuleName: function() { return 'SocketPlugin (http-only)'; },
interpreterProxy: null,
primHandler: null,
handleCounter: 0,
needProxy: new Set(),
// DNS Lookup
lastLookup: null,
// Constants
TCP_Socket_Type: 0,
Resolver_Uninitialized: 0,
Resolver_Ready: 1,
Resolver_Busy: 2,
Resolver_Error: 3,
Socket_InvalidSocket: -1,
Socket_Unconnected: 0,
Socket_WaitingForConnection: 1,
Socket_Connected: 2,
Socket_OtherEndClosed: 3,
Socket_ThisEndClosed: 4,
setInterpreter: function(anInterpreter) {
this.interpreterProxy = anInterpreter;
this.primHandler = this.interpreterProxy.vm.primHandler;
return true;
},
// A socket handle emulates socket behavior
_newSocketHandle: function(sendBufSize, connSemaIdx, readSemaIdx, writeSemaIdx) {
var plugin = this;
return {
host: null,
port: null,
connSemaIndex: connSemaIdx,
readSemaIndex: readSemaIdx,
writeSemaIndex: writeSemaIdx,
sendBuffer: null,
sendTimeout: null,
response: null,
responseReadUntil: 0,
responseReceived: false,
status: plugin.Socket_Unconnected,
_signalSemaphore: function(semaIndex) {
if (semaIndex <= 0) return;
plugin.primHandler.signalSemaphoreWithIndex(semaIndex);
},
_signalConnSemaphore: function() { this._signalSemaphore(this.connSemaIndex); },
_signalReadSemaphore: function() { this._signalSemaphore(this.readSemaIndex); },
_signalWriteSemaphore: function() { this._signalSemaphore(this.writeSemaIndex); },
_otherEndClosed: function() {
this.status = plugin.Socket_OtherEndClosed;
this._signalConnSemaphore();
},
_hostAndPort: function() { return this.host + ':' + this.port; },
_requestNeedsProxy: function() {
return plugin.needProxy.has(this._hostAndPort());
},
_getURL: function(targetURL, isRetry) {
var url = '';
if (isRetry || this._requestNeedsProxy()) {
var proxy = typeof SqueakJS === "object" && SqueakJS.options.proxy;
url += proxy || 'https://api.allorigins.win/raw?url=';
}
if (this.port !== 443) {
url += 'http://' + this._hostAndPort() + targetURL;
} else {
url += 'https://' + this.host + targetURL;
}
return url;
},
_performRequest: function() {
var request = new TextDecoder("utf-8").decode(this.sendBuffer);
var headerLines = request.split('\r\n\r\n')[0].split('\n');
// Split header lines and parse first line
var firstHeaderLineItems = headerLines[0].split(' ');
var httpMethod = firstHeaderLineItems[0];
if (httpMethod !== 'GET' && httpMethod !== 'PUT' &&
httpMethod !== 'POST') {
this._otherEndClosed();
return -1;
}
var targetURL = firstHeaderLineItems[1];
// Extract possible data to send
var data = null;
for (var i = 1; i < headerLines.length; i++) {
var line = headerLines[i];
if (line.match(/Content-Length:/i)) {
var contentLength = parseInt(line.substr(16));
var end = this.sendBuffer.byteLength;
data = this.sendBuffer.subarray(end - contentLength, end);
break;
}
}
if (window.fetch) {
this._performFetchAPIRequest(targetURL, httpMethod, data, headerLines);
} else {
this._performXMLHTTPRequest(targetURL, httpMethod, data, headerLines);
}
},
_performFetchAPIRequest: function(targetURL, httpMethod, data, requestLines) {
var thisHandle = this;
var headers = {};
for (var i = 1; i < requestLines.length; i++) {
var lineItems = requestLines[i].split(':');
if (lineItems.length === 2) {
headers[lineItems[0]] = lineItems[1].trim();
}
}
if (typeof SqueakJS === "object" && SqueakJS.options.ajax) {
headers["X-Requested-With"] = "XMLHttpRequest";
}
var init = {
method: httpMethod,
headers: headers,
body: data,
mode: 'cors'
};
fetch(this._getURL(targetURL), init)
.then(thisHandle._handleFetchAPIResponse.bind(thisHandle))
.catch(function (e) {
var url = thisHandle._getURL(targetURL, true);
console.warn('Retrying with CORS proxy: ' + url);
fetch(url, init)
.then(function(res) {
console.log('Success: ' + url);
thisHandle._handleFetchAPIResponse(res);
plugin.needProxy.add(thisHandle._hostAndPort());
})
.catch(function (e) {
// KLUDGE! This is just a workaround for a broken
// proxy server - we should remove it when
// crossorigin.me is fixed
console.warn('Fetch API failed, retrying with XMLHttpRequest');
thisHandle._performXMLHTTPRequest(targetURL, httpMethod, data, requestLines);
});
});
},
_handleFetchAPIResponse: function(res) {
if (this.response === null) {
var header = ['HTTP/1.0 ', res.status, ' ', res.statusText, '\r\n'];
res.headers.forEach(function(value, key, array) {
header = header.concat([key, ': ', value, '\r\n']);
});
header.push('\r\n');
this.response = [new TextEncoder('utf-8').encode(header.join(''))];
}
this._readIncremental(res.body.getReader());
},
_readIncremental: function(reader) {
var thisHandle = this;
return reader.read().then(function (result) {
if (result.done) {
thisHandle.responseReceived = true;
return;
}
thisHandle.response.push(result.value);
thisHandle._signalReadSemaphore();
return thisHandle._readIncremental(reader);
});
},
_performXMLHTTPRequest: function(targetURL, httpMethod, data, requestLines){
var thisHandle = this;
var contentType;
for (var i = 1; i < requestLines.length; i++) {
var line = requestLines[i];
if (line.match(/Content-Type:/i)) {
contentType = encodeURIComponent(line.substr(14));
break;
}
}
var httpRequest = new XMLHttpRequest();
httpRequest.open(httpMethod, this._getURL(targetURL));
if (contentType !== undefined) {
httpRequest.setRequestHeader('Content-type', contentType);
}
if (typeof SqueakJS === "object" && SqueakJS.options.ajax) {
httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
httpRequest.responseType = "arraybuffer";
httpRequest.onload = function (oEvent) {
thisHandle._handleXMLHTTPResponse(this);
};
httpRequest.onerror = function(e) {
var url = thisHandle._getURL(targetURL, true);
console.warn('Retrying with CORS proxy: ' + url);
var retry = new XMLHttpRequest();
retry.open(httpMethod, url);
retry.responseType = httpRequest.responseType;
if (typeof SqueakJS === "object" && SqueakJS.options.ajaxx) {
retry.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
retry.onload = function(oEvent) {
console.log('Success: ' + url);
thisHandle._handleXMLHTTPResponse(this);
plugin.needProxy.add(thisHandle._hostAndPort());
};
retry.onerror = function() {
thisHandle._otherEndClosed();
console.error("Failed to download:\n" + url);
};
retry.send(data);
};
httpRequest.send(data);
},
_handleXMLHTTPResponse: function(response) {
this.responseReceived = true;
var content = response.response;
if (!content) {
this._otherEndClosed();
return;
}
// Recreate header
var header = new TextEncoder('utf-8').encode(
'HTTP/1.0 ' + response.status + ' ' + response.statusText +
'\r\n' + response.getAllResponseHeaders() + '\r\n');
// Concat header and response
var res = new Uint8Array(header.byteLength + content.byteLength);
res.set(header, 0);
res.set(new Uint8Array(content), header.byteLength);
this.response = [res];
this._signalReadSemaphore();
},
connect: function(host, port) {
this.host = host;
this.port = port;
this.status = plugin.Socket_Connected;
this._signalConnSemaphore();
this._signalWriteSemaphore(); // Immediately ready to write
},
close: function() {
if (this.status == plugin.Socket_Connected ||
this.status == plugin.Socket_OtherEndClosed ||
this.status == plugin.Socket_WaitingForConnection) {
this.status = plugin.Socket_Unconnected;
this._signalConnSemaphore();
}
},
destroy: function() {
this.status = plugin.Socket_InvalidSocket;
},
dataAvailable: function() {
if (this.status == plugin.Socket_InvalidSocket) return false;
if (this.status == plugin.Socket_Connected) {
if (this.response && this.response.length > 0) {
this._signalReadSemaphore();
return true;
}
if (this.responseSentCompletly) {
// Signal older Socket implementations that they reached the end
this.status = plugin.Socket_OtherEndClosed;
this._signalConnSemaphore();
}
}
return false;
},
recv: function(count) {
if (this.response === null) return [];
var data = this.response[0];
if (data.length > count) {
var rest = data.subarray(count);
if (rest) {
this.response[0] = rest;
} else {
this.response.shift();
}
data = data.subarray(0, count);
} else {
this.response.shift();
}
if (this.responseReceived && this.response.length === 0) {
this.responseSentCompletly = true;
}
return data;
},
send: function(data, start, end) {
if (this.sendTimeout !== null) {
window.clearTimeout(this.sendTimeout);
}
this.lastSend = Date.now();
var newBytes = data.bytes.subarray(start, end);
if (this.sendBuffer === null) {
this.sendBuffer = newBytes;
} else {
var newLength = this.sendBuffer.byteLength + newBytes.byteLength;
var newBuffer = new Uint8Array(newLength);
newBuffer.set(this.sendBuffer, 0);
newBuffer.set(newBytes, this.sendBuffer.byteLength);
this.sendBuffer = newBuffer;
}
// Give image some time to send more data before performing requests
this.sendTimeout = setTimeout(this._performRequest.bind(this), 50);
return newBytes.byteLength;
}
};
},
primitiveResolverLocalAddress: function(argCount) {
// NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
/* var promise,
RTCPeerConnection = /*window.RTCPeerConnection || // window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) {
promise = () => new Promise((resolve, reject) => {
var rtc = new RTCPeerConnection({iceServers:[]});
if (1 || window.mozRTCPeerConnection) {
// FF [and now Chrome!] needs a channel/stream to proceed
rtc.createDataChannel('', {reliable:false});
};
rtc.onicecandidate = function (evt) {
// convert the candidate to SDP so we can run it through our general parser
// see https://twitter.com/lancestout/status/525796175425720320 for details
if (evt.candidate) grepSDP("a="+evt.candidate.candidate);
};
rtc.createOffer(
function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
},
function (e) { reject("offer failed"); });
var addrs = Object.create(null);
addrs["0.0.0.0"] = false;
function grepSDP(sdp) {
var hosts = [];
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
addr = parts[4],
type = parts[7];
if (type === 'host') {
resolve(addr)}
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
var parts = line.split(' ')
addr = parts[2];
}
});
}
})} else {reject("No WebRTC!");}
promise().then((addr) => {
var bytearray = this.interpreterProxy.instantiateClassindexableSize(this.interpreterProxy.classArray(), 4),
i = 0
addr.split(".").map(string => {bytearray.pointers[i++] = Number(string)})
this.interpreterProxy.popthenPush(1, bytearray);
}, (message) => {
return false}) */
this.interpreterProxy.primitiveFail();
},
primitiveHasSocketAccess: function(argCount) {
this.interpreterProxy.popthenPush(1, this.interpreterProxy.trueObject());
return true;
},
primitiveInitializeNetwork: function(argCount) {
this.interpreterProxy.pop(1);
return true;
},
primitiveResolverNameLookupResult: function(argCount) {
if (argCount !== 0) return false;
var inet;
if (this.lastLookup !== null) {
inet = this.primHandler.makeStString(this.lastLookup);
this.lastLookup = null;
} else {
inet = this.interpreterProxy.nilObject();
}
this.interpreterProxy.popthenPush(1, inet);
return true;
},
primitiveResolverStartNameLookup: function(argCount) {
if (argCount !== 1) return false;
this.lastLookup = this.interpreterProxy.stackValue(0).bytesAsString();
this.interpreterProxy.popthenPush(1, this.interpreterProxy.nilObject());
return true;
},
primitiveResolverStatus: function(argCount) {
this.interpreterProxy.popthenPush(1, this.Resolver_Ready);
return true;
},
primitiveSocketConnectionStatus: function(argCount) {
if (argCount !== 1) return false;
var handle = this.interpreterProxy.stackObjectValue(0).handle;
if (handle === undefined) return false;
var status = handle.status;
if (status === undefined) status = this.Socket_InvalidSocket;
this.interpreterProxy.popthenPush(1, status);
return true;
},
primitiveSocketConnectToPort: function(argCount) {
if (argCount !== 3) return false;
var handle = this.interpreterProxy.stackObjectValue(2).handle;
if (handle === undefined) return false;
var host = this.interpreterProxy.stackObjectValue(1).bytesAsString();
var port = this.interpreterProxy.stackIntegerValue(0);
handle.connect(host, port);
this.interpreterProxy.popthenPush(argCount,
this.interpreterProxy.nilObject());
return true;
},
primitiveSocketCloseConnection: function(argCount) {
if (argCount !== 1) return false;
var handle = this.interpreterProxy.stackObjectValue(0).handle;
if (handle === undefined) return false;
handle.close();
this.interpreterProxy.popthenPush(1, this.interpreterProxy.nilObject());
return true;
},
primitiveSocketCreate3Semaphores: function(argCount) {
if (argCount !== 7) return false;
var writeSemaIndex = this.interpreterProxy.stackIntegerValue(0);
var readSemaIndex = this.interpreterProxy.stackIntegerValue(1);
var semaIndex = this.interpreterProxy.stackIntegerValue(2);
var sendBufSize = this.interpreterProxy.stackIntegerValue(3);
var socketType = this.interpreterProxy.stackIntegerValue(5);
if (socketType !== this.TCP_Socket_Type) return false;
var name = '{SqueakJS Socket #' + (++this.handleCounter) + '}';
var sqHandle = this.primHandler.makeStString(name);
sqHandle.handle = this._newSocketHandle(sendBufSize, semaIndex,
readSemaIndex, writeSemaIndex);
this.interpreterProxy.popthenPush(argCount, sqHandle);
return true;
},
primitiveSocketDestroy: function(argCount) {
if (argCount !== 1) return false;
var handle = this.interpreterProxy.stackObjectValue(0).handle;
if (handle === undefined) return false;
handle.destroy();
this.interpreterProxy.popthenPush(1, handle.status);
return true;
},
primitiveSocketReceiveDataAvailable: function(argCount) {
if (argCount !== 1) return false;
var handle = this.interpreterProxy.stackObjectValue(0).handle;
if (handle === undefined) return false;
var ret = this.interpreterProxy.falseObject();
if (handle.dataAvailable()) {
ret = this.interpreterProxy.trueObject();
}
this.interpreterProxy.popthenPush(1, ret);
return true;
},
primitiveSocketReceiveDataBufCount: function(argCount) {
if (argCount !== 4) return false;
var handle = this.interpreterProxy.stackObjectValue(3).handle;
if (handle === undefined) return false;
var target = this.interpreterProxy.stackObjectValue(2);
var start = this.interpreterProxy.stackIntegerValue(1) - 1;
var count = this.interpreterProxy.stackIntegerValue(0);
if ((start + count) > target.bytes.length) return false;
var bytes = handle.recv(count);
target.bytes.set(bytes, start);
this.interpreterProxy.popthenPush(argCount, bytes.length);
return true;
},
primitiveSocketSendDataBufCount: function(argCount) {
if (argCount !== 4) return false;
var handle = this.interpreterProxy.stackObjectValue(3).handle;
if (handle === undefined) return false;
var data = this.interpreterProxy.stackObjectValue(2);
var start = this.interpreterProxy.stackIntegerValue(1) - 1;
if (start < 0 ) return false;
var count = this.interpreterProxy.stackIntegerValue(0);
var end = start + count;
if (end > data.length) return false;
var res = handle.send(data, start, end);
this.interpreterProxy.popthenPush(1, res);
return true;
},
primitiveSocketSendDone: function(argCount) {
if (argCount !== 1) return false;
this.interpreterProxy.popthenPush(1, this.interpreterProxy.trueObject());
return true;
},
};
}
function registerSocketPlugin() {
if (typeof Squeak === "object" && window.Squeak.registerExternalModule) {
window.Squeak.registerExternalModule('SocketPlugin', SocketPlugin());
} else setTimeout(registerSocketPlugin, 100);
};
registerSocketPlugin();