fix(bilibili): preserve video pages and CDN playback headers

This commit is contained in:
NoSetViolin committed 2026-11-12 18:07:52 +09:00
1 parent 17008ae52e
commit b7779ab108
5 files changed
+65 -2

No files matched your search

+3
View File
@@ -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) => {
+1 -1
View File
@@ -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}`),
);
+30
View File
@@ -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",
+26
View File
@@ -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)", () => {
+5 -1
View File
@@ -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