diff --git a/README.md b/README.md index 9510f6e..991e4dd 100644 --- a/README.md +++ b/README.md @@ -196,15 +196,15 @@ sudo docker compose down #### 🌐 代理配置 -| 变量名 | 描述 | 默认值 | -| :------------------------------ | :---------------------------------------------------------------------------------------------------------- | :-------- | -| `INITIAL_AUTH_INDEX` | 启动时使用的初始身份验证索引。 | `0` | -| `ENABLE_AUTH_UPDATE` | 是否启用自动保存凭证更新。设为 `true` 启用后,将在每次登录/切换账号成功时以及每 24 小时自动更新 auth 文件。 | `false` | -| `MAX_RETRIES` | 请求失败后的最大重试次数(仅对假流式和非流式生效)。 | `3` | -| `RETRY_DELAY` | 两次重试之间的间隔(毫秒)。 | `2000` | -| `SWITCH_ON_USES` | 自动切换帐户前允许的请求次数(设为 `0` 禁用)。 | `40` | -| `FAILURE_THRESHOLD` | 切换帐户前允许的连续失败次数(设为 `0` 禁用)。 | `3` | -| `IMMEDIATE_SWITCH_STATUS_CODES` | 触发立即切换帐户的 HTTP 状态码(逗号分隔)。 | `429,503` | +| 变量名 | 描述 | 默认值 | +| :------------------------------ | :------------------------------------------------------------------------------------------------------------------------- | :-------- | +| `INITIAL_AUTH_INDEX` | 启动时使用的初始身份验证索引。 | `0` | +| `ENABLE_AUTH_UPDATE` | 是否启用自动保存凭证更新。默认为启用状态,将在每次登录/切换账号成功时以及每 24 小时自动更新 auth 文件。设为 `false` 禁用。 | `true` | +| `MAX_RETRIES` | 请求失败后的最大重试次数(仅对假流式和非流式生效)。 | `3` | +| `RETRY_DELAY` | 两次重试之间的间隔(毫秒)。 | `2000` | +| `SWITCH_ON_USES` | 自动切换帐户前允许的请求次数(设为 `0` 禁用)。 | `40` | +| `FAILURE_THRESHOLD` | 切换帐户前允许的连续失败次数(设为 `0` 禁用)。 | `3` | +| `IMMEDIATE_SWITCH_STATUS_CODES` | 触发立即切换帐户的 HTTP 状态码(逗号分隔)。 | `429,503` | #### 🗒️ 其他配置 diff --git a/README_EN.md b/README_EN.md index 8aca014..08df92e 100644 --- a/README_EN.md +++ b/README_EN.md @@ -196,15 +196,15 @@ This endpoint is forwarded to the official Gemini API format endpoint. #### 🌐 Proxy Configuration -| Variable | Description | Default | -| :------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------- | -| `INITIAL_AUTH_INDEX` | Initial authentication index to use on startup. | `0` | -| `ENABLE_AUTH_UPDATE` | Whether to enable automatic auth credential updates. If set to `true`, the auth file will be automatically updated upon successful login/account switch and every 24 hours. | `false` | -| `MAX_RETRIES` | Maximum number of retries for failed requests (only effective for fake streaming and non-streaming). | `3` | -| `RETRY_DELAY` | Delay between retries in milliseconds. | `2000` | -| `SWITCH_ON_USES` | Number of requests before automatically switching accounts (`0` to disable). | `40` | -| `FAILURE_THRESHOLD` | Number of consecutive failures before switching accounts (`0` to disable). | `3` | -| `IMMEDIATE_SWITCH_STATUS_CODES` | HTTP status codes that trigger immediate account switching (comma-separated). | `429,503` | +| Variable | Description | Default | +| :------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------- | +| `INITIAL_AUTH_INDEX` | Initial authentication index to use on startup. | `0` | +| `ENABLE_AUTH_UPDATE` | Whether to enable automatic auth credential updates. Defaults to enabled. The auth file will be automatically updated upon successful login/account switch and every 24 hours. Set to `false` to disable. | `true` | +| `MAX_RETRIES` | Maximum number of retries for failed requests (only effective for fake streaming and non-streaming). | `3` | +| `RETRY_DELAY` | Delay between retries in milliseconds. | `2000` | +| `SWITCH_ON_USES` | Number of requests before automatically switching accounts (`0` to disable). | `40` | +| `FAILURE_THRESHOLD` | Number of consecutive failures before switching accounts (`0` to disable). | `3` | +| `IMMEDIATE_SWITCH_STATUS_CODES` | HTTP status codes that trigger immediate account switching (comma-separated). | `429,503` | #### 🗒️ Other Configuration diff --git a/src/core/BrowserManager.js b/src/core/BrowserManager.js index 2ee6ad0..803a288 100644 --- a/src/core/BrowserManager.js +++ b/src/core/BrowserManager.js @@ -352,7 +352,7 @@ class BrowserManager { // 3. Auto-Save Auth: Every ~24 hours (21600 ticks * 4s = 86400s) if (tickCount % 21600 === 0) { - if (this._currentAuthIndex !== -1) { + if (this._currentAuthIndex >= 0) { try { this.logger.info("[HealthMonitor] 💾 Triggering daily periodic auth file update..."); await this._updateAuthFile(this._currentAuthIndex); @@ -609,6 +609,16 @@ class BrowserManager { this._currentAuthIndex = -1; throw new Error(`Invalid authIndex: ${authIndex}. Must be >= 0.`); } + + // [Auth Switch] Save current auth data before switching + if (this.browser && this._currentAuthIndex >= 0) { + try { + await this._updateAuthFile(this._currentAuthIndex); + } catch (e) { + this.logger.warn(`[Browser] Failed to save current auth during switch: ${e.message}`); + } + } + if (!this.browser) { this.logger.info("🚀 [Browser] Main browser instance not running, performing first-time launch..."); if (!fs.existsSync(this.browserExecutablePath)) { diff --git a/src/core/FormatConverter.js b/src/core/FormatConverter.js index bd8e3fe..e87223a 100644 --- a/src/core/FormatConverter.js +++ b/src/core/FormatConverter.js @@ -173,7 +173,7 @@ class FormatConverter { /** * Convert OpenAI request format to Google Gemini format * @param {object} openaiBody - OpenAI format request body - * @returns {{ googleRequest: object, cleanModelName: string }} - Converted request and cleaned model name + * @returns {Promise<{ googleRequest: object, cleanModelName: string }>} - Converted request and cleaned model name */ async translateOpenAIToGoogle(openaiBody) { // eslint-disable-line no-unused-vars diff --git a/src/utils/ConfigLoader.js b/src/utils/ConfigLoader.js index 44da057..719404d 100644 --- a/src/utils/ConfigLoader.js +++ b/src/utils/ConfigLoader.js @@ -23,7 +23,7 @@ class ConfigLoader { apiKeys: [], apiKeySource: "Not set", browserExecutablePath: null, - enableAuthUpdate: false, + enableAuthUpdate: true, failureThreshold: 3, forceThinking: false, forceUrlContext: false, @@ -61,7 +61,7 @@ class ConfigLoader { if (process.env.FORCE_URL_CONTEXT) config.forceUrlContext = process.env.FORCE_URL_CONTEXT.toLowerCase() === "true"; if (process.env.ENABLE_AUTH_UPDATE) - config.enableAuthUpdate = process.env.ENABLE_AUTH_UPDATE.toLowerCase() === "true"; + config.enableAuthUpdate = process.env.ENABLE_AUTH_UPDATE.toLowerCase() !== "false"; let rawCodes = process.env.IMMEDIATE_SWITCH_STATUS_CODES; let codesSource = "environment variable";