refactor: 重构获取anonymous token以及xeapikey的获取

This commit is contained in:
Takanashi Hoshino
2026-09-13 11:13:56 +08:00
parent a8c781fd64
commit c478367c6c
11 changed files with 656 additions and 78 deletions
+40 -25
View File
@@ -7,9 +7,7 @@ const { PacProxyAgent } = require('pac-proxy-agent')
const http = require('http')
const https = require('https')
const tunnel = require('tunnel')
const fs = require('fs')
const path = require('path')
const tmpPath = require('os').tmpdir()
const runtimeState = require('./runtimeState')
const {
cookieToJson,
cookieObjToString,
@@ -25,26 +23,6 @@ const {
getToken: antiCheatTokenV3,
} = require('../module/register_checktoken_v3')
// 预先读取匿名token并缓存
const anonymous_token = fs.readFileSync(
path.resolve(tmpPath, './anonymous_token'),
'utf-8',
)
const xeapiPublicKeyPath = path.resolve(tmpPath, './xeapi_public_key')
let xeapi_public_key = null
const loadXeapiPublicKey = () => {
if (!xeapi_public_key && fs.existsSync(xeapiPublicKeyPath)) {
try {
xeapi_public_key = JSON.parse(
fs.readFileSync(xeapiPublicKeyPath, 'utf-8'),
)
} catch (error) {
console.log('[ERR]', error)
}
}
return xeapi_public_key
}
// 预先绑定常用函数和常量
const floor = Math.floor
const random = Math.random
@@ -169,7 +147,8 @@ const processCookieObject = (cookie, crypto) => {
}
if (!processedCookie.MUSIC_U) {
processedCookie.MUSIC_A = processedCookie.MUSIC_A || anonymous_token
processedCookie.MUSIC_A =
processedCookie.MUSIC_A || runtimeState.getAnonymousToken()
}
return processedCookie
@@ -209,6 +188,42 @@ const createRequest = async (uri, data, options) => {
break
}
// 懒加载匿名 token:仅当内存中缺失时刷新一次,失败不阻塞本次请求。
// 兼容 serverless 冷启动——不假设启动阶段已刷新过。
// 刷新类请求自身跳过(否则会等待自己造成死锁)。
if (
!runtimeState.getAnonymousToken() &&
!runtimeState.isAnonymousTokenRefreshing()
) {
try {
const { ensureAnonymousToken } = require('./credentials')
await ensureAnonymousToken()
} catch (error) {
console.log('[ERR]', error)
}
}
// 懒加载 xeapi public key:仅在本次请求确实使用 xeapi 加密、且内存缺失时刷新。
// 必须在进入同步 executor 前完成,因为加密过程需要同步拿到本状态。
const resolvedCrypto =
options.crypto === '' || options.crypto === undefined
? APP_CONF.encrypt
? 'eapi'
: 'api'
: options.crypto
if (
resolvedCrypto === 'xeapi' &&
!runtimeState.getXeapiPublicKey() &&
!runtimeState.isXeapiPublicKeyRefreshing()
) {
try {
const { ensureXeapiPublicKey } = require('./credentials')
await ensureXeapiPublicKey()
} catch (error) {
console.log('[ERR]', error)
}
}
return new Promise((resolve, reject) => {
// 变量声明和初始化
const headers = options.headers ? { ...options.headers } : {}
@@ -273,7 +288,7 @@ const createRequest = async (uri, data, options) => {
break
case 'xeapi':
const xeapiPublicKey = loadXeapiPublicKey()
const xeapiPublicKey = runtimeState.getXeapiPublicKey()
if (!xeapiPublicKey) {
throw new Error('xeapi public key is missing')
}