feat(network): 添加网络接口信息获取功能
- 新增 network-interface 依赖用于获取系统网络接口- 实现 Rust 端 get_network_interfaces 命令- 在 Svelte 前端调用 Tauri 命令并展示网络接口信息 - 添加接口信息表格展示及错误处理 - 更新布局结构移除多余注释和空行
This commit is contained in:
37
src-tauri/src/network.rs
Normal file
37
src-tauri/src/network.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct InterfaceInfo {
|
||||
pub name: String,
|
||||
pub addr: String, // IP 地址
|
||||
pub mac: Option<String>, // MAC 地址,可能不存在
|
||||
}
|
||||
|
||||
|
||||
fn map_interface_to_info(interface: &NetworkInterface) -> InterfaceInfo {
|
||||
let mac_address = interface.mac_addr.as_ref().map(|mac| mac.to_string());
|
||||
let ip_addr = interface.addr.iter().find(|addr| addr.ip().is_ipv4())
|
||||
.map(|addr| addr.ip().to_string())
|
||||
.unwrap_or_else(|| "N/A".to_string());
|
||||
|
||||
InterfaceInfo {
|
||||
name: interface.name.clone(),
|
||||
addr: ip_addr,
|
||||
mac: mac_address,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_network_interfaces() -> Result<Vec<InterfaceInfo>, String> {
|
||||
match NetworkInterface::show() {
|
||||
Ok(interfaces) => {
|
||||
let info_list: Vec<InterfaceInfo> = interfaces.into_iter()
|
||||
.map(|interface| map_interface_to_info(&interface))
|
||||
.collect();
|
||||
Ok(info_list)
|
||||
}
|
||||
Err(e) => Err(format!("无法获取网络接口信息: {}", e)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user