diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 1fa7612..cdf75b0 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -7,7 +7,7 @@ use once_cell::sync::OnceCell; use parking_lot::Mutex; use serde_yaml::Mapping; use std::{sync::Arc, time::Duration}; -use sysinfo::System; +use sysinfo::{ProcessRefreshKind, RefreshKind, System}; use tauri::api::process::{Command, CommandChild, CommandEvent}; use tokio::time::sleep; @@ -70,12 +70,6 @@ impl CoreManager { pub async fn run_core(&self) -> Result<()> { let config_path = Config::generate_file(ConfigType::Run)?; - #[allow(unused_mut)] - let mut should_kill = match self.sidecar.lock().take() { - Some(_) => true, - None => false, - }; - // 关闭tun模式 let mut disable = Mapping::new(); let mut tun = Mapping::new(); @@ -84,23 +78,19 @@ impl CoreManager { log::debug!(target: "app", "disable tun mode"); let _ = clash_api::patch_configs(&disable).await; - let mut system = System::new(); - system.refresh_all(); - let procs = system.processes_by_name("verge-mihomo"); - for proc in procs { - log::debug!(target: "app", "kill all clash process"); - proc.kill(); - } - if *self.use_service_mode.lock() { log::debug!(target: "app", "stop the core by service"); log_err!(service::stop_core_by_service().await); - should_kill = true; - } + } else { + let mut system = System::new_with_specifics( + RefreshKind::new().with_processes(ProcessRefreshKind::everything()), + ); + let procs = system.processes_by_name("verge-mihomo"); - // 这里得等一会儿 - if should_kill { - sleep(Duration::from_millis(500)).await; + for proc in procs { + log::debug!(target: "app", "kill all clash process"); + proc.kill(); + } } // 服务模式 @@ -237,8 +227,9 @@ impl CoreManager { let mut sidecar = self.sidecar.lock(); let _ = sidecar.take(); - let mut system = System::new(); - system.refresh_all(); + let mut system = System::new_with_specifics( + RefreshKind::new().with_processes(ProcessRefreshKind::everything()), + ); let procs = system.processes_by_name("verge-mihomo"); for proc in procs { log::debug!(target: "app", "kill all clash process"); diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index 57a1928..e4d6503 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -107,48 +107,13 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> { Config::clash().draft().patch_config(patch.clone()); let res = { - let redir_port = patch.get("redir-port"); - let tproxy_port = patch.get("tproxy-port"); - let mixed_port = patch.get("mixed-port"); - let socks_port = patch.get("socks-port"); - let port = patch.get("port"); - let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false); - if mixed_port.is_some() && !enable_random_port { - let changed = mixed_port.unwrap() - != Config::verge() - .latest() - .verge_mixed_port - .unwrap_or(Config::clash().data().get_mixed_port()); - // 检查端口占用 - if changed { - if let Some(port) = mixed_port.unwrap().as_u64() { - if !port_scanner::local_port_available(port as u16) { - Config::clash().discard(); - bail!("port already in use"); - } - } - } - }; - // 激活订阅 - if redir_port.is_some() - || tproxy_port.is_some() - || mixed_port.is_some() - || socks_port.is_some() - || port.is_some() - || patch.get("secret").is_some() - || patch.get("external-controller").is_some() - { + if patch.get("secret").is_some() || patch.get("external-controller").is_some() { Config::generate().await?; CoreManager::global().run_core().await?; handle::Handle::refresh_clash(); } - // 更新系统代理 - if mixed_port.is_some() { - log_err!(sysopt::Sysopt::global().init_sysproxy()); - } - if patch.get("mode").is_some() { log_err!(handle::Handle::update_systray_part()); } @@ -174,7 +139,6 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> { /// 一般都是一个个的修改 pub async fn patch_verge(patch: IVerge) -> Result<()> { Config::verge().draft().patch_config(patch.clone()); - let tun_mode = patch.enable_tun_mode; let auto_launch = patch.enable_auto_launch; let system_proxy = patch.enable_system_proxy; @@ -182,7 +146,7 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { let pac_content = patch.pac_file_content; let proxy_bypass = patch.system_proxy_bypass; let language = patch.language; - let port = patch.verge_mixed_port; + let mixed_port = patch.verge_mixed_port; #[cfg(target_os = "macos")] let tray_icon = patch.tray_icon; let common_tray_icon = patch.common_tray_icon; @@ -190,41 +154,62 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { let tun_tray_icon = patch.tun_tray_icon; #[cfg(not(target_os = "windows"))] let redir_enabled = patch.verge_redir_enabled; + #[cfg(not(target_os = "windows"))] + let redir_port = patch.verge_redir_port; #[cfg(target_os = "linux")] let tproxy_enabled = patch.verge_tproxy_enabled; + #[cfg(target_os = "linux")] + let tproxy_port = patch.verge_tproxy_port; let socks_enabled = patch.verge_socks_enabled; + let socks_port = patch.verge_socks_port; let http_enabled = patch.verge_http_enabled; + let http_port = patch.verge_port; let res = { let service_mode = patch.enable_service_mode; - + let mut generated = false; if service_mode.is_some() { log::debug!(target: "app", "change service mode to {}", service_mode.unwrap()); - - Config::generate().await?; - CoreManager::global().run_core().await?; + if !generated { + Config::generate().await?; + CoreManager::global().run_core().await?; + generated = true; + } } else if tun_mode.is_some() { update_core_config().await?; } #[cfg(not(target_os = "windows"))] - if redir_enabled.is_some() { - Config::generate().await?; - CoreManager::global().run_core().await?; + if redir_enabled.is_some() || redir_port.is_some() { + if !generated { + Config::generate().await?; + CoreManager::global().run_core().await?; + generated = true; + } } #[cfg(target_os = "linux")] - if tproxy_enabled.is_some() { - Config::generate().await?; - CoreManager::global().run_core().await?; + if tproxy_enabled.is_some() || tproxy_port.is_some() { + if !generated { + Config::generate().await?; + CoreManager::global().run_core().await?; + generated = true; + } } - if socks_enabled.is_some() || http_enabled.is_some() { - Config::generate().await?; - CoreManager::global().run_core().await?; + if socks_enabled.is_some() + || http_enabled.is_some() + || socks_port.is_some() + || http_port.is_some() + || mixed_port.is_some() + { + if !generated { + Config::generate().await?; + CoreManager::global().run_core().await?; + } } if auto_launch.is_some() { sysopt::Sysopt::global().update_launch()?; } if system_proxy.is_some() || proxy_bypass.is_some() - || port.is_some() + || mixed_port.is_some() || pac.is_some() || pac_content.is_some() { diff --git a/src/components/setting/mods/clash-port-viewer.tsx b/src/components/setting/mods/clash-port-viewer.tsx index 1593c48..75a9536 100644 --- a/src/components/setting/mods/clash-port-viewer.tsx +++ b/src/components/setting/mods/clash-port-viewer.tsx @@ -13,7 +13,6 @@ export const ClashPortViewer = forwardRef((props, ref) => { const { clashInfo, patchInfo } = useClashInfo(); const { verge, patchVerge } = useVerge(); - const [open, setOpen] = useState(false); const [redirPort, setRedirPort] = useState( verge?.verge_redir_port ?? clashInfo?.redir_port ?? 7895 @@ -94,24 +93,57 @@ export const ClashPortViewer = forwardRef((props, ref) => { return; } try { - if (OS !== "windows") { - await patchInfo({ "redir-port": redirPort }); - await patchVerge({ verge_redir_port: redirPort }); - await patchVerge({ verge_redir_enabled: redirEnabled }); + if (OS === "windows") { + await patchInfo({ + "mixed-port": mixedPort, + "socks-port": socksPort, + port, + }); + await patchVerge({ + verge_mixed_port: mixedPort, + verge_socks_port: socksPort, + verge_socks_enabled: socksEnabled, + verge_port: port, + verge_http_enabled: httpEnabled, + }); + } + if (OS === "macos") { + await patchInfo({ + "redir-port": redirPort, + "mixed-port": mixedPort, + "socks-port": socksPort, + port, + }); + await patchVerge({ + verge_redir_port: redirPort, + verge_redir_enabled: redirEnabled, + verge_mixed_port: mixedPort, + verge_socks_port: socksPort, + verge_socks_enabled: socksEnabled, + verge_port: port, + verge_http_enabled: httpEnabled, + }); } if (OS === "linux") { - await patchInfo({ "tproxy-port": tproxyPort }); - await patchVerge({ verge_tproxy_port: tproxyPort }); - await patchVerge({ verge_tproxy_enabled: tproxyEnabled }); + await patchInfo({ + "redir-port": redirPort, + "tproxy-port": tproxyPort, + "mixed-port": mixedPort, + "socks-port": socksPort, + port, + }); + await patchVerge({ + verge_redir_port: redirPort, + verge_redir_enabled: redirEnabled, + verge_tproxy_port: tproxyPort, + verge_tproxy_enabled: tproxyEnabled, + verge_mixed_port: mixedPort, + verge_socks_port: socksPort, + verge_socks_enabled: socksEnabled, + verge_port: port, + verge_http_enabled: httpEnabled, + }); } - await patchInfo({ "mixed-port": mixedPort }); - await patchInfo({ "socks-port": socksPort }); - await patchInfo({ port }); - await patchVerge({ verge_mixed_port: mixedPort }); - await patchVerge({ verge_socks_port: socksPort }); - await patchVerge({ verge_port: port }); - await patchVerge({ verge_socks_enabled: socksEnabled }); - await patchVerge({ verge_http_enabled: httpEnabled }); setOpen(false); Notice.success(t("Clash Port Modified"), 1000); } catch (err: any) {