Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src-tauri/src/config/clash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
49 changes: 48 additions & 1 deletion src-tauri/src/core/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
};
Expand Down Expand Up @@ -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");
Expand Down