From 409b16b49f7602de1a9a66555321ac466f0d5aaa Mon Sep 17 00:00:00 2001 From: Tunglies Date: Thu, 6 Nov 2025 13:37:11 +0800 Subject: [PATCH] refactor: streamline admin check logic and improve get_running_mode return type (#5325) --- src-tauri/src/cmd/system.rs | 49 +++++++++++++------------------------ 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/src-tauri/src/cmd/system.rs b/src-tauri/src/cmd/system.rs index 2486c52d..8ba1e6c6 100644 --- a/src-tauri/src/cmd/system.rs +++ b/src-tauri/src/cmd/system.rs @@ -1,16 +1,29 @@ +use std::sync::Arc; + use super::CmdResult; use crate::{ - core::{CoreManager, handle}, + core::{CoreManager, handle, manager::RunningMode}, logging, module::sysinfo::PlatformSpecification, utils::logging::Type, }; +#[cfg(target_os = "windows")] +use deelevate::{PrivilegeLevel, Token}; use once_cell::sync::Lazy; use tauri_plugin_clipboard_manager::ClipboardExt; use tokio::time::Instant; // 存储应用启动时间的全局变量 static APP_START_TIME: Lazy = Lazy::new(Instant::now); +#[cfg(not(target_os = "windows"))] +static APPS_RUN_AS_ADMIN: Lazy = Lazy::new(|| unsafe { libc::geteuid() } == 0); +#[cfg(target_os = "windows")] +static APPS_RUN_AS_ADMIN: Lazy = Lazy::new(|| { + Token::with_current_process() + .and_then(|token| token.privilege_level()) + .map(|level| level != PrivilegeLevel::NotPrivileged) + .unwrap_or(false) +}); #[tauri::command] pub async fn export_diagnostic_info() -> CmdResult<()> { @@ -34,8 +47,8 @@ pub async fn get_system_info() -> CmdResult { /// 获取当前内核运行模式 #[tauri::command] -pub async fn get_running_mode() -> Result { - Ok(CoreManager::global().get_running_mode().to_string()) +pub async fn get_running_mode() -> Result, String> { + Ok(CoreManager::global().get_running_mode()) } /// 获取应用的运行时间(毫秒) @@ -46,34 +59,6 @@ pub fn get_app_uptime() -> CmdResult { /// 检查应用是否以管理员身份运行 #[tauri::command] -#[cfg(target_os = "windows")] pub fn is_admin() -> CmdResult { - use deelevate::{PrivilegeLevel, Token}; - - let result = Token::with_current_process() - .and_then(|token| token.privilege_level()) - .map(|level| level != PrivilegeLevel::NotPrivileged) - .unwrap_or(false); - - Ok(result) -} - -/// 非Windows平台检测是否以管理员身份运行 -#[tauri::command] -#[cfg(not(target_os = "windows"))] -pub fn is_admin() -> CmdResult { - #[cfg(target_os = "macos")] - { - Ok(unsafe { libc::geteuid() } == 0) - } - - #[cfg(target_os = "linux")] - { - Ok(unsafe { libc::geteuid() } == 0) - } - - #[cfg(not(any(target_os = "macos", target_os = "linux")))] - { - Ok(false) - } + Ok(*APPS_RUN_AS_ADMIN) }