Skip to content

Commit

Permalink
update: anonymous_token配置抽离, 生成稳定性问题修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Binaryify committed Sep 8, 2023
1 parent 6741d9a commit 52c6021
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
.history
examples/moddef.json
bin
anonymous_token
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# 更新日志
### 4.11.1 | 2023.09.08
- `anonymous_token` 配置抽离

- `anonymous_token` 生成稳定性问题修复

### 4.11.0 | 2023.09.07
- 新增 `播客搜索`,`播客上传声音`接口 #1789

Expand Down
9 changes: 8 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@
254. 回忆坐标
255. 播客搜索
256. 播客声音上传
257. 验证接口-二维码生成
258. 验证接口-二维码检测

## 安装

Expand Down Expand Up @@ -4229,6 +4231,7 @@ type='1009' 获取其 id, 如`/search?keywords= 代码时间 &type=1009`
**必选参数:**
`qr`: `/verify/getQr`接口返回的`qr`字符串
## 离线访问此文档
此文档同时也是 Progressive Web Apps(PWA), 加入了 serviceWorker, 可离线访问
Expand Down
7 changes: 2 additions & 5 deletions generateConfig.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
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 {
const res = await register_anonimous()
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',
)
}
Expand Down
10 changes: 1 addition & 9 deletions module/register_anonimous.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`,
)
Expand All @@ -41,7 +34,6 @@ module.exports = async (query, request) => {
realIP: query.realIP,
},
)

if (result.body.code === 200) {
result = {
status: 200,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NeteaseCloudMusicApi",
"version": "4.11.0",
"version": "4.11.1",
"description": "网易云音乐 NodeJS 版 API",
"scripts": {
"start": "node app.js",
Expand Down
3 changes: 1 addition & 2 deletions util/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"anonymous_token": "de91e1f8119d32e01cc73efcb82c0a30c9137e8d4f88dbf5e3d7bf3f28998f21add2bc8204eeee5e56c0bbb8743574b46ca2c10c35dc172199bef9bf4d60ecdeab066bb4dc737d1c3324751bcc9aaf44c3061cd18d77b7a0",
"resourceTypeMap": {
"0": "R_SO_4_",
"1": "R_MV_5_",
Expand All @@ -10,4 +9,4 @@
"6": "A_EV_2_",
"7": "A_DR_14_"
}
}
}
6 changes: 4 additions & 2 deletions util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 52c6021

Please sign in to comment.