From 44cc86a1a5aa2219426608591c1bc20223c8fab7 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 17 Dec 2018 22:54:54 +0100 Subject: [PATCH] added support for explicit proxy --- src/services/request.js | 39 ++++++++++++++++++++++++++++++--------- src/services/sync.js | 3 ++- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/services/request.js b/src/services/request.js index 697e1547e..02e546cc4 100644 --- a/src/services/request.js +++ b/src/services/request.js @@ -6,11 +6,9 @@ const url = require('url'); // this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs // this allows to support system proxy -// TODO: add proxy support - see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client - function exec(opts) { const client = getClient(opts); - const parsedUrl = url.parse(opts.url); + const parsedTargetUrl = url.parse(opts.url); return new Promise(async (resolve, reject) => { try { @@ -25,15 +23,34 @@ function exec(opts) { headers['Authorization'] = `Basic ${token}`; } + let host = parsedTargetUrl.hostname; + let protocol = parsedTargetUrl.protocol; + let port = parsedTargetUrl.port; + let path = parsedTargetUrl.path; + + if (opts.proxy) { + // see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client + const parsedProxyUrl = url.parse(opts.proxy); + + protocol = parsedProxyUrl.protocol; + host = parsedProxyUrl.hostname; + port = parsedProxyUrl.port; + path = opts.url; + + console.log("Using proxy " + opts.proxy); + + headers['Host'] = parsedTargetUrl.host; // host also includes port + } + const request = client.request({ method: opts.method, // url is used by electron net module url: opts.url, // 4 fields below are used by http and https node modules - protocol: parsedUrl.protocol, - host: parsedUrl.hostname, - port: parsedUrl.port, - path: parsedUrl.path, + protocol, + host, + port, + path, timeout: opts.timeout, headers }); @@ -70,11 +87,15 @@ function exec(opts) { } function getClient(opts) { - if (utils.isElectron()) { + // it's not clear how to explicitly configure proxy (as opposed to system proxy) + // so in that case we always use node's modules + if (utils.isElectron() && !opts.proxy) { return require('electron').net; } else { - const {protocol} = url.parse(opts.url); + // in case there's explicit proxy then we need to use protocol of the proxy since we're actually + // connecting to the proxy server and not to the end-target server + const {protocol} = url.parse(opts.proxy || opts.url); if (protocol === 'http:' || protocol === 'https:') { return require(protocol.substr(0, protocol.length - 1)); diff --git a/src/services/sync.js b/src/services/sync.js index 5ef1fb0e3..175704142 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -223,7 +223,8 @@ async function syncRequest(syncContext, method, requestPath, body) { url: await syncOptions.getSyncServerHost() + requestPath, cookieJar: syncContext.cookieJar, timeout: await syncOptions.getSyncTimeout(), - body + body, + proxy: proxyToggle ? await syncOptions.getSyncProxy() : null }); }