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.
@@ -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
- req.realUrl = res.realUrl = options.isPlugin
198
- ? req._realUrl || req.fullUrl
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
- options.href,
207
- options.isPlugin ? null : req,
208
- function (err, hostIp, hostPort) {
209
- var proxyRule = resRules.proxy;
210
- var proxyUrl =
211
- !options.isPlugin && proxyRule
212
- ? util.rule.getMatcher(proxyRule)
213
- : null;
214
- var headers = req.headers;
215
- var auth, isInternalProxy, isHttpsProxy, origPath, origProxy;
216
- if (!hostIp) {
217
- if (options.localDNS) {
218
- req.curUrl = '';
219
- } else if (proxyUrl) {
220
- isHttpsProxy = proxyRule.isHttps;
221
- isInternalProxy = proxyRule.isInternal || util.isInternalProxy(req);
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
- req.curUrl = 'http:' + util.removeProtocol(proxyUrl);
233
- } else {
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
- if (!req.setServerPort) {
238
- req.setServerPort = function (port) {
239
- req.serverPort = port;
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
- rules.resolveHost(req, function (err, ip, port, hostRule) {
243
- var setHostsInfo = function (_ip, _port, _host, withPort) {
244
- ip = _ip || '127.0.0.1';
245
- port = _port;
246
- req.dnsTime = Date.now() - now;
247
- req.hostIp = util.joinIpPort(_ip, withPort && _port);
248
- if (_host) {
249
- resRules.host = _host;
250
- }
251
- };
252
- if (proxyUrl && proxyRule && hostRule) {
253
- proxyRule.host = hostRule;
254
- hostRule = null;
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
- setHostsInfo(hostIp || ip, hostPort || port, hostRule);
257
- if (err) {
258
- showDnsError(res, err);
259
- return;
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 (req.disable.keepalive) {
262
- req.disable.keepAlive = true;
278
+ if (proxyOptions.auth) {
279
+ auth = 'Basic ' + util.toBuffer(proxyOptions.auth + '').toString('base64');
280
+ } else {
281
+ auth = headers['proxy-authorization'];
263
282
  }
264
- var isHttps = options.protocol == 'https:';
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
- isProxyPort = util.isProxyPort(proxyPort);
296
- if (isProxyPort && util.isLocalAddress(ip)) {
297
- req.setServerPort(config.port);
298
- res.response(
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
- } else {
310
- var curServerPort = options.port || (isHttps ? 443 : 80);
311
- var proxyHeaders = {
312
- host: util.joinIpPort(options.hostname, curServerPort),
313
- 'proxy-connection': req.disable.proxyConnection
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
- pluginMgr.getTunnelKeys().forEach(function (k) {
318
- var val = headers[k];
319
- if (val) {
320
- proxyHeaders[k] = val;
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
- util.setClientId(
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
- var phost = req._phost;
355
- var opts = {
356
- isSocks: isSocks,
357
- isHttps: isHttps,
358
- _phost: phost,
359
- proxyServername: isHttpsProxy
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
- proxyHost: ip,
363
- clientIp: proxyHeaders[clientIpKey],
364
- proxyPort: proxyPort,
365
- url: options.href,
366
- auth: proxyOptions.auth,
367
- headers: proxyHeaders
368
- };
369
- if (phost) {
370
- options.host = phost.hostname;
371
- if (phost.port > 0) {
372
- options.port = phost.port;
373
- } else if (!options.port) {
374
- options.port = isHttps ? 443 : 80;
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
- options._proxyOptions = opts;
381
- opts.proxyType = isSocks
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
- options._proxyPort = opts.proxyPort;
387
- origProxy = opts;
388
- request(options);
389
- }
390
- return;
381
+ options._proxyPort = opts.proxyPort;
382
+ origProxy = opts;
383
+ request(options);
391
384
  }
385
+ return;
386
+ }
392
387
 
393
- if (auth) {
394
- headers['proxy-authorization'] = auth;
395
- }
388
+ if (auth) {
389
+ headers['proxy-authorization'] = auth;
396
390
  }
391
+ }
397
392
 
398
- req.hostIp = util.joinIpPort(ip, port);
399
- port = proxyOptions ? proxyOptions.port : port || options.port;
400
- options.host = ip; //设置ip
401
- var curPort = port || (isHttps ? 443 : 80);
402
- isProxyPort = util.isProxyPort(curPort);
403
- var isLocalAddress = util.isLocalAddress(options.host);
404
- if (isProxyPort && isLocalAddress) {
405
- var redirectHost = config.customLocalUIHost || ip;
406
- var redirectPort = config.realPort || config.port;
407
- res.response(
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
- } else {
419
- if (
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
- headers[config.WEBUI_HEAD] = 1;
425
- }
426
- setAgent(isLocalAddress);
427
- request(options, port, true);
419
+ headers[config.WEBUI_HEAD] = 1;
428
420
  }
429
- function request(options, serverPort, direct) {
430
- options.headers = headers;
431
- options.method = req.method;
432
- util.setRejectUnauthorized(req, options);
433
- if (
434
- !options.isPlugin &&
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
- headers.host = originHost;
439
- }
440
- if (req.disable.keepAlive) {
441
- headers.connection = 'close';
442
- }
443
- if (direct) {
444
- options.port = serverPort;
445
- if (proxyUrl) {
446
- origPath = options.path || '';
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
- delete options.hostname; //防止自动dns
451
- delete options.protocol;
452
- if (isHttps && !req.disable.servername) {
453
- options.servername = util.parseHost(headers.host)[0];
454
- }
455
- var piped;
456
- var maxRetryCount = 1;
457
- var retryCount = 0;
458
- var retryXHost = 0;
459
- var resetCount = 0;
460
- var curClient, timer, newIp;
461
- var setProxyAgent = function (options, proxyOpts) {
462
- proxyOpts.cacheKey = options.cacheKey;
463
- proxyOpts.proxyTunnelPath = util.getProxyTunnelPath(
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
- proxyOpts.enableIntercept = true;
468
- options.agent = proxyOpts.isSocks
462
+ proxyOpts.enableIntercept = true;
463
+ options.agent = proxyOpts.isSocks
469
464
  ? config.getSocksAgent(proxyOpts)
470
465
  : config.getHttpsAgent(proxyOpts, options);
471
- };
472
- var retry = function (err) {
473
- clearTimeout(timer);
474
- timer = null;
475
- if (curClient) {
476
- curClient.removeListener('error', retry);
477
- curClient.removeListener('close', retry);
478
- curClient.on('error', util.noop);
479
- curClient.destroy();
480
- curClient = null;
481
- }
482
- if (req._hasError || req._hasRespond) {
483
- return;
484
- }
485
- if (err) {
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
- if (util.notStarted(err)) {
488
- if (!newIp && (newIp = util.getLocalhostIP(err, req, originDomain, options.host))) {
489
- options.host = newIp;
490
- setHostsInfo(newIp);
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
- if (retryCount >= maxRetryCount) {
498
- var toHttp;
499
- if (
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
- isHttps = false;
506
- toHttp = true;
507
- req.httpsTime = req.httpsTime || Date.now();
508
- req.useHttp = true;
509
- if (origProxy) {
510
- origProxy.isHttps = false;
511
- if (req._phost && !req._phost.port) {
512
- options.port = 80;
513
- origProxy.headers.host =
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
- var code = err && err.code;
522
- if (
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
- var stack = util.getErrorStack(
523
+ var stack = util.getErrorStack(
529
524
  err || new Error('socket connect timeout')
530
525
  );
531
- res.response(util.wrapGatewayError(stack));
532
- } else {
533
- ++resetCount;
534
- send();
535
- }
536
- return;
526
+ res.response(util.wrapGatewayError(stack));
527
+ } else {
528
+ ++resetCount;
529
+ send();
537
530
  }
538
- ++retryCount;
539
- if (proxyUrl) {
540
- if (X_RE.test(proxyUrl)) {
541
- proxyUrl = '';
542
- req._phost = undefined;
543
- if (isInternalProxy) {
544
- isHttps = origProto === 'https:';
545
- }
546
- req.curUrl = req.realUrl;
547
- delete options._proxyPort;
548
- rules.resolveHost(
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
- return;
564
- }
565
- } else if (
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
- ++maxRetryCount;
571
- ++retryXHost;
572
- if (retryXHost > 1) {
573
- req.curUrl = req.realUrl;
574
- delete options._proxyPort;
575
- rules.lookupHost(req, function (_err, _ip) {
576
- setHostsInfo(_ip);
577
- if (_err) {
578
- showDnsError(res, _err);
579
- return;
580
- }
581
- options.host = ip;
582
- options.port = originPort;
583
- send();
584
- });
585
- return;
586
- }
587
- } else if (
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
- ++maxRetryCount;
592
- if (maxRetryCount > 2 || util.checkTlsError(err)) {
593
- isHttps = false;
594
- req.httpsTime = req.httpsTime || Date.now();
595
- req.useHttp = true;
596
- setAgent(util.isLocalAddress(options.host));
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
- req.useH2 = false;
606
- req.setServerPort(
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
- if (origPath != null && proxyUrl) {
610
- origPath = null;
611
- options.path =
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
- var useHttps = isHttps;
618
- if (sock) {
619
- options.agent = null;
620
- options.createConnection = function () {
621
- return sock;
622
- };
623
- } else {
624
- var proxyOpts = options._proxyOptions;
625
- if (proxyOpts) {
626
- if (!req.useHttpH2 || proxyOpts._phost) {
627
- setProxyAgent(options, proxyOpts);
628
- } else {
629
- options.host = proxyOpts.proxyHost;
630
- options.port = proxyOpts.proxyPort;
631
- useHttps = useHttps || isHttpsProxy;
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
- options.protocol = useHttps ? 'https:' : 'http:';
637
- if (useHttps && !req.disable.secureOptions) {
638
- util.setSecureOptions(options);
639
- }
640
- try {
641
- var client = (useHttps ? https : http).request(
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
- curClient = client;
646
- req._clientReq = client;
647
- client.once('error', retry);
648
- client.once('socket', function (socket) {
649
- if (socket.connecting || socket._connecting) {
650
- if (TIMEOUT) {
651
- timer = setTimeout(function () {
652
- socket.destroy();
653
- timer = null;
654
- retry();
655
- }, TIMEOUT);
656
- }
657
- socket.once(
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
- } else {
671
- retryCount = maxRetryCount;
672
- piped = true;
673
- pipeClient(req, client);
674
- socket.resume();
675
- }
676
- });
677
- } catch (e) {
678
- res.response(
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
- if (req.disable.clientIp || req.disable.clientIP) {
684
- delete headers[clientIpKey];
685
- } else {
686
- var forwardedFor = util.getMatcherValue(
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
- if (net.isIP(forwardedFor)) {
690
- headers[clientIpKey] = forwardedFor;
691
- } else if (req._customXFF) {
692
- headers[clientIpKey] = req._customXFF;
693
- } else if (
694
- (!options.isPlugin &&
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
- delete headers[clientIpKey];
702
- } else {
703
- headers[clientIpKey] = req.clientIp;
704
- }
696
+ delete headers[clientIpKey];
697
+ } else {
698
+ headers[clientIpKey] = req.clientIp;
705
699
  }
706
- util.deleteReqHeaders(req);
707
- var optHeaders = options.headers;
708
- var transfer =
700
+ }
701
+ util.deleteReqHeaders(req);
702
+ var optHeaders = options.headers;
703
+ var transfer =
709
704
  options.method === 'DELETE' &&
710
705
  optHeaders['transfer-encoding'];
711
- if (transfer) {
712
- delete optHeaders['transfer-encoding'];
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
- var clientId = optHeaders[config.CLIENT_ID_HEADER];
715
- if (clientId) {
716
- if (!options.isPlugin && !req._customClientId && !util.isKeepClientId(req, proxyUrl)) {
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
- if (
724
+ }
725
+ if (
731
726
  req.useH2 &&
732
727
  (isInternalProxy ||
733
728
  headers[config.HTTPS_FIELD] ||
734
- options.isPlugin)
729
+ isPlugin)
735
730
  ) {
736
- headers[config.ALPN_PROTOCOL_HEADER] = 'h2';
737
- }
738
- options.headers = optHeaders = formatHeaders(
731
+ headers[config.ALPN_PROTOCOL_HEADER] = 'h2';
732
+ }
733
+ options.headers = optHeaders = formatHeaders(
739
734
  optHeaders,
740
735
  req.rawHeaderNames
741
736
  );
742
- delete headers[config.WEBUI_HEAD];
743
- delete headers[config.HTTPS_FIELD];
744
- delete headers[config.ALPN_PROTOCOL_HEADER];
745
- if (transfer) {
746
- optHeaders['Transfer-Encoding'] = transfer;
747
- }
748
- if (options.isPlugin) {
749
- optHeaders[config.PLUGIN_HOOK_NAME_HEADER] =
750
- config.PLUGIN_HOOKS.HTTP;
751
- }
752
- req.noReqBody = !util.hasRequestBody(req);
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
- req.useH2 = false;
760
- }
761
- req.setServerPort(
753
+ req.useH2 = false;
754
+ }
755
+ req.setServerPort(
762
756
  options._proxyPort || options.port || (isHttps ? 443 : 80)
763
757
  );
764
- req.options = options;
765
- isHttps &&
758
+ req.options = options;
759
+ isHttps &&
766
760
  util.setClientCert(options, key, cert, isPfx, cacheKey);
767
- util.addMatchedRules(req);
768
- h2.request(req, res, send);
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
  });