From 1ce0ab12343e51077d8e65db171fea4235d989ef Mon Sep 17 00:00:00 2001 From: kjqwer <2990346238@qq.com> Date: Sat, 29 Nov 2025 07:16:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/start.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/backend/start.js b/backend/start.js index f2f2c20..cec9c6e 100644 --- a/backend/start.js +++ b/backend/start.js @@ -67,6 +67,31 @@ function loadConfig() { return config; } + +// 获取Windows系统代理配置 +function getWindowsProxy() { + if (os.platform() !== 'win32') return null; + + try { + const { execSync } = require('child_process'); + // 查询注册表获取代理设置 + const output = execSync('reg query "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"', { encoding: 'utf8' }); + + // 检查代理是否启用 (ProxyEnable = 1) + const proxyEnableMatch = output.match(/ProxyEnable\s+REG_DWORD\s+0x([0-9a-fA-F]+)/); + if (proxyEnableMatch && parseInt(proxyEnableMatch[1], 16) === 1) { + // 获取代理服务器地址 + const proxyServerMatch = output.match(/ProxyServer\s+REG_SZ\s+([^\r\n]+)/); + if (proxyServerMatch) { + return proxyServerMatch[1]; + } + } + } catch (error) { + // 忽略错误,可能没有权限或键不存在 + } + return null; +} + // 加载配置 const config = loadConfig(); @@ -98,14 +123,46 @@ if (config.proxy.enabled === true || (config.proxy.enabled === "auto" && config. } } else if (config.proxy.enabled === "auto") { // 自动检测系统代理 - const systemProxy = process.env.HTTP_PROXY || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.https_proxy; + let systemProxy = process.env.HTTP_PROXY || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.https_proxy; + + // 如果环境变量没有设置,尝试从Windows注册表获取 + if (!systemProxy && os.platform() === 'win32') { + const winProxy = getWindowsProxy(); + if (winProxy) { + systemProxy = winProxy; + logger.info(`通过注册表检测到系统代理: ${systemProxy}`); + } + } + if (systemProxy) { logger.info(`检测到系统代理: ${systemProxy}`); // 从系统代理URL中提取端口 - const match = systemProxy.match(/http:\/\/127\.0\.0\.1:(\d+)/); + // 支持格式: + // 1. http://127.0.0.1:7890 + // 2. 127.0.0.1:7890 + // 3. localhost:7890 + const match = systemProxy.match(/(?:127\.0\.0\.1|localhost):(\d+)/); if (match) { process.env.PROXY_PORT = match[1]; + + // 确保HTTP_PROXY环境变量被设置,供axios/proxy-agent使用 + let proxyUrl = systemProxy; + if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) { + proxyUrl = `http://${proxyUrl}`; + } + + if (!process.env.HTTP_PROXY) { + process.env.HTTP_PROXY = proxyUrl; + logger.info(`已设置 HTTP_PROXY 环境变量: ${proxyUrl}`); + } + if (!process.env.HTTPS_PROXY) { + process.env.HTTPS_PROXY = proxyUrl; + logger.info(`已设置 HTTPS_PROXY 环境变量: ${proxyUrl}`); + } + logger.info(`自动设置代理端口为: ${match[1]}`); + } else { + logger.warn(`无法从代理字符串中解析端口: ${systemProxy}`); } } else { logger.info('未检测到系统代理,将尝试使用系统代理环境变量');