From c148a1e6b593f73778999cc59b1b9a8df5038c81 Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Wed, 3 Jan 2018 08:26:29 +1000 Subject: [PATCH 1/5] Added proxy support from environment variable --- index.js | 11 ++++++++++- package.json | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b9e4694..d1cade5 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ var Vow = require('vow'); var extend = require('extend'); var WebSocket = require('ws'); var EventEmitter = require('events').EventEmitter; +var HttpsProxyAgent = require('https-proxy-agent'); +var Url = require('url'); class Bot extends EventEmitter { /** @@ -19,6 +21,13 @@ class Bot extends EventEmitter { this.name = params.name; console.assert(params.token, 'token must be defined'); + + // Use a HTTP Proxy if defined in the environment. We need to accomodate both uppercase and lowercase environment definitions + this._agent = null; + if ((typeof process.env.http_proxy !== 'undefined' && process.env.http_proxy) || (typeof process.env.HTTP_PROXY !== 'undefined' && process.env.HTTP_PROXY)) { + this._agent = new HttpsProxyAgent(Url.parse(process.env.http_proxy || process.env.HTTP_PROXY)); + } + this.login(); } @@ -47,7 +56,7 @@ class Bot extends EventEmitter { * Establish a WebSocket connection */ connect() { - this.ws = new WebSocket(this.wsUrl); + this.ws = new WebSocket(this.wsUrl, {agent: this._agent}); this.ws.on('open', function(data) { this.emit('open', data); diff --git a/package.json b/package.json index d07d92e..6054535 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ }, "dependencies": { "extend": "^2.0.1", + "https-proxy-agent": "^2.1.1", "lodash": "^4.17.2", "request": "^2.56.0", "vow": "^0.4.9", From 955c4106362ccc89cb9e05afbd1bf708c79f2aae Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Wed, 3 Jan 2018 09:27:30 +1000 Subject: [PATCH 2/5] Added proxy instructions to readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 33a9b61..2e47c68 100644 --- a/README.md +++ b/README.md @@ -115,3 +115,7 @@ bot.postMessageToUser('user', 'hi').always(function(data) { }) ``` +### HTTP Proxy Support + +This package can work behind a corporate proxy when the `HTTP_PROXY` environment variable is set, +ie: `HTTP_PROXY=http://10.60.10.1:3128` From 6fb3d158a412fe9121382b593ae589186f65e69b Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Tue, 9 Jan 2018 13:51:51 +1000 Subject: [PATCH 3/5] Catch and display proxy url errors --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d1cade5..7c2ef10 100644 --- a/index.js +++ b/index.js @@ -22,10 +22,20 @@ class Bot extends EventEmitter { console.assert(params.token, 'token must be defined'); - // Use a HTTP Proxy if defined in the environment. We need to accomodate both uppercase and lowercase environment definitions + // Use a HTTP Proxy if defined in the environment. We need to accommodate both uppercase and lowercase environment definitions this._agent = null; if ((typeof process.env.http_proxy !== 'undefined' && process.env.http_proxy) || (typeof process.env.HTTP_PROXY !== 'undefined' && process.env.HTTP_PROXY)) { - this._agent = new HttpsProxyAgent(Url.parse(process.env.http_proxy || process.env.HTTP_PROXY)); + try { + // Url.parse can throw it's own errors but strangely doesn't throw when the url is not really valid. + var proxy_url_obj = Url.parse(process.env.http_proxy || process.env.HTTP_PROXY); + if (!proxy_url_obj.hostname) { + throw new Error('Invalid HTTP_PROXY environment variable value'); + } + + this._agent = new HttpsProxyAgent(proxy_url_obj); + } catch (err) { + console.log(err.message); + } } this.login(); From 94605153b0d9dcf87421e71d4b1a3f9a088002bf Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Tue, 9 Jan 2018 13:56:40 +1000 Subject: [PATCH 4/5] Code standards compliance --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7c2ef10..7705480 100644 --- a/index.js +++ b/index.js @@ -22,19 +22,19 @@ class Bot extends EventEmitter { console.assert(params.token, 'token must be defined'); - // Use a HTTP Proxy if defined in the environment. We need to accommodate both uppercase and lowercase environment definitions + // Use a HTTP Proxy if defined in the environment. We need to accomodate both uppercase and lowercase environment definitions this._agent = null; - if ((typeof process.env.http_proxy !== 'undefined' && process.env.http_proxy) || (typeof process.env.HTTP_PROXY !== 'undefined' && process.env.HTTP_PROXY)) { + if ((typeof process.env['http_proxy'] !== 'undefined' && process.env['http_proxy']) || (typeof process.env['HTTP_PROXY'] !== 'undefined' && process.env['HTTP_PROXY'])) { try { // Url.parse can throw it's own errors but strangely doesn't throw when the url is not really valid. - var proxy_url_obj = Url.parse(process.env.http_proxy || process.env.HTTP_PROXY); - if (!proxy_url_obj.hostname) { + var proxyUrl = Url.parse(process.env['http_proxy'] || process.env['HTTP_PROXY']); + if (!proxyUrl.hostname) { throw new Error('Invalid HTTP_PROXY environment variable value'); } - this._agent = new HttpsProxyAgent(proxy_url_obj); - } catch (err) { - console.log(err.message); + this._agent = new HttpsProxyAgent(proxyUrl); + } catch (e) { + console.error(e); } } From bd91802112ba05153e2ce0b26f949a23d3d1f3df Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Tue, 9 Jan 2018 14:05:23 +1000 Subject: [PATCH 5/5] Move proxy configuration to a function, now supports 'proxy' param in the constructor --- index.js | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 7705480..a6bc701 100644 --- a/index.js +++ b/index.js @@ -22,22 +22,7 @@ class Bot extends EventEmitter { console.assert(params.token, 'token must be defined'); - // Use a HTTP Proxy if defined in the environment. We need to accomodate both uppercase and lowercase environment definitions - this._agent = null; - if ((typeof process.env['http_proxy'] !== 'undefined' && process.env['http_proxy']) || (typeof process.env['HTTP_PROXY'] !== 'undefined' && process.env['HTTP_PROXY'])) { - try { - // Url.parse can throw it's own errors but strangely doesn't throw when the url is not really valid. - var proxyUrl = Url.parse(process.env['http_proxy'] || process.env['HTTP_PROXY']); - if (!proxyUrl.hostname) { - throw new Error('Invalid HTTP_PROXY environment variable value'); - } - - this._agent = new HttpsProxyAgent(proxyUrl); - } catch (e) { - console.error(e); - } - } - + this._configureProxy(params.proxy || undefined); this.login(); } @@ -512,6 +497,34 @@ class Bot extends EventEmitter { }); }); } + + /** + * Use a HTTP Proxy is supplied in the params of the custructor, otherwise look at the environment vars + * for proxy configuration. We need to accomodate both uppercase and lowercase environment definitions. + * + * @param {string} proxy + * @private + */ + _configureProxy(proxy) { + if (!proxy && ((typeof process.env['http_proxy'] !== 'undefined' && process.env['http_proxy']) || (typeof process.env['HTTP_PROXY'] !== 'undefined' && process.env['HTTP_PROXY']))) { + proxy = process.env['http_proxy'] || process.env['HTTP_PROXY']; + } + + this._agent = null; + if (proxy) { + try { + // Url.parse can throw it's own errors but strangely doesn't throw when the url is not really valid. + var proxyUrl = Url.parse(process.env['http_proxy'] || process.env['HTTP_PROXY']); + if (!proxyUrl.hostname) { + throw new Error('Invalid HTTP_PROXY environment variable value'); + } + + this._agent = new HttpsProxyAgent(proxyUrl); + } catch (e) { + console.error(e); + } + } + } } module.exports = Bot;