From 178fd8e82853ba04b9b32e9f9780d125765af8b4 Mon Sep 17 00:00:00 2001 From: GyDi Date: Thu, 11 Aug 2022 03:26:08 +0800 Subject: [PATCH] feat: adjust tun mode config --- src-tauri/src/config/mod.rs | 6 +- src-tauri/src/config/tun.rs | 81 ++++++++++++++++++++++++ src-tauri/src/core/clash.rs | 122 ++++++++++++++++++------------------ src-tauri/src/core/mod.rs | 1 + 4 files changed, 147 insertions(+), 63 deletions(-) create mode 100644 src-tauri/src/config/tun.rs diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs index c1e6120..bcd72a7 100644 --- a/src-tauri/src/config/mod.rs +++ b/src-tauri/src/config/mod.rs @@ -1,10 +1,12 @@ mod field; mod merge; mod script; +mod tun; pub(self) use self::field::*; use self::merge::*; use self::script::*; +use self::tun::*; use crate::core::PrfData; use serde_yaml::Mapping; use std::collections::HashMap; @@ -16,10 +18,9 @@ pub fn runtime_config( profile_config: Mapping, profile_enhanced: Vec, valid: Vec, - // tun_enable: bool, + tun_mode: bool, ) -> (Mapping, HashMap) { let mut config = profile_config; - let mut result_map = HashMap::new(); profile_enhanced.into_iter().for_each(|data| { @@ -51,6 +52,7 @@ pub fn runtime_config( } config = use_filter(config, use_clash_fields()); + config = use_tun(config, tun_mode); config = use_sort(config); (config, result_map) diff --git a/src-tauri/src/config/tun.rs b/src-tauri/src/config/tun.rs new file mode 100644 index 0000000..3c9a8d0 --- /dev/null +++ b/src-tauri/src/config/tun.rs @@ -0,0 +1,81 @@ +use serde_yaml::{Mapping, Value}; + +macro_rules! revise { + ($map: expr, $key: expr, $val: expr) => { + let ret_key = Value::String($key.into()); + $map.insert(ret_key, Value::from($val)); + }; +} + +// if key not exists then append value +macro_rules! append { + ($map: expr, $key: expr, $val: expr) => { + let ret_key = Value::String($key.into()); + if !$map.contains_key(&ret_key) { + $map.insert(ret_key, Value::from($val)); + } + }; +} + +pub fn use_tun(mut config: Mapping, enable: bool) -> 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); + if enable { + append!(tun_val, "stack", "gvisor"); + append!(tun_val, "dns-hijack", vec!["198.18.0.2:53"]); + append!(tun_val, "auto-route", true); + append!(tun_val, "auto-detect-interface", true); + } + + revise!(config, "tun", tun_val); + + if enable { + use_dns_for_tun(config) + } else { + config + } +} + +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); + + // 借鉴cfw的默认配置 + append!(dns_val, "enhanced-mode", "fake-ip"); + 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 +} diff --git a/src-tauri/src/core/clash.rs b/src-tauri/src/core/clash.rs index e94bdd4..9c99b8f 100644 --- a/src-tauri/src/core/clash.rs +++ b/src-tauri/src/core/clash.rs @@ -163,79 +163,79 @@ impl Clash { Ok((change_port, change_mode)) } - /// revise the `tun` and `dns` config - pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping { - macro_rules! revise { - ($map: expr, $key: expr, $val: expr) => { - let ret_key = Value::String($key.into()); - $map.insert(ret_key, Value::from($val)); - }; - } + // /// revise the `tun` and `dns` config + // pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping { + // macro_rules! revise { + // ($map: expr, $key: expr, $val: expr) => { + // let ret_key = Value::String($key.into()); + // $map.insert(ret_key, Value::from($val)); + // }; + // } - // if key not exists then append value - macro_rules! append { - ($map: expr, $key: expr, $val: expr) => { - let ret_key = Value::String($key.into()); - if !$map.contains_key(&ret_key) { - $map.insert(ret_key, Value::from($val)); - } - }; - } + // // if key not exists then append value + // macro_rules! append { + // ($map: expr, $key: expr, $val: expr) => { + // let ret_key = Value::String($key.into()); + // if !$map.contains_key(&ret_key) { + // $map.insert(ret_key, Value::from($val)); + // } + // }; + // } - // tun config - let tun_val = config.get(&Value::from("tun")); - let mut new_tun = Mapping::new(); + // // tun config + // let tun_val = config.get(&Value::from("tun")); + // let mut new_tun = Mapping::new(); - if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() { - new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone(); - } + // if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() { + // new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone(); + // } - revise!(new_tun, "enable", enable); + // revise!(new_tun, "enable", enable); - if enable { - append!(new_tun, "stack", "gvisor"); - append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]); - append!(new_tun, "auto-route", true); - append!(new_tun, "auto-detect-interface", true); - } + // if enable { + // append!(new_tun, "stack", "gvisor"); + // append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]); + // append!(new_tun, "auto-route", true); + // append!(new_tun, "auto-detect-interface", true); + // } - revise!(config, "tun", new_tun); + // revise!(config, "tun", new_tun); - if enable { - // dns config - let dns_val = config.get(&Value::from("dns")); - let mut new_dns = Mapping::new(); + // if enable { + // // dns config + // let dns_val = config.get(&Value::from("dns")); + // let mut new_dns = Mapping::new(); - if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() { - new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone(); - } - revise!(new_dns, "enable", enable); + // if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() { + // new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone(); + // } + // revise!(new_dns, "enable", enable); - // 借鉴cfw的默认配置 - append!(new_dns, "enhanced-mode", "fake-ip"); - append!( - new_dns, - "nameserver", - vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"] - ); - append!(new_dns, "fallback", vec![] as Vec<&str>); + // // 借鉴cfw的默认配置 + // append!(new_dns, "enhanced-mode", "fake-ip"); + // append!( + // new_dns, + // "nameserver", + // vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"] + // ); + // append!(new_dns, "fallback", vec![] as Vec<&str>); - #[cfg(target_os = "windows")] - append!( - new_dns, - "fake-ip-filter", - vec![ - "dns.msftncsi.com", - "www.msftncsi.com", - "www.msftconnecttest.com" - ] - ); + // #[cfg(target_os = "windows")] + // append!( + // new_dns, + // "fake-ip-filter", + // vec![ + // "dns.msftncsi.com", + // "www.msftncsi.com", + // "www.msftconnecttest.com" + // ] + // ); - revise!(config, "dns", new_dns); - } + // revise!(config, "dns", new_dns); + // } - config - } + // config + // } // /// only 5 default fields available (clash config fields) // /// convert to lowercase diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index abe01fb..b4e9bae 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -363,6 +363,7 @@ impl Core { profile_config, profile_enhanced.chain, profile_enhanced.valid, + tun_mode, ); dbg!(result);