Skip to content

Commit

Permalink
Fix createProxyAgent on Electron 29
Browse files Browse the repository at this point in the history
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and indutny-signal authored Mar 18, 2024
1 parent e4a84b3 commit f70860c
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions ts/util/createProxyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down

0 comments on commit f70860c

Please sign in to comment.