Skip to content

Commit ac8cded

Browse files
author
Kanhaiya Lal Singh
authored
Merge pull request #3 from LambdaTest/proxy-fix
Proxy fix for requested URLs
2 parents 67f21d7 + 8c2a459 commit ac8cded

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

lib/conf/node-tunnel-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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"]}
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":"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"]}

lib/config.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
var httpTunnelConfigPath =
22
'https://s3.amazonaws.com/downloads.lambdatest.com/tunnel/node-tunnel-config.json',
3-
httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog';
4-
var https = require('https');
5-
var urlParse = require('url');
6-
module.exports = function(fnCallback) {
3+
httpTunnelLogUrl = 'https://oinwgsy681.execute-api.us-east-1.amazonaws.com/prod/addLog',
4+
https = require('https'),
5+
urlParse = require('url'),
6+
HttpsProxyAgent = require('https-proxy-agent'),
7+
util = require('./util');
8+
module.exports = function(options, fnCallback) {
9+
var reqOptions = urlParse.parse(httpTunnelConfigPath);
10+
var proxyOpts = util.getProxyOpts_(options);
11+
if (Object.keys(proxyOpts).length) {
12+
reqOptions.agent = new HttpsProxyAgent(proxyOpts);
13+
}
714
https
8-
.get(httpTunnelConfigPath, resp => {
15+
.get(reqOptions, resp => {
916
let json = '';
1017
resp.on('data', chunk => {
1118
json += chunk;
@@ -16,7 +23,7 @@ module.exports = function(fnCallback) {
1623
}
1724
return fnCallback(false, {
1825
jsonResponse: json,
19-
logger: setupLogger_(json)
26+
logger: setupLogger_(options, json)
2027
});
2128
});
2229
})
@@ -30,7 +37,7 @@ module.exports = function(fnCallback) {
3037
* @param {Object} jsonResponse path of downloaded binary.
3138
* @return {Object|Error} Return log method or Error is any
3239
*/
33-
function setupLogger_(jsonResponse) {
40+
function setupLogger_(options, jsonResponse) {
3441
try {
3542
var logger;
3643
if (jsonResponse.logEnable) {
@@ -59,7 +66,7 @@ function setupLogger_(jsonResponse) {
5966
}
6067
});
6168
var _httpTunnelLogUrl = urlParse.parse(httpTunnelLogUrl);
62-
var options = {
69+
var reqOptions = {
6370
hostname: _httpTunnelLogUrl.hostname,
6471
port: _httpTunnelLogUrl.port,
6572
path: _httpTunnelLogUrl.path,
@@ -70,7 +77,11 @@ function setupLogger_(jsonResponse) {
7077
'Content-Length': data.length
7178
}
7279
};
73-
var req = https.request(options, res => {
80+
var proxyOpts = util.getProxyOpts_(options);
81+
if (Object.keys(proxyOpts).length) {
82+
reqOptions.agent = new HttpsProxyAgent(proxyOpts);
83+
}
84+
var req = https.request(reqOptions, res => {
7485
res.on('data', d => {
7586
process.stdout.write(d);
7687
});

lib/tunnel.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var childProcess = require('child_process'),
44
https = require('https'),
55
http = require('http'),
66
urlParse = require('url'),
7+
HttpsProxyAgent = require('https-proxy-agent'),
8+
util = require('./util'),
79
getPort = require('get-port'),
810
os = require('os'),
911
logger,
@@ -43,7 +45,7 @@ function Tunnel() {
4345
return reject({ message: 'user and key is required' });
4446
}
4547
// Configure logger
46-
Config_(function(error, response) {
48+
Config_(options, function(error, response) {
4749
if (error) {
4850
throw new Error(error);
4951
}
@@ -389,7 +391,7 @@ function verifyToken_(options, fnCallback) {
389391
});
390392

391393
var _httpAuthUrl = urlParse.parse(httpTunnelConfig.jsonResponse.AuthUrl);
392-
var options = {
394+
var reqOptions = {
393395
hostname: _httpAuthUrl.hostname,
394396
port: _httpAuthUrl.port,
395397
path: _httpAuthUrl.path,
@@ -402,8 +404,11 @@ function verifyToken_(options, fnCallback) {
402404
version: packageVersion
403405
}
404406
};
405-
406-
var req = https.request(options, resp => {
407+
var proxyOpts = util.getProxyOpts_(options);
408+
if (Object.keys(proxyOpts).length) {
409+
reqOptions.agent = new HttpsProxyAgent(proxyOpts);
410+
}
411+
var req = https.request(reqOptions, resp => {
407412
let json = '';
408413
resp.on('data', chunk => {
409414
json += chunk;
@@ -756,6 +761,13 @@ function getBinaryPath_(that, options, fnCallback) {
756761
conf.proxyHost = options['proxyHost'] || options['proxyhost'];
757762
conf.proxyPort = options['proxyPort'] || options['proxyport'];
758763
}
764+
if (
765+
(options['proxyUser'] || options['proxyuser']) &&
766+
(options['proxyPass'] || options['proxypass'])
767+
) {
768+
conf.proxyUser = options['proxyUser'] || options['proxyuser'];
769+
conf.proxyPass = options['proxyPass'] || options['proxypass'];
770+
}
759771
that.binary.binaryPath_(conf, fnCallback);
760772
} else {
761773
return fnCallback(that.binaryPath);
@@ -778,8 +790,9 @@ function retryTunnelName_(self, infoAPIPort, retries, fnCallback) {
778790
if (retries >= 0) {
779791
// local Server path for getting tunnelName
780792
var url = 'http://127.0.0.1:' + infoAPIPort + '/api/v1.0/info';
793+
var reqOptions = urlParse.parse(url);
781794
http
782-
.get(url, response => {
795+
.get(reqOptions, response => {
783796
let json = '';
784797
response.on('data', chunk => {
785798
json += chunk;
@@ -847,4 +860,4 @@ function clearTimeout_(timeoutId) {
847860
}
848861
}
849862
module.exports = Tunnel;
850-
module.exports.Tunnel = Tunnel;
863+
module.exports.Tunnel = Tunnel;

lib/tunnel_binary.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var localTunnelConfig_ = require('./conf/node-tunnel-config.json'),
33
https = require('https'),
44
urlParse = require('url'),
55
HttpsProxyAgent = require('https-proxy-agent'),
6+
util = require('./util'),
67
unzip = require('unzipper'),
78
fs = require('fs'),
89
path = require('path'),
@@ -91,11 +92,9 @@ function TunnelBinary(httpTunnelConfig) {
9192
var self = this;
9293
// Set Proxy If User passed this to in arguments.
9394
var options = urlParse.parse(this.httpPath);
94-
if (conf.proxyHost && conf.proxyPort) {
95-
options.agent = new HttpsProxyAgent({
96-
host: conf.proxyHost,
97-
port: conf.proxyPort
98-
});
95+
var proxyOpts = util.getProxyOpts_(conf);
96+
if (Object.keys(proxyOpts).length) {
97+
options.agent = new HttpsProxyAgent(proxyOpts);
9998
}
10099
// Get binary as zip File from https Server and put this to local folder.
101100
// After fully download, unzip and change mode to excutable.

lib/util.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var urlParse = require('url');
2+
module.exports = {
3+
getProxyOpts_
4+
};
5+
6+
/**
7+
* getProxyOpts_ is used to configre request
8+
* @param {Object} options.
9+
* @return {Object|Error} Return proxyOpts
10+
*/
11+
function getProxyOpts_(options) {
12+
var proxyOpts = {};
13+
if (options.proxyHost && options.proxyPort) {
14+
proxyOpts = {
15+
host: options.proxyHost,
16+
port: options.proxyPort
17+
};
18+
} else if (process.env.http_proxy) {
19+
proxyOpts = urlParse.parse(process.env.http_proxy);
20+
}
21+
if ((options.proxyUser && !options.proxyPass) || (!options.proxyUser && options.proxyPass)) {
22+
throw Error('proxyUser and proxyPass must be both or none');
23+
} else if (options.proxyUser && options.proxyPass) {
24+
if (Object.keys(proxyOpts).length) {
25+
proxyOpts.auth = options.proxyUser + ':' + options.proxyPass;
26+
}
27+
}
28+
return proxyOpts
29+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/node-tunnel",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "Nodejs bindings for LambdaTest Tunnel",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)