diff --git a/apps/server/src/egress.ts b/apps/server/src/egress.ts index 0318a063..244a744f 100644 --- a/apps/server/src/egress.ts +++ b/apps/server/src/egress.ts @@ -72,14 +72,19 @@ export function fetchVia(client: Deno.HttpClient): typeof globalThis.fetch { globalThis.fetch(input, { ...init, client } as RequestInit)) as typeof globalThis.fetch; } -/** 站点请求的出口:开代理且有代理地址 → proxy,否则 direct */ +/** 站点请求的出口:开代理 → 设置里的站点代理地址,没填则用进程的 HTTPS_PROXY / HTTP_PROXY;都没有 → direct */ export interface SiteEgress { mode: "direct" | "proxy"; proxyUrl: string | null; } -export function siteEgress(proxy: boolean, proxyUrl: string | null | undefined): SiteEgress { - return proxy && proxyUrl ? { mode: "proxy", proxyUrl } : { mode: "direct", proxyUrl: null }; +export function siteEgress( + proxy: boolean, + proxyUrl: string | null | undefined, + env: Record = ENV_SNAPSHOT, +): SiteEgress { + const url = proxy ? proxyUrl || env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy : null; + return url ? { mode: "proxy", proxyUrl: url } : { mode: "direct", proxyUrl: null }; } /** 出口的比较键(不含代理口令):direct / proxy:<主机:端口> */ diff --git a/apps/server/src/modules/sources-identity/routes/sources.ts b/apps/server/src/modules/sources-identity/routes/sources.ts index a4e387dc..3c16c7b2 100644 --- a/apps/server/src/modules/sources-identity/routes/sources.ts +++ b/apps/server/src/modules/sources-identity/routes/sources.ts @@ -184,7 +184,8 @@ export function sourcesRoutes(app: ModuleApp, rt: () => SourcesRuntime) { snapshot_enabled: kind === "pt" ? (body.snapshotEnabled ?? false) : publicDef ? (body.snapshotEnabled ?? true) : false, snapshot_interval_sec: body.snapshotIntervalSec ?? null, - proxy: body.proxy ?? false, + // 内置公开 BT 站(Nyaa 等)国内多数直连不通:默认走代理 + proxy: body.proxy ?? !!publicDef, // 不能搜索的站点不进默认搜索集合 search_default: kind === "pt" ? (body.searchDefault ?? true) : publicDef ? (body.searchDefault ?? false) : false, diff --git a/apps/server/test/sources-identity/egress.test.ts b/apps/server/test/sources-identity/egress.test.ts index dc8222cf..411623a7 100644 --- a/apps/server/test/sources-identity/egress.test.ts +++ b/apps/server/test/sources-identity/egress.test.ts @@ -128,6 +128,11 @@ describe("外网请求按环境变量(行为不变,改为显式)", () => { it("出口键不含代理口令", () => { expect(egressKey(siteEgress(false, "http://u:p@relay:7891"))).toBe("direct"); expect(egressKey(siteEgress(true, "http://u:p@relay:7891"))).toBe("proxy:http://relay:7891"); - expect(egressKey(siteEgress(true, null))).toBe("direct"); + expect(egressKey(siteEgress(true, null, {}))).toBe("direct"); + // 设置里没填站点代理地址:沿用进程的 HTTPS_PROXY + expect(egressKey(siteEgress(true, null, { HTTPS_PROXY: "http://172.17.0.1:7891" }))).toBe( + "proxy:http://172.17.0.1:7891", + ); + expect(egressKey(siteEgress(false, null, { HTTPS_PROXY: "http://172.17.0.1:7891" }))).toBe("direct"); }); }); diff --git a/apps/server/test/sources-identity/site-kinds.test.ts b/apps/server/test/sources-identity/site-kinds.test.ts index deed504a..848240da 100644 --- a/apps/server/test/sources-identity/site-kinds.test.ts +++ b/apps/server/test/sources-identity/site-kinds.test.ts @@ -138,6 +138,7 @@ describe.skipIf(!up)("站点类型:API、调度与 RSS", () => { kind: "public_bt", adapter: "api", snapshotEnabled: true, + proxy: true, searchDefault: false, }); expect(nyaa.body.data.capabilities).toEqual(expect.arrayContaining(["search", "list"]));