refactor(Proxy): Improve system proxy monitoring and configuration handling

This commit is contained in:
suyao 2025-03-12 23:25:26 +08:00 committed by 亢奋猫
parent 06ab8f35ce
commit bac4dcf73c

View File

@ -14,15 +14,13 @@ export class ProxyManager {
private config: ProxyConfig private config: ProxyConfig
private proxyAgent: HttpsProxyAgent | null = null private proxyAgent: HttpsProxyAgent | null = null
private proxyUrl: string | null = null private proxyUrl: string | null = null
private systemProxyInterval: NodeJS.Timeout | null = null
constructor() { constructor() {
this.config = { this.config = {
mode: 'none', mode: 'none',
url: '' url: ''
} }
if (this.config.mode === 'system') {
this.monitorSystemProxy()
}
} }
private async setSessionsProxy(config: _ProxyConfig): Promise<void> { private async setSessionsProxy(config: _ProxyConfig): Promise<void> {
@ -31,16 +29,28 @@ export class ProxyManager {
} }
private async monitorSystemProxy(): Promise<void> { private async monitorSystemProxy(): Promise<void> {
setInterval(async () => { // Clear any existing interval first
this.clearSystemProxyMonitor()
// Set new interval
this.systemProxyInterval = setInterval(async () => {
await this.setSystemProxy() await this.setSystemProxy()
}, 10000) }, 10000)
} }
private clearSystemProxyMonitor(): void {
if (this.systemProxyInterval) {
clearInterval(this.systemProxyInterval)
this.systemProxyInterval = null
}
}
async configureProxy(config: ProxyConfig): Promise<void> { async configureProxy(config: ProxyConfig): Promise<void> {
try { try {
this.config = config this.config = config
this.clearSystemProxyMonitor()
if (this.config.mode === 'system') { if (this.config.mode === 'system') {
await this.setSystemProxy() await this.setSystemProxy()
this.monitorSystemProxy()
} else if (this.config.mode == 'custom') { } else if (this.config.mode == 'custom') {
await this.setCustomProxy() await this.setCustomProxy()
} else { } else {