added support for explicit proxy

This commit is contained in:
azivner 2018-12-17 22:54:54 +01:00
parent 7c3bbfd45e
commit 44cc86a1a5
2 changed files with 32 additions and 10 deletions

View File

@ -6,11 +6,9 @@ const url = require('url');
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs // this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
// this allows to support system proxy // 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) { function exec(opts) {
const client = getClient(opts); const client = getClient(opts);
const parsedUrl = url.parse(opts.url); const parsedTargetUrl = url.parse(opts.url);
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
@ -25,15 +23,34 @@ function exec(opts) {
headers['Authorization'] = `Basic ${token}`; 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({ const request = client.request({
method: opts.method, method: opts.method,
// url is used by electron net module // url is used by electron net module
url: opts.url, url: opts.url,
// 4 fields below are used by http and https node modules // 4 fields below are used by http and https node modules
protocol: parsedUrl.protocol, protocol,
host: parsedUrl.hostname, host,
port: parsedUrl.port, port,
path: parsedUrl.path, path,
timeout: opts.timeout, timeout: opts.timeout,
headers headers
}); });
@ -70,11 +87,15 @@ function exec(opts) {
} }
function getClient(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; return require('electron').net;
} }
else { 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:') { if (protocol === 'http:' || protocol === 'https:') {
return require(protocol.substr(0, protocol.length - 1)); return require(protocol.substr(0, protocol.length - 1));

View File

@ -223,7 +223,8 @@ async function syncRequest(syncContext, method, requestPath, body) {
url: await syncOptions.getSyncServerHost() + requestPath, url: await syncOptions.getSyncServerHost() + requestPath,
cookieJar: syncContext.cookieJar, cookieJar: syncContext.cookieJar,
timeout: await syncOptions.getSyncTimeout(), timeout: await syncOptions.getSyncTimeout(),
body body,
proxy: proxyToggle ? await syncOptions.getSyncProxy() : null
}); });
} }