From b04d7020e5fca5af26b1d010168802b2240cf1f9 Mon Sep 17 00:00:00 2001 From: nsrCodes Date: Fri, 13 Feb 2026 12:11:29 +0530 Subject: [PATCH] fix: resolve localhost requests with IP literal URLs Follow-up to #276. The custom DNS lookup added in that PR is never invoked by Node.js when the URL contains a raw IP literal (127.0.0.1, [::1], 0.0.0.0) because Node skips DNS resolution for IPs entirely. Additionally, `new URL("http://[::1]:3000").hostname` returns `[::1]` (with brackets), so the original IPv6 hostname check never matched. Fix both issues by: 1. Comparing against the bracketed `[::1]` form and adding `0.0.0.0` 2. Rewriting IP literal URLs to the concrete working address when the hostname isn't already correct Co-Authored-By: Claude Opus 4.6 --- src/main/actions/getProxiedAxios.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/actions/getProxiedAxios.ts b/src/main/actions/getProxiedAxios.ts index 9466c5b..8a7b14f 100644 --- a/src/main/actions/getProxiedAxios.ts +++ b/src/main/actions/getProxiedAxios.ts @@ -12,6 +12,7 @@ import { const LOCAL_IPV4 = "127.0.0.1"; const LOCAL_IPV6 = "::1"; +const LOCAL_UNSPECIFIED = "0.0.0.0"; const checkConnection = (host: string, port: number): Promise => { return new Promise((resolve) => { @@ -104,14 +105,28 @@ function createAxiosInstance( const url = new URL(requestUrl); const { hostname, port: urlPort, protocol } = url; - if (hostname === "localhost" || hostname === LOCAL_IPV6 || hostname === LOCAL_IPV4) { - // convert string port to integer + const isLocalhost = hostname === "localhost" + || hostname === LOCAL_IPV4 + || hostname === `[${LOCAL_IPV6}]` + || hostname === LOCAL_UNSPECIFIED; + + if (isLocalhost) { const port = urlPort ? parseInt(urlPort, 10) : protocol === "https:" ? 443 : 80; const lookup = await createLocalhostLookup(port); - requestConfig.httpAgent = new http.Agent({ lookup }); requestConfig.httpsAgent = new https.Agent({ lookup }); + + // Node.js skips DNS lookup for raw IP literals, so the custom lookup + // above has no effect. Rewrite the URL to the concrete working IP. + if (hostname !== "localhost") { + const ipv6Works = await checkConnection(LOCAL_IPV6, port).catch(() => false); + const targetIp = ipv6Works ? `[${LOCAL_IPV6}]` : LOCAL_IPV4; + + if (hostname !== targetIp) { + requestConfig.url = requestUrl.replace(hostname, targetIp); + } + } } return requestConfig;