mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
fix(netease): support configurable realIP and align playback User-Agent (#190)
* Add configurable NetEase real IP in web settings * Match NetEase playback User-Agent to cookie platform * fix(netease): keep playback UA aligned with cookie profile --------- Co-authored-by: TIANYAO ZHANG <88520881+ZHANGTIANYAO1@users.noreply.github.com>
This commit is contained in:
1 parent
2b657ae338
commit
2792a86a6c
2 files changed
+84
-1
No files matched your search
@@ -1,3 +1,6 @@
|
||||
import { once } from "node:events";
|
||||
import { createServer } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import {
|
||||
parseLyrics,
|
||||
@@ -8,6 +11,63 @@ import {
|
||||
NeteaseProvider,
|
||||
} from "./netease.js";
|
||||
|
||||
describe("NeteaseProvider playback user agent", () => {
|
||||
it("matches the playback UA to each account's platform without changing cookies or other requests", async () => {
|
||||
const requests: URL[] = [];
|
||||
const server = createServer((req, res) => {
|
||||
requests.push(new URL(req.url!, "http://localhost"));
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(JSON.stringify({ code: 200, data: [{ url: "https://example.com/song.mp3" }] }));
|
||||
});
|
||||
server.listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
try {
|
||||
const provider = new NeteaseProvider(
|
||||
`http://127.0.0.1:${(server.address() as AddressInfo).port}`,
|
||||
);
|
||||
const cases = [
|
||||
["", "NeteaseMusicDesktop"],
|
||||
["MUSIC_U=shared", "NeteaseMusicDesktop"],
|
||||
["MUSIC_U=shared; os=pc", "NeteaseMusicDesktop"],
|
||||
["MUSIC_U=shared; os=android", "Android"],
|
||||
["MUSIC_U=shared; os=android; os=iphone", "iPhone"],
|
||||
["MUSIC_U=shared; os=iphone", "iPhone"],
|
||||
["MUSIC_U=shared; os=linux", "Linux"],
|
||||
["MUSIC_U=shared; os=unknown", "NeteaseMusicDesktop"],
|
||||
["MUSIC_U=shared; os=%69phone", "iPhone"],
|
||||
["MUSIC_U=shared; os=%invalid", "NeteaseMusicDesktop"],
|
||||
];
|
||||
for (const [cookie, uaFamily] of cases) {
|
||||
provider.setCookie(cookie);
|
||||
expect(await provider.getSongUrl("1", "standard")).toMatchObject({ url: "https://example.com/song.mp3" });
|
||||
const params = requests.at(-1)!.searchParams;
|
||||
expect(params.get("ua")).toContain(uaFamily);
|
||||
expect(params.get("level")).toBe("standard");
|
||||
expect(params.get("cookie")).toBe(cookie || null);
|
||||
expect(provider.getCookie()).toBe(cookie);
|
||||
}
|
||||
|
||||
provider.setCookie("MUSIC_U=shared; os=pc");
|
||||
const personal = provider.withCookie("MUSIC_U=personal; os=android");
|
||||
await personal.getSongUrl("2");
|
||||
expect(requests.at(-1)!.searchParams.get("ua")).toContain("Android");
|
||||
expect(requests.at(-1)!.searchParams.get("cookie")).toBe("MUSIC_U=personal; os=android");
|
||||
await provider.getSongUrl("3");
|
||||
expect(requests.at(-1)!.searchParams.get("ua")).toContain("NeteaseMusicDesktop");
|
||||
expect(provider.getCookie()).toBe("MUSIC_U=shared; os=pc");
|
||||
|
||||
requests.length = 0;
|
||||
await provider.search("hello");
|
||||
await provider.getQrCode();
|
||||
expect(requests).toHaveLength(6);
|
||||
expect(requests.every((url) => !url.searchParams.has("ua"))).toBe(true);
|
||||
} finally {
|
||||
server.closeAllConnections();
|
||||
await new Promise<void>((resolve, reject) => server.close((err) => err ? reject(err) : resolve()));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("NetEase adapter", () => {
|
||||
it("parses LRC format lyrics", () => {
|
||||
const lrc = `[00:00.00] 作词 : 周杰伦
|
||||
|
||||
+24
-1
@@ -125,6 +125,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;
|
||||
@@ -199,7 +222,7 @@ export class NeteaseProvider implements MusicProvider {
|
||||
async getSongUrl(songId: string, quality?: string): Promise<SongUrlResult | null> {
|
||||
const level = quality ?? this.quality;
|
||||
const res = await this.api.get("/song/url/v1", {
|
||||
params: { id: songId, level, ...this.cookieParams },
|
||||
params: { id: songId, level, ua: getPlaybackUserAgent(this.cookie), ...this.cookieParams },
|
||||
});
|
||||
const item = res.data?.data?.[0];
|
||||
const url = item?.url;
|
||||
|
||||
Reference in new issue
Block a user