index.js
9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* jshint node:true */
'use strict';
/**
* @fileOverview
* Global proxy settings.
*/
var globalTunnel = exports;
exports.constructor = function() {};
var http = require('http');
var https = require('https');
var urlParse = require('url').parse;
var urlStringify = require('url').format;
var pick = require('lodash/pick');
var assign = require('lodash/assign');
var clone = require('lodash/clone');
var tunnel = require('tunnel');
var npmConfig = require('npm-conf');
var encodeUrl = require('encodeurl');
var agents = require('./lib/agents');
exports.agents = agents;
var ENV_VAR_PROXY_SEARCH_ORDER = [
'https_proxy',
'HTTPS_PROXY',
'http_proxy',
'HTTP_PROXY'
];
var NPM_CONFIG_PROXY_SEARCH_ORDER = ['https-proxy', 'http-proxy', 'proxy'];
// Save the original settings for restoration later.
var ORIGINALS = {
http: pick(http, 'globalAgent', ['request', 'get']),
https: pick(https, 'globalAgent', ['request', 'get']),
env: pick(process.env, ENV_VAR_PROXY_SEARCH_ORDER)
};
var loggingEnabled =
process &&
process.env &&
process.env.DEBUG &&
process.env.DEBUG.toLowerCase().indexOf('global-tunnel') !== -1 &&
console &&
typeof console.log === 'function';
function log(message) {
if (loggingEnabled) {
console.log('DEBUG global-tunnel: ' + message);
}
}
function resetGlobals() {
assign(http, ORIGINALS.http);
assign(https, ORIGINALS.https);
var val;
for (var key in ORIGINALS.env) {
if (Object.prototype.hasOwnProperty.call(ORIGINALS.env, key)) {
val = ORIGINALS.env[key];
if (val !== null && val !== undefined) {
process.env[key] = val;
}
}
}
}
/**
* Parses the de facto `http_proxy` environment.
*/
function tryParse(url) {
if (!url) {
return null;
}
var parsed = urlParse(url);
return {
protocol: parsed.protocol,
host: parsed.hostname,
port: parseInt(parsed.port, 10),
proxyAuth: parsed.auth
};
}
// Stringifies the normalized parsed config
function stringifyProxy(conf) {
return encodeUrl(
urlStringify({
protocol: conf.protocol,
hostname: conf.host,
port: conf.port,
auth: conf.proxyAuth
})
);
}
globalTunnel.isProxying = false;
globalTunnel.proxyUrl = null;
globalTunnel.proxyConfig = null;
function findEnvVarProxy() {
var i;
var key;
var val;
var result;
for (i = 0; i < ENV_VAR_PROXY_SEARCH_ORDER.length; i++) {
key = ENV_VAR_PROXY_SEARCH_ORDER[i];
val = process.env[key];
if (val !== null && val !== undefined) {
// 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];
log('Found proxy in environment variable ' + ENV_VAR_PROXY_SEARCH_ORDER[i]);
}
}
if (!result) {
// __GLOBAL_TUNNEL_DEPENDENCY_NPMCONF__ is a hook to override the npm-conf module
var config =
(global.__GLOBAL_TUNNEL_DEPENDENCY_NPMCONF__ &&
global.__GLOBAL_TUNNEL_DEPENDENCY_NPMCONF__()) ||
npmConfig();
for (i = 0; i < NPM_CONFIG_PROXY_SEARCH_ORDER.length && !val; i++) {
val = config.get(NPM_CONFIG_PROXY_SEARCH_ORDER[i]);
}
if (val) {
log('Found proxy in npm config ' + NPM_CONFIG_PROXY_SEARCH_ORDER[i]);
result = val;
}
}
return result;
}
/**
* Overrides the node http/https `globalAgent`s to use the configured proxy.
*
* If the config is empty, the `http_proxy` environment variable is checked.
* If that's not present, the NPM `http-proxy` configuration is checked.
* If neither are present no proxying will be enabled.
*
* @param {object} conf - Options
* @param {string} conf.host - Hostname or IP of the HTTP proxy to use
* @param {int} conf.port - TCP port of the proxy
* @param {string} [conf.protocol='http'] - The protocol of the proxy, 'http' or 'https'
* @param {string} [conf.proxyAuth] - Credentials for the proxy in the form userId:password
* @param {string} [conf.connect='https'] - Which protocols will use the CONNECT method 'neither', 'https' or 'both'
* @param {int} [conf.sockets=5] Maximum number of TCP sockets to use in each pool. There are two different pools for HTTP and HTTPS
* @param {object} [conf.httpsOptions] - HTTPS options
*/
globalTunnel.initialize = function(conf) {
// Don't do anything if already proxying.
// To change the settings `.end()` should be called first.
if (globalTunnel.isProxying) {
log('Already proxying');
return;
}
try {
// This has an effect of also removing the proxy config
// from the global env to prevent other modules (like request) doing
// double handling
var envVarProxy = findEnvVarProxy();
if (conf && typeof conf === 'string') {
// Passed string - parse it as a URL
conf = tryParse(conf);
} else if (conf) {
// Passed object - take it but clone for future mutations
conf = clone(conf);
} else if (envVarProxy) {
// Nothing passed - parse from the env
conf = tryParse(envVarProxy);
} else {
log('No configuration found, not proxying');
// No config - do nothing
return;
}
log('Proxy configuration to be used is ' + JSON.stringify(conf, null, 2));
if (!conf.host) {
throw new Error('upstream proxy host is required');
}
if (!conf.port) {
throw new Error('upstream proxy port is required');
}
if (conf.protocol === undefined) {
conf.protocol = 'http:'; // Default to proxy speaking http
}
if (!/:$/.test(conf.protocol)) {
conf.protocol += ':';
}
if (!conf.connect) {
conf.connect = 'https'; // Just HTTPS by default
}
if (['both', 'neither', 'https'].indexOf(conf.connect) < 0) {
throw new Error('valid connect options are "neither", "https", or "both"');
}
var connectHttp = conf.connect === 'both';
var connectHttps = conf.connect !== 'neither';
if (conf.httpsOptions) {
conf.innerHttpsOpts = conf.httpsOptions;
conf.outerHttpsOpts = conf.innerHttpsOpts;
}
http.globalAgent = globalTunnel._makeAgent(conf, 'http', connectHttp);
https.globalAgent = globalTunnel._makeAgent(conf, 'https', connectHttps);
http.request = globalTunnel._makeHttp('request', http, 'http');
https.request = globalTunnel._makeHttp('request', https, 'https');
http.get = globalTunnel._makeHttp('get', http, 'http');
https.get = globalTunnel._makeHttp('get', https, 'https');
globalTunnel.isProxying = true;
globalTunnel.proxyUrl = stringifyProxy(conf);
globalTunnel.proxyConfig = clone(conf);
} catch (e) {
resetGlobals();
throw e;
}
};
var _makeAgent = function(conf, innerProtocol, useCONNECT) {
log('Creating proxying agent');
var outerProtocol = conf.protocol;
innerProtocol += ':';
var opts = {
proxy: pick(conf, 'host', 'port', 'protocol', 'localAddress', 'proxyAuth'),
maxSockets: conf.sockets
};
opts.proxy.innerProtocol = innerProtocol;
if (useCONNECT) {
if (conf.proxyHttpsOptions) {
assign(opts.proxy, conf.proxyHttpsOptions);
}
if (conf.originHttpsOptions) {
assign(opts, conf.originHttpsOptions);
}
if (outerProtocol === 'https:') {
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttps(opts);
}
return tunnel.httpOverHttps(opts);
}
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttp(opts);
}
return tunnel.httpOverHttp(opts);
}
if (conf.originHttpsOptions) {
throw new Error('originHttpsOptions must be combined with a tunnel:true option');
}
if (conf.proxyHttpsOptions) {
// NB: not opts.
assign(opts, conf.proxyHttpsOptions);
}
if (outerProtocol === 'https:') {
return new agents.OuterHttpsAgent(opts);
}
return new agents.OuterHttpAgent(opts);
};
/**
* Construct an agent based on:
* - is the connection to the proxy secure?
* - is the connection to the origin secure?
* - the address of the proxy
*/
globalTunnel._makeAgent = function(conf, innerProtocol, useCONNECT) {
var agent = _makeAgent(conf, innerProtocol, useCONNECT);
// Set the protocol to match that of the target request type
agent.protocol = innerProtocol + ':';
return agent;
};
/**
* Override for http/https, makes sure to default the agent
* to the global agent. Due to how node implements it in lib/http.js, the
* globalAgent we define won't get used (node uses a module-scoped variable,
* not the exports field).
* @param {string} method 'request' or 'get', http/https methods
* @param {string|object} options http/https request url or options
* @param {function} [cb]
* @private
*/
globalTunnel._makeHttp = function(method, httpOrHttps, protocol) {
return function(options, callback) {
if (typeof options === 'string') {
options = urlParse(options);
} else {
options = clone(options);
}
// Respect the default agent provided by node's lib/https.js
if (
(options.agent === null || options.agent === undefined) &&
typeof options.createConnection !== 'function' &&
(options.host || options.hostname)
) {
options.agent = options._defaultAgent || httpOrHttps.globalAgent;
}
// Set the default port ourselves to prevent Node doing it based on the proxy agent protocol
if (options.protocol === 'https:' || (!options.protocol && protocol === 'https')) {
options.port = options.port || 443;
}
if (options.protocol === 'http:' || (!options.protocol && protocol === 'http')) {
options.port = options.port || 80;
}
log(
'Requesting to ' +
(options.protocol || protocol) +
'//' +
(options.host || options.hostname) +
':' +
options.port
);
return ORIGINALS[protocol][method].call(httpOrHttps, options, callback);
};
};
/**
* Restores global http/https agents.
*/
globalTunnel.end = function() {
resetGlobals();
globalTunnel.isProxying = false;
globalTunnel.proxyUrl = null;
globalTunnel.proxyConfig = null;
};