From 5759bed687770b93cb85e6c8b197757f05bbc6f5 Mon Sep 17 00:00:00 2001 From: Slinetrac Date: Fri, 7 Nov 2025 18:58:13 +0800 Subject: [PATCH] refactor(linux): replace users crate with libc::geteuid for root detection Closes #5333 --- src-tauri/Cargo.lock | 11 ----------- src-tauri/Cargo.toml | 3 --- src-tauri/src/core/service.rs | 27 +++++++++++++++++---------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index d433be369..993563eea 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1158,7 +1158,6 @@ dependencies = [ "tauri-plugin-window-state", "tokio", "tokio-stream", - "users", "warp", "winapi", "windows-sys 0.61.2", @@ -8699,16 +8698,6 @@ dependencies = [ "url", ] -[[package]] -name = "users" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" -dependencies = [ - "libc", - "log", -] - [[package]] name = "utf-8" version = "0.7.6" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7cb1cf3da..6311a8083 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -112,9 +112,6 @@ windows-sys = { version = "0.61.2", features = [ "Win32_UI_WindowsAndMessaging", ] } -[target.'cfg(target_os = "linux")'.dependencies] -users = "0.11.0" - [target.'cfg(unix)'.dependencies] signal-hook = "0.3.18" diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 28e0dc97a..284033900 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -122,7 +122,6 @@ async fn reinstall_service() -> Result<()> { #[cfg(target_os = "linux")] async fn uninstall_service() -> Result<()> { logging!(info, Type::Service, "uninstall service"); - use users::get_effective_uid; let uninstall_path = tauri::utils::platform::current_exe()?.with_file_name("clash-verge-service-uninstall"); @@ -134,13 +133,14 @@ async fn uninstall_service() -> Result<()> { let uninstall_shell: String = uninstall_path.to_string_lossy().replace(" ", "\\ "); let elevator = crate::utils::help::linux_elevator(); - let status = match get_effective_uid() { - 0 => StdCommand::new(uninstall_shell).status()?, - _ => StdCommand::new(elevator) + let status = if linux_running_as_root() { + StdCommand::new(&uninstall_path).status()? + } else { + StdCommand::new(elevator) .arg("sh") .arg("-c") .arg(uninstall_shell) - .status()?, + .status()? }; logging!( info, @@ -163,7 +163,6 @@ async fn uninstall_service() -> Result<()> { #[allow(clippy::unused_async)] async fn install_service() -> Result<()> { logging!(info, Type::Service, "install service"); - use users::get_effective_uid; let install_path = tauri::utils::platform::current_exe()?.with_file_name("clash-verge-service-install"); @@ -175,13 +174,14 @@ async fn install_service() -> Result<()> { let install_shell: String = install_path.to_string_lossy().replace(" ", "\\ "); let elevator = crate::utils::help::linux_elevator(); - let status = match get_effective_uid() { - 0 => StdCommand::new(install_shell).status()?, - _ => StdCommand::new(elevator) + let status = if linux_running_as_root() { + StdCommand::new(&install_path).status()? + } else { + StdCommand::new(elevator) .arg("sh") .arg("-c") .arg(install_shell) - .status()?, + .status()? }; logging!( info, @@ -218,6 +218,13 @@ async fn reinstall_service() -> Result<()> { } } +#[cfg(target_os = "linux")] +fn linux_running_as_root() -> bool { + const ROOT_UID: u32 = 0; + + unsafe { libc::geteuid() == ROOT_UID } +} + #[cfg(target_os = "macos")] async fn uninstall_service() -> Result<()> { use crate::utils::i18n::t;