修复重启没应用代理问题

This commit is contained in:
2025-10-09 10:21:27 +08:00
parent ac6dfeca89
commit ba4078b66b
4 changed files with 119 additions and 12 deletions
+39 -2
View File
@@ -1,14 +1,51 @@
const { defaultLogger } = require('./utils/logger');
const Fse = require('fs-extra');
const Path = require('path');
// 创建logger实例
const logger = defaultLogger.child('ProxyConfig');
// 配置文件路径
const CONFIG_FILE_DIR = require('appdata-path').getAppDataPath('pmanager');
const CONFIG_FILE = Path.resolve(CONFIG_FILE_DIR, 'config.json');
// 读取用户配置中的代理设置
function getUserProxyConfig() {
try {
if (Fse.existsSync(CONFIG_FILE)) {
const config = Fse.readJsonSync(CONFIG_FILE);
if (config.proxy) {
// 解析代理URL获取端口
const proxyUrl = config.proxy;
const match = proxyUrl.match(/http:\/\/127\.0\.0\.1:(\d+)/);
if (match) {
return parseInt(match[1]);
}
}
}
} catch (error) {
logger.debug('读取用户代理配置失败:', error.message);
}
return null;
}
// 代理配置
const proxyConfig = {
// 系统代理配置
system: {
host: '127.0.0.1',
port: process.env.PROXY_PORT ? parseInt(process.env.PROXY_PORT) : 7890,
port: (() => {
// 优先级:环境变量 > 用户配置 > 默认值
if (process.env.PROXY_PORT) {
return parseInt(process.env.PROXY_PORT);
}
const userPort = getUserProxyConfig();
if (userPort) {
logger.info('从用户配置读取代理端口:', userPort);
return userPort;
}
return 7890;
})(),
protocol: 'http'
},
@@ -38,4 +75,4 @@ const proxyConfig = {
}
};
module.exports = proxyConfig;
module.exports = proxyConfig;