From ba645e5ccb190168dbe9c62060b10dff63e19a62 Mon Sep 17 00:00:00 2001 From: zbn297427669 <17957946+zbn297427669@users.noreply.github.com> Date: Mon, 2 Nov 2026 15:53:06 +0800 Subject: [PATCH 1/4] test: catch bot tests up with the de-click transport ramp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upstream a0f0b49..6f165b5 added the pause/resume fade tail but shipped no test updates, leaving 5 assertions red (also reproducible on pristine upstream/main, so not merge-induced). - instance-recovery/instance harnesses: stub players borrow AudioPlayer.prototype.pause/resume, which now call this.setTransportRamp(). Give the stubs that method or they throw TypeError on pause/resume. - voice-failure: pausing deliberately emits a fade tail, so the frame count no longer freezes at exactly 50. Assert the de-click fade completes, then that the scheduler and elapsed clock are genuinely frozen — the actual behavior under test. No production code changed. Co-Authored-By: Claude Code --- src/bot/instance-recovery.test.ts | 4 ++++ src/bot/instance.test.ts | 4 ++++ src/bot/voice-failure.test.ts | 12 ++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/bot/instance-recovery.test.ts b/src/bot/instance-recovery.test.ts index 6f57aec..b556b7d 100644 --- a/src/bot/instance-recovery.test.ts +++ b/src/bot/instance-recovery.test.ts @@ -29,6 +29,10 @@ function makeBot() { player.getVolume = () => 50; player.pause = AudioPlayer.prototype.pause; player.resume = AudioPlayer.prototype.resume; + // The real pause()/resume() drive the de-click transport ramp + // (a0f0b49..6f165b5). A stub player must expose it too, or the prototype + // methods throw "setTransportRamp is not a function". + player.setTransportRamp = vi.fn(); player.play = vi.fn((_url: string, position: number) => { player.sessionId++; player.elapsed = position; player.state = "playing"; }); player.stop = vi.fn(() => { player.sessionId++; player.state = "idle"; }); const provider = { getSongUrl: vi.fn(async (): Promise<{ url: string } | null> => ({ url: "fresh" })) }; diff --git a/src/bot/instance.test.ts b/src/bot/instance.test.ts index 685a1e7..eaffd8c 100644 --- a/src/bot/instance.test.ts +++ b/src/bot/instance.test.ts @@ -1780,6 +1780,10 @@ describe("BotInstance trackEnd — stale playback sessions", () => { player.getPlaybackSessionId = AudioPlayer.prototype.getPlaybackSessionId; player.pause = AudioPlayer.prototype.pause; player.resume = AudioPlayer.prototype.resume; + // The real pause()/resume() drive the de-click transport ramp + // (a0f0b49..6f165b5). A stub player must expose it too, or the prototype + // methods throw "setTransportRamp is not a function". + player.setTransportRamp = vi.fn(); player.play = vi.fn(() => { player.sessionId++; player.state = "playing"; }); const provider = { getSongUrl: vi.fn(async () => ({ url: "fresh" })) }; const advances: string[] = []; diff --git a/src/bot/voice-failure.test.ts b/src/bot/voice-failure.test.ts index a81bd9f..794ea8a 100644 --- a/src/bot/voice-failure.test.ts +++ b/src/bot/voice-failure.test.ts @@ -65,9 +65,17 @@ describe("BotInstance persistent voice send failure", () => { expect(frames).toBe(50); h.tsClient.emit("voiceSendFailed", { consecutiveFailures: 100, durationMs: 2000 }); vi.advanceTimersByTime(3000); - expect(frames).toBe(50); - expect(h.player.getElapsed()).toBe(1); + // Pausing emits a short de-click fade tail (pauseFrames) so the last + // audible frames ramp down instead of cutting off (a0f0b49). What this + // test guards is what happens AFTER that: the scheduler must go quiet + // and elapsed must stop advancing. expect(h.player.getState()).toBe("paused"); + const framesAfterFade = frames; + const elapsedAfterFade = h.player.getElapsed(); + expect(framesAfterFade).toBeLessThan(50 + 20); + vi.advanceTimersByTime(3000); + expect(frames).toBe(framesAfterFade); + expect(h.player.getElapsed()).toBe(elapsedAfterFade); } finally { h.player.stop(); } }); From b7779ab108b45b768eabbc451d15a8e44fd1e93d Mon Sep 17 00:00:00 2001 From: NoSetViolin Date: Thu, 12 Nov 2026 17:41:30 +0900 Subject: [PATCH 2/4] fix(bilibili): preserve video pages and CDN playback headers --- src/audio/player.test.ts | 3 +++ src/audio/player.ts | 2 +- src/bot/instance.test.ts | 30 ++++++++++++++++++++++++++++++ src/bot/song-ref.test.ts | 26 ++++++++++++++++++++++++++ src/bot/song-ref.ts | 6 +++++- 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/audio/player.test.ts b/src/audio/player.test.ts index 74531f3..9763c5d 100644 --- a/src/audio/player.test.ts +++ b/src/audio/player.test.ts @@ -50,6 +50,7 @@ describe("buildFfmpegArgs", () => { "https://szbdyd.com/audio.m4s", "https://stream.mcdn.szbdyd.com/audio.m4s", "https://xy219x131x72x38xy.mcdn.bilivideo.cn.szbdyd.com/audio.m4s", + "https://b-bacb4fc51cs41d5p0dxdau053w.edge.mountaintoys.cn/audio.m4s", "HTTPS://UPOS-SZ-MIRRORCOS.BILIVIDEO.COM:443/audio.m4s", "https://upos-sz-mirrorcos.bilivideo.com./audio.m4s", ])("uses Bilibili headers and input-side seeking for the actual CDN host in %s", (url) => { @@ -70,6 +71,8 @@ describe("buildFfmpegArgs", () => { "https://bilibili.com.evil.example/audio.m4s", "https://evilbilibili.com/audio.m4s", "https://szbdyd.com.evil.example/audio.m4s", + "https://mountaintoys.cn.evil.example/audio.m4s", + "https://evilmountaintoys.cn/audio.m4s", "https://evil.example/audio.m4s?redirect=https://upos-sz-mirrorcos.bilivideo.com/x", "https://bilibili.com@evil.example/audio.m4s", ])("does not trust Bilibili text outside an allowed hostname in %s", (url) => { diff --git a/src/audio/player.ts b/src/audio/player.ts index c7d29ba..748c542 100644 --- a/src/audio/player.ts +++ b/src/audio/player.ts @@ -98,7 +98,7 @@ export function buildFfmpegArgs(url: string, seekSeconds: number): string[] { } const isHttp = httpHostname !== null; const isBilibili = httpHostname !== null && - ["bilivideo.com", "bilivideo.cn", "bilibili.com", "szbdyd.com"].some( + ["bilivideo.com", "bilivideo.cn", "bilibili.com", "szbdyd.com", "mountaintoys.cn"].some( domain => httpHostname === domain || httpHostname.endsWith(`.${domain}`), ); diff --git a/src/bot/instance.test.ts b/src/bot/instance.test.ts index 685a1e7..020e7fb 100644 --- a/src/bot/instance.test.ts +++ b/src/bot/instance.test.ts @@ -1544,6 +1544,36 @@ describe("BotInstance live-queue persistence (#119)", () => { }); describe("BotInstance Bilibili multi-P resolution", () => { + it("passes the page from a TeamSpeak video link to the Bilibili provider", async () => { + const song = { + id: "BV1yxHQeYEuE?p=2", + name: "Video - P2", + artist: "Uploader", + album: "", + duration: 180, + coverUrl: "", + platform: "bilibili" as const, + }; + const provider = { platform: "bilibili", getSongDetail: vi.fn().mockResolvedValue(song) }; + const ctx = { + config: { commandPrefix: "!" }, + assertProviderEnabled: vi.fn(), + getProviderFor: vi.fn().mockReturnValue(provider), + }; + const args = "[URL]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2[/URL]"; + + const result = await (BotInstance.prototype as any).resolvePlayQuery.call(ctx, { + name: "play", + args, + rawArgs: [args], + flags: new Set(), + }); + + expect(ctx.getProviderFor).toHaveBeenCalledWith("bilibili"); + expect(provider.getSongDetail).toHaveBeenCalledWith("BV1yxHQeYEuE?p=2"); + expect(result.song).toEqual(song); + }); + it("resolves multi-P search result to P1 with accurate duration and name", async () => { const multiPSongDetail = { id: "BV1multiP?p=1", diff --git a/src/bot/song-ref.test.ts b/src/bot/song-ref.test.ts index d6e23f9..540183a 100644 --- a/src/bot/song-ref.test.ts +++ b/src/bot/song-ref.test.ts @@ -101,6 +101,32 @@ describe("parseSongRef (#90 exact-song selection)", () => { expect(parseSongRef("https://www.bilibili.com/video/BV1yxHQeYEuE")).toEqual({ id: "BV1yxHQeYEuE", platform: "bilibili" }); expect(parseSongRef("https://b23.tv/BV1yxHQeYEuE")).toEqual({ id: "BV1yxHQeYEuE", platform: "bilibili" }); }); + + it.each([ + "https://www.bilibili.com/video/BV1yxHQeYEuE?p=2", + "https://b23.tv/BV1yxHQeYEuE?p=2", + "https://www.bilibili.com/video/BV1yxHQeYEuE/?from=share&p=2&vd_source=test#reply", + "https://www.bilibili.com/video/BV1yxHQeYEuE/?from=share?p=3&p=2", + "https://www.bilibili.com/video/BV1yxHQeYEuE/?p=%32", + "[URL]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2[/URL]", + "[url]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2#reply[/url]", + ])("preserves the Bilibili page query in %s", (input) => { + expect(parseSongRef(input)).toEqual({ id: "BV1yxHQeYEuE?p=2", platform: "bilibili" }); + }); + + it.each([ + "?p=2extra", + "?p=-2", + "?page=2", + "?sp=2", + "?from=share?p=2", + "?from=share%3Fp%3D2", + "?from=share#reply&p=2", + "#reply?p=2", + ])("ignores an invalid or unrelated Bilibili page query %s", (query) => { + expect(parseSongRef(`https://www.bilibili.com/video/BV1yxHQeYEuE${query}`)) + .toEqual({ id: "BV1yxHQeYEuE", platform: "bilibili" }); + }); }); describe("parseSelectionIndex (#90 pick from last search)", () => { diff --git a/src/bot/song-ref.ts b/src/bot/song-ref.ts index 6578067..afdf6e1 100644 --- a/src/bot/song-ref.ts +++ b/src/bot/song-ref.ts @@ -69,7 +69,11 @@ export function parseSongRef(raw: string): SongRef | null { // a "BV..." token never collides with them). const bv = /BV[0-9A-Za-z]{8,12}/.exec(q); if (bv && (/^BV[0-9A-Za-z]{8,12}$/.test(q) || /bilibili\.com|b23\.tv/i.test(q))) { - return { id: bv[0], platform: "bilibili" }; + // TeamSpeak wraps pasted links in [URL] tags. Keep the page query while + // ignoring fragments, which can contain unrelated player state. + const query = /\?(.*)$/.exec(stripUrlBBCode(q).trim().split("#", 1)[0])?.[1]; + const page = new URLSearchParams(query).get("p"); + return { id: page && /^\d+$/.test(page) ? `${bv[0]}?p=${page}` : bv[0], platform: "bilibili" }; } // NetEase song URL. Only treat `id=N` as a SONG id when the URL is not a From 79e9cd797c38f1aceca472d886231ea49dde392d Mon Sep 17 00:00:00 2001 From: TIANYAO ZHANG <88520881+ZHANGTIANYAO1@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:40:27 +0800 Subject: [PATCH 3/4] fix(bilibili): read the page query from the pasted video link --- src/bot/song-ref.test.ts | 2 ++ src/bot/song-ref.ts | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bot/song-ref.test.ts b/src/bot/song-ref.test.ts index 540183a..64fb568 100644 --- a/src/bot/song-ref.test.ts +++ b/src/bot/song-ref.test.ts @@ -110,6 +110,8 @@ describe("parseSongRef (#90 exact-song selection)", () => { "https://www.bilibili.com/video/BV1yxHQeYEuE/?p=%32", "[URL]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2[/URL]", "[url]https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2#reply[/url]", + "[URL=https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2]点击播放[/URL]", + "参考 https://example.com/?p=9 视频 https://www.bilibili.com/video/BV1yxHQeYEuE/?p=2 请播放", ])("preserves the Bilibili page query in %s", (input) => { expect(parseSongRef(input)).toEqual({ id: "BV1yxHQeYEuE?p=2", platform: "bilibili" }); }); diff --git a/src/bot/song-ref.ts b/src/bot/song-ref.ts index afdf6e1..ba1d1aa 100644 --- a/src/bot/song-ref.ts +++ b/src/bot/song-ref.ts @@ -69,9 +69,9 @@ export function parseSongRef(raw: string): SongRef | null { // a "BV..." token never collides with them). const bv = /BV[0-9A-Za-z]{8,12}/.exec(q); if (bv && (/^BV[0-9A-Za-z]{8,12}$/.test(q) || /bilibili\.com|b23\.tv/i.test(q))) { - // TeamSpeak wraps pasted links in [URL] tags. Keep the page query while - // ignoring fragments, which can contain unrelated player state. - const query = /\?(.*)$/.exec(stripUrlBBCode(q).trim().split("#", 1)[0])?.[1]; + // Extract the query attached to this BV id, including in [URL=...] tags. + const suffix = q.slice(bv.index + bv[0].length); + const query = /^\/?\?([^#\s[\]]*)/.exec(suffix)?.[1]; const page = new URLSearchParams(query).get("p"); return { id: page && /^\d+$/.test(page) ? `${bv[0]}?p=${page}` : bv[0], platform: "bilibili" }; } From 2792a86a6c8f609693d766d8ee57435e22821308 Mon Sep 17 00:00:00 2001 From: Yiling <2184704602@qq.com> Date: Wed, 18 Nov 2026 16:49:36 +0800 Subject: [PATCH 4/4] 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> --- src/music/netease.test.ts | 60 +++++++++++++++++++++++++++++++++++++++ src/music/netease.ts | 25 +++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/music/netease.test.ts b/src/music/netease.test.ts index 4e8e961..b3c220d 100644 --- a/src/music/netease.test.ts +++ b/src/music/netease.test.ts @@ -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((resolve, reject) => server.close((err) => err ? reject(err) : resolve())); + } + }); +}); + describe("NetEase adapter", () => { it("parses LRC format lyrics", () => { const lrc = `[00:00.00] 作词 : 周杰伦 diff --git a/src/music/netease.ts b/src/music/netease.ts index 541735e..de57f32 100644 --- a/src/music/netease.ts +++ b/src/music/netease.ts @@ -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 { 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;