whistle 2.10.12 → 2.10.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/assets/emudhra.pem +48 -0
- package/bin/api.js +44 -10
- package/biz/webui/cgi-bin/client-ips.js +7 -0
- package/biz/webui/htdocs/index.html +1 -1
- package/biz/webui/htdocs/js/index.js +50 -49
- package/lib/config.js +31 -6
- package/lib/https/ca.js +21 -12
- package/lib/https/emudhra.js +39 -0
- package/lib/https/index.js +13 -3
- package/lib/init.js +1 -0
- package/lib/inspectors/res.js +397 -403
- package/lib/plugins/index.js +1 -1
- package/lib/rules/rules.js +27 -9
- package/lib/service/composer.js +33 -21
- package/lib/service/service.js +0 -1
- package/lib/tunnel.js +8 -7
- package/lib/upgrade.js +1 -0
- package/lib/util/data-server.js +5 -3
- package/lib/util/drain.js +14 -4
- package/lib/util/index.js +27 -1
- package/lib/util/parse-url.js +16 -1
- package/package.json +4 -4
package/lib/inspectors/res.js
CHANGED
|
@@ -194,108 +194,103 @@ module.exports = function (req, res, next) {
|
|
|
194
194
|
req.request = function (options) {
|
|
195
195
|
readFirstChunk(req, function () {
|
|
196
196
|
options = options || req.options;
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
: options.href;
|
|
197
|
+
var isPlugin = options.isPlugin;
|
|
198
|
+
req.realUrl = res.realUrl = isPlugin ? req._realUrl || req.fullUrl : options.href;
|
|
200
199
|
var originPort = options.port;
|
|
201
200
|
var originHost = options.host;
|
|
202
201
|
var originDomain = options.hostname;
|
|
203
202
|
var now = Date.now();
|
|
204
203
|
rules.getClientCert(req, function (key, cert, isPfx, cacheKey) {
|
|
205
|
-
rules.getProxy(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (isInternalProxy) {
|
|
223
|
-
req._isInternalProxy = true;
|
|
224
|
-
if (options.protocol === 'https:') {
|
|
225
|
-
headers[config.HTTPS_FIELD] = 1;
|
|
226
|
-
origProto = options.protocol;
|
|
227
|
-
options.protocol = null;
|
|
228
|
-
}
|
|
229
|
-
} else if (proxyRule.isHttp2https) {
|
|
230
|
-
options.protocol = 'https:';
|
|
204
|
+
rules.getProxy(options.href, isPlugin ? null : req, function (err, hostIp, hostPort) {
|
|
205
|
+
var proxyRule = resRules.proxy;
|
|
206
|
+
var proxyUrl = !isPlugin && proxyRule ? util.rule.getMatcher(proxyRule) : null;
|
|
207
|
+
var headers = req.headers;
|
|
208
|
+
var auth, isInternalProxy, isHttpsProxy, origPath, origProxy;
|
|
209
|
+
if (!hostIp) {
|
|
210
|
+
if (options.localDNS) {
|
|
211
|
+
req.curUrl = '';
|
|
212
|
+
} else if (proxyUrl) {
|
|
213
|
+
isHttpsProxy = proxyRule.isHttps;
|
|
214
|
+
isInternalProxy = proxyRule.isInternal || util.isInternalProxy(req);
|
|
215
|
+
if (isInternalProxy) {
|
|
216
|
+
req._isInternalProxy = true;
|
|
217
|
+
if (options.protocol === 'https:') {
|
|
218
|
+
headers[config.HTTPS_FIELD] = 1;
|
|
219
|
+
origProto = options.protocol;
|
|
220
|
+
options.protocol = null;
|
|
231
221
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
req.curUrl = options.href;
|
|
222
|
+
} else if (proxyRule.isHttp2https) {
|
|
223
|
+
options.protocol = 'https:';
|
|
235
224
|
}
|
|
225
|
+
req.curUrl = 'http:' + util.removeProtocol(proxyUrl);
|
|
226
|
+
} else {
|
|
227
|
+
req.curUrl = options.href;
|
|
236
228
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
229
|
+
}
|
|
230
|
+
if (!req.setServerPort) {
|
|
231
|
+
req.setServerPort = function (port) {
|
|
232
|
+
req.serverPort = port;
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
rules.resolveHost(req, function (err, ip, port, hostRule) {
|
|
236
|
+
var setHostsInfo = function (_ip, _port, _host, withPort) {
|
|
237
|
+
ip = _ip || '127.0.0.1';
|
|
238
|
+
port = _port;
|
|
239
|
+
req.dnsTime = Date.now() - now;
|
|
240
|
+
req.hostIp = util.joinIpPort(_ip, withPort && _port);
|
|
241
|
+
if (_host) {
|
|
242
|
+
resRules.host = _host;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
if (proxyUrl && proxyRule && hostRule) {
|
|
246
|
+
proxyRule.host = hostRule;
|
|
247
|
+
hostRule = null;
|
|
241
248
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
249
|
+
setHostsInfo(hostIp || ip, hostPort || port, hostRule);
|
|
250
|
+
if (err) {
|
|
251
|
+
showDnsError(res, err);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (req.disable.keepalive) {
|
|
255
|
+
req.disable.keepAlive = true;
|
|
256
|
+
}
|
|
257
|
+
var isHttps = options.protocol == 'https:';
|
|
258
|
+
var proxyOptions, isProxyPort, isSocks;
|
|
259
|
+
var setAgent = function (isLocal) {
|
|
260
|
+
if (req.disable.keepAlive || (isHttps && cert)) {
|
|
261
|
+
options.agent = false;
|
|
262
|
+
} else if (isLocal) {
|
|
263
|
+
options.agent = config.localAgent;
|
|
264
|
+
} else {
|
|
265
|
+
options.agent = isHttps ? config.httpsAgent : config.httpAgent;
|
|
255
266
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
267
|
+
};
|
|
268
|
+
checkH2(req, isHttps);
|
|
269
|
+
if (proxyUrl) {
|
|
270
|
+
proxyOptions = parseUrl(proxyUrl);
|
|
271
|
+
proxyOptions.host = ip;
|
|
272
|
+
proxyOptions.auth = proxyOptions.auth || req._pacAuth;
|
|
273
|
+
isSocks = proxyRule.isSocks;
|
|
274
|
+
var proxyPort = proxyOptions.port;
|
|
275
|
+
if (!proxyPort) {
|
|
276
|
+
proxyPort = proxyOptions.port = isSocks ? 1080 : isHttpsProxy ? 443 : 80;
|
|
260
277
|
}
|
|
261
|
-
if (
|
|
262
|
-
|
|
278
|
+
if (proxyOptions.auth) {
|
|
279
|
+
auth = 'Basic ' + util.toBuffer(proxyOptions.auth + '').toString('base64');
|
|
280
|
+
} else {
|
|
281
|
+
auth = headers['proxy-authorization'];
|
|
263
282
|
}
|
|
264
|
-
|
|
265
|
-
var proxyOptions, isProxyPort, isSocks;
|
|
266
|
-
var setAgent = function (disable) {
|
|
267
|
-
if (disable || req.disable.keepAlive || (isHttps && cert)) {
|
|
268
|
-
options.agent = false;
|
|
269
|
-
} else {
|
|
270
|
-
options.agent = isHttps ? config.httpsAgent : config.httpAgent;
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
checkH2(req, isHttps);
|
|
274
|
-
if (proxyUrl) {
|
|
275
|
-
proxyOptions = parseUrl(proxyUrl);
|
|
276
|
-
proxyOptions.host = ip;
|
|
277
|
-
proxyOptions.auth = proxyOptions.auth || req._pacAuth;
|
|
278
|
-
isSocks = proxyRule.isSocks;
|
|
279
|
-
var proxyPort = proxyOptions.port;
|
|
280
|
-
if (!proxyPort) {
|
|
281
|
-
proxyPort = proxyOptions.port = isSocks ? 1080 : isHttpsProxy ? 443 : 80;
|
|
282
|
-
}
|
|
283
|
-
if (proxyOptions.auth) {
|
|
284
|
-
auth = 'Basic ' + util.toBuffer(proxyOptions.auth + '').toString('base64');
|
|
285
|
-
} else {
|
|
286
|
-
auth = headers['proxy-authorization'];
|
|
287
|
-
}
|
|
288
|
-
if (
|
|
283
|
+
if (
|
|
289
284
|
isHttps ||
|
|
290
285
|
(req.useH2 && !isInternalProxy) ||
|
|
291
286
|
isSocks ||
|
|
292
287
|
isHttpsProxy ||
|
|
293
288
|
req._phost
|
|
294
289
|
) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
290
|
+
isProxyPort = util.isProxyPort(proxyPort);
|
|
291
|
+
if (isProxyPort && util.isLocalAddress(ip)) {
|
|
292
|
+
req.setServerPort(config.port);
|
|
293
|
+
res.response(
|
|
299
294
|
util.wrapResponse({
|
|
300
295
|
statusCode: 302,
|
|
301
296
|
headers: {
|
|
@@ -306,105 +301,105 @@ module.exports = function (req, res, next) {
|
|
|
306
301
|
}
|
|
307
302
|
})
|
|
308
303
|
);
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
304
|
+
} else {
|
|
305
|
+
var curServerPort = options.port || (isHttps ? 443 : 80);
|
|
306
|
+
var proxyHeaders = {
|
|
307
|
+
host: util.joinIpPort(options.hostname, curServerPort),
|
|
308
|
+
'proxy-connection': req.disable.proxyConnection
|
|
314
309
|
? 'close'
|
|
315
310
|
: 'keep-alive'
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
if (auth) {
|
|
324
|
-
proxyHeaders['proxy-authorization'] = auth;
|
|
325
|
-
}
|
|
326
|
-
if (req.disable.proxyUA) {
|
|
327
|
-
delete proxyHeaders['user-agent'];
|
|
328
|
-
} else if (headers['user-agent']) {
|
|
329
|
-
proxyHeaders['user-agent'] = headers['user-agent'];
|
|
330
|
-
}
|
|
331
|
-
if (!util.isLocalAddress(req.clientIp)) {
|
|
332
|
-
proxyHeaders[clientIpKey] = req.clientIp;
|
|
333
|
-
}
|
|
334
|
-
if (isHttps || req.useH2) {
|
|
335
|
-
util.checkIfAddInterceptPolicy(proxyHeaders, headers);
|
|
336
|
-
}
|
|
337
|
-
if (isProxyPort) {
|
|
338
|
-
proxyHeaders[config.WEBUI_HEAD] = 1;
|
|
339
|
-
}
|
|
340
|
-
if (util.isProxyPort(curServerPort) || util.isLocalPHost(req, isHttps)) {
|
|
341
|
-
headers[config.WEBUI_HEAD] = 1;
|
|
342
|
-
}
|
|
343
|
-
var clientId = headers[config.CLIENT_ID_HEADER];
|
|
344
|
-
if (clientId) {
|
|
345
|
-
proxyHeaders[config.CLIENT_ID_HEADER] = clientId;
|
|
311
|
+
};
|
|
312
|
+
pluginMgr.getTunnelKeys().forEach(function (k) {
|
|
313
|
+
var val = headers[k];
|
|
314
|
+
if (val) {
|
|
315
|
+
proxyHeaders[k] = val;
|
|
346
316
|
}
|
|
347
|
-
|
|
317
|
+
});
|
|
318
|
+
if (auth) {
|
|
319
|
+
proxyHeaders['proxy-authorization'] = auth;
|
|
320
|
+
}
|
|
321
|
+
if (req.disable.proxyUA) {
|
|
322
|
+
delete proxyHeaders['user-agent'];
|
|
323
|
+
} else if (headers['user-agent']) {
|
|
324
|
+
proxyHeaders['user-agent'] = headers['user-agent'];
|
|
325
|
+
}
|
|
326
|
+
if (!util.isLocalAddress(req.clientIp)) {
|
|
327
|
+
proxyHeaders[clientIpKey] = req.clientIp;
|
|
328
|
+
}
|
|
329
|
+
if (isHttps || req.useH2) {
|
|
330
|
+
util.checkIfAddInterceptPolicy(proxyHeaders, headers);
|
|
331
|
+
}
|
|
332
|
+
if (isProxyPort) {
|
|
333
|
+
proxyHeaders[config.WEBUI_HEAD] = 1;
|
|
334
|
+
}
|
|
335
|
+
if (util.isProxyPort(curServerPort) || util.isLocalPHost(req, isHttps)) {
|
|
336
|
+
headers[config.WEBUI_HEAD] = 1;
|
|
337
|
+
}
|
|
338
|
+
var clientId = headers[config.CLIENT_ID_HEADER];
|
|
339
|
+
if (clientId) {
|
|
340
|
+
proxyHeaders[config.CLIENT_ID_HEADER] = clientId;
|
|
341
|
+
}
|
|
342
|
+
util.setClientId(
|
|
348
343
|
proxyHeaders,
|
|
349
344
|
req.enable,
|
|
350
345
|
req.disable,
|
|
351
346
|
req.clientIp,
|
|
352
347
|
isInternalProxy
|
|
353
348
|
);
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
349
|
+
var phost = req._phost;
|
|
350
|
+
var opts = {
|
|
351
|
+
isSocks: isSocks,
|
|
352
|
+
isHttps: isHttps,
|
|
353
|
+
_phost: phost,
|
|
354
|
+
proxyServername: isHttpsProxy
|
|
360
355
|
? proxyOptions.hostname
|
|
361
356
|
: null,
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
}
|
|
376
|
-
proxyHeaders.host = util.joinIpPort(options.host, options.port);
|
|
377
|
-
} else {
|
|
378
|
-
options.host = options.hostname;
|
|
357
|
+
proxyHost: ip,
|
|
358
|
+
clientIp: proxyHeaders[clientIpKey],
|
|
359
|
+
proxyPort: proxyPort,
|
|
360
|
+
url: options.href,
|
|
361
|
+
auth: proxyOptions.auth,
|
|
362
|
+
headers: proxyHeaders
|
|
363
|
+
};
|
|
364
|
+
if (phost) {
|
|
365
|
+
options.host = phost.hostname;
|
|
366
|
+
if (phost.port > 0) {
|
|
367
|
+
options.port = phost.port;
|
|
368
|
+
} else if (!options.port) {
|
|
369
|
+
options.port = isHttps ? 443 : 80;
|
|
379
370
|
}
|
|
380
|
-
|
|
381
|
-
|
|
371
|
+
proxyHeaders.host = util.joinIpPort(options.host, options.port);
|
|
372
|
+
} else {
|
|
373
|
+
options.host = options.hostname;
|
|
374
|
+
}
|
|
375
|
+
options._proxyOptions = opts;
|
|
376
|
+
opts.proxyType = isSocks
|
|
382
377
|
? 'socks'
|
|
383
378
|
: isHttpsProxy
|
|
384
379
|
? 'https'
|
|
385
380
|
: 'http';
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
return;
|
|
381
|
+
options._proxyPort = opts.proxyPort;
|
|
382
|
+
origProxy = opts;
|
|
383
|
+
request(options);
|
|
391
384
|
}
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
392
387
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
388
|
+
if (auth) {
|
|
389
|
+
headers['proxy-authorization'] = auth;
|
|
396
390
|
}
|
|
391
|
+
}
|
|
397
392
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
393
|
+
req.hostIp = util.joinIpPort(ip, port);
|
|
394
|
+
port = proxyOptions ? proxyOptions.port : port || options.port;
|
|
395
|
+
options.host = ip; //设置ip
|
|
396
|
+
var curPort = port || (isHttps ? 443 : 80);
|
|
397
|
+
isProxyPort = util.isProxyPort(curPort);
|
|
398
|
+
var isLocalAddress = util.isLocalAddress(options.host);
|
|
399
|
+
if (isProxyPort && isLocalAddress) {
|
|
400
|
+
var redirectHost = config.customLocalUIHost || ip;
|
|
401
|
+
var redirectPort = config.realPort || config.port;
|
|
402
|
+
res.response(
|
|
408
403
|
util.wrapResponse({
|
|
409
404
|
statusCode: 302,
|
|
410
405
|
headers: {
|
|
@@ -415,137 +410,137 @@ module.exports = function (req, res, next) {
|
|
|
415
410
|
}
|
|
416
411
|
})
|
|
417
412
|
);
|
|
418
|
-
|
|
419
|
-
|
|
413
|
+
} else {
|
|
414
|
+
if (
|
|
420
415
|
isProxyPort ||
|
|
421
416
|
util.isProxyPort(options.port || (isHttps ? 443 : 80)) ||
|
|
422
417
|
util.isLocalPHost(req, isHttps)
|
|
423
418
|
) {
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
setAgent(isLocalAddress);
|
|
427
|
-
request(options, port, true);
|
|
419
|
+
headers[config.WEBUI_HEAD] = 1;
|
|
428
420
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
421
|
+
setAgent(isLocalAddress);
|
|
422
|
+
request(options, port, true);
|
|
423
|
+
}
|
|
424
|
+
function request(options, serverPort, direct) {
|
|
425
|
+
options.headers = headers;
|
|
426
|
+
options.method = req.method;
|
|
427
|
+
util.setRejectUnauthorized(req, options);
|
|
428
|
+
if (
|
|
429
|
+
!isPlugin &&
|
|
435
430
|
!req._customHost &&
|
|
436
431
|
(req.fullUrl !== req.realUrl || !headers.host)
|
|
437
432
|
) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
433
|
+
headers.host = originHost;
|
|
434
|
+
}
|
|
435
|
+
if (req.disable.keepAlive) {
|
|
436
|
+
headers.connection = 'close';
|
|
437
|
+
}
|
|
438
|
+
if (direct) {
|
|
439
|
+
options.port = serverPort;
|
|
440
|
+
if (proxyUrl) {
|
|
441
|
+
origPath = options.path || '';
|
|
448
442
|
}
|
|
443
|
+
}
|
|
449
444
|
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
445
|
+
delete options.hostname; //防止自动dns
|
|
446
|
+
delete options.protocol;
|
|
447
|
+
if (isHttps && !req.disable.servername) {
|
|
448
|
+
options.servername = util.parseHost(headers.host)[0];
|
|
449
|
+
}
|
|
450
|
+
var piped;
|
|
451
|
+
var maxRetryCount = 1;
|
|
452
|
+
var retryCount = 0;
|
|
453
|
+
var retryXHost = 0;
|
|
454
|
+
var resetCount = 0;
|
|
455
|
+
var curClient, timer, newIp;
|
|
456
|
+
var setProxyAgent = function (options, proxyOpts) {
|
|
457
|
+
proxyOpts.cacheKey = options.cacheKey;
|
|
458
|
+
proxyOpts.proxyTunnelPath = util.getProxyTunnelPath(
|
|
464
459
|
req,
|
|
465
460
|
isHttps
|
|
466
461
|
);
|
|
467
|
-
|
|
468
|
-
|
|
462
|
+
proxyOpts.enableIntercept = true;
|
|
463
|
+
options.agent = proxyOpts.isSocks
|
|
469
464
|
? config.getSocksAgent(proxyOpts)
|
|
470
465
|
: config.getHttpsAgent(proxyOpts, options);
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
466
|
+
};
|
|
467
|
+
var retry = function (err) {
|
|
468
|
+
clearTimeout(timer);
|
|
469
|
+
timer = null;
|
|
470
|
+
if (curClient) {
|
|
471
|
+
curClient.removeListener('error', retry);
|
|
472
|
+
curClient.removeListener('close', retry);
|
|
473
|
+
curClient.on('error', util.noop);
|
|
474
|
+
curClient.destroy();
|
|
475
|
+
curClient = null;
|
|
476
|
+
}
|
|
477
|
+
if (req._hasError || req._hasRespond) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (err) {
|
|
486
481
|
// Chrome 浏览器自动兼容 127.0.0.1 和 ::1
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}
|
|
492
|
-
} else if (isHttps && !options.ciphers && util.isCiphersError(err)) {
|
|
493
|
-
extend(options, util.getTlsOptions(resRules));
|
|
494
|
-
return send();
|
|
482
|
+
if (util.notStarted(err)) {
|
|
483
|
+
if (!newIp && (newIp = util.getLocalhostIP(err, req, originDomain, options.host))) {
|
|
484
|
+
options.host = newIp;
|
|
485
|
+
setHostsInfo(newIp);
|
|
495
486
|
}
|
|
487
|
+
} else if (isHttps && !options.ciphers && util.isCiphersError(err)) {
|
|
488
|
+
extend(options, util.getTlsOptions(resRules));
|
|
489
|
+
return send();
|
|
496
490
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
491
|
+
}
|
|
492
|
+
if (retryCount >= maxRetryCount) {
|
|
493
|
+
var toHttp;
|
|
494
|
+
if (
|
|
500
495
|
isHttps &&
|
|
501
496
|
(!piped || req.noReqBody) &&
|
|
502
497
|
util.checkTlsError(err) &&
|
|
503
498
|
util.checkAuto2Http(req, ip, proxyUrl)
|
|
504
499
|
) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
500
|
+
isHttps = false;
|
|
501
|
+
toHttp = true;
|
|
502
|
+
req.httpsTime = req.httpsTime || Date.now();
|
|
503
|
+
req.useHttp = true;
|
|
504
|
+
if (origProxy) {
|
|
505
|
+
origProxy.isHttps = false;
|
|
506
|
+
if (req._phost && !req._phost.port) {
|
|
507
|
+
options.port = 80;
|
|
508
|
+
origProxy.headers.host =
|
|
514
509
|
req._phost.hostname + ':80';
|
|
515
|
-
}
|
|
516
|
-
setProxyAgent(options, origProxy);
|
|
517
|
-
} else {
|
|
518
|
-
setAgent(util.isLocalAddress(ip));
|
|
519
510
|
}
|
|
511
|
+
setProxyAgent(options, origProxy);
|
|
512
|
+
} else {
|
|
513
|
+
setAgent(util.isLocalAddress(ip));
|
|
520
514
|
}
|
|
521
|
-
|
|
522
|
-
|
|
515
|
+
}
|
|
516
|
+
var code = err && err.code;
|
|
517
|
+
if (
|
|
523
518
|
!toHttp &&
|
|
524
519
|
(resetCount > 1 ||
|
|
525
520
|
(code !== 'EPROTO' && code !== 'ECONNRESET') ||
|
|
526
521
|
(piped && !req.noReqBody))
|
|
527
522
|
) {
|
|
528
|
-
|
|
523
|
+
var stack = util.getErrorStack(
|
|
529
524
|
err || new Error('socket connect timeout')
|
|
530
525
|
);
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
return;
|
|
526
|
+
res.response(util.wrapGatewayError(stack));
|
|
527
|
+
} else {
|
|
528
|
+
++resetCount;
|
|
529
|
+
send();
|
|
537
530
|
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
++retryCount;
|
|
534
|
+
if (proxyUrl) {
|
|
535
|
+
if (X_RE.test(proxyUrl)) {
|
|
536
|
+
proxyUrl = '';
|
|
537
|
+
req._phost = undefined;
|
|
538
|
+
if (isInternalProxy) {
|
|
539
|
+
isHttps = origProto === 'https:';
|
|
540
|
+
}
|
|
541
|
+
req.curUrl = req.realUrl;
|
|
542
|
+
delete options._proxyPort;
|
|
543
|
+
rules.resolveHost(
|
|
549
544
|
req,
|
|
550
545
|
function (_err, _ip, _port, _host) {
|
|
551
546
|
setAgent(util.isLocalAddress(_ip));
|
|
@@ -560,101 +555,101 @@ module.exports = function (req, res, next) {
|
|
|
560
555
|
send();
|
|
561
556
|
}
|
|
562
557
|
);
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
} else if (
|
|
566
561
|
retryXHost < 2 &&
|
|
567
562
|
req.rules.host &&
|
|
568
563
|
X_RE.test(req.rules.host.matcher)
|
|
569
564
|
) {
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
565
|
+
++maxRetryCount;
|
|
566
|
+
++retryXHost;
|
|
567
|
+
if (retryXHost > 1) {
|
|
568
|
+
req.curUrl = req.realUrl;
|
|
569
|
+
delete options._proxyPort;
|
|
570
|
+
rules.lookupHost(req, function (_err, _ip) {
|
|
571
|
+
setHostsInfo(_ip);
|
|
572
|
+
if (_err) {
|
|
573
|
+
showDnsError(res, _err);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
options.host = ip;
|
|
577
|
+
options.port = originPort;
|
|
578
|
+
send();
|
|
579
|
+
});
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
} else if (
|
|
588
583
|
isHttps &&
|
|
589
584
|
util.checkAuto2Http(req, ip, proxyUrl)
|
|
590
585
|
) {
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
send();
|
|
600
|
-
};
|
|
601
|
-
var send = function (sock) {
|
|
602
|
-
if (req._hasError) {
|
|
603
|
-
return;
|
|
586
|
+
++maxRetryCount;
|
|
587
|
+
if (maxRetryCount > 2 || util.checkTlsError(err)) {
|
|
588
|
+
isHttps = false;
|
|
589
|
+
req.httpsTime = req.httpsTime || Date.now();
|
|
590
|
+
req.useHttp = true;
|
|
591
|
+
setAgent(util.isLocalAddress(options.host));
|
|
604
592
|
}
|
|
605
|
-
|
|
606
|
-
|
|
593
|
+
}
|
|
594
|
+
send();
|
|
595
|
+
};
|
|
596
|
+
var send = function (sock) {
|
|
597
|
+
if (req._hasError) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
req.useH2 = false;
|
|
601
|
+
req.setServerPort(
|
|
607
602
|
options._proxyPort || options.port || (isHttps ? 443 : 80)
|
|
608
603
|
);
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
604
|
+
if (origPath != null && proxyUrl) {
|
|
605
|
+
origPath = null;
|
|
606
|
+
options.path =
|
|
612
607
|
(isHttps ? 'https:' : 'http:') +
|
|
613
608
|
'//' +
|
|
614
609
|
(headers.host || options.host) +
|
|
615
610
|
(options.path || '/');
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
}
|
|
633
|
-
delete options._proxyOptions;
|
|
611
|
+
}
|
|
612
|
+
var useHttps = isHttps;
|
|
613
|
+
if (sock) {
|
|
614
|
+
options.agent = null;
|
|
615
|
+
options.createConnection = function () {
|
|
616
|
+
return sock;
|
|
617
|
+
};
|
|
618
|
+
} else {
|
|
619
|
+
var proxyOpts = options._proxyOptions;
|
|
620
|
+
if (proxyOpts) {
|
|
621
|
+
if (!req.useHttpH2 || proxyOpts._phost) {
|
|
622
|
+
setProxyAgent(options, proxyOpts);
|
|
623
|
+
} else {
|
|
624
|
+
options.host = proxyOpts.proxyHost;
|
|
625
|
+
options.port = proxyOpts.proxyPort;
|
|
626
|
+
useHttps = useHttps || isHttpsProxy;
|
|
634
627
|
}
|
|
628
|
+
delete options._proxyOptions;
|
|
635
629
|
}
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
630
|
+
}
|
|
631
|
+
options.protocol = useHttps ? 'https:' : 'http:';
|
|
632
|
+
if (useHttps && !req.disable.secureOptions) {
|
|
633
|
+
util.setSecureOptions(options);
|
|
634
|
+
}
|
|
635
|
+
try {
|
|
636
|
+
var client = (useHttps ? https : http).request(
|
|
642
637
|
options,
|
|
643
638
|
res.response
|
|
644
639
|
);
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
640
|
+
curClient = client;
|
|
641
|
+
req._clientReq = client;
|
|
642
|
+
client.once('error', retry);
|
|
643
|
+
client.once('socket', function (socket) {
|
|
644
|
+
if (socket.connecting || socket._connecting) {
|
|
645
|
+
if (TIMEOUT) {
|
|
646
|
+
timer = setTimeout(function () {
|
|
647
|
+
socket.destroy();
|
|
648
|
+
timer = null;
|
|
649
|
+
retry();
|
|
650
|
+
}, TIMEOUT);
|
|
651
|
+
}
|
|
652
|
+
socket.once(
|
|
658
653
|
isHttpsProxy || isHttps
|
|
659
654
|
? 'secureConnect'
|
|
660
655
|
: 'connect',
|
|
@@ -667,112 +662,111 @@ module.exports = function (req, res, next) {
|
|
|
667
662
|
pipeClient(req, client);
|
|
668
663
|
}
|
|
669
664
|
);
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
665
|
+
} else {
|
|
666
|
+
retryCount = maxRetryCount;
|
|
667
|
+
piped = true;
|
|
668
|
+
pipeClient(req, client);
|
|
669
|
+
socket.resume();
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
} catch (e) {
|
|
673
|
+
res.response(
|
|
679
674
|
util.wrapGatewayError(util.getErrorStack(e))
|
|
680
675
|
);
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
if (req.disable.clientIp || req.disable.clientIP) {
|
|
679
|
+
delete headers[clientIpKey];
|
|
680
|
+
} else {
|
|
681
|
+
var forwardedFor = util.getMatcherValue(
|
|
687
682
|
resRules.forwardedFor
|
|
688
683
|
);
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
(!
|
|
684
|
+
if (net.isIP(forwardedFor)) {
|
|
685
|
+
headers[clientIpKey] = forwardedFor;
|
|
686
|
+
} else if (req._customXFF) {
|
|
687
|
+
headers[clientIpKey] = req._customXFF;
|
|
688
|
+
} else if (
|
|
689
|
+
(!isPlugin &&
|
|
695
690
|
!req.enable.clientIp &&
|
|
696
691
|
!req.enable.clientIP &&
|
|
697
692
|
!req.enableXFF &&
|
|
698
693
|
(isHttps || isSocks || !proxyUrl)) ||
|
|
699
694
|
util.isLocalAddress(req.clientIp)
|
|
700
695
|
) {
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
}
|
|
696
|
+
delete headers[clientIpKey];
|
|
697
|
+
} else {
|
|
698
|
+
headers[clientIpKey] = req.clientIp;
|
|
705
699
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
700
|
+
}
|
|
701
|
+
util.deleteReqHeaders(req);
|
|
702
|
+
var optHeaders = options.headers;
|
|
703
|
+
var transfer =
|
|
709
704
|
options.method === 'DELETE' &&
|
|
710
705
|
optHeaders['transfer-encoding'];
|
|
711
|
-
|
|
712
|
-
|
|
706
|
+
if (transfer) {
|
|
707
|
+
delete optHeaders['transfer-encoding'];
|
|
708
|
+
}
|
|
709
|
+
var clientId = optHeaders[config.CLIENT_ID_HEADER];
|
|
710
|
+
if (clientId) {
|
|
711
|
+
if (!isPlugin && !req._customClientId && !util.isKeepClientId(req, proxyUrl)) {
|
|
712
|
+
req._origClientId = clientId;
|
|
713
|
+
util.removeClientId(optHeaders);
|
|
713
714
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
req._origClientId = clientId;
|
|
718
|
-
util.removeClientId(optHeaders);
|
|
719
|
-
}
|
|
720
|
-
req.setClientId && req.setClientId(clientId);
|
|
721
|
-
} else {
|
|
722
|
-
util.setClientId(
|
|
715
|
+
req.setClientId && req.setClientId(clientId);
|
|
716
|
+
} else {
|
|
717
|
+
util.setClientId(
|
|
723
718
|
optHeaders,
|
|
724
719
|
req.enable,
|
|
725
720
|
req.disable,
|
|
726
721
|
req.clientIp,
|
|
727
722
|
isInternalProxy
|
|
728
723
|
);
|
|
729
|
-
|
|
730
|
-
|
|
724
|
+
}
|
|
725
|
+
if (
|
|
731
726
|
req.useH2 &&
|
|
732
727
|
(isInternalProxy ||
|
|
733
728
|
headers[config.HTTPS_FIELD] ||
|
|
734
|
-
|
|
729
|
+
isPlugin)
|
|
735
730
|
) {
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
731
|
+
headers[config.ALPN_PROTOCOL_HEADER] = 'h2';
|
|
732
|
+
}
|
|
733
|
+
options.headers = optHeaders = formatHeaders(
|
|
739
734
|
optHeaders,
|
|
740
735
|
req.rawHeaderNames
|
|
741
736
|
);
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
if (
|
|
737
|
+
delete headers[config.WEBUI_HEAD];
|
|
738
|
+
delete headers[config.HTTPS_FIELD];
|
|
739
|
+
delete headers[config.ALPN_PROTOCOL_HEADER];
|
|
740
|
+
if (transfer) {
|
|
741
|
+
optHeaders['Transfer-Encoding'] = transfer;
|
|
742
|
+
}
|
|
743
|
+
if (isPlugin) {
|
|
744
|
+
optHeaders[config.PLUGIN_HOOK_NAME_HEADER] = config.PLUGIN_HOOKS.HTTP;
|
|
745
|
+
}
|
|
746
|
+
req.noReqBody = !util.hasRequestBody(req);
|
|
747
|
+
if (
|
|
754
748
|
req.method === 'DELETE' &&
|
|
755
749
|
(req._hasInjectBody ||
|
|
756
750
|
headers['transfer-encoding'] ||
|
|
757
751
|
headers['content-length'] > 0)
|
|
758
752
|
) {
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
753
|
+
req.useH2 = false;
|
|
754
|
+
}
|
|
755
|
+
req.setServerPort(
|
|
762
756
|
options._proxyPort || options.port || (isHttps ? 443 : 80)
|
|
763
757
|
);
|
|
764
|
-
|
|
765
|
-
|
|
758
|
+
req.options = options;
|
|
759
|
+
isHttps &&
|
|
766
760
|
util.setClientCert(options, key, cert, isPfx, cacheKey);
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
761
|
+
util.addMatchedRules(req);
|
|
762
|
+
h2.request(req, res, send);
|
|
763
|
+
}
|
|
764
|
+
},
|
|
771
765
|
req.pluginRules,
|
|
772
766
|
req.rulesFileMgr,
|
|
773
767
|
req.headerRulesMgr
|
|
774
768
|
);
|
|
775
|
-
|
|
769
|
+
}
|
|
776
770
|
);
|
|
777
771
|
});
|
|
778
772
|
});
|