From dce1395af11f8c47342c4bf93ec42dc71d332f25 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Fri, 28 Feb 2025 23:15:03 +0800 Subject: [PATCH] feat: quiting when enable tun mode no more blocking system network (#2805) --- src-tauri/src/cmds.rs | 2 +- src-tauri/src/core/hotkey.rs | 2 +- src-tauri/src/core/tray/mod.rs | 6 +++--- src-tauri/src/feat.rs | 16 ++++++++++------ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 0b628598..f9e6ed0c 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -334,7 +334,7 @@ pub fn get_verge_config() -> CmdResult { #[tauri::command] pub async fn patch_verge_config(payload: IVerge) -> CmdResult { - wrap_err!(feat::patch_verge(payload).await) + wrap_err!(feat::patch_verge(payload, false).await) } #[tauri::command] diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index 011ed013..33050eec 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -140,7 +140,7 @@ impl Hotkey { "clash_mode_global" => || feat::change_clash_mode("global".into()), "clash_mode_direct" => || feat::change_clash_mode("direct".into()), "toggle_system_proxy" => || feat::toggle_system_proxy(), - "toggle_tun_mode" => || feat::toggle_tun_mode(), + "toggle_tun_mode" => || feat::toggle_tun_mode(None), "quit" => || feat::quit(Some(0)), _ => { diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 5deef135..b1a358d4 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -87,7 +87,7 @@ impl Tray { { match tray_event.as_str() { "system_proxy" => feat::toggle_system_proxy(), - "tun_mode" => feat::toggle_tun_mode(), + "tun_mode" => feat::toggle_tun_mode(None), "main_window" => resolve::create_window(), _ => {} } @@ -102,7 +102,7 @@ impl Tray { { match tray_event.as_str() { "system_proxy" => feat::toggle_system_proxy(), - "tun_mode" => feat::toggle_tun_mode(), + "tun_mode" => feat::toggle_tun_mode(None), "main_window" => resolve::create_window(), _ => {} } @@ -594,7 +594,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { } "open_window" => resolve::create_window(), "system_proxy" => feat::toggle_system_proxy(), - "tun_mode" => feat::toggle_tun_mode(), + "tun_mode" => feat::toggle_tun_mode(None), "copy_env" => feat::copy_clash_env(), "open_app_dir" => crate::log_err!(cmds::open_app_dir()), "open_core_dir" => crate::log_err!(cmds::open_core_dir()), diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index b0749dc7..d3d73c23 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -151,7 +151,7 @@ pub fn toggle_system_proxy() { match patch_verge(IVerge { enable_system_proxy: Some(!enable), ..IVerge::default() - }) + }, false) .await { Ok(_) => handle::Handle::refresh_verge(), @@ -176,7 +176,7 @@ pub fn toggle_proxy_profile(profile_index: String) { } // 切换tun模式 -pub fn toggle_tun_mode() { +pub fn toggle_tun_mode(not_save_file: Option) { let enable = Config::verge().data().enable_tun_mode; let enable = enable.unwrap_or(false); @@ -184,7 +184,7 @@ pub fn toggle_tun_mode() { match patch_verge(IVerge { enable_tun_mode: Some(!enable), ..IVerge::default() - }) + }, not_save_file.unwrap_or(false)) .await { Ok(_) => handle::Handle::refresh_verge(), @@ -196,6 +196,7 @@ pub fn toggle_tun_mode() { pub fn quit(code: Option) { let app_handle = handle::Handle::global().app_handle().unwrap(); handle::Handle::global().set_is_exiting(); + toggle_tun_mode(Some(true)); resolve::resolve_reset(); log_err!(handle::Handle::global().get_window().unwrap().close()); app_handle.exit(code.unwrap_or(0)); @@ -236,7 +237,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> { /// 修改verge的订阅 /// 一般都是一个个的修改 -pub async fn patch_verge(patch: IVerge) -> Result<()> { +pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> { Config::verge().draft().patch_config(patch.clone()); let tun_mode = patch.enable_tun_mode; @@ -397,7 +398,9 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { match res { Ok(()) => { Config::verge().apply(); - Config::verge().data().save_file()?; + if !not_save_file { + Config::verge().data().save_file()?; + } Ok(()) } @@ -617,7 +620,8 @@ pub async fn restore_webdav_backup(filename: String) -> Result<()> { webdav_username, webdav_password, ..IVerge::default() - }) + }, + false) .await ); // 最后删除临时文件