fix: improve proxy configuration handling and logging in ProxyManager (#8334)

* fix: improve proxy configuration handling and logging in ProxyManager

* fix: reduce proxy refresh interval from 30 seconds to 10 seconds in ProxyManager

* format log

* change log level

* delete duplicate call api
This commit is contained in:
beyondkmp 2025-07-21 10:19:51 +08:00 committed by GitHub
parent bfc3b0e54e
commit 38a731aa8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 33 deletions

View File

@ -36,21 +36,17 @@ export class ProxyManager {
// Clear any existing interval first
this.clearSystemProxyMonitor()
// Set new interval
this.systemProxyInterval = setInterval(
async () => {
const currentProxy = await getSystemProxy()
if (currentProxy && currentProxy.proxyUrl.toLowerCase() === this.config.proxyRules) {
return
}
this.systemProxyInterval = setInterval(async () => {
const currentProxy = await getSystemProxy()
if (currentProxy && currentProxy.proxyUrl.toLowerCase() === this.config?.proxyRules) {
return
}
await this.configureProxy({
mode: 'system',
proxyRules: currentProxy?.proxyUrl.toLowerCase()
})
},
// 1 minutes
1000 * 60
)
await this.configureProxy({
mode: 'system',
proxyRules: currentProxy?.proxyUrl.toLowerCase()
})
}, 1000 * 60)
}
private clearSystemProxyMonitor(): void {
@ -61,7 +57,7 @@ export class ProxyManager {
}
async configureProxy(config: ProxyConfig): Promise<void> {
logger.info('configureProxy', config.mode, config.proxyRules)
logger.debug('configureProxy: %s %s', config?.mode, config?.proxyRules)
if (this.isSettingProxy) {
return
}
@ -79,13 +75,10 @@ export class ProxyManager {
if (config.mode === 'system') {
const currentProxy = await getSystemProxy()
if (currentProxy) {
logger.info('current system proxy', currentProxy.proxyUrl)
logger.info('current system proxy: %s', currentProxy.proxyUrl)
this.config.proxyRules = currentProxy.proxyUrl.toLowerCase()
this.monitorSystemProxy()
} else {
// no system proxy, use direct mode
this.config.mode = 'direct'
}
this.monitorSystemProxy()
}
this.setGlobalProxy()
@ -131,8 +124,7 @@ export class ProxyManager {
}
private setGlobalHttpProxy(config: ProxyConfig) {
const proxyUrl = config.proxyRules
if (config.mode === 'direct' || !proxyUrl) {
if (config.mode === 'direct' || !config.proxyRules) {
http.get = this.originalHttpGet
http.request = this.originalHttpRequest
https.get = this.originalHttpsGet
@ -225,17 +217,11 @@ export class ProxyManager {
}
private async setSessionsProxy(config: ProxyConfig): Promise<void> {
let c = config
if (config.mode === 'direct' || !config.proxyRules) {
c = { mode: 'direct' }
}
const sessions = [session.defaultSession, session.fromPartition('persist:webview')]
await Promise.all(sessions.map((session) => session.setProxy(c)))
await Promise.all(sessions.map((session) => session.setProxy(config)))
// set proxy for electron
app.setProxy(c)
app.setProxy(config)
}
}

View File

@ -94,7 +94,6 @@ const GeneralSettings: FC = () => {
}
dispatch(_setProxyUrl(proxyUrl))
window.api.setProxy(proxyUrl)
}
const proxyModeOptions: { value: 'system' | 'custom' | 'none'; label: string }[] = [
@ -106,10 +105,8 @@ const GeneralSettings: FC = () => {
const onProxyModeChange = (mode: 'system' | 'custom' | 'none') => {
dispatch(setProxyMode(mode))
if (mode === 'system') {
window.api.setProxy('system')
dispatch(_setProxyUrl(undefined))
} else if (mode === 'none') {
window.api.setProxy(undefined)
dispatch(_setProxyUrl(undefined))
}
}