refactor: tun mode is turned on and off, does not depend on services, and does not affect other configurations

This commit is contained in:
huzibaca
2024-10-05 22:45:59 +08:00
parent 7869ce060f
commit 5fdb52d8d0
6 changed files with 28 additions and 158 deletions

View File

@@ -25,7 +25,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
// config.yaml 的订阅
let clash_config = { Config::clash().latest().0.clone() };
let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled, enable_service_mode) = {
let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled) = {
let verge = Config::verge();
let verge = verge.latest();
(
@@ -34,7 +34,6 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
verge.enable_builtin_enhanced.unwrap_or(true),
verge.verge_socks_enabled.unwrap_or(false),
verge.verge_http_enabled.unwrap_or(false),
verge.enable_service_mode.unwrap_or(false),
)
};
#[cfg(not(target_os = "windows"))]
@@ -260,7 +259,9 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
});
}
config = use_tun(config, enable_tun, enable_service_mode).await;
if enable_tun {
config = use_tun(config).await;
}
config = use_sort(config);
let mut exists_set = HashSet::new();

View File

@@ -1,4 +1,3 @@
use crate::{core::service, log_err};
use serde_yaml::{Mapping, Value};
macro_rules! revise {
@@ -9,6 +8,7 @@ macro_rules! revise {
}
// if key not exists then append value
#[allow(unused_macros)]
macro_rules! append {
($map: expr, $key: expr, $val: expr) => {
let ret_key = Value::String($key.into());
@@ -18,66 +18,13 @@ macro_rules! append {
};
}
pub async fn use_tun(mut config: Mapping, enable: bool, enable_service_mode: bool) -> Mapping {
pub async fn use_tun(mut config: Mapping) -> 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);
revise!(tun_val, "enable", true);
revise!(config, "tun", tun_val);
if enable_service_mode {
handle_dns_service(enable).await;
if enable {
return use_dns_for_tun(config);
}
}
config
}
async fn handle_dns_service(enable: bool) {
if enable {
log_err!(service::set_dns_by_service().await);
} else {
log_err!(service::unset_dns_by_service().await);
}
}
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);
append!(dns_val, "enhanced-mode", "fake-ip");
append!(dns_val, "fake-ip-range", "198.18.0.1/16");
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
return config;
}