审查明细见 CODE-AUDIT.md。本提交覆盖其中的代码级修复:
P0-2 migu 解密流异常逃逸 → 进程崩溃
src/http/migu-stream.ts 把 transform/onChunk 就地 try/catch,转 reject 并 res.destroy()。
为什么:二者在 res 的 data 事件回调里同步执行,抛出的异常既不在 Promise 链上、
也不在路由层 try/catch 内,会逃逸成 uncaughtException(全项目无全局处理器)直接杀进程。
已用最小复现验证:修复前 exit 9,修复后正常 reject。
P0-1 /login/lr 缺失 → lr 取流 100% 不可用
src/routes-login.ts 补 POST/DELETE /login/lr。
为什么:sdk/lr/auth.ts 早已实现 login、该文件也已 import lrAuth,只差路由注册;
lr 取流强制要求登录态,缺此入口等于整个平台不可用,而 README 已将其文档化。
P0-3 + P1-7 凭据存储
sdk/auth/store.ts 写入改 0600 + 「临时文件 + rename」原子替换;解析失败改为改名备份而非静默清空;
新增 DATA_DIR 环境变量(测试隔离用)。
为什么:原先直接覆写明文 JSON(0644)且解析失败静默置空,写入中途被杀会留下截断 JSON,
下次启动解析失败后,任意一次 setCredentials 都会以空表覆盖,导致全部平台凭据丢失。
P1-9 / P1-10 / P1-11 / P1-12 平台模块缺陷
sdk/kg/krcDecode.ts:空歌词显式 resolve('')(原先裸 return 让 Promise 永久 pending,请求挂死)
sdk/kg/lyric.ts:重试改调 getLyricDownload(原先误调 getLyric,参数语义不同导致重试恒失败)
sdk/kw/songList.ts:重试改自递归(原先误调 getListDetail,返回类型错位且把 tryNum 当 page)
sdk/kw/songList.ts:删除 URL 模板中多余的右花括号
P1-6 /song-list 参数契约
src/routes.ts 移除不生效的 limit 声明。
为什么:各平台 getList 签名不一致(如 kw 为 (sortId, tagId, page, tryNum)),无法统一透传,
保留声明只会让文档与实现不符。
P1-4/5/13/15 冒烟测试加固
test/smoke-test.ts:子实例统一登记 + 进程退出回收(原先异常路径留下孤儿实例,曾累积 5 个);
启动超时 10s→30s 且失败时附子进程输出、提前退出立即报错;显式 SOURCE_ENABLED='' 与 DATA_DIR
隔离(原先继承 .env,取流用例走外部音源掩盖 SDK 回归,且 /login/refresh 会改写真实 data/auth.json);
AUTH 断言移入 test()(原先裸抛错会跳过实例回收);修正 642 行过期断言并补 kw parse 正向用例。
P1-8 src/auth/store.ts 删除引用已不存在命令 `pnpm build` 的过期注释。
src/server.ts 头注释的接口清单改为 path 风格,与实际路由一致。
注:src/routes.ts、src/routes-login.ts、src/server.ts、test/smoke-test.ts 同时承载了工作区
既有的在途改动(parse/playlist/album 路由接入与相应用例),一并随本提交入库。
149 lines
5.1 KiB
TypeScript
149 lines
5.1 KiB
TypeScript
// 登录态存储 —— 各平台凭据持久化(data/auth.json,不入库)
|
||
// 结构:{ wy: {...}, kg: {...}, tx: {...}, _kg_device: {...}, _qs_device: {...}, _qs_passport_device: {...} }
|
||
// 凭据是明文敏感数据:文件以 0600 创建;写入走「同目录临时文件 + rename」原子替换,
|
||
// 避免写中途崩溃留下截断 JSON(那会导致下次启动解析失败而丢掉全部平台凭据)。
|
||
// DATA_DIR 环境变量可覆盖存储目录(测试用,避免污染真实登录态)。
|
||
|
||
import { existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'
|
||
import { dirname, join } from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
const DATA_DIR = process.env.DATA_DIR
|
||
? process.env.DATA_DIR
|
||
: join(dirname(fileURLToPath(import.meta.url)), '../../data')
|
||
const DATA_FILE = join(DATA_DIR, 'auth.json')
|
||
|
||
export interface WyCredentials {
|
||
cookie: string
|
||
musicU: string
|
||
userid?: number
|
||
nickname?: string | null
|
||
avatar?: string | null
|
||
vipType?: number | null
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface KgCredentials {
|
||
token: string
|
||
userid: number | string
|
||
nickname?: string | null
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface KgDevice {
|
||
guid: string
|
||
mid: string
|
||
dfid: string
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface TxCredentials {
|
||
cookie: string
|
||
// App 抓包的完整凭据(可选):提供后可自动续期(music.login.LoginServer),
|
||
// 仅浏览器 cookie 时 qm_keyst 无法主动续期,只能做 VIP 检测
|
||
openId?: string
|
||
accessToken?: string
|
||
refreshToken?: string
|
||
refreshKey?: string
|
||
uin?: string
|
||
vipType?: 'svip' | 'vip' | 'normal' | null
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface XmCredentials {
|
||
// www.ximalaya.com 登录 Cookie(须含 1&_token,从已登录浏览器请求头复制)
|
||
// baseInfo 付费内容授权依赖它;无法主动续期,失效后需重新复制绑定
|
||
cookie: string
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface MeCredentials {
|
||
// App 登录 Cookie(须含 token=,从猫耳 App 抓包或已登录 m.missevan.com 浏览器复制)
|
||
// App API 付费声音授权依赖它;无法主动续期
|
||
cookie: string
|
||
updatedAt?: string
|
||
}
|
||
|
||
export interface LrCredentials {
|
||
// m.lrts.me 登录会话 Cookie(/login/lr 账号密码登录后由服务端保存)
|
||
// 取流接口必须携带(免费章节也要求登录态)
|
||
cookie: string
|
||
phone?: string
|
||
updatedAt?: string
|
||
}
|
||
|
||
let cache: Record<string, any> = {}
|
||
let loaded = false
|
||
|
||
const load = (): Record<string, any> => {
|
||
if (loaded) return cache
|
||
if (!existsSync(DATA_FILE)) {
|
||
cache = {}
|
||
loaded = true
|
||
return cache
|
||
}
|
||
try {
|
||
cache = JSON.parse(readFileSync(DATA_FILE, 'utf8'))
|
||
} catch (err) {
|
||
// 解析失败绝不能静默清空:先改名备份,让残留凭据仍可人工抢救;
|
||
// 否则下一次 setCredentials 就会用空表覆盖掉本可恢复的文件。
|
||
const backup = `${DATA_FILE}.corrupt-${Date.now()}`
|
||
try {
|
||
renameSync(DATA_FILE, backup)
|
||
console.error(`[auth/store] ${DATA_FILE} 解析失败,已备份为 ${backup},本次以空凭据启动: ${(err as Error).message}`)
|
||
} catch {
|
||
console.error(`[auth/store] ${DATA_FILE} 解析失败且备份未成功,本次以空凭据启动: ${(err as Error).message}`)
|
||
}
|
||
cache = {}
|
||
}
|
||
loaded = true
|
||
return cache
|
||
}
|
||
|
||
const save = (data: Record<string, any>): void => {
|
||
cache = data
|
||
loaded = true
|
||
mkdirSync(dirname(DATA_FILE), { recursive: true })
|
||
// 原子写:先落同目录临时文件(0600,仅属主可读),再 rename 覆盖。
|
||
// 直接覆写会在进程中途被杀时留下截断 JSON —— 那正是「凭据全丢」的触发路径。
|
||
const tmp = `${DATA_FILE}.${process.pid}.tmp`
|
||
try {
|
||
writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 0o600 })
|
||
renameSync(tmp, DATA_FILE)
|
||
} catch (err) {
|
||
try { unlinkSync(tmp) } catch { /* 清理失败不掩盖原始错误 */ }
|
||
throw err
|
||
}
|
||
}
|
||
|
||
export const getCredentials = (platform: string): any => load()[platform] ?? null
|
||
|
||
export const setCredentials = (platform: string, credentials: Record<string, any>): void => {
|
||
const data = load()
|
||
data[platform] = { ...data[platform], ...credentials, updatedAt: new Date().toISOString() }
|
||
save(data)
|
||
}
|
||
|
||
export const clearCredentials = (platform: string): void => {
|
||
const data = load()
|
||
delete data[platform]
|
||
save(data)
|
||
}
|
||
|
||
/** wy 登录 cookie 字符串(未登录返回空串) */
|
||
export const getWyCookie = (): string => (getCredentials('wy') as WyCredentials | null)?.cookie || ''
|
||
|
||
/** kg 登录态(未登录返回 null) */
|
||
export const getKgAuth = (): { token: string; userid: number | string } | null => {
|
||
const cred = getCredentials('kg') as KgCredentials | null
|
||
return cred?.token ? { token: cred.token, userid: cred.userid || 0 } : null
|
||
}
|
||
|
||
/** tx(QQ) 登录态:cookie 与其中的 uin(未登录返回 null) */
|
||
export const getTxAuth = (): { cookie: string; uin: string } | null => {
|
||
const cred = getCredentials('tx') as TxCredentials | null
|
||
if (!cred?.cookie) return null
|
||
const uin = cred.cookie.match(/uin=(\d+)/)?.[1] ?? '0'
|
||
return { cookie: cred.cookie, uin }
|
||
}
|