Skip to content

Commit fa69736

Browse files
committed
revert: some changes
This reverts commit c959866.
1 parent 73a5e9d commit fa69736

3 files changed

Lines changed: 35 additions & 163 deletions

File tree

generateConfig.js

Lines changed: 26 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -4,167 +4,38 @@ const { register_anonimous } = require('./main')
44
const { cookieToJson, generateRandomChineseIP } = require('./util/index')
55
const { getXeapiPublicKey } = require('./util/xeapiKey')
66
const tmpPath = require('os').tmpdir()
7-
const logger = require('./util/logger')
87

9-
const MAX_RETRIES = 3
10-
const RETRY_DELAY_MS = 1000
11-
12-
function sleep(ms) {
13-
return new Promise((resolve) => setTimeout(resolve, ms))
14-
}
15-
16-
function isRetryableError(err) {
17-
const msg = (err && err.message) || ''
18-
const status =
19-
(err && err.status) || (err && err.response && err.response.status)
20-
if (
21-
msg.includes('ETIMEDOUT') ||
22-
msg.includes('ECONNRESET') ||
23-
msg.includes('ECONNREFUSED') ||
24-
msg.includes('socket hang up') ||
25-
msg.includes('request timeout') ||
26-
msg.includes('timeout') ||
27-
msg.includes('network') ||
28-
msg.includes('Network')
29-
) {
30-
return true
31-
}
32-
if (status && status >= 500) {
33-
return true
34-
}
35-
return false
36-
}
37-
38-
/** @returns {{ success: boolean, error?: Error }} */
39-
async function fetchAnonymousToken() {
40-
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
41-
try {
42-
const res = await register_anonimous()
43-
const cookie = res.body.cookie
44-
if (cookie) {
45-
const cookieObj = cookieToJson(cookie)
46-
fs.writeFileSync(
47-
path.resolve(tmpPath, 'anonymous_token'),
48-
cookieObj.MUSIC_A,
49-
'utf-8',
50-
)
51-
logger.success('[generateConfig] 匿名 token 注册成功')
52-
return { success: true }
53-
}
54-
// 返回了但没有 cookie,视为异常但不再重试
55-
logger.warn(
56-
`[generateConfig] 匿名注册返回了空 cookie (attempt ${attempt})`,
57-
)
58-
return {
59-
success: false,
60-
error: new Error('empty cookie from anonymous register'),
61-
}
62-
} catch (err) {
63-
if (isRetryableError(err) && attempt < MAX_RETRIES) {
64-
const delay = RETRY_DELAY_MS * Math.pow(2, attempt - 1)
65-
logger.warn(
66-
`[generateConfig] 获取匿名 token 失败 (attempt ${attempt}/${MAX_RETRIES}), ${delay}ms 后重试...`,
67-
)
68-
await sleep(delay)
69-
continue
70-
}
71-
// 不可重试 或 已达最大次数
72-
if (attempt >= MAX_RETRIES) {
73-
logger.error(
74-
`[generateConfig] 获取匿名 token 已达最大重试次数 (${MAX_RETRIES}):`,
75-
err.message,
76-
)
77-
} else {
78-
logger.error(
79-
`[generateConfig] 获取匿名 token 失败 (不可重试):`,
80-
err.message,
81-
)
82-
}
83-
return { success: false, error: err }
84-
}
85-
}
86-
return { success: false, error: new Error('unreachable') }
87-
}
88-
89-
/**
90-
* 获取 xeapi public key,带重试
91-
* @returns {{ success: boolean, error?: Error }}
92-
*/
93-
async function fetchXeapiPublicKey() {
94-
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
95-
try {
96-
let currentPublicKey = {}
97-
try {
98-
currentPublicKey = JSON.parse(
99-
fs.readFileSync(path.resolve(tmpPath, 'xeapi_public_key'), 'utf-8'),
100-
)
101-
} catch (_) {
102-
// 本地无缓存文件,用空对象正常请求
103-
}
104-
const publicKey = await getXeapiPublicKey(
105-
currentPublicKey,
106-
global.deviceId,
107-
)
8+
async function generateConfig() {
9+
global.cnIp = generateRandomChineseIP()
10+
try {
11+
const res = await register_anonimous()
12+
const cookie = res.body.cookie
13+
if (cookie) {
14+
const cookieObj = cookieToJson(cookie)
10815
fs.writeFileSync(
109-
path.resolve(tmpPath, 'xeapi_public_key'),
110-
JSON.stringify(publicKey),
16+
path.resolve(tmpPath, 'anonymous_token'),
17+
cookieObj.MUSIC_A,
11118
'utf-8',
11219
)
113-
logger.success('[generateConfig] xeapi public key 获取成功')
114-
return { success: true }
115-
} catch (err) {
116-
if (isRetryableError(err) && attempt < MAX_RETRIES) {
117-
const delay = RETRY_DELAY_MS * Math.pow(2, attempt - 1)
118-
logger.warn(
119-
`[generateConfig] 获取 xeapi public key 失败 (attempt ${attempt}/${MAX_RETRIES}), ${delay}ms 后重试...`,
120-
)
121-
await sleep(delay)
122-
continue
123-
}
124-
if (attempt >= MAX_RETRIES) {
125-
logger.error(
126-
`[generateConfig] 获取 xeapi public key 已达最大重试次数 (${MAX_RETRIES}):`,
127-
err.message,
128-
)
129-
} else {
130-
logger.error(
131-
`[generateConfig] 获取 xeapi public key 失败 (不可重试):`,
132-
err.message,
133-
)
134-
}
135-
return { success: false, error: err }
13620
}
21+
} catch (error) {
22+
console.log(error)
13723
}
138-
return { success: false, error: new Error('unreachable') }
139-
}
140-
141-
/**
142-
* 生成配置(匿名 token + xeapi public key),带容错重试
143-
* @returns {{ tokenOk: boolean, keyOk: boolean }}
144-
*/
145-
async function generateConfig() {
146-
global.cnIp = generateRandomChineseIP()
147-
148-
// 两个任务并行执行,互不影响喵~
149-
const [tokenResult, keyResult] = await Promise.all([
150-
fetchAnonymousToken(),
151-
fetchXeapiPublicKey(),
152-
])
153-
154-
if (!tokenResult.success) {
155-
logger.warn('[generateConfig] 匿名 token 获取失败')
156-
}
157-
if (!keyResult.success) {
158-
logger.warn('[generateConfig] xeapi public key 获取失败')
159-
}
160-
161-
if (tokenResult.success && keyResult.success) {
162-
logger.success('[generateConfig] 配置初始化完成')
163-
}
164-
165-
return {
166-
tokenOk: tokenResult.success,
167-
keyOk: keyResult.success,
24+
try {
25+
let currentPublicKey = {}
26+
try {
27+
currentPublicKey = JSON.parse(
28+
fs.readFileSync(path.resolve(tmpPath, 'xeapi_public_key'), 'utf-8'),
29+
)
30+
} catch (_) {}
31+
const publicKey = await getXeapiPublicKey(currentPublicKey, global.deviceId)
32+
fs.writeFileSync(
33+
path.resolve(tmpPath, 'xeapi_public_key'),
34+
JSON.stringify(publicKey),
35+
'utf-8',
36+
)
37+
} catch (error) {
38+
console.log(error)
16839
}
16940
}
17041
module.exports = generateConfig

module/register_anonimous.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function cloudmusic_dll_encode_id(some_id) {
2626

2727
module.exports = async (query, request) => {
2828
const deviceId = generateDeviceId()
29+
logger.info(`Successfully registered anonimous token, deviceId: ${deviceId}`)
2930
global.deviceId = deviceId
3031
const encodedId = CryptoJS.enc.Base64.stringify(
3132
CryptoJS.enc.Utf8.parse(

server.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const { cookieToJson } = require('./util/index')
1010
const fileUpload = require('express-fileupload')
1111
const decode = require('safe-decode-uri-component')
1212
const logger = require('./util/logger.js')
13-
const { APP_CONF } = require('./util/config.json')
1413

1514
/**
1615
* The version check result.
@@ -300,15 +299,15 @@ async function constructServer(moduleDefs) {
300299
)
301300

302301
try {
303-
let usedCrypto = ''
304302
const moduleResponse = await moduleDef.module(query, (...params) => {
303+
// 参数注入客户端IP
305304
const obj = [...params]
306305
const options = obj[2] || {}
307-
usedCrypto = options.crypto || ''
308306
let ip = ''
309307

310308
if (options.randomCNIP) {
311309
ip = global.cnIp
310+
// logger.info('Using random Chinese IP for request:', ip)
312311
} else {
313312
ip = req.ip
314313

@@ -318,6 +317,7 @@ async function constructServer(moduleDefs) {
318317
if (ip == '::1') {
319318
ip = global.cnIp
320319
}
320+
// logger.info('Requested from ip:', ip)
321321
}
322322

323323
obj[2] = {
@@ -327,10 +327,7 @@ async function constructServer(moduleDefs) {
327327

328328
return request(...obj)
329329
})
330-
const displayCrypto = usedCrypto || (APP_CONF.encrypt ? 'eapi' : 'api')
331-
logger.info(
332-
`Request Success: [${displayCrypto}] ${decode(req.originalUrl)}`,
333-
)
330+
logger.info(`Request Success: ${decode(req.originalUrl)}`)
334331

335332
// 夹带私货部分:如果开启了通用解锁,并且是获取歌曲URL的接口,则尝试解锁(如果需要的话)ヾ(≧▽≦*)o
336333
if (
@@ -448,7 +445,10 @@ async function serveNcmApi(options) {
448445
╩ ╩╩ ╩ ╚═╝╝╚╝╩ ╩╩ ╩╝╚╝╚═╝╚═╝═╩╝
449446
`)
450447
logger.info(`
451-
- Server started successfully @ http://${host ? host : 'localhost'}:${port}`)
448+
- Server started successfully @ http://${host ? host : 'localhost'}:${port}
449+
- Environment: ${process.env.NODE_ENV || 'development'}
450+
- Node Version: ${process.version}
451+
- Process ID: ${process.pid}`)
452452
})
453453

454454
return appExt

0 commit comments

Comments
 (0)