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 {