feat: adjust tun mode config

This commit is contained in:
GyDi 2022-08-11 03:26:08 +08:00
parent 1641e02a7d
commit 178fd8e828
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
4 changed files with 147 additions and 63 deletions

View File

@ -1,10 +1,12 @@
mod field; mod field;
mod merge; mod merge;
mod script; mod script;
mod tun;
pub(self) use self::field::*; pub(self) use self::field::*;
use self::merge::*; use self::merge::*;
use self::script::*; use self::script::*;
use self::tun::*;
use crate::core::PrfData; use crate::core::PrfData;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::collections::HashMap; use std::collections::HashMap;
@ -16,10 +18,9 @@ pub fn runtime_config(
profile_config: Mapping, profile_config: Mapping,
profile_enhanced: Vec<PrfData>, profile_enhanced: Vec<PrfData>,
valid: Vec<String>, valid: Vec<String>,
// tun_enable: bool, tun_mode: bool,
) -> (Mapping, HashMap<String, ResultLog>) { ) -> (Mapping, HashMap<String, ResultLog>) {
let mut config = profile_config; let mut config = profile_config;
let mut result_map = HashMap::new(); let mut result_map = HashMap::new();
profile_enhanced.into_iter().for_each(|data| { profile_enhanced.into_iter().for_each(|data| {
@ -51,6 +52,7 @@ pub fn runtime_config(
} }
config = use_filter(config, use_clash_fields()); config = use_filter(config, use_clash_fields());
config = use_tun(config, tun_mode);
config = use_sort(config); config = use_sort(config);
(config, result_map) (config, result_map)

View File

@ -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
}

View File

@ -163,79 +163,79 @@ impl Clash {
Ok((change_port, change_mode)) Ok((change_port, change_mode))
} }
/// revise the `tun` and `dns` config // /// revise the `tun` and `dns` config
pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping { // pub fn _tun_mode(mut config: Mapping, enable: bool) -> Mapping {
macro_rules! revise { // macro_rules! revise {
($map: expr, $key: expr, $val: expr) => { // ($map: expr, $key: expr, $val: expr) => {
let ret_key = Value::String($key.into()); // let ret_key = Value::String($key.into());
$map.insert(ret_key, Value::from($val)); // $map.insert(ret_key, Value::from($val));
}; // };
} // }
// if key not exists then append value // // if key not exists then append value
macro_rules! append { // macro_rules! append {
($map: expr, $key: expr, $val: expr) => { // ($map: expr, $key: expr, $val: expr) => {
let ret_key = Value::String($key.into()); // let ret_key = Value::String($key.into());
if !$map.contains_key(&ret_key) { // if !$map.contains_key(&ret_key) {
$map.insert(ret_key, Value::from($val)); // $map.insert(ret_key, Value::from($val));
} // }
}; // };
} // }
// tun config // // tun config
let tun_val = config.get(&Value::from("tun")); // let tun_val = config.get(&Value::from("tun"));
let mut new_tun = Mapping::new(); // let mut new_tun = Mapping::new();
if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() { // if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone(); // new_tun = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
} // }
revise!(new_tun, "enable", enable); // revise!(new_tun, "enable", enable);
if enable { // if enable {
append!(new_tun, "stack", "gvisor"); // append!(new_tun, "stack", "gvisor");
append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]); // append!(new_tun, "dns-hijack", vec!["198.18.0.2:53"]);
append!(new_tun, "auto-route", true); // append!(new_tun, "auto-route", true);
append!(new_tun, "auto-detect-interface", true); // append!(new_tun, "auto-detect-interface", true);
} // }
revise!(config, "tun", new_tun); // revise!(config, "tun", new_tun);
if enable { // if enable {
// dns config // // dns config
let dns_val = config.get(&Value::from("dns")); // let dns_val = config.get(&Value::from("dns"));
let mut new_dns = Mapping::new(); // let mut new_dns = Mapping::new();
if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() { // if dns_val.is_some() && dns_val.as_ref().unwrap().is_mapping() {
new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone(); // new_dns = dns_val.as_ref().unwrap().as_mapping().unwrap().clone();
} // }
revise!(new_dns, "enable", enable); // revise!(new_dns, "enable", enable);
// 借鉴cfw的默认配置 // // 借鉴cfw的默认配置
append!(new_dns, "enhanced-mode", "fake-ip"); // append!(new_dns, "enhanced-mode", "fake-ip");
append!( // append!(
new_dns, // new_dns,
"nameserver", // "nameserver",
vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"] // vec!["114.114.114.114", "223.5.5.5", "8.8.8.8"]
); // );
append!(new_dns, "fallback", vec![] as Vec<&str>); // append!(new_dns, "fallback", vec![] as Vec<&str>);
#[cfg(target_os = "windows")] // #[cfg(target_os = "windows")]
append!( // append!(
new_dns, // new_dns,
"fake-ip-filter", // "fake-ip-filter",
vec![ // vec![
"dns.msftncsi.com", // "dns.msftncsi.com",
"www.msftncsi.com", // "www.msftncsi.com",
"www.msftconnecttest.com" // "www.msftconnecttest.com"
] // ]
); // );
revise!(config, "dns", new_dns); // revise!(config, "dns", new_dns);
} // }
config // config
} // }
// /// only 5 default fields available (clash config fields) // /// only 5 default fields available (clash config fields)
// /// convert to lowercase // /// convert to lowercase

View File

@ -363,6 +363,7 @@ impl Core {
profile_config, profile_config,
profile_enhanced.chain, profile_enhanced.chain,
profile_enhanced.valid, profile_enhanced.valid,
tun_mode,
); );
dbg!(result); dbg!(result);