itns-sidechain/browser/wsproxy.js

205 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-06-13 01:06:01 -07:00
'use strict';
const assert = require('assert');
2017-06-29 20:54:07 -07:00
const net = require('net');
2017-10-23 13:27:50 -07:00
const EventEmitter = require('events');
const bsock = require('bsock');
2017-10-30 21:44:18 -07:00
const IP = require('binet');
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
class WSProxy extends EventEmitter {
constructor(options) {
super();
if (!options)
options = {};
this.options = options;
this.ports = new Set();
this.io = bsock.server();
this.sockets = new WeakMap();
if (options.ports) {
for (const port of options.ports)
this.ports.add(port);
}
2016-06-05 20:10:44 -07:00
2017-11-16 20:32:26 -08:00
this.init();
}
2016-06-05 20:10:44 -07:00
2017-11-16 20:32:26 -08:00
init() {
this.io.on('error', (err) => {
this.emit('error', err);
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
this.io.on('socket', (ws) => {
this.handleSocket(ws);
});
}
2017-11-16 20:32:26 -08:00
handleSocket(ws) {
const state = new SocketState(this, ws);
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
// Use a weak map to avoid
// mutating the websocket object.
this.sockets.set(ws, state);
2016-06-05 20:10:44 -07:00
2017-11-16 20:32:26 -08:00
ws.on('error', (err) => {
this.emit('error', err);
});
2016-06-05 20:10:44 -07:00
2017-12-14 19:07:25 -08:00
ws.bind('tcp connect', (port, host) => {
this.handleConnect(ws, port, host);
2017-11-16 20:32:26 -08:00
});
}
2016-06-05 20:10:44 -07:00
2017-12-14 19:07:25 -08:00
handleConnect(ws, port, host) {
2017-11-16 20:32:26 -08:00
const state = this.sockets.get(ws);
assert(state);
2016-06-05 20:10:44 -07:00
2017-11-16 20:32:26 -08:00
if (state.socket) {
this.log('Client is trying to reconnect (%s).', state.host);
return;
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
if ((port & 0xffff) !== port
|| typeof host !== 'string'
|| host.length === 0) {
this.log('Client gave bad arguments (%s).', state.host);
ws.fire('tcp close');
ws.destroy();
return;
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
let raw, addr;
try {
raw = IP.toBuffer(host);
addr = IP.toString(raw);
} catch (e) {
this.log('Client gave a bad host: %s (%s).', host, state.host);
ws.fire('tcp error', {
message: 'EHOSTUNREACH',
code: 'EHOSTUNREACH'
});
ws.destroy();
return;
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
if (!IP.isRoutable(raw) || IP.isOnion(raw)) {
this.log(
'Client is trying to connect to a bad ip: %s (%s).',
addr, state.host);
ws.fire('tcp error', {
message: 'ENETUNREACH',
code: 'ENETUNREACH'
});
ws.destroy();
return;
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
if (!this.ports.has(port)) {
this.log('Client is connecting to non-whitelist port (%s).', state.host);
ws.fire('tcp error', {
message: 'ENETUNREACH',
code: 'ENETUNREACH'
});
ws.destroy();
return;
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
let socket;
try {
socket = state.connect(port, addr);
this.log('Connecting to %s (%s).', state.remoteHost, state.host);
} catch (e) {
this.log(e.message);
this.log('Closing %s (%s).', state.remoteHost, state.host);
ws.fire('tcp error', {
message: 'ENETUNREACH',
code: 'ENETUNREACH'
});
2017-10-23 13:27:50 -07:00
ws.destroy();
2016-08-27 17:12:53 -07:00
return;
}
2017-11-16 20:32:26 -08:00
socket.on('connect', () => {
ws.fire('tcp connect', socket.remoteAddress, socket.remotePort);
});
socket.on('data', (data) => {
ws.fire('tcp data', data.toString('hex'));
});
2017-11-16 20:32:26 -08:00
socket.on('error', (err) => {
ws.fire('tcp error', {
message: err.message,
code: err.code || null
});
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
socket.on('timeout', () => {
ws.fire('tcp timeout');
});
socket.on('close', () => {
this.log('Closing %s (%s).', state.remoteHost, state.host);
2017-10-23 13:27:50 -07:00
ws.fire('tcp close');
ws.destroy();
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp data', (data) => {
if (typeof data !== 'string')
return;
socket.write(Buffer.from(data, 'hex'));
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp keep alive', (enable, delay) => {
socket.setKeepAlive(enable, delay);
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp no delay', (enable) => {
socket.setNoDelay(enable);
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp set timeout', (timeout) => {
socket.setTimeout(timeout);
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp pause', () => {
socket.pause();
});
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
ws.bind('tcp resume', () => {
socket.resume();
2016-06-05 20:10:44 -07:00
});
2017-12-14 19:07:25 -08:00
ws.on('disconnect', () => {
2017-11-16 20:32:26 -08:00
socket.destroy();
});
}
2017-02-03 09:46:05 -08:00
2017-11-16 20:32:26 -08:00
log(...args) {
process.stdout.write('wsproxy: ');
console.log(...args);
}
2016-08-27 17:12:53 -07:00
2017-11-16 20:32:26 -08:00
attach(server) {
this.io.attach(server);
}
2016-08-27 17:12:53 -07:00
}
2017-11-16 20:32:26 -08:00
class SocketState {
constructor(server, socket) {
this.socket = null;
this.host = socket.host;
this.remoteHost = null;
}
connect(port, host) {
this.socket = net.connect(port, host);
this.remoteHost = IP.toHostname(host, port);
return this.socket;
}
}
2016-08-27 17:12:53 -07:00
module.exports = WSProxy;