feat: Optimize kernel startup logic

1. tun mode startup logic
2. Remove invalid creation process PID logic
This commit is contained in:
huzibaca
2024-09-12 15:35:08 +08:00
parent 2396a6b35a
commit 9ec8c903c5
5 changed files with 18 additions and 31 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) = {
let (clash_core, enable_tun, enable_builtin, socks_enabled, http_enabled, enable_service_mode) = {
let verge = Config::verge();
let verge = verge.latest();
(
@@ -34,6 +34,7 @@ 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"))]
@@ -259,7 +260,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
});
}
config = use_tun(config, enable_tun).await;
config = use_tun(config, enable_tun, enable_service_mode).await;
config = use_sort(config);
let mut exists_set = HashSet::new();

View File

@@ -18,28 +18,33 @@ macro_rules! append {
};
}
pub async fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
pub async fn use_tun(mut config: Mapping, enable: bool, enable_service_mode: 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);
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);
use_dns_for_tun(config)
} else {
log_err!(service::unset_dns_by_service().await);
config
}
}