feat(music): optional outbound proxy for the NetEase sidecar

The -460 documented in the previous commit has no code-level fix: NetEase
decides by IP, so a cloud-datacenter host gets refused regardless of account,
quality or retry budget. The only real remedy is to let the request leave from
somewhere else.

The sidecar already supports that, and nobody had noticed: `NeteaseCloudMusicApi`
reads a `proxy` REQUEST PARAMETER and tunnels through it itself
(`util/request.js`, via the `tunnel` package — `httpOverHttp`/`httpsOverHttp`).
So this needs **no new dependency, no global-agent patching, and no change to
how the sidecar is started**. It is one string carried on the calls we already
make.

What was added:

- `NeteaseProvider.setProxy()` / `getProxy()`. The value goes into
  `requestParams`, a single getter that already existed for the cookie — which
  is why all ~20 call sites (search, playlist, album, artist, lyric, playback)
  pick it up without touching each one again.
- `config.musicProxy` (default `""` = direct, i.e. exactly the old behaviour).
  Only `http(s)://host[:port]` is accepted; anything else falls back to direct
  rather than silently breaking every music request.
- `index.ts` reads it, falls back to `HTTPS_PROXY`/`HTTP_PROXY` from the
  environment, and logs `NetEase requests will go through a proxy` with the
  credentials redacted.

Deliberate limits:

- Only HTTP proxies. The sidecar implements this with a CONNECT tunnel, so a
  `socks5://` value would be parsed and then fail. Documented on the setter.
- Audio is NOT proxied. The player pulls the mp3 from NetEase's CDN with ffmpeg,
  which knows nothing about this setting. If a deployment needs those requests
  to share the API's egress IP, it needs a network-level route, not this option.
- Config wins over the environment: a container-wide `HTTPS_PROXY` set for
  unrelated tools must not silently reroute music traffic.

6 new tests: no parameter by default, carried on every call once set, carried on
the playback call specifically, kept alongside the cookie, blank means direct,
values are trimmed.

This is additive and off by default — merging it changes nothing until someone
sets `musicProxy`.
This commit is contained in:
jiayuLian committed 2026-11-08 11:21:37 +00:00
1 parent 4c25cfe5e5
commit 2daae59014
4 files changed
+172 -20

No files matched your search

Executable → Regular
+23
View File
@@ -150,6 +150,18 @@ export interface BotConfig {
* a hand-edited config cannot turn every failed track into a long stall.
*/
playUrlRetries: number;
/**
* Outbound HTTP proxy for the NetEase sidecar, e.g. `http://192.168.1.2:8118`.
*
* NetEase rejects cloud-datacenter IPs with `-460` ("网络环境存在风险") no matter
* which account is used, so on a VPS the only fix is to send the request out
* through somewhere else. Empty (the default) = direct, i.e. unchanged.
*
* Only `http://` proxies are supported — the sidecar implements this with a
* CONNECT tunnel. When left empty, `HTTPS_PROXY`/`HTTP_PROXY` from the
* environment is used as a fallback (see index.ts).
*/
musicProxy: string;
// Public base URL used when generating share links (e.g. the bot专属链接).
// Leave empty to use the browser's current origin. Example:
// "https://music.example.com" or "http://1.2.3.4:3000"
@@ -208,6 +220,7 @@ export function getDefaultConfig(): BotConfig {
// Two extra attempts: enough to ride out a transient NetEase hiccup without
// making a genuinely unlicensed track feel stuck (~1s of backoff total).
playUrlRetries: 2,
musicProxy: "",
publicUrl: "",
trustProxy: false,
guestMode: {
@@ -421,6 +434,15 @@ export function loadConfig(path: string): BotConfig {
? Math.min(5, Math.max(0, Math.trunc(rawRetries)))
: defaults.playUrlRetries;
// Outbound proxy for the NetEase sidecar. Only http(s) URLs are kept: a
// stray value here would silently break every music request, so anything
// unparseable is dropped back to "direct" instead of being passed through.
const rawMusicProxy = partial.musicProxy;
const musicProxy =
typeof rawMusicProxy === "string" && /^https?:\/\/[^\s]+$/i.test(rawMusicProxy.trim())
? rawMusicProxy.trim()
: defaults.musicProxy;
// Voice ducking is opt-in and the retained-volume percentage is consumed
// directly by the audio path. Only a plain-object block with correctly
// typed, finite and in-range fields may override the safe defaults.
@@ -484,6 +506,7 @@ export function loadConfig(path: string): BotConfig {
savedQueuesEnabled,
playKeepsQueue,
playUrlRetries,
musicProxy,
voiceDucking,
defaultPlatform: defaultPlatformPref,
};