@@ -7,7 +7,6 @@ import http = require('http');
77import https = require( 'https' ) ;
88import * as fs from 'fs/promises' ;
99import * as h2Client from 'http2-wrapper' ;
10- import CacheableLookup from 'cacheable-lookup' ;
1110import { decode as decodeBase64 } from 'base64-arraybuffer' ;
1211import { Transform } from 'stream' ;
1312import { stripIndent , oneLine } from 'common-tags' ;
@@ -60,7 +59,6 @@ import {
6059 withDeserializedCallbackBuffers ,
6160 WithSerializedCallbackBuffers
6261} from '../../serialization/body-serialization' ;
63- import { CachedDns , DnsLookupFunction } from '../../util/dns' ;
6462import { ErrorLike , isErrorLike } from '../../util/error' ;
6563
6664import { assertParamDereferenced , RuleParameters } from '../rule-parameters' ;
@@ -74,7 +72,9 @@ import {
7472 OVERRIDABLE_REQUEST_PSEUDOHEADERS ,
7573 buildOverriddenBody ,
7674 UPSTREAM_TLS_OPTIONS ,
77- shouldUseStrictHttps
75+ shouldUseStrictHttps ,
76+ getClientRelativeHostname ,
77+ getDnsLookupFunction
7878} from '../passthrough-handling' ;
7979
8080import {
@@ -380,33 +380,6 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
380380 return this . _trustedCACertificates ;
381381 }
382382
383- private _cacheableLookupInstance : CacheableLookup | CachedDns | undefined ;
384- private lookup ( ) : DnsLookupFunction {
385- if ( ! this . lookupOptions ) {
386- if ( ! this . _cacheableLookupInstance ) {
387- // By default, use 10s caching of hostnames, just to reduce the delay from
388- // endlessly 10ms query delay for 'localhost' with every request.
389- this . _cacheableLookupInstance = new CachedDns ( 10000 ) ;
390- }
391- return this . _cacheableLookupInstance . lookup ;
392- } else {
393- if ( ! this . _cacheableLookupInstance ) {
394- this . _cacheableLookupInstance = new CacheableLookup ( {
395- maxTtl : this . lookupOptions . maxTtl ,
396- errorTtl : this . lookupOptions . errorTtl ,
397- // As little caching of "use the fallback server" as possible:
398- fallbackDuration : 0
399- } ) ;
400-
401- if ( this . lookupOptions . servers ) {
402- this . _cacheableLookupInstance . servers = this . lookupOptions . servers ;
403- }
404- }
405-
406- return this . _cacheableLookupInstance . lookup ;
407- }
408- }
409-
410383 async handle ( clientReq : OngoingRequest , clientRes : OngoingResponse ) {
411384 // Don't let Node add any default standard headers - we want full control
412385 dropDefaultHeaders ( clientRes ) ;
@@ -434,14 +407,11 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
434407
435408 const isH2Downstream = isHttp2 ( clientReq ) ;
436409
437- if ( isLocalhostAddress ( hostname ) && clientReq . remoteIpAddress && ! isLocalhostAddress ( clientReq . remoteIpAddress ) ) {
438- // If we're proxying localhost traffic from another remote machine, then we should really be proxying
439- // back to that machine, not back to ourselves! Best example is docker containers: if we capture & inspect
440- // their localhost traffic, it should still be sent back into that docker container.
441- hostname = clientReq . remoteIpAddress ;
442-
443- // We don't update the host header - from the POV of the target, it's still localhost traffic.
444- }
410+ hostname = await getClientRelativeHostname (
411+ hostname ,
412+ clientReq . remoteIpAddress ,
413+ getDnsLookupFunction ( this . lookupOptions )
414+ ) ;
445415
446416 if ( this . forwarding ) {
447417 const { targetHost, updateHostHeader } = this . forwarding ;
@@ -747,7 +717,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
747717 headers : shouldTryH2Upstream
748718 ? rawHeadersToObjectPreservingCase ( rawHeaders )
749719 : flattenPairedRawHeaders ( rawHeaders ) as any ,
750- lookup : this . lookup ( ) as typeof dns . lookup ,
720+ lookup : getDnsLookupFunction ( this . lookupOptions ) as typeof dns . lookup ,
751721 // ^ Cast required to handle __promisify__ type hack in the official Node types
752722 agent,
753723 // TLS options:
0 commit comments