http: use bsock instead of socket.io.
This commit is contained in:
parent
a6dc571c2d
commit
c84afad1f6
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const bweb = require('bweb');
|
||||
const fs = require('../lib/utils/fs');
|
||||
const HTTPBase = require('../lib/http/base');
|
||||
const WSProxy = require('./wsproxy');
|
||||
|
||||
const index = fs.readFileSync(`${__dirname}/index.html`);
|
||||
@ -15,7 +15,7 @@ const proxy = new WSProxy({
|
||||
ports: [8333, 18333, 18444, 28333, 28901]
|
||||
});
|
||||
|
||||
const server = new HTTPBase({
|
||||
const server = bweb.server({
|
||||
port: Number(process.argv[2]) || 8080,
|
||||
sockets: false
|
||||
});
|
||||
@ -52,6 +52,6 @@ server.get('/bcoin-worker.js', (req, res) => {
|
||||
res.send(200, worker, 'js');
|
||||
});
|
||||
|
||||
proxy.attach(server.server);
|
||||
proxy.attach(server.http);
|
||||
|
||||
server.open();
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
const assert = require('assert');
|
||||
const net = require('net');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const IOServer = require('socket.io');
|
||||
const EventEmitter = require('events');
|
||||
const bsock = require('bsock');
|
||||
const util = require('../lib/utils/util');
|
||||
const digest = require('../lib/crypto/digest');
|
||||
const IP = require('../lib/utils/ip');
|
||||
@ -26,7 +26,7 @@ function WSProxy(options) {
|
||||
this.target = options.target || TARGET;
|
||||
this.pow = options.pow === true;
|
||||
this.ports = new Set();
|
||||
this.io = new IOServer();
|
||||
this.io = bsock.server();
|
||||
this.sockets = new WeakMap();
|
||||
|
||||
if (options.ports) {
|
||||
@ -44,7 +44,7 @@ WSProxy.prototype.init = function init() {
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
this.io.on('connection', (ws) => {
|
||||
this.io.on('socket', (ws) => {
|
||||
this.handleSocket(ws);
|
||||
});
|
||||
};
|
||||
@ -56,13 +56,13 @@ WSProxy.prototype.handleSocket = function handleSocket(ws) {
|
||||
// mutating the websocket object.
|
||||
this.sockets.set(ws, state);
|
||||
|
||||
ws.emit('info', state.toInfo());
|
||||
ws.fire('info', state.toInfo());
|
||||
|
||||
ws.on('error', (err) => {
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
ws.on('tcp connect', (port, host, nonce) => {
|
||||
ws.listen('tcp connect', (port, host, nonce) => {
|
||||
this.handleConnect(ws, port, host, nonce);
|
||||
});
|
||||
};
|
||||
@ -80,16 +80,16 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
|
||||
|| typeof host !== 'string'
|
||||
|| host.length === 0) {
|
||||
this.log('Client gave bad arguments (%s).', state.host);
|
||||
ws.emit('tcp close');
|
||||
ws.disconnect();
|
||||
ws.fire('tcp close');
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.pow) {
|
||||
if (!util.isU32(nonce)) {
|
||||
this.log('Client did not solve proof of work (%s).', state.host);
|
||||
ws.emit('tcp close');
|
||||
ws.disconnect();
|
||||
ws.fire('tcp close');
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -103,8 +103,8 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
|
||||
|
||||
if (digest.hash256(pow).compare(this.target) > 0) {
|
||||
this.log('Client did not solve proof of work (%s).', state.host);
|
||||
ws.emit('tcp close');
|
||||
ws.disconnect();
|
||||
ws.fire('tcp close');
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -115,11 +115,11 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
|
||||
addr = IP.toString(raw);
|
||||
} catch (e) {
|
||||
this.log('Client gave a bad host: %s (%s).', host, state.host);
|
||||
ws.emit('tcp error', {
|
||||
ws.fire('tcp error', {
|
||||
message: 'EHOSTUNREACH',
|
||||
code: 'EHOSTUNREACH'
|
||||
});
|
||||
ws.disconnect();
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -127,21 +127,21 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
|
||||
this.log(
|
||||
'Client is trying to connect to a bad ip: %s (%s).',
|
||||
addr, state.host);
|
||||
ws.emit('tcp error', {
|
||||
ws.fire('tcp error', {
|
||||
message: 'ENETUNREACH',
|
||||
code: 'ENETUNREACH'
|
||||
});
|
||||
ws.disconnect();
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.ports.has(port)) {
|
||||
this.log('Client is connecting to non-whitelist port (%s).', state.host);
|
||||
ws.emit('tcp error', {
|
||||
ws.fire('tcp error', {
|
||||
message: 'ENETUNREACH',
|
||||
code: 'ENETUNREACH'
|
||||
});
|
||||
ws.disconnect();
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -152,66 +152,66 @@ WSProxy.prototype.handleConnect = function handleConnect(ws, port, host, nonce)
|
||||
} catch (e) {
|
||||
this.log(e.message);
|
||||
this.log('Closing %s (%s).', state.remoteHost, state.host);
|
||||
ws.emit('tcp error', {
|
||||
ws.fire('tcp error', {
|
||||
message: 'ENETUNREACH',
|
||||
code: 'ENETUNREACH'
|
||||
});
|
||||
ws.disconnect();
|
||||
ws.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
socket.on('connect', () => {
|
||||
ws.emit('tcp connect', socket.remoteAddress, socket.remotePort);
|
||||
ws.fire('tcp connect', socket.remoteAddress, socket.remotePort);
|
||||
});
|
||||
|
||||
socket.on('data', (data) => {
|
||||
ws.emit('tcp data', data.toString('hex'));
|
||||
ws.fire('tcp data', data.toString('hex'));
|
||||
});
|
||||
|
||||
socket.on('error', (err) => {
|
||||
ws.emit('tcp error', {
|
||||
ws.fire('tcp error', {
|
||||
message: err.message,
|
||||
code: err.code || null
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('timeout', () => {
|
||||
ws.emit('tcp timeout');
|
||||
ws.fire('tcp timeout');
|
||||
});
|
||||
|
||||
socket.on('close', () => {
|
||||
this.log('Closing %s (%s).', state.remoteHost, state.host);
|
||||
ws.emit('tcp close');
|
||||
ws.disconnect();
|
||||
ws.fire('tcp close');
|
||||
ws.destroy();
|
||||
});
|
||||
|
||||
ws.on('tcp data', (data) => {
|
||||
ws.listen('tcp data', (data) => {
|
||||
if (typeof data !== 'string')
|
||||
return;
|
||||
socket.write(Buffer.from(data, 'hex'));
|
||||
});
|
||||
|
||||
ws.on('tcp keep alive', (enable, delay) => {
|
||||
ws.listen('tcp keep alive', (enable, delay) => {
|
||||
socket.setKeepAlive(enable, delay);
|
||||
});
|
||||
|
||||
ws.on('tcp no delay', (enable) => {
|
||||
ws.listen('tcp no delay', (enable) => {
|
||||
socket.setNoDelay(enable);
|
||||
});
|
||||
|
||||
ws.on('tcp set timeout', (timeout) => {
|
||||
ws.listen('tcp set timeout', (timeout) => {
|
||||
socket.setTimeout(timeout);
|
||||
});
|
||||
|
||||
ws.on('tcp pause', () => {
|
||||
ws.listen('tcp pause', () => {
|
||||
socket.pause();
|
||||
});
|
||||
|
||||
ws.on('tcp resume', () => {
|
||||
ws.listen('tcp resume', () => {
|
||||
socket.resume();
|
||||
});
|
||||
|
||||
ws.on('disconnect', () => {
|
||||
ws.listen('disconnect', () => {
|
||||
socket.destroy();
|
||||
});
|
||||
};
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
|
||||
const assert = require('assert');
|
||||
const EventEmitter = require('events');
|
||||
const IOClient = require('socket.io-client');
|
||||
const bsock = require('bsock');
|
||||
const util = require('../utils/util');
|
||||
const digest = require('../crypto/digest');
|
||||
const BufferWriter = require('../utils/writer');
|
||||
@ -21,7 +21,7 @@ function ProxySocket(uri) {
|
||||
|
||||
this.info = null;
|
||||
|
||||
this.socket = new IOClient(uri, { reconnection: false });
|
||||
this.socket = bsock.connect(uri);
|
||||
this.sendBuffer = [];
|
||||
this.recvBuffer = [];
|
||||
this.paused = false;
|
||||
@ -39,7 +39,7 @@ function ProxySocket(uri) {
|
||||
Object.setPrototypeOf(ProxySocket.prototype, EventEmitter.prototype);
|
||||
|
||||
ProxySocket.prototype._init = function _init() {
|
||||
this.socket.on('info', (info) => {
|
||||
this.socket.listen('info', (info) => {
|
||||
if (this.closed)
|
||||
return;
|
||||
|
||||
@ -57,7 +57,7 @@ ProxySocket.prototype._init = function _init() {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
this.socket.on('tcp connect', (addr, port) => {
|
||||
this.socket.listen('tcp connect', (addr, port) => {
|
||||
if (this.closed)
|
||||
return;
|
||||
this.remoteAddress = addr;
|
||||
@ -65,7 +65,7 @@ ProxySocket.prototype._init = function _init() {
|
||||
this.emit('connect');
|
||||
});
|
||||
|
||||
this.socket.on('tcp data', (data) => {
|
||||
this.socket.listen('tcp data', (data) => {
|
||||
data = Buffer.from(data, 'hex');
|
||||
if (this.paused) {
|
||||
this.recvBuffer.push(data);
|
||||
@ -75,24 +75,24 @@ ProxySocket.prototype._init = function _init() {
|
||||
this.emit('data', data);
|
||||
});
|
||||
|
||||
this.socket.on('tcp close', (data) => {
|
||||
this.socket.listen('tcp close', (data) => {
|
||||
if (this.closed)
|
||||
return;
|
||||
this.closed = true;
|
||||
this.emit('close');
|
||||
});
|
||||
|
||||
this.socket.on('tcp error', (e) => {
|
||||
this.socket.listen('tcp error', (e) => {
|
||||
const err = new Error(e.message);
|
||||
err.code = e.code;
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
this.socket.on('tcp timeout', () => {
|
||||
this.socket.listen('tcp timeout', () => {
|
||||
this.emit('timeout');
|
||||
});
|
||||
|
||||
this.socket.on('disconnect', () => {
|
||||
this.socket.listen('disconnect', () => {
|
||||
if (this.closed)
|
||||
return;
|
||||
this.closed = true;
|
||||
@ -139,7 +139,7 @@ ProxySocket.prototype.connect = function connect(port, host) {
|
||||
util.log('Solved proof of work: %d', nonce);
|
||||
}
|
||||
|
||||
this.socket.emit('tcp connect', port, host, nonce);
|
||||
this.socket.fire('tcp connect', port, host, nonce);
|
||||
|
||||
for (const chunk of this.sendBuffer)
|
||||
this.write(chunk);
|
||||
@ -148,15 +148,15 @@ ProxySocket.prototype.connect = function connect(port, host) {
|
||||
};
|
||||
|
||||
ProxySocket.prototype.setKeepAlive = function setKeepAlive(enable, delay) {
|
||||
this.socket.emit('tcp keep alive', enable, delay);
|
||||
this.socket.fire('tcp keep alive', enable, delay);
|
||||
};
|
||||
|
||||
ProxySocket.prototype.setNoDelay = function setNoDelay(enable) {
|
||||
this.socket.emit('tcp no delay', enable);
|
||||
this.socket.fire('tcp no delay', enable);
|
||||
};
|
||||
|
||||
ProxySocket.prototype.setTimeout = function setTimeout(timeout, callback) {
|
||||
this.socket.emit('tcp set timeout', timeout);
|
||||
this.socket.fire('tcp set timeout', timeout);
|
||||
if (callback)
|
||||
this.on('timeout', callback);
|
||||
};
|
||||
@ -173,7 +173,7 @@ ProxySocket.prototype.write = function write(data, callback) {
|
||||
|
||||
this.bytesWritten += data.length;
|
||||
|
||||
this.socket.emit('tcp data', data.toString('hex'));
|
||||
this.socket.fire('tcp data', data.toString('hex'));
|
||||
|
||||
if (callback)
|
||||
callback();
|
||||
@ -201,7 +201,7 @@ ProxySocket.prototype.destroy = function destroy() {
|
||||
if (this.closed)
|
||||
return;
|
||||
this.closed = true;
|
||||
this.socket.disconnect();
|
||||
this.socket.destroy();
|
||||
};
|
||||
|
||||
ProxySocket.connect = function connect(uri, port, host) {
|
||||
|
||||
@ -28,7 +28,8 @@
|
||||
"n64": "0.0.18",
|
||||
"breq": "^0.0.1",
|
||||
"bclient": "^0.0.1",
|
||||
"bweb": "^0.0.1"
|
||||
"bweb": "^0.0.1",
|
||||
"bsock": "^0.0.1"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"bcoin-native": "0.0.23",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user