修复无法登录问题

This commit is contained in:
2025-11-29 07:16:15 +08:00
parent 0e8766c0b4
commit 1ce0ab1234
+59 -2
View File
@@ -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('未检测到系统代理,将尝试使用系统代理环境变量');