diff --git a/src-tauri/src/config/clash.rs b/src-tauri/src/config/clash.rs index 70169ad..8646e1d 100644 --- a/src-tauri/src/config/clash.rs +++ b/src-tauri/src/config/clash.rs @@ -124,6 +124,26 @@ impl IClashTemp { Err(_) => "127.0.0.1:9090".into(), } } + + pub fn get_tun_device_ip(&self) -> String { + let config = &self.0; + + let ip = config + .get("dns") + .and_then(|value| match value { + Value::Mapping(val_map) => Some(val_map.get("fake-ip-range").and_then( + |fake_ip_range| match fake_ip_range { + Value::String(ip_range_val) => Some(ip_range_val.replace("1/16", "2")), + _ => None, + }, + )), + _ => None, + }) + // 默认IP + .unwrap_or(Some("198.18.0.2".to_string())); + + ip.unwrap() + } } #[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)] diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 6b3053b..7a4b679 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -104,7 +104,31 @@ impl CoreManager { if should_kill { sleep(Duration::from_millis(500)).await; } + #[cfg(target_os = "macos")] + { + let enable_tun = Config::verge().latest().enable_tun_mode.clone(); + let enable_tun = enable_tun.unwrap_or(false); + if enable_tun { + log::debug!(target: "app", "try to set system dns"); + + match (|| async { + let tun_device_ip = Config::clash().clone().latest().get_tun_device_ip(); + // 执行 networksetup -setdnsservers Wi-Fi $tun_device_ip + Command::new("networksetup") + .args(["-setdnsservers", "Wi-Fi", tun_device_ip.as_str()]) + .output() + })() + .await + { + Ok(_) => return Ok(()), + Err(err) => { + // 修改这个值,免得stop出错 + log::error!(target: "app", "{err}"); + } + } + } + } #[cfg(target_os = "windows")] { use super::win_service; @@ -145,7 +169,7 @@ impl CoreManager { let config_path = dirs::path_to_str(&config_path)?; // fix #212 - let args = match clash_core.as_str() { + let args: Vec<&str> = match clash_core.as_str() { "clash-meta" => vec!["-m", "-d", app_dir, "-f", config_path], _ => vec!["-d", app_dir, "-f", config_path], }; @@ -247,6 +271,29 @@ impl CoreManager { return Ok(()); } + #[cfg(target_os = "macos")] + { + let enable_tun = Config::verge().latest().enable_tun_mode.clone(); + let enable_tun = enable_tun.unwrap_or(false); + + if enable_tun { + log::debug!(target: "app", "try to set system dns"); + + match (|| { + // 执行 networksetup -setdnsservers Wi-Fi "Empty" + Command::new("networksetup") + .args(["-setdnsservers", "Wi-Fi", "Empty"]) + .output() + })() { + Ok(_) => return Ok(()), + Err(err) => { + // 修改这个值,免得stop出错 + *self.use_service_mode.lock() = false; + log::error!(target: "app", "{err}"); + } + } + } + } let mut sidecar = self.sidecar.lock(); if let Some(child) = sidecar.take() { log::debug!(target: "app", "stop the core by sidecar");