diff --git a/ts/util/createProxyAgent.ts b/ts/util/createProxyAgent.ts index 9e6347190e2..7ce3f8bf4a7 100644 --- a/ts/util/createProxyAgent.ts +++ b/ts/util/createProxyAgent.ts @@ -4,7 +4,7 @@ import { ProxyAgent } from 'proxy-agent'; import net from 'net'; import { URL } from 'url'; -import type { LookupOneOptions, LookupAddress } from 'dns'; +import type { LookupOptions, LookupAddress } from 'dns'; import { lookup } from 'dns/promises'; import * as log from '../logging/log'; @@ -37,14 +37,7 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent { } const port = portStr ? parseInt(portStr, 10) : defaultPort; - async function happyLookup( - host: string, - opts: LookupOneOptions - ): Promise<LookupAddress> { - if (opts.all) { - throw new Error('createProxyAgent: all=true lookup is not supported'); - } - + async function happyLookup(host: string): Promise<LookupAddress> { const addresses = await lookup(host, { all: true }); // SOCKS 4/5 resolve target host before sending it to the proxy. @@ -79,18 +72,25 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent { return address; } + type CoercedCallbackType = ( + err: NodeJS.ErrnoException | null, + address: string | Array<LookupAddress>, + family?: number + ) => void; + async function happyLookupWithCallback( host: string, - opts: LookupOneOptions, - callback: ( - err: NodeJS.ErrnoException | null, - address: string, - family: number - ) => void + opts: LookupOptions, + callback: CoercedCallbackType ): Promise<void> { try { - const { address, family } = await happyLookup(host, opts); - callback(null, address, family); + const addr = await happyLookup(host); + if (opts.all) { + callback(null, [addr]); + } else { + const { address, family } = addr; + callback(null, address, family); + } } catch (error) { callback(error, '', -1); } @@ -99,7 +99,14 @@ export function createProxyAgent(proxyUrl: string): ProxyAgent { return new ProxyAgent({ lookup: port !== undefined - ? (...args) => drop(happyLookupWithCallback(...args)) + ? (host, opts, callback) => + drop( + happyLookupWithCallback( + host, + opts, + callback as CoercedCallbackType + ) + ) : undefined, getProxyForUrl() { return proxyUrl;