feat: 实现仪表盘页面及系统信息采集功能
新增系统信息采集后端服务,引入 sysinfo 依赖获取 CPU、内存、主机名、系统版本、架构和 IP 地址等数据;新增系统相关 API 接口和 TypeScript 类型定义;重构仪表盘页面,整合服务器/隧道数量统计、系统信息展示、资源占用监控和 Frpc 运行日志功能;新增中英文国际化文案支持,完善前端交互逻辑。
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
pub mod tunnel;
|
||||
pub mod server;
|
||||
pub mod server;
|
||||
pub mod system;
|
||||
@@ -0,0 +1,72 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use sysinfo::{Networks, System};
|
||||
use tauri::State;
|
||||
use tauri_helper::auto_collect_command;
|
||||
|
||||
use crate::models::{ApiResponse, SystemDetails, SystemInfo};
|
||||
use crate::services::system::SystemStats;
|
||||
|
||||
/// 获取系统资源占用(CPU / 内存百分比)
|
||||
#[tauri::command]
|
||||
#[auto_collect_command]
|
||||
pub async fn get_system_info(
|
||||
state: State<'_, SystemStats>,
|
||||
) -> Result<ApiResponse<SystemInfo>, String> {
|
||||
let (cpu, memory) = state.snapshot();
|
||||
Ok(ApiResponse::success(SystemInfo { cpu, memory }))
|
||||
}
|
||||
|
||||
/// 获取系统信息(主机名、系统版本、架构、IP 地址)
|
||||
#[tauri::command]
|
||||
#[auto_collect_command]
|
||||
pub async fn get_system_details() -> Result<ApiResponse<SystemDetails>, String> {
|
||||
Ok(ApiResponse::success(collect_details()))
|
||||
}
|
||||
|
||||
fn collect_details() -> SystemDetails {
|
||||
let hostname = System::host_name().unwrap_or_else(|| "-".to_string());
|
||||
let os = System::long_os_version()
|
||||
.or_else(System::os_version)
|
||||
.or_else(System::name)
|
||||
.unwrap_or_else(|| "-".to_string());
|
||||
let arch = System::cpu_arch();
|
||||
|
||||
// 采集本机 IPv4 地址(过滤回环地址),并去重
|
||||
let networks = Networks::new_with_refreshed_list();
|
||||
let mut ips: Vec<String> = Vec::new();
|
||||
for data in networks.list().values() {
|
||||
for ip_net in data.ip_networks() {
|
||||
if let IpAddr::V4(ipv4) = ip_net.addr {
|
||||
if !ipv4.is_loopback() {
|
||||
ips.push(ipv4.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ips.sort();
|
||||
ips.dedup();
|
||||
|
||||
SystemDetails {
|
||||
hostname,
|
||||
os,
|
||||
arch,
|
||||
ips,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn details_are_collected() {
|
||||
let d = collect_details();
|
||||
println!(
|
||||
"hostname={}, os={}, arch={}, ips={:?}",
|
||||
d.hostname, d.os, d.arch, d.ips
|
||||
);
|
||||
assert!(!d.hostname.is_empty());
|
||||
assert!(!d.arch.is_empty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user