From a3f0e2f7522d9eb38d247239a5bfce6081e59d50 Mon Sep 17 00:00:00 2001 From: kanhaiya Date: Mon, 4 May 2020 17:15:28 +0530 Subject: [PATCH 1/2] Proxy fix for requested URLs --- lib/conf/node-tunnel-config.json | 2 +- lib/config.js | 62 +++++++++++++++++++++++++++----- lib/tunnel.js | 38 +++++++++++++++++--- lib/tunnel_binary.js | 17 +++++++-- package.json | 2 +- 5 files changed, 103 insertions(+), 18 deletions(-) diff --git a/lib/conf/node-tunnel-config.json b/lib/conf/node-tunnel-config.json index ce611f8..42dbbd7 100644 --- a/lib/conf/node-tunnel-config.json +++ b/lib/conf/node-tunnel-config.json @@ -1 +1 @@ -{"tunnelBinary":{"mac":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"e75d47b0f7947925ddc9e7843ff41024"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"f0650d1d444b7f39df92c5bcc9a898c6"}},"win":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"ce24f90aa3dfb4d875a28150705f16d1"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"09d8e29dc1ba5f809b110a14414b52ce"}},"linux":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"5de336ce0afc3f2987177b18827bdb32"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"3c5e5938475ca6a51fafecf18a1c9c12"}},"freebsd":{"32bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"34935e55c36d25bf88c3fd98828be205"},"64bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"5750dd654c543ba960f468842b641cbd"}}},"AuthUrl":"https://accounts.lambdatest.com/api/user/token/auth","logEnable":true,"latest":"1.1.3","supportedVersions":["1.0.0","1.0.1","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.1.4","2.0.0","3.0.0","4.0.0","5.0.0"]} \ No newline at end of file +{"tunnelBinary":{"mac":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"34433fb765db9fe993cc51a79a9dc056"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/LT_Mac.zip","binaryName":"LT_Mac.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/mac/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"6ca43777f406f8d658320f2bf0e57169"}},"win":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"479633a1aa44065814c792b7ddcbe02a"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/LT_Windows.zip","binaryName":"LT_Windows.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/windows/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"2019aeadb57594dca15d9b95fdbbcc69"}},"linux":{"32bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"6aebc2fd59120aeb81b8041509713a73"},"64bit":{"httpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/LT_Linux.zip","binaryName":"LT_Linux.zip","newHttpPath":"https://downloads.lambdatest.com/tunnel/linux/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"af49cab379849f774978fcccb9cb4e33"}},"freebsd":{"32bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/32bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"8a6dddd85d37576de4f5dc1bd0e54b4d"},"64bit":{"newHttpPath":"https://downloads.lambdatest.com/tunnel/freebsd/64bit/ltcomponent.zip","newBinaryName":"ltcomponent.zip","hash":"c75bae7743d743d0c265118005cb5d6a"}}},"AuthUrl":"https://accounts.lambdatest.com/api/user/token/auth","logEnable":true,"latest":"1.1.4","supportedVersions":["1.0.0","1.0.1","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9","1.1.0","1.1.1","1.1.2","1.1.3","1.1.4","2.0.0","3.0.0","4.0.0","5.0.0"]} \ No newline at end of file diff --git a/lib/config.js b/lib/config.js index 46f91ac..e443e00 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,11 +1,32 @@ var httpTunnelConfigPath = 'https://s3.amazonaws.com/downloads.lambdatest.com/tunnel/node-tunnel-config.json', - httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog'; -var https = require('https'); -var urlParse = require('url'); -module.exports = function(fnCallback) { + httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog', + https = require('https'), + urlParse = require('url'), + HttpsProxyAgent = require('https-proxy-agent'); +module.exports = function(options, fnCallback) { + var reqOptions = urlParse.parse(httpTunnelConfigPath); + var proxyOpts = {}; + if (options.proxyHost && options.proxyPort) { + proxyOpts = { + host: options.proxyHost, + port: options.proxyPort + }; + } else if (process.env.http_proxy) { + proxyOpts = urlParse.parse(process.env.http_proxy); + } + if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) { + throw Error('proxyUser and proxyPass must be both or none'); + } else if (options.proxyUser && options.proxyPass) { + if (Object.keys(proxyOpts).length) { + proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; + } + } + if (Object.keys(proxyOpts).length) { + reqOptions.agent = new HttpsProxyAgent(proxyOpts); + } https - .get(httpTunnelConfigPath, resp => { + .get(reqOptions, resp => { let json = ''; resp.on('data', chunk => { json += chunk; @@ -16,7 +37,7 @@ module.exports = function(fnCallback) { } return fnCallback(false, { jsonResponse: json, - logger: setupLogger_(json) + logger: setupLogger_(options, json) }); }); }) @@ -30,7 +51,7 @@ module.exports = function(fnCallback) { * @param {Object} jsonResponse path of downloaded binary. * @return {Object|Error} Return log method or Error is any */ -function setupLogger_(jsonResponse) { +function setupLogger_(options, jsonResponse) { try { var logger; if (jsonResponse.logEnable) { @@ -59,7 +80,7 @@ function setupLogger_(jsonResponse) { } }); var _httpTunnelLogUrl = urlParse.parse(httpTunnelLogUrl); - var options = { + var reqOptions = { hostname: _httpTunnelLogUrl.hostname, port: _httpTunnelLogUrl.port, path: _httpTunnelLogUrl.path, @@ -70,7 +91,30 @@ function setupLogger_(jsonResponse) { 'Content-Length': data.length } }; - var req = https.request(options, res => { + + var proxyOpts = {}; + if (options.proxyHost && options.proxyPort) { + proxyOpts = { + host: options.proxyHost, + port: options.proxyPort + }; + } else if (process.env.http_proxy) { + proxyOpts = urlParse.parse(process.env.http_proxy); + } + if ( + (options.proxyUser && !options.proxyPass) || + (!options.proxyUser && options.proxyPass) + ) { + throw Error('proxyUser and proxyPass must be both or none'); + } else if (options.proxyUser && options.proxyPass) { + if (Object.keys(proxyOpts).length) { + proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; + } + } + if (Object.keys(proxyOpts).length) { + reqOptions.agent = new HttpsProxyAgent(proxyOpts); + } + var req = https.request(reqOptions, res => { res.on('data', d => { process.stdout.write(d); }); diff --git a/lib/tunnel.js b/lib/tunnel.js index ba99907..7c37589 100644 --- a/lib/tunnel.js +++ b/lib/tunnel.js @@ -4,6 +4,7 @@ var childProcess = require('child_process'), https = require('https'), http = require('http'), urlParse = require('url'), + HttpsProxyAgent = require('https-proxy-agent'), getPort = require('get-port'), os = require('os'), logger, @@ -43,7 +44,7 @@ function Tunnel() { return reject({ message: 'user and key is required' }); } // Configure logger - Config_(function(error, response) { + Config_(options, function(error, response) { if (error) { throw new Error(error); } @@ -389,7 +390,7 @@ function verifyToken_(options, fnCallback) { }); var _httpAuthUrl = urlParse.parse(httpTunnelConfig.jsonResponse.AuthUrl); - var options = { + var reqOptions = { hostname: _httpAuthUrl.hostname, port: _httpAuthUrl.port, path: _httpAuthUrl.path, @@ -403,7 +404,26 @@ function verifyToken_(options, fnCallback) { } }; - var req = https.request(options, resp => { + var proxyOpts = {}; + if (options.proxyHost && options.proxyPort) { + proxyOpts = { + host: options.proxyHost, + port: options.proxyPort + }; + } else if (process.env.http_proxy) { + proxyOpts = urlParse.parse(process.env.http_proxy); + } + if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) { + throw Error('proxyUser and proxyPass must be both or none'); + } else if (options.proxyUser && options.proxyPass) { + if (Object.keys(proxyOpts).length) { + proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; + } + } + if (Object.keys(proxyOpts).length) { + reqOptions.agent = new HttpsProxyAgent(proxyOpts); + } + var req = https.request(reqOptions, resp => { let json = ''; resp.on('data', chunk => { json += chunk; @@ -756,6 +776,13 @@ function getBinaryPath_(that, options, fnCallback) { conf.proxyHost = options['proxyHost'] || options['proxyhost']; conf.proxyPort = options['proxyPort'] || options['proxyport']; } + if ( + (options['proxyUser'] || options['proxyuser']) && + (options['proxyPass'] || options['proxypass']) + ) { + conf.proxyUser = options['proxyUser'] || options['proxyuser']; + conf.proxyPass = options['proxyPass'] || options['proxypass']; + } that.binary.binaryPath_(conf, fnCallback); } else { return fnCallback(that.binaryPath); @@ -778,8 +805,9 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) { if (retries >= 0) { // local Server path for getting tunnelName var url = 'http://127.0.0.1:' + infoAPIPort + '/api/v1.0/info'; + var reqOptions = urlParse.parse(url); http - .get(url, response => { + .get(reqOptions, response => { let json = ''; response.on('data', chunk => { json += chunk; @@ -847,4 +875,4 @@ function clearTimeout_(timeoutId) { } } module.exports = Tunnel; -module.exports.Tunnel = Tunnel; \ No newline at end of file +module.exports.Tunnel = Tunnel; diff --git a/lib/tunnel_binary.js b/lib/tunnel_binary.js index 53ac619..a733ec2 100644 --- a/lib/tunnel_binary.js +++ b/lib/tunnel_binary.js @@ -91,11 +91,24 @@ function TunnelBinary(httpTunnelConfig) { var self = this; // Set Proxy If User passed this to in arguments. var options = urlParse.parse(this.httpPath); + var proxyOpts = {}; if (conf.proxyHost && conf.proxyPort) { - options.agent = new HttpsProxyAgent({ + proxyOpts = { host: conf.proxyHost, port: conf.proxyPort - }); + }; + } else if (process.env.http_proxy) { + proxyOpts = urlParse.parse(process.env.http_proxy); + } + if ((conf.proxyUser && !conf.proxyPass) || (!conf.proxyUser && conf.proxyPass)) { + throw Error('proxyUser and proxyPass must be both or none'); + } else if (conf.proxyUser && conf.proxyPass) { + if (Object.keys(proxyOpts).length) { + proxyOpts.auth = conf.proxyUser + ':' + conf.proxyPass; + } + } + if (Object.keys(proxyOpts).length) { + options.agent = new HttpsProxyAgent(proxyOpts); } // Get binary as zip File from https Server and put this to local folder. // After fully download, unzip and change mode to excutable. diff --git a/package.json b/package.json index c454ca5..1cd3c25 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lambdatest/node-tunnel", - "version": "1.1.3", + "version": "1.1.4", "description": "Nodejs bindings for LambdaTest Tunnel", "main": "index.js", "repository": { From 8c2a4593067c285d2ccbacf20f37c50f60ba341b Mon Sep 17 00:00:00 2001 From: kanhaiya Date: Mon, 4 May 2020 18:14:36 +0530 Subject: [PATCH 2/2] Proxy fix Refactor code --- lib/config.js | 41 ++++------------------------------------- lib/tunnel.js | 19 ++----------------- lib/tunnel_binary.js | 18 ++---------------- lib/util.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 70 deletions(-) create mode 100644 lib/util.js diff --git a/lib/config.js b/lib/config.js index e443e00..b1f332e 100644 --- a/lib/config.js +++ b/lib/config.js @@ -3,25 +3,11 @@ var httpTunnelConfigPath = httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog', https = require('https'), urlParse = require('url'), - HttpsProxyAgent = require('https-proxy-agent'); + HttpsProxyAgent = require('https-proxy-agent'), + util = require('./util'); module.exports = function(options, fnCallback) { var reqOptions = urlParse.parse(httpTunnelConfigPath); - var proxyOpts = {}; - if (options.proxyHost && options.proxyPort) { - proxyOpts = { - host: options.proxyHost, - port: options.proxyPort - }; - } else if (process.env.http_proxy) { - proxyOpts = urlParse.parse(process.env.http_proxy); - } - if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) { - throw Error('proxyUser and proxyPass must be both or none'); - } else if (options.proxyUser && options.proxyPass) { - if (Object.keys(proxyOpts).length) { - proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; - } - } + var proxyOpts = util.getProxyOpts_(options); if (Object.keys(proxyOpts).length) { reqOptions.agent = new HttpsProxyAgent(proxyOpts); } @@ -91,26 +77,7 @@ function setupLogger_(options, jsonResponse) { 'Content-Length': data.length } }; - - var proxyOpts = {}; - if (options.proxyHost && options.proxyPort) { - proxyOpts = { - host: options.proxyHost, - port: options.proxyPort - }; - } else if (process.env.http_proxy) { - proxyOpts = urlParse.parse(process.env.http_proxy); - } - if ( - (options.proxyUser && !options.proxyPass) || - (!options.proxyUser && options.proxyPass) - ) { - throw Error('proxyUser and proxyPass must be both or none'); - } else if (options.proxyUser && options.proxyPass) { - if (Object.keys(proxyOpts).length) { - proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; - } - } + var proxyOpts = util.getProxyOpts_(options); if (Object.keys(proxyOpts).length) { reqOptions.agent = new HttpsProxyAgent(proxyOpts); } diff --git a/lib/tunnel.js b/lib/tunnel.js index 7c37589..78b2d97 100644 --- a/lib/tunnel.js +++ b/lib/tunnel.js @@ -5,6 +5,7 @@ var childProcess = require('child_process'), http = require('http'), urlParse = require('url'), HttpsProxyAgent = require('https-proxy-agent'), + util = require('./util'), getPort = require('get-port'), os = require('os'), logger, @@ -403,23 +404,7 @@ function verifyToken_(options, fnCallback) { version: packageVersion } }; - - var proxyOpts = {}; - if (options.proxyHost && options.proxyPort) { - proxyOpts = { - host: options.proxyHost, - port: options.proxyPort - }; - } else if (process.env.http_proxy) { - proxyOpts = urlParse.parse(process.env.http_proxy); - } - if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) { - throw Error('proxyUser and proxyPass must be both or none'); - } else if (options.proxyUser && options.proxyPass) { - if (Object.keys(proxyOpts).length) { - proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; - } - } + var proxyOpts = util.getProxyOpts_(options); if (Object.keys(proxyOpts).length) { reqOptions.agent = new HttpsProxyAgent(proxyOpts); } diff --git a/lib/tunnel_binary.js b/lib/tunnel_binary.js index a733ec2..1c5ef33 100644 --- a/lib/tunnel_binary.js +++ b/lib/tunnel_binary.js @@ -3,6 +3,7 @@ var localTunnelConfig_ = require('./conf/node-tunnel-config.json'), https = require('https'), urlParse = require('url'), HttpsProxyAgent = require('https-proxy-agent'), + util = require('./util'), unzip = require('unzipper'), fs = require('fs'), path = require('path'), @@ -91,22 +92,7 @@ function TunnelBinary(httpTunnelConfig) { var self = this; // Set Proxy If User passed this to in arguments. var options = urlParse.parse(this.httpPath); - var proxyOpts = {}; - if (conf.proxyHost && conf.proxyPort) { - proxyOpts = { - host: conf.proxyHost, - port: conf.proxyPort - }; - } else if (process.env.http_proxy) { - proxyOpts = urlParse.parse(process.env.http_proxy); - } - if ((conf.proxyUser && !conf.proxyPass) || (!conf.proxyUser && conf.proxyPass)) { - throw Error('proxyUser and proxyPass must be both or none'); - } else if (conf.proxyUser && conf.proxyPass) { - if (Object.keys(proxyOpts).length) { - proxyOpts.auth = conf.proxyUser + ':' + conf.proxyPass; - } - } + var proxyOpts = util.getProxyOpts_(conf); if (Object.keys(proxyOpts).length) { options.agent = new HttpsProxyAgent(proxyOpts); } diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..6e0d226 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,29 @@ +var urlParse = require('url'); +module.exports = { + getProxyOpts_ +}; + +/** + * getProxyOpts_ is used to configre request + * @param {Object} options. + * @return {Object|Error} Return proxyOpts + */ +function getProxyOpts_(options) { + var proxyOpts = {}; + if (options.proxyHost && options.proxyPort) { + proxyOpts = { + host: options.proxyHost, + port: options.proxyPort + }; + } else if (process.env.http_proxy) { + proxyOpts = urlParse.parse(process.env.http_proxy); + } + if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) { + throw Error('proxyUser and proxyPass must be both or none'); + } else if (options.proxyUser && options.proxyPass) { + if (Object.keys(proxyOpts).length) { + proxyOpts.auth = options.proxyUser + ':' + options.proxyPass; + } + } + return proxyOpts +}