| |
| require('dotenv').config(); |
|
|
| |
| const envChecker = require('./src/utils/envChecker'); |
| console.log('启动前检查环境配置...'); |
| envChecker.enforceEnvCheck(); |
|
|
| |
| console.log('环境检查通过,已适配最新GitHub Actions工作流参数'); |
|
|
| const cookieRefresher = require('./src/utils/cookieRefresher'); |
| const keyManager = require('./src/utils/keyManager'); |
| const config = require('./src/config/config'); |
|
|
| |
| const args = process.argv.slice(2); |
| const targetApiKey = args.length > 0 ? args[0] : null; |
| const forceRefresh = args.includes('--force') || args.includes('-f'); |
|
|
| |
| const MIN_COOKIE_COUNT = config.refresh.minCookieCount; |
|
|
| |
| const COOKIE_REFRESH_MODE = process.env.COOKIE_REFRESH_MODE || 'append'; |
|
|
| |
| async function main() { |
| console.log('===== 自动刷新 Cookie 开始 ====='); |
| console.log(`最小 Cookie 数量: ${MIN_COOKIE_COUNT}`); |
| console.log(`Cookie 刷新模式: ${COOKIE_REFRESH_MODE} (${COOKIE_REFRESH_MODE === 'replace' ? '替换现有cookie' : '追加新cookie'})`); |
| |
| if (targetApiKey) { |
| console.log(`指定刷新 API Key: ${targetApiKey}`); |
| } |
| |
| if (forceRefresh) { |
| console.log('强制刷新模式: 忽略 Cookie 数量检查'); |
| } |
| |
| try { |
| |
| const apiKeys = keyManager.getAllApiKeys(); |
| |
| if (apiKeys.length === 0) { |
| console.log('警告: 系统中没有找到任何 API Key'); |
| |
| |
| const envApiKeys = Object.keys(config.apiKeys); |
| if (envApiKeys.length > 0) { |
| console.log(`检测到环境变量中有 ${envApiKeys.length} 个 API Key,但尚未加载到系统中`); |
| console.log('正在重新初始化 API Keys...'); |
| |
| |
| keyManager.initializeApiKeys(); |
| |
| |
| const refreshedApiKeys = keyManager.getAllApiKeys(); |
| if (refreshedApiKeys.length > 0) { |
| console.log(`成功加载 ${refreshedApiKeys.length} 个 API Key,继续刷新流程`); |
| |
| } else { |
| console.log('初始化后仍未找到 API Key,请检查配置'); |
| console.log('===== 自动刷新 Cookie 结束 ====='); |
| return; |
| } |
| } else { |
| console.log('环境变量中也没有配置 API Key,请先添加 API Key'); |
| console.log('===== 自动刷新 Cookie 结束 ====='); |
| return; |
| } |
| } |
| |
| |
| const updatedApiKeys = keyManager.getAllApiKeys(); |
| console.log(`系统中共有 ${updatedApiKeys.length} 个 API Key`); |
| |
| |
| if (targetApiKey && !updatedApiKeys.includes(targetApiKey)) { |
| console.error(`错误: 指定的 API Key "${targetApiKey}" 不存在`); |
| console.log('===== 自动刷新 Cookie 异常结束 ====='); |
| return; |
| } |
| |
| |
| const keysToProcess = targetApiKey ? [targetApiKey] : updatedApiKeys; |
| |
| |
| const sortedKeys = keysToProcess.sort((a, b) => { |
| const aCount = keyManager.getAllCookiesForApiKey(a).length; |
| const bCount = keyManager.getAllCookiesForApiKey(b).length; |
| return aCount - bCount; |
| }); |
| |
| |
| let refreshedCount = 0; |
| let needRefreshCount = 0; |
| |
| for (const apiKey of sortedKeys) { |
| const cookies = keyManager.getAllCookiesForApiKey(apiKey); |
| console.log(`API Key: ${apiKey}, Cookie 数量: ${cookies.length}`); |
| |
| |
| if (forceRefresh || cookies.length < MIN_COOKIE_COUNT) { |
| needRefreshCount++; |
| if (forceRefresh) { |
| console.log(`强制刷新 API Key: ${apiKey}`); |
| } else { |
| console.log(`API Key ${apiKey} 的 Cookie 数量不足,需要刷新`); |
| } |
| |
| |
| console.log(`开始自动刷新 Cookie,目标 API Key: ${apiKey},最小 Cookie 数量: ${MIN_COOKIE_COUNT},刷新模式: ${COOKIE_REFRESH_MODE}`); |
| const result = await cookieRefresher.autoRefreshCookies(apiKey, MIN_COOKIE_COUNT); |
| |
| if (result.success) { |
| refreshedCount++; |
| console.log(`刷新结果: ${result.message}`); |
| |
| |
| if (COOKIE_REFRESH_MODE === 'replace') { |
| console.log(`使用替换模式: 现有cookie已全部标记为无效,系统现在只使用新cookie`); |
| } else { |
| console.log(`使用追加模式: 现有cookie已保留,新cookie已添加到系统`); |
| } |
| } else { |
| console.error(`刷新失败: ${result.message}`); |
| } |
| } else { |
| console.log(`API Key ${apiKey} 的 Cookie 数量足够,不需要刷新`); |
| } |
| } |
| |
| console.log('===== 自动刷新 Cookie 完成 ====='); |
| console.log(`共有 ${needRefreshCount} 个 API Key 需要刷新,成功刷新 ${refreshedCount} 个`); |
| } catch (error) { |
| console.error('自动刷新 Cookie 失败:', error); |
| console.log('===== 自动刷新 Cookie 异常结束 ====='); |
| } |
| } |
|
|
| |
| main().catch(console.error); |