mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +08:00
refactor: streamline admin check logic and improve get_running_mode return type (#5325)
This commit is contained in:
@@ -1,16 +1,29 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::CmdResult;
|
use super::CmdResult;
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{CoreManager, handle},
|
core::{CoreManager, handle, manager::RunningMode},
|
||||||
logging,
|
logging,
|
||||||
module::sysinfo::PlatformSpecification,
|
module::sysinfo::PlatformSpecification,
|
||||||
utils::logging::Type,
|
utils::logging::Type,
|
||||||
};
|
};
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
use deelevate::{PrivilegeLevel, Token};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||||
use tokio::time::Instant;
|
use tokio::time::Instant;
|
||||||
|
|
||||||
// 存储应用启动时间的全局变量
|
// 存储应用启动时间的全局变量
|
||||||
static APP_START_TIME: Lazy<Instant> = Lazy::new(Instant::now);
|
static APP_START_TIME: Lazy<Instant> = Lazy::new(Instant::now);
|
||||||
|
#[cfg(not(target_os = "windows"))]
|
||||||
|
static APPS_RUN_AS_ADMIN: Lazy<bool> = Lazy::new(|| unsafe { libc::geteuid() } == 0);
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
static APPS_RUN_AS_ADMIN: Lazy<bool> = Lazy::new(|| {
|
||||||
|
Token::with_current_process()
|
||||||
|
.and_then(|token| token.privilege_level())
|
||||||
|
.map(|level| level != PrivilegeLevel::NotPrivileged)
|
||||||
|
.unwrap_or(false)
|
||||||
|
});
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn export_diagnostic_info() -> CmdResult<()> {
|
pub async fn export_diagnostic_info() -> CmdResult<()> {
|
||||||
@@ -34,8 +47,8 @@ pub async fn get_system_info() -> CmdResult<String> {
|
|||||||
|
|
||||||
/// 获取当前内核运行模式
|
/// 获取当前内核运行模式
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn get_running_mode() -> Result<String, String> {
|
pub async fn get_running_mode() -> Result<Arc<RunningMode>, String> {
|
||||||
Ok(CoreManager::global().get_running_mode().to_string())
|
Ok(CoreManager::global().get_running_mode())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 获取应用的运行时间(毫秒)
|
/// 获取应用的运行时间(毫秒)
|
||||||
@@ -46,34 +59,6 @@ pub fn get_app_uptime() -> CmdResult<u128> {
|
|||||||
|
|
||||||
/// 检查应用是否以管理员身份运行
|
/// 检查应用是否以管理员身份运行
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
pub fn is_admin() -> CmdResult<bool> {
|
pub fn is_admin() -> CmdResult<bool> {
|
||||||
use deelevate::{PrivilegeLevel, Token};
|
Ok(*APPS_RUN_AS_ADMIN)
|
||||||
|
|
||||||
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<bool> {
|
|
||||||
#[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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user