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