update: anonymous_token配置抽离, 生成稳定性问题修复

This commit is contained in:
binaryify 2023-09-08 21:46:42 +08:00
parent 6741d9a920
commit 52c6021d89
9 changed files with 26 additions and 20 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ node_modules
.history
examples/moddef.json
bin
anonymous_token

View File

@ -1,4 +1,9 @@
# 更新日志
### 4.11.1 | 2023.09.08
- `anonymous_token` 配置抽离
- `anonymous_token` 生成稳定性问题修复
### 4.11.0 | 2023.09.07
- 新增 `播客搜索`,`播客上传声音`接口 #1789

9
app.js
View File

@ -1,6 +1,13 @@
#!/usr/bin/env node
const generateConfig = require('./generateConfig')
const fs = require('fs')
const path = require('path')
async function start() {
// 检测是否存在 anonymous_token 文件,没有则生成
if (!fs.existsSync('./anonymous_token')) {
fs.writeFileSync(path.resolve(__dirname, 'anonymous_token'), '', 'utf-8')
}
const generateConfig = require('./generateConfig')
await generateConfig()
require('./server').serveNcmApi({
checkVersion: true,

View File

@ -272,6 +272,8 @@
254. 回忆坐标
255. 播客搜索
256. 播客声音上传
257. 验证接口-二维码生成
258. 验证接口-二维码检测
## 安装
@ -4229,6 +4231,7 @@ type='1009' 获取其 id, 如`/search?keywords= 代码时间 &type=1009`
**必选参数:**
`qr`: `/verify/getQr`接口返回的`qr`字符串
## 离线访问此文档
此文档同时也是 Progressive Web Apps(PWA), 加入了 serviceWorker, 可离线访问

View File

@ -1,7 +1,6 @@
const fs = require('fs')
const { register_anonimous } = require('./main')
const { cookieToJson } = require('./util/index')
const config = require('./util/config.json')
const path = require('path')
async function generateConfig() {
try {
@ -9,11 +8,9 @@ async function generateConfig() {
const cookie = res.body.cookie
if (cookie) {
const cookieObj = cookieToJson(cookie)
let newConfig = { ...config }
newConfig.anonymous_token = cookieObj.MUSIC_A
fs.writeFileSync(
path.resolve(__dirname, 'util/config.json'),
JSON.stringify(newConfig, null, 2),
path.resolve(__dirname, 'anonymous_token'),
cookieObj.MUSIC_A,
'utf-8',
)
}

View File

@ -11,17 +11,10 @@ function cloudmusic_dll_encode_id(some_id) {
const digest = crypto.createHash('md5').update(xored).digest()
return digest.toString('base64')
}
function createRandomDeviceId() {
const t = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
const e = 6
let n = ''
for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * e))
return n
}
module.exports = async (query, request) => {
query.cookie.os = 'iOS'
const deviceId = createRandomDeviceId()
const deviceId = `NMUSIC`
const encodedId = Buffer.from(
`${deviceId} ${cloudmusic_dll_encode_id(deviceId)}`,
)
@ -41,7 +34,6 @@ module.exports = async (query, request) => {
realIP: query.realIP,
},
)
if (result.body.code === 200) {
result = {
status: 200,

View File

@ -1,6 +1,6 @@
{
"name": "NeteaseCloudMusicApi",
"version": "4.11.0",
"version": "4.11.1",
"description": "网易云音乐 NodeJS 版 API",
"scripts": {
"start": "node app.js",

View File

@ -1,5 +1,4 @@
{
"anonymous_token": "de91e1f8119d32e01cc73efcb82c0a30c9137e8d4f88dbf5e3d7bf3f28998f21add2bc8204eeee5e56c0bbb8743574b46ca2c10c35dc172199bef9bf4d60ecdeab066bb4dc737d1c3324751bcc9aaf44c3061cd18d77b7a0",
"resourceTypeMap": {
"0": "R_SO_4_",
"1": "R_MV_5_",
@ -10,4 +9,4 @@
"6": "A_EV_2_",
"7": "A_DR_14_"
}
}
}

View File

@ -5,8 +5,10 @@ const { PacProxyAgent } = require('pac-proxy-agent')
const http = require('http')
const https = require('https')
const tunnel = require('tunnel')
const fs = require('fs')
const anonymous_token = fs.readFileSync('./anonymous_token', 'utf-8')
console.log(anonymous_token)
const { URLSearchParams, URL } = require('url')
const config = require('../util/config.json')
// request.debug = true // 开启可看到更详细信息
const chooseUserAgent = (ua = false) => {
@ -72,7 +74,7 @@ const createRequest = (method, url, data = {}, options) => {
if (!options.cookie.MUSIC_U) {
// 游客
if (!options.cookie.MUSIC_A) {
options.cookie.MUSIC_A = config.anonymous_token
options.cookie.MUSIC_A = anonymous_token
}
}
headers['Cookie'] = Object.keys(options.cookie)