From 154bc6fb4d9f73681d677217a45c294b654debb7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 22 Aug 2016 22:09:21 -0700 Subject: [PATCH] browser: improve wsproxy. --- browser/proxysocket.js | 2 +- browser/wsproxy.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/browser/proxysocket.js b/browser/proxysocket.js index c253df76..c16175ab 100644 --- a/browser/proxysocket.js +++ b/browser/proxysocket.js @@ -102,7 +102,7 @@ ProxySocket.prototype.connect = function connect(port, host) { nonce++; assert(nonce <= 0xffffffff, 'Could not create socket.'); pow.writeUInt32LE(nonce, 0, true); - } while (utils.cmp(utils.dsha256(pow), this.target) >= 0); + } while (utils.cmp(utils.dsha256(pow), this.target) > 0); utils.log('Solved proof of work: %d', nonce); } diff --git a/browser/wsproxy.js b/browser/wsproxy.js index 41df1c35..95fd3bb8 100644 --- a/browser/wsproxy.js +++ b/browser/wsproxy.js @@ -50,7 +50,8 @@ module.exports = function wsproxy(options) { return; if (!utils.isNumber(port) - || typeof host !== 'string') { + || typeof host !== 'string' + || host.length === 0) { utils.error('Client gave bad arguments.'); ws.emit('tcp close'); ws.disconnect(); @@ -72,7 +73,7 @@ module.exports = function wsproxy(options) { pow.writeString(host, 'ascii'); pow = pow.render(); - if (utils.cmp(utils.dsha256(pow), target) >= 0) { + if (utils.cmp(utils.dsha256(pow), target) > 0) { utils.error('Client did not solve proof of work.'); ws.emit('tcp close'); ws.disconnect(); @@ -103,8 +104,9 @@ module.exports = function wsproxy(options) { try { socket = net.connect(port, host); - utils.error('Connecting to %s:%d.', host, port); + utils.log('Connecting to %s:%d.', host, port); } catch (e) { + utils.error(e.message); utils.error('Closing %s:%d.', host, port); ws.emit('tcp close'); ws.disconnect(); @@ -127,12 +129,14 @@ module.exports = function wsproxy(options) { }); socket.on('close', function() { - utils.error('Closing %s:%d.', host, port); + utils.log('Closing %s:%d.', host, port); ws.emit('tcp close'); ws.disconnect(); }); ws.on('tcp data', function(data) { + if (typeof data !== 'string') + return; socket.write(new Buffer(data, 'hex')); });