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 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<PrfData>,
valid: Vec<String>,
// tun_enable: bool,
tun_mode: bool,
) -> (Mapping, HashMap<String, ResultLog>) {
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)

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))
}
/// 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

View File

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