diff --git a/src/main/services/ProxyManager.ts b/src/main/services/ProxyManager.ts index 60bef773c4..a04a727e46 100644 --- a/src/main/services/ProxyManager.ts +++ b/src/main/services/ProxyManager.ts @@ -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 { - 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 { - 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) } } diff --git a/src/renderer/src/pages/settings/GeneralSettings.tsx b/src/renderer/src/pages/settings/GeneralSettings.tsx index 497cfca0c8..93bc0292ce 100644 --- a/src/renderer/src/pages/settings/GeneralSettings.tsx +++ b/src/renderer/src/pages/settings/GeneralSettings.tsx @@ -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)) } }