mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 05:03:45 +08:00
feat: Optimize kernel startup logic
1. tun mode startup logic 2. Remove invalid creation process PID logic
This commit is contained in:
parent
894960ef5a
commit
4a76997044
@ -2,11 +2,11 @@ use crate::config::*;
|
|||||||
use crate::core::{clash_api, handle, logger::Logger, service};
|
use crate::core::{clash_api, handle, logger::Logger, service};
|
||||||
use crate::log_err;
|
use crate::log_err;
|
||||||
use crate::utils::dirs;
|
use crate::utils::dirs;
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Result};
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use serde_yaml::Mapping;
|
use serde_yaml::Mapping;
|
||||||
use std::{fs, io::Write, sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
|
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||||
@ -177,22 +177,7 @@ impl CoreManager {
|
|||||||
|
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
if let Some(app_handle) = app_handle.as_ref() {
|
||||||
let cmd = app_handle.shell().sidecar(clash_core)?;
|
let cmd = app_handle.shell().sidecar(clash_core)?;
|
||||||
let (mut rx, cmd_child) = cmd.args(args).spawn()?;
|
let (mut rx, _) = cmd.args(args).spawn()?;
|
||||||
|
|
||||||
// 将pid写入文件中
|
|
||||||
crate::log_err!((|| {
|
|
||||||
let pid = cmd_child.pid();
|
|
||||||
let path = dirs::clash_pid_path()?;
|
|
||||||
fs::File::create(path)
|
|
||||||
.context("failed to create the pid file")?
|
|
||||||
.write(format!("{pid}").as_bytes())
|
|
||||||
.context("failed to write pid to the file")?;
|
|
||||||
<Result<()>>::Ok(())
|
|
||||||
})());
|
|
||||||
|
|
||||||
let mut sidecar = self.sidecar.lock();
|
|
||||||
*sidecar = Some(cmd_child);
|
|
||||||
drop(sidecar);
|
|
||||||
|
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
while let Some(event) = rx.recv().await {
|
while let Some(event) = rx.recv().await {
|
||||||
|
@ -4,7 +4,7 @@ use anyhow::{bail, Result};
|
|||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tauri::{AppHandle, WebviewWindow, Manager, Emitter};
|
use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct Handle {
|
pub struct Handle {
|
||||||
|
@ -25,7 +25,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, HashMap<String, ResultLog>) {
|
|||||||
// config.yaml 的订阅
|
// config.yaml 的订阅
|
||||||
let clash_config = { Config::clash().latest().0.clone() };
|
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 = Config::verge();
|
||||||
let verge = verge.latest();
|
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.enable_builtin_enhanced.unwrap_or(true),
|
||||||
verge.verge_socks_enabled.unwrap_or(false),
|
verge.verge_socks_enabled.unwrap_or(false),
|
||||||
verge.verge_http_enabled.unwrap_or(false),
|
verge.verge_http_enabled.unwrap_or(false),
|
||||||
|
verge.enable_service_mode.unwrap_or(false),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[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);
|
config = use_sort(config);
|
||||||
|
|
||||||
let mut exists_set = HashSet::new();
|
let mut exists_set = HashSet::new();
|
||||||
|
@ -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_key = Value::from("tun");
|
||||||
let tun_val = config.get(&tun_key);
|
let tun_val = config.get(&tun_key);
|
||||||
|
|
||||||
if !enable && tun_val.is_none() {
|
if !enable && tun_val.is_none() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tun_val = tun_val.map_or(Mapping::new(), |val| {
|
let mut tun_val = tun_val.map_or(Mapping::new(), |val| {
|
||||||
val.as_mapping().cloned().unwrap_or(Mapping::new())
|
val.as_mapping().cloned().unwrap_or(Mapping::new())
|
||||||
});
|
});
|
||||||
|
|
||||||
revise!(tun_val, "enable", enable);
|
revise!(tun_val, "enable", enable);
|
||||||
|
|
||||||
revise!(config, "tun", tun_val);
|
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 {
|
if enable {
|
||||||
log_err!(service::set_dns_by_service().await);
|
log_err!(service::set_dns_by_service().await);
|
||||||
use_dns_for_tun(config)
|
|
||||||
} else {
|
} else {
|
||||||
log_err!(service::unset_dns_by_service().await);
|
log_err!(service::unset_dns_by_service().await);
|
||||||
config
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,10 +102,6 @@ pub fn profiles_path() -> Result<PathBuf> {
|
|||||||
Ok(app_home_dir()?.join(PROFILE_YAML))
|
Ok(app_home_dir()?.join(PROFILE_YAML))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clash_pid_path() -> Result<PathBuf> {
|
|
||||||
Ok(app_home_dir()?.join("clash.pid"))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
pub fn service_path() -> Result<PathBuf> {
|
pub fn service_path() -> Result<PathBuf> {
|
||||||
Ok(app_resources_dir()?.join("clash-verge-service"))
|
Ok(app_resources_dir()?.join("clash-verge-service"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user