From cb15a38cb3c7caf997bce0d6f618e85c5add5679 Mon Sep 17 00:00:00 2001 From: Sline Date: Tue, 7 Oct 2025 07:18:07 +0800 Subject: [PATCH] fix: restore periodic proxy guard checks removed in event-driven migration (#4954) * fix: restore periodic proxy guard checks removed in event-driven migration * style: cargo fmt --- src-tauri/src/core/event_driven_proxy.rs | 29 +++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/core/event_driven_proxy.rs b/src-tauri/src/core/event_driven_proxy.rs index 71bf2ea60..d6e093b4f 100644 --- a/src-tauri/src/core/event_driven_proxy.rs +++ b/src-tauri/src/core/event_driven_proxy.rs @@ -85,6 +85,7 @@ struct ProxyConfig { sys_enabled: bool, pac_enabled: bool, guard_enabled: bool, + guard_duration: u64, } static PROXY_MANAGER: Lazy = Lazy::new(EventDrivenProxyManager::new); @@ -184,16 +185,40 @@ impl EventDrivenProxyManager { let mut event_stream = UnboundedReceiverStream::new(event_rx); let mut query_stream = UnboundedReceiverStream::new(query_rx); + // 初始化定时器,用于周期性检查代理设置 + let config = Self::get_proxy_config().await; + let mut guard_interval = tokio::time::interval(Duration::from_secs(config.guard_duration)); + // 防止首次立即触发 + guard_interval.tick().await; + loop { tokio::select! { Some(event) = event_stream.next() => { log::debug!(target: "app", "处理代理事件: {event:?}"); + let event_clone = event.clone(); // 保存一份副本用于后续检查 Self::handle_event(&state, event).await; + + // 检查是否是配置变更事件,如果是,则可能需要更新定时器 + if matches!(event_clone, ProxyEvent::ConfigChanged | ProxyEvent::AppStarted) { + let new_config = Self::get_proxy_config().await; + // 重新设置定时器间隔 + guard_interval = tokio::time::interval(Duration::from_secs(new_config.guard_duration)); + // 防止首次立即触发 + guard_interval.tick().await; + } } Some(query) = query_stream.next() => { let result = Self::handle_query(&state).await; let _ = query.response_tx.send(result); } + _ = guard_interval.tick() => { + // 定时检查代理设置 + let config = Self::get_proxy_config().await; + if config.guard_enabled && config.sys_enabled { + log::debug!(target: "app", "定时检查代理设置"); + Self::check_and_restore_proxy(&state).await; + } + } else => { // 两个通道都关闭时退出 log::info!(target: "app", "事件或查询通道关闭,代理管理器停止"); @@ -439,19 +464,21 @@ impl EventDrivenProxyManager { } async fn get_proxy_config() -> ProxyConfig { - let (sys_enabled, pac_enabled, guard_enabled) = { + let (sys_enabled, pac_enabled, guard_enabled, guard_duration) = { let verge_config = Config::verge().await; let verge = verge_config.latest_ref(); ( verge.enable_system_proxy.unwrap_or(false), verge.proxy_auto_config.unwrap_or(false), verge.enable_proxy_guard.unwrap_or(false), + verge.proxy_guard_duration.unwrap_or(30), // 默认30秒 ) }; ProxyConfig { sys_enabled, pac_enabled, guard_enabled, + guard_duration, } }