From c6aad6448ccf1857ef0cacdf0fc141594be0b22f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 25 Jan 2017 01:20:58 -0800 Subject: [PATCH] socks: error handling. --- lib/net/socks.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/net/socks.js b/lib/net/socks.js index a4eb1deb..6f789f95 100644 --- a/lib/net/socks.js +++ b/lib/net/socks.js @@ -67,23 +67,26 @@ SOCKS.errors = [ 'Unknown proxy error' ]; -SOCKS.prototype.error = function error(msg) { +SOCKS.prototype.error = function error(err) { + var msg; + if (this.destroyed) return; - if (msg instanceof Error) { - this.emit('error', msg); + if (err instanceof Error) { + this.emit('error', err); this.destroy(); return; } + msg = util.fmt.apply(util, arguments); this.emit('error', new Error(msg)); this.destroy(); }; SOCKS.prototype.getError = function getError(code) { if (code >= SOCKS.errors.length) - return 'Unknown'; + return SOCKS.errors[9]; return SOCKS.errors[code]; }; @@ -108,7 +111,7 @@ SOCKS.prototype.startTimeout = function startTimeout() { this.timeout = setTimeout(function() { var state = SOCKS.statesByVal[self.state]; self.timeout = null; - self.error('SOCKS request timed out (state=' + state + ').'); + self.error('SOCKS request timed out (state=%s).', state); }, 8000); }; @@ -210,7 +213,7 @@ SOCKS.prototype.handleClose = function handleClose() { if (this.state !== this.target) { state = SOCKS.statesByVal[this.target]; - this.error('SOCKS request destroyed (target=' + state + ').'); + this.error('SOCKS request destroyed (state=%s).', state); return; } @@ -291,7 +294,7 @@ SOCKS.prototype.handleHandshake = function handleHandshake(data) { this.auth(); break; default: - this.error('SOCKS handshake error: ' + data[1]); + this.error('SOCKS handshake error: %d.', data[1]); break; } }; @@ -339,7 +342,7 @@ SOCKS.prototype.handleAuth = function handleAuth(data) { } if (data[1] !== 0x00) { - this.error('SOCKS auth failure: ' + data[0]); + this.error('SOCKS auth failure: %d.', data[0]); return; } @@ -407,7 +410,7 @@ SOCKS.prototype.sendProxy = function sendProxy() { }; SOCKS.prototype.handleProxy = function handleProxy(data) { - var br, len, host, port, err; + var br, len, host, port, msg; if (data.length < 6) { this.error('Bad packet size for SOCKS connect.'); @@ -420,8 +423,8 @@ SOCKS.prototype.handleProxy = function handleProxy(data) { } if (data[1] !== 0x00) { - err = this.getError(data[1]); - this.error('SOCKS connect error: ' + err); + msg = this.getError(data[1]); + this.error('SOCKS connect error: %s.', msg); return; } @@ -460,7 +463,7 @@ SOCKS.prototype.handleProxy = function handleProxy(data) { port = br.readU16BE(); break; default: - this.error('Unknown SOCKS connect response: ' + data[4]); + this.error('Unknown SOCKS address type: %d.', data[4]); return; } @@ -491,7 +494,7 @@ SOCKS.prototype.sendResolve = function sendResolve() { }; SOCKS.prototype.handleResolve = function handleResolve(data) { - var ip, err; + var ip, msg; if (data.length !== 10) { this.error('Bad packet size for tor resolve.'); @@ -504,13 +507,13 @@ SOCKS.prototype.handleResolve = function handleResolve(data) { } if (data[1] !== 0x00) { - err = this.getError(data[1]); - this.emit('Tor resolve error: ' + err + ' (' + this.name + ')'); + msg = this.getError(data[1]); + this.error('Tor resolve error: %s (%s).', msg, this.name); return; } if (data[2] !== 0x00) { - this.error('Unknown tor resolve error: ' + data[2]); + this.error('Unknown tor resolve error: %d.', data[2]); return; }