Spaces:
Running
Running
File size: 8,196 Bytes
f120063 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | const axios = require('axios')
const { sha256Encrypt, JwtDecode } = require('./tools')
const { logger } = require('./logger')
const { getProxyAgent, getChatBaseUrl, applyProxyToAxiosConfig } = require('./proxy-helper')
/**
* 令牌管理器
* 负责令牌的获取、验证、刷新等操作
*/
class TokenManager {
constructor() {
this.defaultHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0'
}
}
/**
* 获取登录端点
* @returns {string} 登录端点URL
*/
get loginEndpoint() {
return `${getChatBaseUrl()}/api/v1/auths/signin`
}
/**
* 用户登录获取令牌
* @param {string} email - 邮箱
* @param {string} password - 密码
* @returns {Promise<string|null>} 令牌或null
*/
async login(email, password) {
try {
const proxyAgent = getProxyAgent()
const requestConfig = {
headers: this.defaultHeaders,
timeout: 10000 // 10秒超时
}
// 添加代理配置
if (proxyAgent) {
requestConfig.httpsAgent = proxyAgent
requestConfig.proxy = false
}
const response = await axios.post(this.loginEndpoint, {
email: email,
password: sha256Encrypt(password)
}, requestConfig)
if (response.data && response.data.token) {
logger.success(`${email} 登录成功:${response.data.token}`, 'AUTH')
return response.data.token
} else {
logger.error(`${email} 登录响应缺少令牌`, 'AUTH')
return null
}
} catch (error) {
if (error.response) {
logger.error(`${email} 登录失败 (${error.response.status})`, 'AUTH', '', error)
} else if (error.request) {
logger.error(`${email} 登录失败: 网络请求超时或无响应`, 'AUTH')
} else {
logger.error(`${email} 登录失败`, 'AUTH', '', error)
}
return null
}
}
/**
* 验证令牌是否有效
* @param {string} token - JWT令牌
* @returns {Object|null} 解码后的令牌信息或null
*/
validateToken(token) {
try {
if (!token) return null
const decoded = JwtDecode(token)
if (!decoded || !decoded.exp) {
return null
}
const now = Math.floor(Date.now() / 1000)
if (decoded.exp <= now) {
return null // 令牌已过期
}
return decoded
} catch (error) {
logger.error('令牌验证失败', 'TOKEN', '', error)
return null
}
}
/**
* 检查令牌是否即将过期
* @param {string} token - JWT令牌
* @param {number} thresholdHours - 过期阈值(小时)
* @returns {boolean} 是否即将过期
*/
isTokenExpiringSoon(token, thresholdHours = 6) {
const decoded = this.validateToken(token)
if (!decoded) return true // 无效令牌视为即将过期
const now = Math.floor(Date.now() / 1000)
const thresholdSeconds = thresholdHours * 60 * 60
return decoded.exp - now < thresholdSeconds
}
/**
* 获取令牌剩余有效时间(小时)
* @param {string} token - JWT令牌
* @returns {number} 剩余小时数,-1表示无效令牌
*/
getTokenRemainingHours(token) {
const decoded = this.validateToken(token)
if (!decoded) return -1
const now = Math.floor(Date.now() / 1000)
const remainingSeconds = decoded.exp - now
return Math.max(0, Math.round(remainingSeconds / 3600))
}
/**
* 刷新单个账户的令牌
* @param {Object} account - 账户对象 {email, password, token, expires}
* @returns {Promise<Object|null>} 更新后的账户对象或null
*/
async refreshToken(account) {
try {
const newToken = await this.login(account.email, account.password)
if (!newToken) {
return null
}
const decoded = this.validateToken(newToken)
if (!decoded) {
logger.error(`刷新后的令牌无效: ${account.email}`, 'TOKEN')
return null
}
const updatedAccount = {
...account,
token: newToken,
expires: decoded.exp
}
const remainingHours = this.getTokenRemainingHours(newToken)
logger.success(`令牌刷新成功: ${account.email} (有效期: ${remainingHours}小时)`, 'TOKEN')
return updatedAccount
} catch (error) {
logger.error(`刷新令牌失败 (${account.email})`, 'TOKEN', '', error)
return null
}
}
/**
* 批量刷新即将过期的令牌
* @param {Array} accounts - 账户列表
* @param {number} thresholdHours - 过期阈值(小时)
* @param {Function} onEachRefresh - 每次刷新成功后的回调函数 (updatedAccount, index, total) => void
* @returns {Promise<Object>} 刷新结果 {refreshed: Array, failed: Array}
*/
async batchRefreshTokens(accounts, thresholdHours = 24, onEachRefresh = null) {
const needsRefresh = accounts.filter(account =>
this.isTokenExpiringSoon(account.token, thresholdHours)
)
if (needsRefresh.length === 0) {
logger.info('没有需要刷新的令牌', 'TOKEN')
return { refreshed: [], failed: [] }
}
logger.info(`发现 ${needsRefresh.length} 个令牌需要刷新`, 'TOKEN')
const refreshed = []
const failed = []
for (let i = 0; i < needsRefresh.length; i++) {
const account = needsRefresh[i]
const updatedAccount = await this.refreshToken(account)
if (updatedAccount) {
refreshed.push(updatedAccount)
// 如果提供了回调函数,立即调用
if (onEachRefresh && typeof onEachRefresh === 'function') {
try {
await onEachRefresh(updatedAccount, i + 1, needsRefresh.length)
} catch (error) {
logger.error(`刷新回调函数执行失败 (${account.email})`, 'TOKEN', '', error)
}
}
} else {
failed.push(account)
}
// 添加延迟避免请求过于频繁
await this._delay(1000)
}
logger.success(`令牌刷新完成: 成功 ${refreshed.length} 个,失败 ${failed.length} 个`, 'TOKEN')
return { refreshed, failed }
}
/**
* 获取健康的令牌统计信息
* @param {Array} accounts - 账户列表
* @returns {Object} 统计信息
*/
getTokenHealthStats(accounts) {
const stats = {
total: accounts.length,
valid: 0,
expired: 0,
expiringSoon: 0,
invalid: 0
}
accounts.forEach(account => {
if (!account.token) {
stats.invalid++
return
}
const decoded = this.validateToken(account.token)
if (!decoded) {
stats.invalid++
return
}
const now = Math.floor(Date.now() / 1000)
if (decoded.exp <= now) {
stats.expired++
} else if (this.isTokenExpiringSoon(account.token, 6)) {
stats.expiringSoon++
} else {
stats.valid++
}
})
return stats
}
/**
* 延迟函数
* @param {number} ms - 延迟毫秒数
* @private
*/
async _delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
}
module.exports = TokenManager
|