Upload unified-server.js
Browse files- unified-server.js +19 -4
unified-server.js
CHANGED
|
@@ -723,6 +723,7 @@ class ProxyServerSystem extends EventEmitter {
|
|
| 723 |
maxRetries: 3, retryDelay: 2000, browserExecutablePath: null,
|
| 724 |
apiKeys: [],
|
| 725 |
immediateSwitchStatusCodes: [],
|
|
|
|
| 726 |
};
|
| 727 |
|
| 728 |
const configPath = path.join(__dirname, 'config.json');
|
|
@@ -746,6 +747,14 @@ class ProxyServerSystem extends EventEmitter {
|
|
| 746 |
if (process.env.API_KEYS) {
|
| 747 |
config.apiKeys = process.env.API_KEYS.split(',');
|
| 748 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 749 |
|
| 750 |
// NEW: 统一处理 immediateSwitchStatusCodes,环境变量优先于 config.json
|
| 751 |
let rawCodes = process.env.IMMEDIATE_SWITCH_STATUS_CODES;
|
|
@@ -779,6 +788,10 @@ class ProxyServerSystem extends EventEmitter {
|
|
| 779 |
this.logger.info(` HTTP 服务端口: ${this.config.httpPort}`);
|
| 780 |
this.logger.info(` 监听地址: ${this.config.host}`);
|
| 781 |
this.logger.info(` 流式模式: ${this.config.streamingMode}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
// MODIFIED: 日志输出已汉化
|
| 783 |
this.logger.info(` 失败计数切换: ${this.config.failureThreshold > 0 ? `连续 ${this.config.failureThreshold} 次失败后切换` : '已禁用'}`);
|
| 784 |
this.logger.info(` 立即切换状态码: ${this.config.immediateSwitchStatusCodes.length > 0 ? this.config.immediateSwitchStatusCodes.join(', ') : '已禁用'}`);
|
|
@@ -796,14 +809,15 @@ class ProxyServerSystem extends EventEmitter {
|
|
| 796 |
try {
|
| 797 |
// 决定启动时使用的认证索引
|
| 798 |
let startupIndex = this.authSource.getFirstAvailableIndex();
|
| 799 |
-
|
|
|
|
| 800 |
|
| 801 |
if (suggestedIndex) {
|
| 802 |
if (this.authSource.getAvailableIndices().includes(suggestedIndex)) {
|
| 803 |
-
this.logger.info(`[System]
|
| 804 |
startupIndex = suggestedIndex;
|
| 805 |
} else {
|
| 806 |
-
this.logger.warn(`[System]
|
| 807 |
}
|
| 808 |
} else {
|
| 809 |
this.logger.info(`[System] 未指定启动索引,将自动使用第一个可用索引: ${startupIndex}`);
|
|
@@ -814,7 +828,8 @@ class ProxyServerSystem extends EventEmitter {
|
|
| 814 |
await this._startWebSocketServer();
|
| 815 |
this.logger.info(`[System] 代理服务器系统启动完成。`);
|
| 816 |
this.emit('started');
|
| 817 |
-
} catch (error)
|
|
|
|
| 818 |
this.logger.error(`[System] 启动失败: ${error.message}`);
|
| 819 |
this.emit('error', error);
|
| 820 |
throw error;
|
|
|
|
| 723 |
maxRetries: 3, retryDelay: 2000, browserExecutablePath: null,
|
| 724 |
apiKeys: [],
|
| 725 |
immediateSwitchStatusCodes: [],
|
| 726 |
+
initialAuthIndex: null, // 新增:为 initialAuthIndex 提供默认值
|
| 727 |
};
|
| 728 |
|
| 729 |
const configPath = path.join(__dirname, 'config.json');
|
|
|
|
| 747 |
if (process.env.API_KEYS) {
|
| 748 |
config.apiKeys = process.env.API_KEYS.split(',');
|
| 749 |
}
|
| 750 |
+
// 新增:处理环境变量,它会覆盖 config.json 中的设置
|
| 751 |
+
if (process.env.INITIAL_AUTH_INDEX) {
|
| 752 |
+
const envIndex = parseInt(process.env.INITIAL_AUTH_INDEX, 10);
|
| 753 |
+
if (!isNaN(envIndex) && envIndex > 0) {
|
| 754 |
+
config.initialAuthIndex = envIndex;
|
| 755 |
+
}
|
| 756 |
+
}
|
| 757 |
+
|
| 758 |
|
| 759 |
// NEW: 统一处理 immediateSwitchStatusCodes,环境变量优先于 config.json
|
| 760 |
let rawCodes = process.env.IMMEDIATE_SWITCH_STATUS_CODES;
|
|
|
|
| 788 |
this.logger.info(` HTTP 服务端口: ${this.config.httpPort}`);
|
| 789 |
this.logger.info(` 监听地址: ${this.config.host}`);
|
| 790 |
this.logger.info(` 流式模式: ${this.config.streamingMode}`);
|
| 791 |
+
// 新增:在日志中显示初始索引的配置
|
| 792 |
+
if (this.config.initialAuthIndex) {
|
| 793 |
+
this.logger.info(` 指定初始认证索引: ${this.config.initialAuthIndex}`);
|
| 794 |
+
}
|
| 795 |
// MODIFIED: 日志输出已汉化
|
| 796 |
this.logger.info(` 失败计数切换: ${this.config.failureThreshold > 0 ? `连续 ${this.config.failureThreshold} 次失败后切换` : '已禁用'}`);
|
| 797 |
this.logger.info(` 立即切换状态码: ${this.config.immediateSwitchStatusCodes.length > 0 ? this.config.immediateSwitchStatusCodes.join(', ') : '已禁用'}`);
|
|
|
|
| 809 |
try {
|
| 810 |
// 决定启动时使用的认证索引
|
| 811 |
let startupIndex = this.authSource.getFirstAvailableIndex();
|
| 812 |
+
// 修改:从 this.config 读取,而不是直接从 process.env
|
| 813 |
+
const suggestedIndex = this.config.initialAuthIndex;
|
| 814 |
|
| 815 |
if (suggestedIndex) {
|
| 816 |
if (this.authSource.getAvailableIndices().includes(suggestedIndex)) {
|
| 817 |
+
this.logger.info(`[System] 使用配置中指定的有效启动索引: ${suggestedIndex}`);
|
| 818 |
startupIndex = suggestedIndex;
|
| 819 |
} else {
|
| 820 |
+
this.logger.warn(`[System] 配置中指定的启动索引 ${suggestedIndex} 无效或不存在,将使用第一个可用索引: ${startupIndex}`);
|
| 821 |
}
|
| 822 |
} else {
|
| 823 |
this.logger.info(`[System] 未指定启动索引,将自动使用第一个可用索引: ${startupIndex}`);
|
|
|
|
| 828 |
await this._startWebSocketServer();
|
| 829 |
this.logger.info(`[System] 代理服务器系统启动完成。`);
|
| 830 |
this.emit('started');
|
| 831 |
+
} catch (error)
|
| 832 |
+
{
|
| 833 |
this.logger.error(`[System] 启动失败: ${error.message}`);
|
| 834 |
this.emit('error', error);
|
| 835 |
throw error;
|