fix(proxy): update http proxy to accept bypass list with undici v7 (#1456)

With the update of undici to v7, the bypass list of addresses (no_proxy addresses) was not ignored
anymore.

fix #1454
This commit is contained in:
Gauthier
2025-03-12 08:25:54 +01:00
committed by GitHub
parent 077e355c77
commit 9891a7577c

View File

@@ -8,8 +8,9 @@ export default async function createCustomProxyAgent(
) { ) {
const defaultAgent = new Agent({ keepAliveTimeout: 5000 }); const defaultAgent = new Agent({ keepAliveTimeout: 5000 });
const skipUrl = (url: string) => { const skipUrl = (url: string | URL) => {
const hostname = new URL(url).hostname; const hostname =
typeof url === 'string' ? new URL(url).hostname : url.hostname;
if (proxySettings.bypassLocalAddresses && isLocalAddress(hostname)) { if (proxySettings.bypassLocalAddresses && isLocalAddress(hostname)) {
return true; return true;
@@ -38,8 +39,7 @@ export default async function createCustomProxyAgent(
dispatch: Dispatcher['dispatch'] dispatch: Dispatcher['dispatch']
): Dispatcher['dispatch'] => { ): Dispatcher['dispatch'] => {
return (opts, handler) => { return (opts, handler) => {
const url = opts.origin?.toString(); return opts.origin && skipUrl(opts.origin)
return url && skipUrl(url)
? defaultAgent.dispatch(opts, handler) ? defaultAgent.dispatch(opts, handler)
: dispatch(opts, handler); : dispatch(opts, handler);
}; };
@@ -60,13 +60,10 @@ export default async function createCustomProxyAgent(
':' + ':' +
proxySettings.port, proxySettings.port,
token, token,
interceptors: {
Client: [noProxyInterceptor],
},
keepAliveTimeout: 5000, keepAliveTimeout: 5000,
}); });
setGlobalDispatcher(proxyAgent); setGlobalDispatcher(proxyAgent.compose(noProxyInterceptor));
} catch (e) { } catch (e) {
logger.error('Failed to connect to the proxy: ' + e.message, { logger.error('Failed to connect to the proxy: ' + e.message, {
label: 'Proxy', label: 'Proxy',
@@ -95,7 +92,11 @@ export default async function createCustomProxyAgent(
} }
function isLocalAddress(hostname: string) { function isLocalAddress(hostname: string) {
if (hostname === 'localhost' || hostname === '127.0.0.1') { if (
hostname === 'localhost' ||
hostname === '127.0.0.1' ||
hostname === '::1'
) {
return true; return true;
} }