2025-03-03 05:58:12 +08:00
|
|
|
use super::CmdResult;
|
2025-03-13 12:51:20 +08:00
|
|
|
use crate::{
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
core::{CoreManager, handle},
|
2025-08-18 02:02:25 +08:00
|
|
|
logging,
|
2025-03-13 12:51:20 +08:00
|
|
|
module::sysinfo::PlatformSpecification,
|
2025-08-18 02:02:25 +08:00
|
|
|
utils::logging::Type,
|
2025-03-13 12:51:20 +08:00
|
|
|
};
|
2025-03-14 13:31:34 +08:00
|
|
|
use once_cell::sync::Lazy;
|
2025-03-14 22:40:56 +08:00
|
|
|
use std::{
|
|
|
|
|
sync::atomic::{AtomicI64, Ordering},
|
|
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
|
|
|
};
|
2025-03-03 05:58:12 +08:00
|
|
|
use tauri_plugin_clipboard_manager::ClipboardExt;
|
|
|
|
|
|
2025-03-14 13:31:34 +08:00
|
|
|
// 存储应用启动时间的全局变量
|
|
|
|
|
static APP_START_TIME: Lazy<AtomicI64> = Lazy::new(|| {
|
|
|
|
|
// 获取当前系统时间,转换为毫秒级时间戳
|
|
|
|
|
let now = SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_millis() as i64;
|
|
|
|
|
|
|
|
|
|
AtomicI64::new(now)
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-03 05:58:12 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn export_diagnostic_info() -> CmdResult<()> {
|
2025-08-18 02:02:25 +08:00
|
|
|
let sysinfo = PlatformSpecification::new_sync();
|
2025-06-27 23:30:35 +08:00
|
|
|
let info = format!("{sysinfo:?}");
|
2025-03-03 05:58:12 +08:00
|
|
|
|
2025-10-08 12:32:40 +08:00
|
|
|
let app_handle = handle::Handle::app_handle();
|
2025-03-03 05:58:12 +08:00
|
|
|
let cliboard = app_handle.clipboard();
|
2025-03-13 12:51:20 +08:00
|
|
|
if cliboard.write_text(info).is_err() {
|
2025-08-18 02:02:25 +08:00
|
|
|
logging!(error, Type::System, "Failed to write to clipboard");
|
2025-03-03 05:58:12 +08:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-03-03 14:42:31 +08:00
|
|
|
|
2025-03-14 13:31:34 +08:00
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn get_system_info() -> CmdResult<String> {
|
2025-08-18 02:02:25 +08:00
|
|
|
let sysinfo = PlatformSpecification::new_sync();
|
2025-06-27 23:30:35 +08:00
|
|
|
let info = format!("{sysinfo:?}");
|
2025-03-14 13:31:34 +08:00
|
|
|
Ok(info)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 14:42:31 +08:00
|
|
|
/// 获取当前内核运行模式
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub async fn get_running_mode() -> Result<String, String> {
|
2025-08-18 02:02:25 +08:00
|
|
|
Ok(CoreManager::global().get_running_mode().to_string())
|
2025-03-03 14:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-14 13:31:34 +08:00
|
|
|
/// 获取应用的运行时间(毫秒)
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
pub fn get_app_uptime() -> CmdResult<i64> {
|
|
|
|
|
let start_time = APP_START_TIME.load(Ordering::Relaxed);
|
|
|
|
|
let now = SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_millis() as i64;
|
|
|
|
|
|
|
|
|
|
Ok(now - start_time)
|
|
|
|
|
}
|
2025-03-31 03:22:24 +08:00
|
|
|
|
|
|
|
|
/// 检查应用是否以管理员身份运行
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
pub fn is_admin() -> CmdResult<bool> {
|
|
|
|
|
use deelevate::{PrivilegeLevel, Token};
|
2025-03-31 08:16:14 +08:00
|
|
|
|
2025-03-31 03:22:24 +08:00
|
|
|
let result = Token::with_current_process()
|
|
|
|
|
.and_then(|token| token.privilege_level())
|
|
|
|
|
.map(|level| level != PrivilegeLevel::NotPrivileged)
|
|
|
|
|
.unwrap_or(false);
|
2025-03-31 08:16:14 +08:00
|
|
|
|
2025-03-31 03:22:24 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2025-03-31 08:16:14 +08:00
|
|
|
|
2025-03-31 03:22:24 +08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
{
|
|
|
|
|
Ok(unsafe { libc::geteuid() } == 0)
|
|
|
|
|
}
|
2025-03-31 08:16:14 +08:00
|
|
|
|
2025-03-31 03:22:24 +08:00
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
|
|
|
|
|
{
|
|
|
|
|
Ok(false)
|
|
|
|
|
}
|
|
|
|
|
}
|