mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 19:32:51 +08:00
Merge remote-tracking branch 'origin/main' into review/185-hardening
# Conflicts: # src/music/netease.ts
This commit is contained in:
commit
ea05787cb7
25 files changed
+1592
-34
No files matched your search
+28
-2
@@ -1,3 +1,4 @@
|
||||
import { normalizeCookie } from "./cookie-normalize.js";
|
||||
import axios, { type AxiosInstance } from "axios";
|
||||
import { RISK_CONTROL_CODE, RiskControlError, SongUrlUnavailable } from "./provider.js";
|
||||
import type { PlayFailure } from "./provider.js";
|
||||
@@ -204,6 +205,29 @@ export const NETEASE_QUALITY_LEVELS = [
|
||||
{ value: "jymaster", label: "超清母带", bitrate: 4000 },
|
||||
] as const;
|
||||
|
||||
function getPlaybackUserAgent(cookie: string): string {
|
||||
let decoded = cookie;
|
||||
try {
|
||||
decoded = decodeURIComponent(cookie);
|
||||
} catch {
|
||||
// A malformed cookie escape must not prevent a playback request.
|
||||
}
|
||||
// The sidecar's cookie parser keeps the last value of a repeated key.
|
||||
const platform = [...decoded.matchAll(/(?:^|;)\s*os\s*=\s*([^;=]*)(?=;|$)/g)].at(-1)?.[1].trim();
|
||||
// NCM API 4.32.0 defaults cookie profiles to PC but eapi's UA to iPhone.
|
||||
// Match its client profiles here, and scope the override to playback only.
|
||||
switch (platform) {
|
||||
case "android":
|
||||
return "NeteaseMusic/9.1.65.240927161425(9001065);Dalvik/2.1.0 (Linux; U; Android 14; 23013RK75C Build/UKQ1.230804.001)";
|
||||
case "iphone":
|
||||
return "NeteaseMusic 9.0.90/5038 (iPhone; iOS 16.2; zh_CN)";
|
||||
case "linux":
|
||||
return "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36";
|
||||
default:
|
||||
return "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36 Chrome/91.0.4472.164 NeteaseMusicDesktop/3.0.18.203152";
|
||||
}
|
||||
}
|
||||
|
||||
export class NeteaseProvider implements MusicProvider {
|
||||
readonly platform = "netease" as const;
|
||||
private api: AxiosInstance;
|
||||
@@ -280,7 +304,7 @@ export class NeteaseProvider implements MusicProvider {
|
||||
let res;
|
||||
try {
|
||||
res = await this.api.get("/song/url/v1", {
|
||||
params: { id: songId, level, ...this.cookieParams },
|
||||
params: { id: songId, level, ua: getPlaybackUserAgent(this.cookie), ...this.cookieParams },
|
||||
});
|
||||
} catch (err) {
|
||||
// -460 arrives as HTTP 400, and left as-is it looks like "this track is
|
||||
@@ -503,7 +527,9 @@ export class NeteaseProvider implements MusicProvider {
|
||||
}
|
||||
|
||||
setCookie(cookie: string): void {
|
||||
this.cookie = cookie;
|
||||
// Normalised so a pasted `Set-Cookie` line cannot put Max-Age/Expires/Path
|
||||
// into the query string — that is what made the sidecar answer 400.
|
||||
this.cookie = normalizeCookie(cookie);
|
||||
}
|
||||
|
||||
getCookie(): string {
|
||||
|
||||
Reference in new issue
Block a user