From 060a50a6e8ecc26fa237179a5b788f325d59af70 Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 13 Feb 2014 14:14:53 -0500 Subject: [PATCH] Connection: add configurable authentication timeout --- lib/Connection.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/Connection.js b/lib/Connection.js index 2409fb1..6f5fdb7 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -57,6 +57,7 @@ function Connection(config) { xoauth: config.xoauth, xoauth2: config.xoauth2, connTimeout: config.connTimeout || 10000, + authTimeout: config.authTimeout || 5000, keepalive: (typeof config.keepalive === 'boolean' ? config.keepalive : true) @@ -65,6 +66,7 @@ function Connection(config) { this._sock = undefined; this._tagcount = 0; this._tmrConn = undefined; + this._tmrAuth = undefined; this._queue = []; this._box = undefined; this._idle = {}; @@ -87,6 +89,7 @@ Connection.prototype.connect = function() { this._sock = undefined; this._tagcount = 0; this._tmrConn = undefined; + this._tmrAuth = undefined; this._queue = []; this._box = undefined; this._idle = {}; @@ -115,10 +118,17 @@ Connection.prototype.connect = function() { clearTimeout(self._tmrConn); self.state = 'connected'; self.debug && self.debug('[connection] Connected to host'); + self._tmrAuth = setTimeout(function() { + var err = new Error('Timed out while authenticating with server'); + err.source = 'timeout-auth'; + self.emit('error', err); + socket.destroy(); + }, config.authTimeout); } this._onError = function(err) { clearTimeout(self._tmrConn); + clearTimeout(self._tmrAuth); clearTimeout(self._tmrKeepalive); self.debug && self.debug('[connection] Error: ' + err); err.source = 'socket'; @@ -128,6 +138,7 @@ Connection.prototype.connect = function() { socket.once('close', function(had_err) { clearTimeout(self._tmrConn); + clearTimeout(self._tmrAuth); clearTimeout(self._tmrKeepalive); self.state = 'disconnected'; self.debug && self.debug('[connection] Closed'); @@ -136,6 +147,7 @@ Connection.prototype.connect = function() { socket.once('end', function() { clearTimeout(self._tmrConn); + clearTimeout(self._tmrAuth); clearTimeout(self._tmrKeepalive); self.state = 'disconnected'; self.debug && self.debug('[connection] Ended'); @@ -1199,6 +1211,7 @@ Connection.prototype._resUntagged = function(info) { } else if (type === 'bad' || type === 'no') { if (this.state === 'connected' && !this._curReq) { clearTimeout(this._tmrConn); + clearTimeout(this._tmrAuth); var err = new Error('Received negative welcome: ' + info.text); err.source = 'protocol'; this.emit('error', err); @@ -1523,6 +1536,7 @@ Connection.prototype._login = function() { var self = this, checkedNS = false; var reentry = function(err) { + clearTimeout(self._tmrAuth); if (err) { self.emit('error', err); return self._sock.destroy();