MCPcopy Index your code
hub / github.com/ZToolsCenter/ZTools / applyProxyToSession

Method applyProxyToSession

src/main/managers/proxyManager.ts:43–74  ·  view source on GitHub ↗

* 应用代理配置到指定 session * @param sess Electron session * @param name session 名称(用于日志)

(sess: Electron.Session, name?: string)

Source from the content-addressed store, hash-verified

41 * @param name session 名称(用于日志)
42 */
43 public async applyProxyToSession(sess: Electron.Session, name?: string): Promise<void> {
44 if (!this.currentConfig.enabled || !this.currentConfig.proxyRules) {
45 // 清除自定义代理配置,恢复使用系统代理
46 await sess.setProxy({
47 mode: 'system' // 使用系统代理
48 })
49 if (name) {
50 console.log(`[Proxy] ${name} 已切换到系统代理`)
51 }
52 return
53 }
54
55 // 应用自定义代理
56 // 绕过规则:使用 Chromium 的代理绕过规则语法
57 // 参考:https://source.chromium.org/chromium/chromium/src/+/main:net/docs/proxy.md
58 const bypassRules = [
59 'localhost', // 绕过 localhost
60 '127.0.0.1', // 绕过 127.0.0.1
61 '::1', // 绕过 IPv6 回环(不需要方括号)
62 '<local>' // 绕过所有本地地址
63 ].join(',') // 使用逗号分隔(不是分号)
64
65 await sess.setProxy({
66 proxyRules: this.currentConfig.proxyRules,
67 proxyBypassRules: bypassRules
68 })
69
70 if (name) {
71 console.log(`[Proxy] ${name} 已应用自定义代理: ${this.currentConfig.proxyRules}`)
72 console.log(`[Proxy] 绕过规则: ${bypassRules}`)
73 }
74 }
75
76 /**
77 * 应用代理配置到默认 session 并清理缓存

Callers 3

createWindowMethod · 0.80
setupPluginSessionMethod · 0.80

Calls 1

setProxyMethod · 0.80

Tested by

no test coverage detected