From 53dec755282e12df84a0b1e1949c9593c745da24 Mon Sep 17 00:00:00 2001 From: thzxx Date: Wed, 10 Jun 2026 15:50:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=A3=E7=90=86=E6=94=B9=E7=94=A8undi?= =?UTF-8?q?ci=20ProxyAgent=E6=98=BE=E5=BC=8F=E6=8C=87=E5=AE=9A=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BE=9D=E8=B5=96=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F(=E6=9B=B4=E5=8F=AF=E9=9D=A0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/ipc.ts | 3 ++- src/main/tool-handlers-system.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 78303ee..b5421f7 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -51,7 +51,7 @@ import { browserOpen, browserScreenshot, browserEvaluate, browserExtract, browse import { startServer, stopServer, stopAllServers, callTool, getAllTools, getServerStatuses, refreshTools, setMCPTimeout } from './mcp-manager.js'; import { getAllowedDirs, getBlockedDirs, setAllowedDirs } from './tool-security.js'; import { startProcess, killProcess, getWorkspaceDir, setWorkspaceDir, listWorkspaceDir } from './workspace.js'; -import { setHTTPTimeout } from './tool-handlers-system.js'; +import { setHTTPTimeout, setProxyUrl } from './tool-handlers-system.js'; /** 工具结果摘要(用于日志面板) */ function summarizeResult(toolName: string, result: Record): string { @@ -233,6 +233,7 @@ export async function setupIPC(): Promise { // ── 代理设置(web_search/web_fetch 走代理)── ipcMain.handle('proxy:set', (_, url: string) => { + setProxyUrl(url); if (url) { process.env.HTTP_PROXY = url; process.env.HTTPS_PROXY = url; diff --git a/src/main/tool-handlers-system.ts b/src/main/tool-handlers-system.ts index 814f5ba..c5b6553 100644 --- a/src/main/tool-handlers-system.ts +++ b/src/main/tool-handlers-system.ts @@ -10,6 +10,28 @@ import { mainWindow } from './main.js'; import { sendLog, resolvePath, type ToolResult } from './tool-handlers-shared.js'; import { getWorkspaceDir } from './workspace.js'; +// ── 代理支持 ── +let _proxyUrl = ''; +let _proxyAgent: any = null; + +function getProxyAgent(): any { + if (!_proxyUrl) return null; + if (_proxyAgent) return _proxyAgent; + try { + const { ProxyAgent } = require('undici'); + _proxyAgent = new ProxyAgent(_proxyUrl); + return _proxyAgent; + } catch { + return null; + } +} + +/** 设置 HTTP 代理(供 IPC 调用) */ +export function setProxyUrl(url: string): void { + _proxyUrl = url; + _proxyAgent = null; // 重建 agent +} + /** 当前工具命令进程(用于用户手动终止) */ let _toolProc: ReturnType | null = null; @@ -186,7 +208,13 @@ async function fetchWithTimeout(url: string, timeout = HTTP_TIMEOUT, headers?: R const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); try { - const resp = await fetch(url, { headers: _headers, signal: controller.signal, redirect: 'follow' }); + const agent = getProxyAgent(); + const resp = await fetch(url, { + headers: _headers, + signal: controller.signal, + redirect: 'follow', + ...(agent && { dispatcher: agent }), + }); clearTimeout(timeoutId); return resp; } catch {