From 7383459b9aa7cad10c36897aff25bdf864d99c50 Mon Sep 17 00:00:00 2001 From: tiegen Date: Sat, 26 Apr 2025 18:09:53 +0800 Subject: [PATCH] feat: clean proxy settings before exit (#3404) --- src-tauri/src/feat/window.rs | 76 +++++++++++++++++++----------------- src-tauri/src/lib.rs | 7 ++++ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src-tauri/src/feat/window.rs b/src-tauri/src/feat/window.rs index 7bc986ad..37e61e07 100644 --- a/src-tauri/src/feat/window.rs +++ b/src-tauri/src/feat/window.rs @@ -64,42 +64,7 @@ pub fn quit() { // 在单独线程中处理资源清理,避免阻塞主线程 std::thread::spawn(move || { - use tokio::time::{timeout, Duration}; - let rt = tokio::runtime::Runtime::new().unwrap(); - let cleanup_result = rt.block_on(async { - // 1. 处理TUN模式 - let tun_success = if Config::verge().data().enable_tun_mode.unwrap_or(false) { - let disable_tun = serde_json::json!({ - "tun": { - "enable": false - } - }); - timeout( - Duration::from_secs(1), - MihomoManager::global().patch_configs(disable_tun), - ) - .await - .is_ok() - } else { - true - }; - - // 2. 顺序执行关键清理 - let proxy_res = timeout( - Duration::from_secs(1), - sysopt::Sysopt::global().reset_sysproxy(), - ) - .await; - - let core_res = timeout(Duration::from_secs(1), CoreManager::global().stop_core()).await; - - // 3. 平台特定清理 - #[cfg(target_os = "macos")] - let _dns_res = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await; - - tun_success && proxy_res.is_ok() && core_res.is_ok() - }); - + let cleanup_result = clean(); app_handle.exit(match cleanup_result { true => 0, false => 1, @@ -107,6 +72,45 @@ pub fn quit() { }); } +pub fn clean() -> bool { + use tokio::time::{timeout, Duration}; + let rt = tokio::runtime::Runtime::new().unwrap(); + let cleanup_result = rt.block_on(async { + // 1. 处理TUN模式 + let tun_success = if Config::verge().data().enable_tun_mode.unwrap_or(false) { + let disable_tun = serde_json::json!({ + "tun": { + "enable": false + } + }); + timeout( + Duration::from_secs(1), + MihomoManager::global().patch_configs(disable_tun), + ) + .await + .is_ok() + } else { + true + }; + + // 2. 顺序执行关键清理 + let proxy_res = timeout( + Duration::from_secs(1), + sysopt::Sysopt::global().reset_sysproxy(), + ) + .await; + + let core_res = timeout(Duration::from_secs(1), CoreManager::global().stop_core()).await; + + // 3. 平台特定清理 + #[cfg(target_os = "macos")] + let _dns_res = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await; + + tun_success && proxy_res.is_ok() && core_res.is_ok() + }); + cleanup_result +} + #[cfg(target_os = "macos")] pub fn hide() { use crate::module::lightweight::add_light_weight_timer; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4b96c60a..c00e865b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -302,6 +302,13 @@ pub fn run() { api.prevent_exit(); } } + tauri::RunEvent::Exit => { + // avoid duplicate cleanup + if core::handle::Handle::global().is_exiting() { + return; + } + feat::clean(); + } tauri::RunEvent::WindowEvent { label, event, .. } => { if label == "main" { match event {