diff --git a/config_example.json b/config_example.json index e65e592..49af163 100644 --- a/config_example.json +++ b/config_example.json @@ -50,7 +50,9 @@ "bind": "127.0.0.1", "port": 0, "advertiseHost": null, - "Comment": "Bilibili video playback (%play https://www.bilibili.com/video/BV... , b23.tv short links and ?p= parts included) — fully anonymous, no cookies needed. The bot talks to Bilibili's WBI-signed web API (survives the HTTP 412 risk-control blocks that hit plain API calls on many hosting IPs), prefers anonymous DASH audio (~130-170 kbps) and falls back to the html5 progressive mp4 for videos without DASH audio. Bilibili's CDN only serves audio to requests with a bilibili Referer, so the bot runs a tiny local HTTP proxy that adds those headers and hands a signed URL to your NodeLink/Lavalink node. Defaults work when the node runs on the same host as the bot (nodelink.host = localhost). bind/port: what the proxy listens on (port 0 = pick a free port). advertiseHost: the host your audio node should use to reach the proxy — set it (e.g. your bot's LAN IP or Docker service name, and bind 0.0.0.0) only when the node runs on a different machine. buvid3/buvid4/b_nut device cookies are generated and managed automatically." + "remoteProxy": null, + "remoteSecret": null, + "Comment": "Bilibili video playback (%play https://www.bilibili.com/video/BV... , b23.tv short links and ?p= parts included) — fully anonymous, no cookies needed. The bot talks to Bilibili's WBI-signed web API (survives the HTTP 412 risk-control blocks that hit plain API calls on many hosting IPs), prefers anonymous DASH audio (~130-170 kbps) and falls back to the html5 progressive mp4 for videos without DASH audio. Bilibili's CDN only serves audio to requests with a bilibili Referer, so the bot runs a tiny local HTTP proxy that adds those headers and hands a signed URL to your NodeLink/Lavalink node. Defaults work when the node runs on the same host as the bot (nodelink.host = localhost). bind/port: what the proxy listens on (port 0 = pick a free port). advertiseHost: the host your audio node should use to reach the proxy — set it (e.g. your bot's LAN IP or Docker service name, and bind 0.0.0.0) only when the node runs on a different machine. buvid3/buvid4/b_nut device cookies are generated and managed automatically. remoteProxy/remoteSecret: for split deployments where the audio node cannot reach the bot host — deploy the header-proxy as a Cloudflare Worker (download/bilibili-proxy-worker.js), then set remoteProxy = https://.workers.dev and remoteSecret = the worker's SECRET variable. When both are set, signed URLs point at the remote proxy and the local proxy never starts (audio flows Bilibili CDN -> worker -> node; the bot needs no inbound ports)." }, "comment-radio": { "message": "If you have radio stations you'd like to connect, add them to the array below. Radio stations must be rendered like following:", diff --git a/src/core/Bot.mjs b/src/core/Bot.mjs index a29643d..a01bf30 100644 --- a/src/core/Bot.mjs +++ b/src/core/Bot.mjs @@ -121,6 +121,8 @@ class Remix { bind: config.bilibili?.bind ?? "127.0.0.1", port: config.bilibili?.port ?? 0, advertiseHost: config.bilibili?.advertiseHost ?? null, + remoteProxy: config.bilibili?.remoteProxy ?? null, + remoteSecret: config.bilibili?.remoteSecret ?? null, }); this.locale = new Locale(typeof config.prefix === "string" && config.prefix ? config.prefix : "%"); diff --git a/src/music/bilibili/BilibiliProxy.mjs b/src/music/bilibili/BilibiliProxy.mjs index f14d9f7..a790771 100644 --- a/src/music/bilibili/BilibiliProxy.mjs +++ b/src/music/bilibili/BilibiliProxy.mjs @@ -41,8 +41,22 @@ const _cfg = { bind: "127.0.0.1", port: 0, advertiseHost: null, + remoteProxy: null, + remoteSecret: null, }; +/** + * Whether signed proxy URLs point at a remote HTTPS header-proxy (e.g. a + * Cloudflare Worker) instead of the in-process HTTP proxy. Remote mode is + * for split deployments where the audio node cannot reach the bot host — + * the node then fetches the public proxy URL directly and the bot never + * touches the audio bytes. + * @returns {boolean} + */ +function remoteMode() { + return Boolean(_cfg.remoteProxy && _cfg.remoteSecret); +} + /** @type {http.Server|null} @description The lazily-started proxy server (unref'd). */ let _server = null; /** @type {Buffer|null} @description Per-process HMAC signing key. */ @@ -59,12 +73,37 @@ let _starting = null; * @param {string} [section.advertiseHost] - Host the audio node should use * to reach the proxy (defaults to the bind address; set when the node runs * on another machine/container). + * @param {string} [section.remoteProxy] - Base URL of a remote HTTPS + * header-proxy (e.g. a Cloudflare Worker deployed from + * download/bilibili-proxy-worker.js). When set — together with + * remoteSecret — signed URLs point there and the local proxy never + * starts. For deployments where the node cannot reach the bot host. + * @param {string} [section.remoteSecret] - Shared HMAC secret the remote + * proxy verifies signatures with (required with remoteProxy). */ export function configureBilibiliProxy(section = {}) { if (section.bind != null) _cfg.bind = String(section.bind); if (section.port != null) _cfg.port = Number(section.port) || 0; if (section.advertiseHost != null) _cfg.advertiseHost = String(section.advertiseHost); if (section.enabled != null) _cfg.enabled = section.enabled !== false; + if (section.remoteProxy != null) { + const base = String(section.remoteProxy).trim().replace(/\/+$/, ""); + _cfg.remoteProxy = base || null; + } + if (section.remoteSecret != null) { + const secret = String(section.remoteSecret); + _cfg.remoteSecret = secret.length > 0 ? secret : null; + } + if (_cfg.remoteProxy && !_cfg.remoteSecret) { + logger.warn("[BilibiliProxy] bilibili.remoteProxy is set but bilibili.remoteSecret is missing — " + + "falling back to the local proxy (the remote proxy cannot verify signed URLs without the shared secret)."); + _cfg.remoteProxy = null; + } + // Remote mode signs with a key derived from the stable shared secret so + // the remote proxy can verify URLs signed by any bot restart. + _key = remoteMode() + ? crypto.createHash("sha256").update(_cfg.remoteSecret).digest() + : null; } /** @@ -87,9 +126,12 @@ function advertiseHost() { /** * Start the proxy server (once per process) and return its listening info. + * In remote mode the local server never starts — the remote proxy is + * reported instead. * @returns {Promise<{port: number, host: string}>} */ export function ensureBilibiliProxy() { + if (remoteMode()) return Promise.resolve({ port: 0, host: _cfg.remoteProxy }); if (_starting) return _starting; _starting = new Promise((resolve, reject) => { try { @@ -127,10 +169,12 @@ export function ensureBilibiliProxy() { } /** - * Current proxy info (without starting it). + * Current proxy info (without starting it). Remote mode reports the remote + * proxy base instead of a local host:port. * @returns {{port: number, host: string}|null} */ export function bilibiliProxyInfo() { + if (remoteMode()) return { port: 0, host: _cfg.remoteProxy }; if (!_server) return null; const addr = _server.address(); const port = typeof addr === "object" && addr ? addr.port : _cfg.port; @@ -188,6 +232,12 @@ function verifySignature(target, expires, sig) { * @returns {Promise} The signed proxy URL (`/s` mode). */ export async function buildSignedProxyUrl(targetUrl, ttlMs = PROXY_TTL_MS) { + if (remoteMode()) { + const expires = Date.now() + ttlMs; + const u = Buffer.from(String(targetUrl), "utf8").toString("base64url"); + const x = signTarget(u, expires); + return _cfg.remoteProxy + "/s?u=" + u + "&e=" + expires + "&x=" + x; + } const { port, host } = await ensureBilibiliProxy(); const expires = Date.now() + ttlMs; const u = Buffer.from(String(targetUrl), "utf8").toString("base64url"); @@ -199,13 +249,13 @@ export async function buildSignedProxyUrl(targetUrl, ttlMs = PROXY_TTL_MS) { * Sign a multi-segment progressive stream into a concatenating proxy URL. * The payload carries each segment's CDN URL and byte size (plus up to four * backup URLs); the `/l` handler streams the segments back-to-back and maps - * Range requests across the concatenation. + * Range requests across the concatenation. Remote mode serves this from the + * configured remote proxy instead of the in-process server. * @param {Array<{url: string, size: number, backupUrls?: Array}>} segments * @param {number} [ttlMs=PROXY_TTL_MS] - Signature lifetime. * @returns {Promise} The signed proxy URL (`/l` mode). */ export async function buildSignedListProxyUrl(segments, ttlMs = PROXY_TTL_MS) { - const { port, host } = await ensureBilibiliProxy(); const payload = JSON.stringify( (Array.isArray(segments) ? segments : []).slice(0, MAX_SEGMENTS).map(s => ({ u: String(s?.url ?? ""), @@ -218,6 +268,8 @@ export async function buildSignedListProxyUrl(segments, ttlMs = PROXY_TTL_MS) { const u = Buffer.from(payload, "utf8").toString("base64url"); const expires = Date.now() + ttlMs; const x = signTarget(u, expires); + if (remoteMode()) return _cfg.remoteProxy + "/l?u=" + u + "&e=" + expires + "&x=" + x; + const { port, host } = await ensureBilibiliProxy(); return "http://" + host + ":" + port + "/l?u=" + u + "&e=" + expires + "&x=" + x; } diff --git a/src/music/bilibili/index.mjs b/src/music/bilibili/index.mjs index e926340..8dd1a14 100644 --- a/src/music/bilibili/index.mjs +++ b/src/music/bilibili/index.mjs @@ -34,6 +34,12 @@ import { logger } from "../../core/Logger.mjs"; * @param {number} [section.port=0] - Proxy listen port (0 = ephemeral). * @param {string} [section.advertiseHost] - Host the audio node uses to * reach the proxy when it runs on another machine/container. + * @param {string} [section.remoteProxy] - Base URL of a remote HTTPS + * header-proxy (e.g. a Cloudflare Worker). When set together with + * remoteSecret, signed URLs point there and the local proxy never starts — + * for deployments where the audio node cannot reach the bot host. + * @param {string} [section.remoteSecret] - Shared HMAC secret the remote + * proxy verifies signed URLs with (required with remoteProxy). */ export function configureBilibili(section = {}) { configureBilibiliProxy(section);