Skip to content

Commit

Permalink
improve env var proxy handling
Browse files Browse the repository at this point in the history
  • Loading branch information
emirotin committed Apr 25, 2017
1 parent f4ddd6f commit 95a3f8c
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ var tunnel = require('tunnel');
var agents = require('./lib/agents');
exports.agents = agents;

// save the original globalAgents for restoration later.
var ENV_VAR_PROXY_SEARCH_ORDER = [ 'https_proxy', 'HTTPS_PROXY', 'http_proxy', 'HTTP_PROXY' ]

// save the original settings for restoration later.
var ORIGINALS = {
http: pick(http, 'globalAgent', 'request'),
https: pick(https, 'globalAgent', 'request')
https: pick(https, 'globalAgent', 'request'),
env: pick(process.env, ENV_VAR_PROXY_SEARCH_ORDER)
};
function resetGlobals() {
assign(http, ORIGINALS.http);
assign(https, ORIGINALS.https);
var val;
for (var key in ORIGINALS.env) {
val = ORIGINALS.env[key];
if (val != null) {
process.env[key] = val;
}
}
}

/**
Expand All @@ -48,6 +58,23 @@ function tryParse(url) {

globalTunnel.isProxying = false;

function findEnvVarProxy() {
var key, val, result;
for (var i = 0; i < ENV_VAR_PROXY_SEARCH_ORDER.length; i++) {
key = ENV_VAR_PROXY_SEARCH_ORDER[i];
val = process.env[key];
if (val != null) {
// get the first non-empty
result = result || val;
// delete all
// NB: we do it here to prevent double proxy handling (and for example path change)
// by us and the `request` module or other sub-dependencies
delete process.env[key];
}
}
return result;
}

/**
* Overrides the node http/https `globalAgent`s to use the configured proxy.
*
Expand All @@ -73,14 +100,13 @@ globalTunnel.initialize = function(conf) {
conf = clone(conf)
} else {
// nothing passed - parse from the env
conf = tryParse(process.env['http_proxy']);
if (!conf) {
var envVarProxy = findEnvVarProxy();

if (!envVarProxy) {
globalTunnel.isProxying = false;
return;
} else {
// TODO: we do it here to prevent double path change by us and the `request` module
// Is it too bad to do so?
delete process.env['http_proxy'];
conf = tryParse(envVarProxy);
}
}

Expand Down

0 comments on commit 95a3f8c

Please sign in to comment.