fix: 代理改用undici ProxyAgent显式指定,不再依赖环境变量(更可靠)
This commit is contained in:
+2
-1
@@ -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, unknown>): string {
|
||||
@@ -233,6 +233,7 @@ export async function setupIPC(): Promise<void> {
|
||||
|
||||
// ── 代理设置(web_search/web_fetch 走代理)──
|
||||
ipcMain.handle('proxy:set', (_, url: string) => {
|
||||
setProxyUrl(url);
|
||||
if (url) {
|
||||
process.env.HTTP_PROXY = url;
|
||||
process.env.HTTPS_PROXY = url;
|
||||
|
||||
@@ -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<typeof spawn> | 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 {
|
||||
|
||||
Reference in New Issue
Block a user