|
1 | | -import { net, session } from "electron"; |
2 | | -import type { ProxyConfig } from "electron"; |
3 | 1 | import { store } from "@main/store"; |
4 | 2 | import { systemLog } from "@main/utils/logger"; |
| 3 | +import { fetch as undiciFetch, ProxyAgent, Socks5ProxyAgent } from "undici"; |
| 4 | +import type { Dispatcher } from "undici"; |
5 | 5 |
|
6 | | -const MAIN_PARTITION = "persist:main"; |
7 | 6 | const PROXY_TEST_URL = "https://www.baidu.com"; |
8 | 7 |
|
9 | | -/** |
10 | | - * 将网络代理配置转换为 Electron ProxyConfig。 |
11 | | - * system 模式使用 { mode: "system" } 跟随系统代理;off 直连。 |
12 | | - */ |
13 | | -const buildProxyConfig = (): ProxyConfig => { |
| 8 | +let proxyAgent: Dispatcher | null = null; |
| 9 | +let proxyAgentUrl = ""; |
| 10 | + |
| 11 | +const isManualProxyProtocol = (value: string): value is "http" | "https" | "socks5" => |
| 12 | + value === "http" || value === "https" || value === "socks5"; |
| 13 | + |
| 14 | +/** 当前手动代理地址;off 或配置无效时返回 null,保持原生直连行为 */ |
| 15 | +export const getNetworkProxyUrl = (): string | null => { |
14 | 16 | const config = store.get("system.networkProxy"); |
15 | | - if (config.protocol === "system") return { mode: "system" }; |
16 | | - if (config.protocol === "off") return { mode: "direct" }; |
| 17 | + if (!isManualProxyProtocol(config.protocol)) return null; |
17 | 18 | const host = config.host.trim(); |
18 | 19 | const port = Number(config.port); |
19 | | - if (!host || !Number.isInteger(port) || port < 1 || port > 65535) return { mode: "direct" }; |
20 | | - return { proxyRules: `${config.protocol}://${host}:${port}` }; |
| 20 | + if (!host || !Number.isInteger(port) || port < 1 || port > 65535) return null; |
| 21 | + return `${config.protocol}://${host}:${port}`; |
21 | 22 | }; |
22 | 23 |
|
23 | | -/** 应用网络代理配置到 Electron 会话 */ |
24 | | -export const applyNetworkProxy = async (): Promise<void> => { |
25 | | - const proxyConfig = buildProxyConfig(); |
26 | | - try { |
27 | | - await Promise.all([ |
28 | | - session.defaultSession.setProxy(proxyConfig), |
29 | | - session.fromPartition(MAIN_PARTITION).setProxy(proxyConfig), |
30 | | - ]); |
31 | | - systemLog.info( |
32 | | - `[proxy] mode=${proxyConfig.mode ?? "fixed_servers"}${proxyConfig.proxyRules ? ` rules=${proxyConfig.proxyRules}` : ""}`, |
33 | | - ); |
34 | | - } catch (err) { |
35 | | - systemLog.warn("[proxy] apply failed", err); |
| 24 | +const getProxyDispatcher = (): Dispatcher | undefined => { |
| 25 | + const url = getNetworkProxyUrl(); |
| 26 | + if (!url) return undefined; |
| 27 | + if (!proxyAgent || proxyAgentUrl !== url) { |
| 28 | + proxyAgent?.close().catch(() => {}); |
| 29 | + proxyAgent = url.startsWith("socks5://") ? new Socks5ProxyAgent(url) : new ProxyAgent(url); |
| 30 | + proxyAgentUrl = url; |
| 31 | + systemLog.info(`[proxy] node fetch proxy=${url}`); |
36 | 32 | } |
| 33 | + return proxyAgent; |
| 34 | +}; |
| 35 | + |
| 36 | +/** Node fetch 包装:关闭代理时完全等价于原生 fetch,开启代理时才注入 dispatcher */ |
| 37 | +export const fetchWithProxy = (input: string | URL, init?: RequestInit): Promise<Response> => { |
| 38 | + const dispatcher = getProxyDispatcher(); |
| 39 | + if (!dispatcher) return fetch(input, init); |
| 40 | + return undiciFetch(input, { ...(init as RequestInit), dispatcher } as Parameters< |
| 41 | + typeof undiciFetch |
| 42 | + >[1]) as unknown as Promise<Response>; |
37 | 43 | }; |
38 | 44 |
|
39 | 45 | /** 测试当前代理是否可用 */ |
40 | 46 | export const testNetworkProxy = async (): Promise<boolean> => { |
41 | | - const proxyConfig = buildProxyConfig(); |
42 | | - if (proxyConfig.mode === "system") return true; |
43 | | - if (proxyConfig.mode === "direct") return false; |
| 47 | + if (!getNetworkProxyUrl()) return false; |
44 | 48 | try { |
45 | | - await applyNetworkProxy(); |
46 | | - const res = await net.fetch(PROXY_TEST_URL, { signal: AbortSignal.timeout(8000) }); |
| 49 | + const res = await fetchWithProxy(PROXY_TEST_URL, { signal: AbortSignal.timeout(8000) }); |
47 | 50 | return res.ok; |
48 | 51 | } catch (err) { |
49 | 52 | systemLog.warn("[proxy] test failed", err); |
|
0 commit comments