@vicyangorg/dsh-music 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -8
- package/client/index.js +2650 -641
- package/docs/DETAIL.md +49 -9
- package/host/accounts.js +83 -0
- package/host/download.js +60 -18
- package/host/liked-sync.js +322 -0
- package/host/likes.js +124 -9
- package/host/main.js +27 -2
- package/host/onboarding.js +10 -4
- package/host/providers/common.js +66 -0
- package/host/providers/netease.js +266 -0
- package/host/providers/qqlogin.js +521 -0
- package/host/providers/qqmusic.js +213 -0
- package/host/routes.js +33 -0
- package/host/source-scan.js +190 -0
- package/host/sync.js +18 -5
- package/host/t2s-extra.js +153 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 网易云音乐(只读)客户端:扫码登录 + 「我喜欢的音乐」歌单。
|
|
3
|
+
*
|
|
4
|
+
* - 非官方接口(与开源 NeteaseCloudMusicApi 同源),只读:不写账号、不拉音源。
|
|
5
|
+
* - 两种通道:plain `/api/...`(表单直传)与 weapi(AES-128-CBC ×2 + 原始 RSA)。
|
|
6
|
+
* 接口层两种都试,哪个先返回 code 200 用哪个;都失败时把“未登录”优先抛成 401。
|
|
7
|
+
* - 零外部依赖:仅 node: 内置模块。
|
|
8
|
+
*/
|
|
9
|
+
import { constants, createCipheriv, createPublicKey, publicEncrypt, randomBytes } from 'node:crypto'
|
|
10
|
+
import { cleanText, fetchJson, httpError, parseCookie, readSetCookies } from './common.js'
|
|
11
|
+
|
|
12
|
+
export const NETEASE_ORIGIN = 'https://music.163.com'
|
|
13
|
+
export const NETEASE_UA =
|
|
14
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0'
|
|
15
|
+
|
|
16
|
+
const PRESET_KEY = '0CoJUm6Qyw8W8jud'
|
|
17
|
+
const IV = '0102030405060708'
|
|
18
|
+
const BASE62 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
|
19
|
+
const PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY-----
|
|
20
|
+
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB
|
|
21
|
+
-----END PUBLIC KEY-----`
|
|
22
|
+
|
|
23
|
+
// 与官方 Web 端 weapi 相同:AES-CBC(PKCS7) 两次,第二次用 16 位随机 base62 key;
|
|
24
|
+
// 再把 key 反序后用 1024bit 公钥做“无填充”RSA(即直接 modPow),服务端用私钥还原。
|
|
25
|
+
function aesCbc(text, key) {
|
|
26
|
+
const cipher = createCipheriv('aes-128-cbc', Buffer.from(key, 'utf8'), Buffer.from(IV, 'utf8'))
|
|
27
|
+
return Buffer.concat([cipher.update(text, 'utf8'), cipher.final()]).toString('base64')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function rsaRaw(input) {
|
|
31
|
+
const key = createPublicKey(PUBLIC_KEY_PEM)
|
|
32
|
+
const size = Math.ceil((key.asymmetricKeyDetails?.modulusLength || 1024) / 8)
|
|
33
|
+
const buf = Buffer.alloc(size, 0)
|
|
34
|
+
Buffer.from(input, 'utf8').copy(buf, size - Buffer.byteLength(input))
|
|
35
|
+
return publicEncrypt({ key, padding: constants.RSA_NO_PADDING }, buf).toString('hex')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function randomSecretKey() {
|
|
39
|
+
const bytes = randomBytes(16)
|
|
40
|
+
let secretKey = ''
|
|
41
|
+
for (let i = 0; i < 16; i++) secretKey += BASE62[bytes[i] % 62]
|
|
42
|
+
return secretKey
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** secretKey 可注入(测试解密用);生产路径随机。 */
|
|
46
|
+
export function weapiEncrypt(object, secretKey = randomSecretKey()) {
|
|
47
|
+
const text = JSON.stringify(object)
|
|
48
|
+
return {
|
|
49
|
+
params: aesCbc(aesCbc(text, PRESET_KEY), secretKey),
|
|
50
|
+
encSecKey: rsaRaw(secretKey.split('').reverse().join('')),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function describe(j) {
|
|
55
|
+
if (!j) return '无响应'
|
|
56
|
+
if (j.code === 'ERR') return j.message
|
|
57
|
+
const msg = j.message || j.msg || ''
|
|
58
|
+
return `code ${j.code}${msg ? ' ' + msg : ''}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isNotLogin(j) {
|
|
62
|
+
const c = Number(j && j.code)
|
|
63
|
+
return c === 301 || c === 302
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createNetease({ fetchImpl, timeoutMs = 15000 } = {}) {
|
|
67
|
+
const baseCookie = {
|
|
68
|
+
__remember_me: 'true',
|
|
69
|
+
os: 'pc',
|
|
70
|
+
appver: '3.0.18.203152',
|
|
71
|
+
osver: 'Microsoft-Windows-10-Professional-build-22631-64bit',
|
|
72
|
+
channel: 'netease',
|
|
73
|
+
versioncode: '140',
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function cookieHeader(cookie) {
|
|
77
|
+
const pairs = { ...baseCookie, ...parseCookie(typeof cookie === 'string' ? cookie : '') }
|
|
78
|
+
return Object.entries(pairs)
|
|
79
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
80
|
+
.join('; ')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function post(path, form, cookie) {
|
|
84
|
+
const { json, res } = await fetchJson(
|
|
85
|
+
NETEASE_ORIGIN + path,
|
|
86
|
+
{
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: {
|
|
89
|
+
'User-Agent': NETEASE_UA,
|
|
90
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
91
|
+
Accept: '*/*',
|
|
92
|
+
Referer: NETEASE_ORIGIN + '/',
|
|
93
|
+
Origin: NETEASE_ORIGIN,
|
|
94
|
+
Cookie: cookieHeader(cookie),
|
|
95
|
+
},
|
|
96
|
+
body: new URLSearchParams(form).toString(),
|
|
97
|
+
},
|
|
98
|
+
fetchImpl,
|
|
99
|
+
timeoutMs,
|
|
100
|
+
)
|
|
101
|
+
return { json, res }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** plain 通道:表单直传 */
|
|
105
|
+
async function plain(path, params, cookie) {
|
|
106
|
+
const { json } = await post(path, params, cookie)
|
|
107
|
+
return json
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** weapi 通道:加密表单(/api/x → /weapi/x) */
|
|
111
|
+
async function weapi(path, params, cookie) {
|
|
112
|
+
const csrf = parseCookie(typeof cookie === 'string' ? cookie : '').__csrf || ''
|
|
113
|
+
const { json } = await post('/weapi/' + path.replace(/^\/api\//, ''), weapiEncrypt({ ...params, csrf_token: csrf }), cookie)
|
|
114
|
+
return json
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function either(what, path, params, cookie) {
|
|
118
|
+
let first = null
|
|
119
|
+
try {
|
|
120
|
+
first = await plain(path, params, cookie)
|
|
121
|
+
if (first && Number(first.code) === 200) return first
|
|
122
|
+
} catch (err) {
|
|
123
|
+
first = { code: 'ERR', message: err.message }
|
|
124
|
+
}
|
|
125
|
+
let second = null
|
|
126
|
+
try {
|
|
127
|
+
second = await weapi(path, params, cookie)
|
|
128
|
+
if (second && Number(second.code) === 200) return second
|
|
129
|
+
} catch (err) {
|
|
130
|
+
second = { code: 'ERR', message: err.message }
|
|
131
|
+
}
|
|
132
|
+
if (isNotLogin(first) || isNotLogin(second)) {
|
|
133
|
+
throw httpError('网易云登录已失效,请重新扫码登录', 401, { code: isNotLogin(first) ? first.code : second.code })
|
|
134
|
+
}
|
|
135
|
+
throw httpError(`${what}失败(${describe(first)};${describe(second)})`, 502)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 生成扫码登录 key;二维码内容由客户端本地渲染 */
|
|
139
|
+
async function qrKey() {
|
|
140
|
+
const j = await plain('/api/login/qrcode/unikey', { type: '3', noToken: 'true' })
|
|
141
|
+
if (!j || Number(j.code) !== 200 || !j.unikey) throw httpError(`获取网易云登录二维码失败(${describe(j)})`, 502)
|
|
142
|
+
return {
|
|
143
|
+
key: String(j.unikey),
|
|
144
|
+
url: `${NETEASE_ORIGIN}/login?codekey=${encodeURIComponent(j.unikey)}`,
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** 轮询扫码状态:801 等待扫码 / 802 待确认 / 803 成功(附 cookie)/ 800 过期 */
|
|
149
|
+
async function qrPoll(key) {
|
|
150
|
+
const { json, res } = await post('/api/login/qrcode/client/login', { key, type: '3', noToken: 'true' })
|
|
151
|
+
const code = Number(json && json.code)
|
|
152
|
+
if (code === 803) {
|
|
153
|
+
const jar = {}
|
|
154
|
+
for (const line of readSetCookies(res)) {
|
|
155
|
+
const m = /^([^=]+)=([^;]*)/.exec(line.trim())
|
|
156
|
+
if (m) jar[m[1].trim()] = m[2]
|
|
157
|
+
}
|
|
158
|
+
if (!jar.MUSIC_U) throw httpError('扫码成功但未拿到登录凭据,请重新扫码', 502)
|
|
159
|
+
const cookie = ['MUSIC_U', '__csrf'].filter((k) => jar[k]).map((k) => `${k}=${jar[k]}`).join('; ')
|
|
160
|
+
return { code: 803, message: json.message || '授权登录成功', cookie }
|
|
161
|
+
}
|
|
162
|
+
if (code === 800 || code === 801 || code === 802) return { code, message: json.message || '' }
|
|
163
|
+
throw httpError(`查询扫码状态失败(${describe(json)})`, 502)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 读取账号信息;登录态失效 → 401 */
|
|
167
|
+
async function account(cookie) {
|
|
168
|
+
const j = await either('获取网易云账号信息', '/api/w/nuser/account/get', {}, cookie)
|
|
169
|
+
const data = j.data && typeof j.data === 'object' ? j.data : j
|
|
170
|
+
const profile = data.profile || j.profile || null
|
|
171
|
+
if (!profile || !profile.userId) throw httpError('网易云登录已失效,请重新扫码登录', 401)
|
|
172
|
+
return {
|
|
173
|
+
uid: String(profile.userId),
|
|
174
|
+
nickname: cleanText(profile.nickname, 64),
|
|
175
|
+
avatarUrl: typeof profile.avatarUrl === 'string' ? profile.avatarUrl : null,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** trackIds 归一:[{ id, at }] → [{ id: String, likedAt }] */
|
|
180
|
+
function toTrackIds(value) {
|
|
181
|
+
return (Array.isArray(value) ? value : [])
|
|
182
|
+
.map((t) => ({ id: String(t.id), likedAt: t.at ? Number(t.at) || null : null }))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 「我喜欢的音乐」歌单(按喜欢时间倒序,带 at 时间戳)
|
|
186
|
+
* user/playlist 的摘要线上已不带 trackIds(只有 trackCount)→ 再取 v6/playlist/detail:
|
|
187
|
+
* trackIds 全量;tracks 只带前 n 首完整信息,其余交给 songDetails 补齐。 */
|
|
188
|
+
async function likedPlaylist(cookie, uid) {
|
|
189
|
+
const j = await either('获取网易云「我喜欢的音乐」', '/api/user/playlist', { uid, limit: '30', offset: '0', includeVideo: 'true' }, cookie)
|
|
190
|
+
const list = Array.isArray(j.playlist) ? j.playlist : []
|
|
191
|
+
const mine = list.filter((p) => p && p.creator && String(p.creator.userId) === String(uid))
|
|
192
|
+
const liked = mine.find((p) => /喜欢的音乐$/.test(p.name || '')) || mine[0]
|
|
193
|
+
if (!liked) throw httpError('未找到网易云「我喜欢的音乐」歌单', 502)
|
|
194
|
+
const out = {
|
|
195
|
+
id: String(liked.id),
|
|
196
|
+
name: cleanText(liked.name, 100),
|
|
197
|
+
trackCount: Number(liked.trackCount) || 0,
|
|
198
|
+
trackIds: toTrackIds(liked.trackIds),
|
|
199
|
+
details: new Map(),
|
|
200
|
+
}
|
|
201
|
+
if (out.trackIds.length === 0) {
|
|
202
|
+
const d = await either('获取网易云「我喜欢的音乐」详情', '/api/v6/playlist/detail', { id: out.id, n: '500', s: '0' }, cookie)
|
|
203
|
+
const p = d.playlist && typeof d.playlist === 'object' ? d.playlist : {}
|
|
204
|
+
out.trackIds = toTrackIds(p.trackIds)
|
|
205
|
+
out.trackCount = Number(p.trackCount) || out.trackCount || out.trackIds.length
|
|
206
|
+
for (const s of Array.isArray(p.tracks) ? p.tracks : []) {
|
|
207
|
+
if (s && s.id != null) out.details.set(String(s.id), s)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return out
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** 回退:喜欢列表(无序、无时间) */
|
|
214
|
+
async function likedIds(cookie, uid) {
|
|
215
|
+
const j = await either('获取网易云喜欢列表', '/api/song/like/get', { uid }, cookie)
|
|
216
|
+
const ids = Array.isArray(j.ids) ? j.ids : []
|
|
217
|
+
return { trackIds: ids.map((id) => ({ id: String(id), likedAt: null })), trackCount: Number(j.count) || ids.length }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 歌曲详情:id → { name, ar[], al, dt }(下架歌曲会缺席) */
|
|
221
|
+
async function songDetails(cookie, ids) {
|
|
222
|
+
const out = new Map()
|
|
223
|
+
const chunkSize = 500
|
|
224
|
+
for (let i = 0; i < ids.length; i += chunkSize) {
|
|
225
|
+
const chunk = ids.slice(i, i + chunkSize)
|
|
226
|
+
const j = await either('获取网易云歌曲详情', '/api/v3/song/detail', { c: JSON.stringify(chunk.map((id) => ({ id: Number(id) }))) }, cookie)
|
|
227
|
+
for (const s of Array.isArray(j.songs) ? j.songs : []) out.set(String(s.id), s)
|
|
228
|
+
}
|
|
229
|
+
return out
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** 归一化后的喜欢列表:[{ platformId, title, artist, album, durationMs, likedAt }] */
|
|
233
|
+
async function likedTracks(cookie, uid) {
|
|
234
|
+
let list
|
|
235
|
+
try {
|
|
236
|
+
list = await likedPlaylist(cookie, uid)
|
|
237
|
+
} catch (err) {
|
|
238
|
+
if (err && err.status === 401) throw err
|
|
239
|
+
list = await likedIds(cookie, uid)
|
|
240
|
+
}
|
|
241
|
+
if (list.trackIds.length === 0) return { playlist: list, tracks: [], unavailable: 0 }
|
|
242
|
+
const known = list.details instanceof Map ? list.details : new Map()
|
|
243
|
+
const missing = list.trackIds.map((t) => t.id).filter((id) => !known.has(id))
|
|
244
|
+
const details = missing.length > 0 ? await songDetails(cookie, missing) : new Map()
|
|
245
|
+
for (const [id, song] of known) if (!details.has(id)) details.set(id, song)
|
|
246
|
+
const tracks = []
|
|
247
|
+
for (const t of list.trackIds) {
|
|
248
|
+
const s = details.get(t.id)
|
|
249
|
+
if (!s) continue
|
|
250
|
+
tracks.push({
|
|
251
|
+
platformId: t.id,
|
|
252
|
+
title: cleanText(s.name, 200),
|
|
253
|
+
artist: (Array.isArray(s.ar) ? s.ar : [])
|
|
254
|
+
.map((a) => cleanText(a && a.name, 80))
|
|
255
|
+
.filter(Boolean)
|
|
256
|
+
.join(' / '),
|
|
257
|
+
album: cleanText(s.al && s.al.name, 120) || null,
|
|
258
|
+
durationMs: s.dt ? Number(s.dt) : null,
|
|
259
|
+
likedAt: t.likedAt,
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
return { playlist: list, tracks, unavailable: list.trackIds.length - tracks.length }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return { qrKey, qrPoll, account, likedPlaylist, likedIds, songDetails, likedTracks }
|
|
266
|
+
}
|