mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-01 01:30:51 +08:00
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:
parent
bfc3b0e54e
commit
38a731aa8f
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user