From 0541a0c69fce818038fa96fd7b192ef9533013b4 Mon Sep 17 00:00:00 2001 From: huzibaca Date: Sat, 5 Oct 2024 22:45:59 +0800 Subject: [PATCH] refactor: tun mode is turned on and off, does not depend on services, and does not affect other configurations --- src-tauri/src/core/service.rs | 40 --------------- src-tauri/src/core/tray.rs | 62 +++++++---------------- src-tauri/src/enhance/mod.rs | 7 +-- src-tauri/src/enhance/tun.rs | 61 ++-------------------- src-tauri/src/utils/resolve.rs | 1 - src/components/setting/setting-system.tsx | 15 +----- 6 files changed, 28 insertions(+), 158 deletions(-) diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 670bcf5..9e67ce7 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -350,43 +350,3 @@ pub(super) async fn stop_core_by_service() -> Result<()> { Ok(()) } - -/// set dns by service -pub async fn set_dns_by_service() -> Result<()> { - let url = format!("{SERVICE_URL}/set_dns"); - let res = reqwest::ClientBuilder::new() - .no_proxy() - .build()? - .post(url) - .send() - .await? - .json::() - .await - .context("failed to connect to the Clash Verge Service")?; - - if res.code != 0 { - bail!(res.msg); - } - - Ok(()) -} - -/// unset dns by service -pub async fn unset_dns_by_service() -> Result<()> { - let url = format!("{SERVICE_URL}/unset_dns"); - let res = reqwest::ClientBuilder::new() - .no_proxy() - .build()? - .post(url) - .send() - .await? - .json::() - .await - .context("failed to connect to the Clash Verge Service")?; - - if res.code != 0 { - bail!(res.msg); - } - - Ok(()) -} diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index 7e8ac62..29eaf58 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -364,51 +364,25 @@ fn create_tray_menu( .unwrap(); let separator = &PredefinedMenuItem::separator(app_handle).unwrap(); - let enable = { - Config::verge() - .latest() - .enable_service_mode - .unwrap_or(false) - }; - let menu = if enable { - tauri::menu::MenuBuilder::new(app_handle) - .items(&[ - open_window, - separator, - rule_mode, - global_mode, - direct_mode, - separator, - system_proxy, - tun_mode, - copy_env, - open_dir, - more, - separator, - quit, - ]) - .build() - .unwrap() - } else { - tauri::menu::MenuBuilder::new(app_handle) - .items(&[ - open_window, - separator, - rule_mode, - global_mode, - direct_mode, - separator, - system_proxy, - copy_env, - open_dir, - more, - separator, - quit, - ]) - .build() - .unwrap() - }; + let menu = tauri::menu::MenuBuilder::new(app_handle) + .items(&[ + open_window, + separator, + rule_mode, + global_mode, + direct_mode, + separator, + system_proxy, + tun_mode, + copy_env, + open_dir, + more, + separator, + quit, + ]) + .build() + .unwrap(); Ok(menu) } diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index c475020..6da1b53 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -25,7 +25,7 @@ pub async fn enhance() -> (Mapping, Vec, HashMap) { // config.yaml 的订阅 let clash_config = { Config::clash().latest().0.clone() }; - let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled, enable_service_mode) = { + let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled) = { let verge = Config::verge(); let verge = verge.latest(); ( @@ -34,7 +34,6 @@ pub async fn enhance() -> (Mapping, Vec, HashMap) { verge.enable_builtin_enhanced.unwrap_or(true), verge.verge_socks_enabled.unwrap_or(false), verge.verge_http_enabled.unwrap_or(false), - verge.enable_service_mode.unwrap_or(false), ) }; #[cfg(not(target_os = "windows"))] @@ -260,7 +259,9 @@ pub async fn enhance() -> (Mapping, Vec, HashMap) { }); } - config = use_tun(config, enable_tun, enable_service_mode).await; + if enable_tun { + config = use_tun(config).await; + } config = use_sort(config); let mut exists_set = HashSet::new(); diff --git a/src-tauri/src/enhance/tun.rs b/src-tauri/src/enhance/tun.rs index d3ca5c0..650ebd0 100644 --- a/src-tauri/src/enhance/tun.rs +++ b/src-tauri/src/enhance/tun.rs @@ -1,4 +1,3 @@ -use crate::{core::service, log_err}; use serde_yaml::{Mapping, Value}; macro_rules! revise { @@ -9,6 +8,7 @@ macro_rules! revise { } // if key not exists then append value +#[allow(unused_macros)] macro_rules! append { ($map: expr, $key: expr, $val: expr) => { let ret_key = Value::String($key.into()); @@ -18,66 +18,13 @@ macro_rules! append { }; } -pub async fn use_tun(mut config: Mapping, enable: bool, enable_service_mode: bool) -> Mapping { +pub async fn use_tun(mut config: Mapping) -> Mapping { let tun_key = Value::from("tun"); let tun_val = config.get(&tun_key); - - if !enable && tun_val.is_none() { - return config; - } let mut tun_val = tun_val.map_or(Mapping::new(), |val| { val.as_mapping().cloned().unwrap_or(Mapping::new()) }); - revise!(tun_val, "enable", enable); + revise!(tun_val, "enable", true); revise!(config, "tun", tun_val); - - if enable_service_mode { - handle_dns_service(enable).await; - if enable { - return use_dns_for_tun(config); - } - } - config -} - -async fn handle_dns_service(enable: bool) { - if enable { - log_err!(service::set_dns_by_service().await); - } else { - log_err!(service::unset_dns_by_service().await); - } -} - -fn use_dns_for_tun(mut config: Mapping) -> Mapping { - let dns_key = Value::from("dns"); - let dns_val = config.get(&dns_key); - - let mut dns_val = dns_val.map_or(Mapping::new(), |val| { - val.as_mapping().cloned().unwrap_or(Mapping::new()) - }); - - // 开启tun将同时开启dns - revise!(dns_val, "enable", true); - - append!(dns_val, "enhanced-mode", "fake-ip"); - append!(dns_val, "fake-ip-range", "198.18.0.1/16"); - append!( - dns_val, - "nameserver", - vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"] - ); - append!(dns_val, "fallback", vec![] as Vec<&str>); - - #[cfg(target_os = "windows")] - append!( - dns_val, - "fake-ip-filter", - vec![ - "dns.msftncsi.com", - "www.msftncsi.com", - "www.msftconnecttest.com" - ] - ); - revise!(config, "dns", dns_val); - config + return config; } diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 6c7a28b..1dee014 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -107,7 +107,6 @@ pub fn resolve_reset() { tauri::async_runtime::block_on(async move { log_err!(sysopt::Sysopt::global().reset_sysproxy().await); log_err!(CoreManager::global().stop_core().await); - log_err!(service::unset_dns_by_service().await); }); } diff --git a/src/components/setting/setting-system.tsx b/src/components/setting/setting-system.tsx index bb827b7..5393b23 100644 --- a/src/components/setting/setting-system.tsx +++ b/src/components/setting/setting-system.tsx @@ -67,20 +67,9 @@ const SettingSystem = ({ onError }: Props) => { onCatch={onError} onFormat={onSwitchFormat} onChange={(e) => { - if (serviceStatus !== "active") { - onChangeData({ enable_tun_mode: false }); - } else { - onChangeData({ enable_tun_mode: e }); - } - }} - onGuard={(e) => { - if (serviceStatus !== "active" && e) { - Notice.error(t("Please Enable Service Mode")); - return Promise.resolve(); - } else { - return patchVerge({ enable_tun_mode: e }); - } + onChangeData({ enable_tun_mode: e }); }} + onGuard={(e) => patchVerge({ enable_tun_mode: e })} >