From eaa173815ac767c045dc524f4f201a2cf7f174c6 Mon Sep 17 00:00:00 2001 From: Aarushsr12 Date: Thu, 29 Jan 2026 16:01:20 +0530 Subject: [PATCH 1/2] fix: remove proxy for api client & resolve localhost requests --- src/main/actions/getProxiedAxios.ts | 100 +++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/src/main/actions/getProxiedAxios.ts b/src/main/actions/getProxiedAxios.ts index c1df8a60..d0a699bc 100644 --- a/src/main/actions/getProxiedAxios.ts +++ b/src/main/actions/getProxiedAxios.ts @@ -2,11 +2,54 @@ import axios, { AxiosInstance } from "axios"; import { readFileSync } from "fs"; import { HttpsProxyAgent, HttpsProxyAgentOptions } from "https-proxy-agent"; import { ClientRequest, RequestOptions } from "agent-base"; +import { Socket } from "net"; +import http from "http"; +import https from "https"; import { addCookiesToRequest, storeCookiesFromResponse, } from "./cookiesHelpers"; +const LOCAL_IPV4 = "127.0.0.1"; +const LOCAL_IPV6 = "::1"; + +const checkConnection = (host: string, port: number): Promise => { + return new Promise((resolve) => { + const socket = new Socket(); + const timeout = 1000; + + socket.setTimeout(timeout); + + socket.once("connect", () => { + socket.destroy(); + resolve(true); + }); + + socket.once("timeout", () => { + socket.destroy(); + resolve(false); + }); + + socket.once("error", () => { + socket.destroy(); + resolve(false); + }); + + socket.connect(port, host); + }); +}; + + +const createLocalhostLookup = async (port: number) => { + const ipv6Works = await checkConnection(LOCAL_IPV6, port).catch(() => false); + const targetIp = ipv6Works ? LOCAL_IPV6 : LOCAL_IPV4; + const targetFamily = ipv6Works ? 6 : 4; + + return (_lookupHostname: string, _options: any, callback: any) => { + callback(null, targetIp, targetFamily); + }; +}; + class PatchedHttpsProxyAgent extends HttpsProxyAgent { ca: unknown; @@ -32,17 +75,48 @@ let proxyConfig: ProxyConfig; function createAxiosInstance( config: ProxyConfig, + enableRQProxy: boolean = false, addStoredCookies: boolean = false ): AxiosInstance { - const instance = axios.create({ - proxy: false, - httpAgent: new HttpsProxyAgent(`http://${config.ip}:${config.port}`), - httpsAgent: new PatchedHttpsProxyAgent({ - host: config.ip, - port: config.port, - ca: readFileSync(config.rootCertPath), - }), - }); + let instance: AxiosInstance; + if (enableRQProxy) { + instance = axios.create({ + proxy: false, + httpAgent: new HttpsProxyAgent(`http://${config.ip}:${config.port}`), + httpsAgent: new PatchedHttpsProxyAgent({ + host: config.ip, + port: config.port, + ca: readFileSync(config.rootCertPath), + }), + }); + } else { + instance = axios.create({ + proxy: false, + }); + + instance.interceptors.request.use(async (requestConfig) => { + const { url: requestUrl } = requestConfig; + + if (!requestUrl) { + return requestConfig; + } + + const url = new URL(requestUrl); + const { hostname, port: urlPort, protocol } = url; + + if (hostname === "localhost") { + // convert string port to integer + 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 }); + } + + return requestConfig; + }); + } instance.interceptors.response.use(storeCookiesFromResponse); if (addStoredCookies) { @@ -60,8 +134,12 @@ export const createOrUpdateAxiosInstance = ( }; try { - proxiedAxios = createAxiosInstance(proxyConfig); - proxiedAxiosWithSessionCookies = createAxiosInstance(proxyConfig, true); + proxiedAxios = createAxiosInstance(proxyConfig, false); + proxiedAxiosWithSessionCookies = createAxiosInstance( + proxyConfig, + false, + true + ); } catch (error) { /* Do nothing */ console.error("Error creating or updating Axios instance:", error); From f8dc0bfd160798b5ae67812d2092343273920fca Mon Sep 17 00:00:00 2001 From: Aarushsr12 Date: Fri, 30 Jan 2026 12:14:12 +0530 Subject: [PATCH 2/2] fix --- src/main/actions/getProxiedAxios.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/actions/getProxiedAxios.ts b/src/main/actions/getProxiedAxios.ts index d0a699bc..9466c5b1 100644 --- a/src/main/actions/getProxiedAxios.ts +++ b/src/main/actions/getProxiedAxios.ts @@ -104,7 +104,7 @@ function createAxiosInstance( const url = new URL(requestUrl); const { hostname, port: urlPort, protocol } = url; - if (hostname === "localhost") { + if (hostname === "localhost" || hostname === LOCAL_IPV6 || hostname === LOCAL_IPV4) { // convert string port to integer const port = urlPort ? parseInt(urlPort, 10) : protocol === "https:" ? 443 : 80;