diff --git a/src/main/utils/chromeRequest.ts b/src/main/utils/chromeRequest.ts index b7ba326..dec696e 100644 --- a/src/main/utils/chromeRequest.ts +++ b/src/main/utils/chromeRequest.ts @@ -25,6 +25,25 @@ export interface Response { url: string } +// 复用单个 session 用于代理请求 +let proxySession: Electron.Session | null = null +let currentProxyUrl: string | null = null +let proxySetupPromise: Promise | null = null + +async function getProxySession(proxyUrl: string): Promise { + if (!proxySession) { + proxySession = session.fromPartition('proxy-requests', { cache: false }) + } + if (currentProxyUrl !== proxyUrl) { + proxySetupPromise = proxySession.setProxy({ proxyRules: proxyUrl }) + currentProxyUrl = proxyUrl + } + if (proxySetupPromise) { + await proxySetupPromise + } + return proxySession +} + /** * Make HTTP request using Chromium's network stack (via electron.net) * This provides better compatibility, HTTP/2 support, and system certificate integration @@ -45,27 +64,17 @@ export async function request( } = options return new Promise((resolve, reject) => { - let sessionToUse: Electron.Session | undefined = session.defaultSession - let tempPartition: string | null = null + let sessionToUse: Electron.Session = session.defaultSession // Set up proxy if specified const setupProxy = async (): Promise => { if (proxy) { - // Create temporary session partition to avoid affecting global proxy settings - tempPartition = `temp-request-${Date.now()}-${Math.random()}` - sessionToUse = session.fromPartition(tempPartition, { cache: false }) const proxyUrl = `${proxy.protocol}://${proxy.host}:${proxy.port}` - await sessionToUse.setProxy({ proxyRules: proxyUrl }) + sessionToUse = await getProxySession(proxyUrl) } } const cleanup = (): void => { - // Cleanup temporary session if created - if (tempPartition) { - // Note: Electron doesn't provide session.destroy(), but temporary sessions - // will be garbage collected when no longer referenced - sessionToUse = undefined - } } setupProxy()