2025-03-01 22:52:43 +08:00
|
|
|
|
use super::CmdResult;
|
2025-03-13 12:51:20 +08:00
|
|
|
|
use crate::wrap_err;
|
2025-03-01 22:52:43 +08:00
|
|
|
|
use network_interface::NetworkInterface;
|
2025-03-13 12:51:20 +08:00
|
|
|
|
use serde_yaml::Mapping;
|
|
|
|
|
|
use sysproxy::{Autoproxy, Sysproxy};
|
2025-05-04 20:35:04 +08:00
|
|
|
|
use tokio::task::spawn_blocking;
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
|
|
|
|
|
/// get the system proxy
|
|
|
|
|
|
#[tauri::command]
|
2025-05-04 20:35:04 +08:00
|
|
|
|
pub async fn get_sys_proxy() -> CmdResult<Mapping> {
|
|
|
|
|
|
let current = spawn_blocking(move || Sysproxy::get_system_proxy())
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map_err(|e| format!("Failed to spawn blocking task for sysproxy: {}", e))?
|
|
|
|
|
|
.map_err(|e| format!("Failed to get system proxy: {}", e))?;
|
|
|
|
|
|
|
2025-03-01 22:52:43 +08:00
|
|
|
|
let mut map = Mapping::new();
|
|
|
|
|
|
map.insert("enable".into(), current.enable.into());
|
|
|
|
|
|
map.insert(
|
|
|
|
|
|
"server".into(),
|
|
|
|
|
|
format!("{}:{}", current.host, current.port).into(),
|
|
|
|
|
|
);
|
|
|
|
|
|
map.insert("bypass".into(), current.bypass.into());
|
|
|
|
|
|
|
|
|
|
|
|
Ok(map)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// get the system proxy
|
|
|
|
|
|
#[tauri::command]
|
2025-05-04 20:35:04 +08:00
|
|
|
|
pub async fn get_auto_proxy() -> CmdResult<Mapping> {
|
|
|
|
|
|
let current = spawn_blocking(move || Autoproxy::get_auto_proxy())
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map_err(|e| format!("Failed to spawn blocking task for autoproxy: {}", e))?
|
|
|
|
|
|
.map_err(|e| format!("Failed to get auto proxy: {}", e))?;
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
|
|
|
|
|
let mut map = Mapping::new();
|
|
|
|
|
|
map.insert("enable".into(), current.enable.into());
|
|
|
|
|
|
map.insert("url".into(), current.url.into());
|
|
|
|
|
|
|
|
|
|
|
|
Ok(map)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-16 10:22:53 +08:00
|
|
|
|
/// 获取系统主机名
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
pub fn get_system_hostname() -> CmdResult<String> {
|
|
|
|
|
|
use gethostname::gethostname;
|
2025-04-17 15:20:12 +08:00
|
|
|
|
|
2025-04-16 10:22:53 +08:00
|
|
|
|
// 获取系统主机名,处理可能的非UTF-8字符
|
|
|
|
|
|
let hostname = match gethostname().into_string() {
|
|
|
|
|
|
Ok(name) => name,
|
|
|
|
|
|
Err(os_string) => {
|
|
|
|
|
|
// 对于包含非UTF-8的主机名,使用调试格式化
|
|
|
|
|
|
let fallback = format!("{:?}", os_string);
|
|
|
|
|
|
// 去掉可能存在的引号
|
|
|
|
|
|
fallback.trim_matches('"').to_string()
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-04-17 15:20:12 +08:00
|
|
|
|
|
2025-04-16 10:22:53 +08:00
|
|
|
|
Ok(hostname)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-01 22:52:43 +08:00
|
|
|
|
/// 获取网络接口列表
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
pub fn get_network_interfaces() -> Vec<String> {
|
|
|
|
|
|
use sysinfo::Networks;
|
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
|
let networks = Networks::new_with_refreshed_list();
|
|
|
|
|
|
for (interface_name, _) in &networks {
|
|
|
|
|
|
result.push(interface_name.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
result
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 获取网络接口详细信息
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
pub fn get_network_interfaces_info() -> CmdResult<Vec<NetworkInterface>> {
|
2025-03-13 12:51:20 +08:00
|
|
|
|
use network_interface::{NetworkInterface, NetworkInterfaceConfig};
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
|
|
|
|
|
let names = get_network_interfaces();
|
|
|
|
|
|
let interfaces = wrap_err!(NetworkInterface::show())?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
for interface in interfaces {
|
|
|
|
|
|
if names.contains(&interface.name) {
|
|
|
|
|
|
result.push(interface);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
|
}
|