* 令牌管理器 * 负责令牌的获取、验证、刷新等操作
| 8 | * 令牌管理器 |
| 9 | * 负责令牌的获取、验证、刷新等操作 |
| 10 | */ |
| 11 | class TokenManager { |
| 12 | constructor() { |
| 13 | // Legacy default; overridden per-account when fingerprint is available |
| 14 | this.defaultHeaders = { |
| 15 | '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' |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * 获取登录端点 |
| 21 | * @returns {string} 登录端点URL |
| 22 | */ |
| 23 | get loginEndpoint() { |
| 24 | return `${getChatBaseUrl()}/api/v1/auths/signin` |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * 用户登录获取令牌 |
| 29 | * @param {string} email - 邮箱 |
| 30 | * @param {string} password - 密码 |
| 31 | * @param {Object} [account] - 账户对象(用于解析账号级代理;为空时回退到全局 PROXY_URL) |
| 32 | * @returns {Promise<string|null>} 令牌或null |
| 33 | */ |
| 34 | async login(email, password, account) { |
| 35 | try { |
| 36 | const proxyAgent = getProxyAgent(account) |
| 37 | // Use per-account fingerprint UA when available; fall back to legacy Edge UA |
| 38 | const ua = (account && account.fingerprint) ? buildUserAgent(account.fingerprint) : this.defaultHeaders['User-Agent'] |
| 39 | const requestConfig = { |
| 40 | headers: { 'User-Agent': ua }, |
| 41 | timeout: 10000 // 10秒超时 |
| 42 | } |
| 43 | |
| 44 | // 添加代理配置 |
| 45 | if (proxyAgent) { |
| 46 | requestConfig.httpsAgent = proxyAgent |
| 47 | requestConfig.proxy = false |
| 48 | } |
| 49 | |
| 50 | const response = await axios.post(this.loginEndpoint, { |
| 51 | email: email, |
| 52 | password: sha256Encrypt(password) |
| 53 | }, requestConfig) |
| 54 | |
| 55 | if (response.data && response.data.token) { |
| 56 | logger.success(`${email} 登录成功:${response.data.token}`, 'AUTH') |
| 57 | return response.data.token |
| 58 | } else { |
| 59 | logger.error(`${email} 登录响应缺少令牌`, 'AUTH') |
| 60 | return null |
| 61 | } |
| 62 | } catch (error) { |
| 63 | if (error.response) { |
| 64 | logger.error(`${email} 登录失败 (${error.response.status})`, 'AUTH', '', error) |
| 65 | } else if (error.request) { |
| 66 | logger.error(`${email} 登录失败: 网络请求超时或无响应`, 'AUTH') |
| 67 | } else { |
nothing calls this directly
no outgoing calls
no test coverage detected