forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.js
More file actions
415 lines (345 loc) · 11.3 KB
/
browser.js
File metadata and controls
415 lines (345 loc) · 11.3 KB
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
/* global WebSocket,Blob,FileReader */
const stream = require('stream')
const util = require('util')
const http = require('http')
const timers = require('timers')
const debug = util.debuglog('net')
const proxy = {
protocol: (window.location.protocol === 'https:') ? 'wss' : 'ws',
hostname: window.location.hostname,
port: window.location.port,
path: '/api/vm/net'
}
function getProxy () {
return proxy
}
function getProxyHost () {
let host = getProxy().hostname
if (getProxy().port) {
host += ':' + getProxy().port
}
return host
}
function getProxyOrigin () {
return getProxy().protocol + '://' + getProxyHost()
}
exports.setProxy = function (options) {
options = options || {}
if (options.protocol) {
proxy.protocol = options.protocol
}
if (options.hostname) {
proxy.hostname = options.hostname
}
if (options.port) {
proxy.port = options.port
}
if (options.path) {
proxy.path = options.path
}
}
exports.createServer = function () {
throw new Error('Cannot create server in a browser')
}
exports.connect = exports.createConnection = function (/* options, connectListener */) {
const args = normalizeConnectArgs(arguments)
debug('createConnection', args)
const s = new Socket(args[0])
return Socket.prototype.connect.apply(s, args)
}
function toNumber (x) { return (x = Number(x)) >= 0 ? x : false }
function isPipeName (s) {
return typeof s === 'string' && toNumber(s) === false
}
// Returns an array [options] or [options, cb]
// It is the same as the argument of Socket.prototype.connect().
function normalizeConnectArgs (args) {
let options = {}
if (typeof args[0] === 'object') {
// connect(options, [cb])
options = args[0]
} else if (isPipeName(args[0])) {
// connect(path, [cb]);
options.path = args[0]
} else {
// connect(port, [host], [cb])
options.port = args[0]
if (typeof args[1] === 'string') {
options.host = args[1]
}
}
const cb = args[args.length - 1]
return typeof cb === 'function' ? [options, cb] : [options]
}
exports._normalizeConnectArgs = normalizeConnectArgs
function Socket (options) {
if (!(this instanceof Socket)) return new Socket(options)
this._connecting = false
this._host = null
if (typeof options === 'number') {
options = { fd: options }
} else if (typeof options === 'undefined') { options = {} }
stream.Duplex.call(this, options)
// these will be set once there is a connection
this.readable = this.writable = false
// handle strings directly
this._writableState.decodeStrings = false
// default to *not* allowing half open sockets
this.allowHalfOpen = (options && options.allowHalfOpen) || false
}
util.inherits(Socket, stream.Duplex)
exports.Socket = Socket
exports.Stream = Socket // Legacy naming.
Socket.prototype.listen = function () {
throw new Error('Cannot listen in a browser')
}
Socket.prototype.setTimeout = function (msecs, callback) {
if (msecs > 0 && isFinite(msecs)) {
timers.enroll(this, msecs)
// timers._unrefActive(this);
if (callback) {
this.once('timeout', callback)
}
} else if (msecs === 0) {
timers.unenroll(this)
if (callback) {
this.removeListener('timeout', callback)
}
}
}
Socket.prototype._onTimeout = function () {
debug('_onTimeout')
this.emit('timeout')
}
Socket.prototype.setNoDelay = function (enable) {}
Socket.prototype.setKeepAlive = function (setting, msecs) {}
Socket.prototype.address = function () {
return {
address: this.remoteAddress,
port: this.remotePort,
family: this.remoteFamily
}
}
Object.defineProperty(Socket.prototype, 'readyState', {
get: function () {
if (this._connecting) {
return 'opening'
} else if (this.readable && this.writable) {
return 'open'
} else if (this.readable && !this.writable) {
return 'readOnly'
} else if (!this.readable && this.writable) {
return 'writeOnly'
} else {
return 'closed'
}
}
})
Socket.prototype.bufferSize = undefined
Socket.prototype._read = function () {}
Socket.prototype.end = function (data, encoding) {
stream.Duplex.prototype.end.call(this, data, encoding)
this.writable = false
if (this._ws) {
this._ws.close()
}
// just in case we're waiting for an EOF.
if (this.readable && !this._readableState.endEmitted) { this.read(0) } else { maybeDestroy(this) }
}
// Call whenever we set writable=false or readable=false
function maybeDestroy (socket) {
if (!socket.readable &&
!socket.writable &&
!socket.destroyed &&
!socket._connecting &&
!socket._writableState.length) {
socket.destroy()
}
}
Socket.prototype.destroySoon = function () {
if (this.writable) { this.end() }
if (this._writableState.finished) { this.destroy() } else { this.once('finish', this.destroy) }
}
Socket.prototype.destroy = function (exception) {
debug('destroy', exception)
if (this.destroyed) {
return
}
this._connecting = false
this.readable = this.writable = false
timers.unenroll(this)
debug('close')
this.destroyed = true
}
Socket.prototype.remoteAddress = null
Socket.prototype.remoteFamily = null
Socket.prototype.remotePort = null
// Used for servers only - not here
Socket.prototype.localAddress = null
Socket.prototype.localPort = null
Socket.prototype.bytesRead = 0
Socket.prototype.bytesWritten = 0
Socket.prototype._write = function (data, encoding, cb) {
const self = this
cb = cb || function () {}
// If we are still connecting, then buffer this for later.
// The Writable logic will buffer up any more writes while
// waiting for this one to be done.
if (this._connecting) {
this._pendingData = data
this._pendingEncoding = encoding
this.once('connect', function () {
this._write(data, encoding, cb)
})
return
}
this._pendingData = null
this._pendingEncoding = ''
if (encoding === 'binary' && typeof data === 'string') { // TODO: maybe apply this for all string inputs?
// Setting encoding is very important for binary data - otherwise the data gets modified
data = Buffer.alloc(data, encoding)
}
// Send the data
this._ws.send(data)
process.nextTick(function () {
// console.log('[tcp] sent: ', data.toString(), data.length);
self.bytesWritten += data.length
cb()
})
}
Socket.prototype.write = function (chunk, encoding, cb) {
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) { throw new TypeError('invalid data') }
return stream.Duplex.prototype.write.apply(this, arguments)
}
Socket.prototype.connect = function (options, cb) {
const self = this
if (typeof options !== 'object') {
// Old API:
// connect(port, [host], [cb])
// connect(path, [cb]);
const args = normalizeConnectArgs(arguments)
return Socket.prototype.connect.apply(this, args)
}
cb = cb || function () {}
if (this.write !== Socket.prototype.write) { this.write = Socket.prototype.write }
if (options.path) {
throw new Error('options.path not supported in the browser')
}
self._connecting = true
self.writable = true
self._host = options.host
const req = http.request({
hostname: getProxy().hostname,
port: getProxy().port,
path: getProxy().path + '/connect',
method: 'POST',
withCredentials: false
}, function (res) {
let json = ''
res.on('data', function (buf) {
json += buf
})
res.on('end', function () {
let data = null
try {
data = JSON.parse(json)
} catch (e) {
data = {
code: res.statusCode,
error: json
}
}
if (data.error) {
self.emit('error', 'Cannot open TCP connection [' + res.statusCode + ']: ' + data.error)
self.destroy()
return
}
self.remoteAddress = data.remote.address
self.remoteFamily = data.remote.family
self.remotePort = data.remote.port
self._connectWebSocket(data.token, function (err) {
if (err) {
cb(err)
return
}
cb()
})
})
})
req.setHeader('Content-Type', 'application/json')
req.write(JSON.stringify(options))
req.end()
return this
}
Socket.prototype._connectWebSocket = function (token, cb) {
const self = this
if (self._ws) {
process.nextTick(function () {
cb()
})
return
}
this._ws = new WebSocket(getProxyOrigin() + getProxy().path + '/socket?token=' + token)
this._handleWebsocket()
if (cb) {
self.on('connect', cb)
}
}
Socket.prototype._handleWebsocket = function () {
const self = this
this._ws.addEventListener('open', function () {
// console.log('TCP OK');
self._connecting = false
self.readable = true
self.emit('connect')
self.read(0)
})
this._ws.addEventListener('error', function (e) {
// `e` doesn't contain anything useful (https://developer.mozilla.org/en/docs/WebSockets/Writing_WebSocket_client_applications#Connection_errors)
console.warn('TCP error', e)
self.emit('error', 'An error occured with the WebSocket')
})
this._ws.addEventListener('message', function (e) {
const contents = e.data
const gotBuffer = function (buffer) {
// console.log('[tcp] received: ' + buffer.toString(), buffer.length);
self.bytesRead += buffer.length
self.push(buffer)
}
if (typeof contents === 'string') {
const buffer = Buffer.alloc(contents)
gotBuffer(buffer)
} else if (window.Blob && contents instanceof Blob) {
const fileReader = new FileReader()
fileReader.addEventListener('load', function (e) {
const buf = fileReader.result
const arr = new Uint8Array(buf)
gotBuffer(Buffer.from(arr))
})
fileReader.readAsArrayBuffer(contents)
} else {
console.warn('Cannot read TCP stream: unsupported message type', contents)
}
})
this._ws.addEventListener('close', function () {
if (self.readyState === 'open') {
// console.log('TCP closed');
self.destroy()
}
})
}
exports.isIP = function (input) {
if (exports.isIPv4(input)) {
return 4
} else if (exports.isIPv6(input)) {
return 6
} else {
return 0
}
}
exports.isIPv4 = function (input) {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(input)
}
exports.isIPv6 = function (input) {
return /^(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([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