mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-10-22 23:51:23 +08:00
refactor: 重构获取anonymous token以及xeapikey的获取
This commit is contained in:
+18
-28
@@ -1,41 +1,31 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const { register_anonimous } = require('./main')
|
||||
const { cookieToJson, generateRandomChineseIP } = require('./util/index')
|
||||
const { getXeapiPublicKey } = require('./util/xeapiKey')
|
||||
const tmpPath = require('os').tmpdir()
|
||||
const runtimeState = require('./util/runtimeState')
|
||||
const { generateRandomChineseIP } = require('./util/index')
|
||||
|
||||
/**
|
||||
* 启动/预热配置。
|
||||
*
|
||||
*/
|
||||
async function generateConfig() {
|
||||
global.cnIp = generateRandomChineseIP()
|
||||
|
||||
// 先加载可选种子,保证网络刷新失败时仍有可用值(迁移/降级)。
|
||||
runtimeState.loadSeed()
|
||||
|
||||
// 启动时主动刷新匿名 token;失败则退回到种子(可能为空,由请求层按需兜底)。
|
||||
try {
|
||||
const res = await register_anonimous()
|
||||
const cookie = res.body.cookie
|
||||
if (cookie) {
|
||||
const cookieObj = cookieToJson(cookie)
|
||||
fs.writeFileSync(
|
||||
path.resolve(tmpPath, 'anonymous_token'),
|
||||
cookieObj.MUSIC_A,
|
||||
'utf-8',
|
||||
)
|
||||
}
|
||||
const { refreshAnonymousToken } = require('./util/credentials')
|
||||
await refreshAnonymousToken()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
try {
|
||||
let currentPublicKey = {}
|
||||
try {
|
||||
currentPublicKey = JSON.parse(
|
||||
fs.readFileSync(path.resolve(tmpPath, 'xeapi_public_key'), 'utf-8'),
|
||||
)
|
||||
} catch (_) {}
|
||||
const publicKey = await getXeapiPublicKey(currentPublicKey, global.deviceId)
|
||||
fs.writeFileSync(
|
||||
path.resolve(tmpPath, 'xeapi_public_key'),
|
||||
JSON.stringify(publicKey),
|
||||
'utf-8',
|
||||
)
|
||||
const { fetchXeapiPublicKey } = require('./util/credentials')
|
||||
const publicKey = await fetchXeapiPublicKey()
|
||||
runtimeState.mergeXeapiPublicKey(publicKey)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = generateConfig
|
||||
|
||||
Reference in New Issue
Block a user