-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
269 lines (234 loc) · 7.4 KB
/
index.js
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
/*jshint node:true */
'use strict';
/**
* @fileOverview
* Global proxy settings.
*/
var globalTunnel = exports;
exports.constructor = function globalTunnel(){};
var http = require('http');
var https = require('https');
var urlParse = require('url').parse;
var pick = require('lodash/pick');
var assign = require('lodash/assign');
var clone = require('lodash/clone');
var tunnel = require('tunnel');
var agents = require('./lib/agents');
exports.agents = agents;
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'),
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;
}
}
}
/**
* 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)
};
}
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.
*
* If the config is empty, the `http_proxy` environment variable is checked. If
* that's not present, no proxying will be enabled.
*
* @param {object} conf
* @param {string} conf.host
* @param {int} conf.port
* @param {int} [conf.sockets] maximum number of sockets to pool (falsy uses
* node's default).
*/
globalTunnel.initialize = function(conf) {
if (globalTunnel.isProxying) {
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 {
globalTunnel.isProxying = false;
return;
}
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 = 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.outerHttpsOpts = conf.innerHttpsOpts = conf.httpsOptions;
}
http.globalAgent = globalTunnel._makeAgent(conf, 'http', connectHttp);
https.globalAgent = globalTunnel._makeAgent(conf, 'https', connectHttps);
http.request = globalTunnel._makeRequest(http, 'http');
https.request = globalTunnel._makeRequest(https, 'https');
globalTunnel.isProxying = true;
} catch (e) {
resetGlobals();
throw e;
}
};
var _makeAgent = function(conf, innerProtocol, useCONNECT) {
var outerProtocol = conf.protocol;
innerProtocol = 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);
} else {
return tunnel.httpOverHttps(opts);
}
} else {
if (innerProtocol === 'https:') {
return tunnel.httpsOverHttp(opts);
} else {
return tunnel.httpOverHttp(opts);
}
}
} else {
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);
} else {
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.request and https.request, 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|object} options http/https request url or options
* @param {function} [cb]
* @private
*/
globalTunnel._makeRequest = function(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
var defaultAgent = options._defaultAgent || httpOrHttps.globalAgent;
// repeat the logic from node's lib/http.js
var agent = options.agent;
if (agent === false) {
// Node does build the new agent with default props in this case,
// but we want to reuse the same global agent
agent = defaultAgent;
} else if ((agent === null || agent === undefined) &&
typeof options.createConnection !== 'function') {
agent = defaultAgent;
}
options.agent = agent;
// 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;
}
return ORIGINALS[protocol].request.call(httpOrHttps, options, callback);
};
};
/**
* Restores global http/https agents.
*/
globalTunnel.end = function() {
resetGlobals();
globalTunnel.isProxying = false;
};