Fix createProxyAgent on Electron 29

This commit is contained in:
Fedor Indutny 2024-03-15 09:42:13 -07:00 committed by GitHub
parent 1e275a917c
commit 73c0e0eb18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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