Fix createProxyAgent on Electron 29
This commit is contained in:
parent
1e275a917c
commit
73c0e0eb18
1 changed files with 25 additions and 18 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue