2025-03-01 22:52:43 +08:00
|
|
|
|
use super::CmdResult;
|
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
|
|
|
|
use crate::core::{EventDrivenProxyManager, async_proxy_query::AsyncProxyQuery};
|
2025-08-22 03:41:14 +08:00
|
|
|
|
use crate::process::AsyncHandler;
|
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-08-30 02:24:47 +08:00
|
|
|
|
use serde_yaml_ng::Mapping;
|
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> {
|
2025-06-21 21:48:39 +08:00
|
|
|
|
log::debug!(target: "app", "异步获取系统代理配置");
|
|
|
|
|
|
|
|
|
|
|
|
let current = AsyncProxyQuery::get_system_proxy().await;
|
2025-05-04 20:35:04 +08:00
|
|
|
|
|
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());
|
|
|
|
|
|
|
2025-06-21 21:48:39 +08:00
|
|
|
|
log::debug!(target: "app", "返回系统代理配置: enable={}, {}:{}", current.enable, current.host, current.port);
|
2025-03-01 22:52:43 +08:00
|
|
|
|
Ok(map)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-21 21:38:42 +08:00
|
|
|
|
/// 获取自动代理配置
|
2025-03-01 22:52:43 +08:00
|
|
|
|
#[tauri::command]
|
2025-05-04 20:35:04 +08:00
|
|
|
|
pub async fn get_auto_proxy() -> CmdResult<Mapping> {
|
2025-06-21 21:38:42 +08:00
|
|
|
|
log::debug!(target: "app", "开始获取自动代理配置(事件驱动)");
|
|
|
|
|
|
|
|
|
|
|
|
let proxy_manager = EventDrivenProxyManager::global();
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
let current = proxy_manager.get_auto_proxy_cached().await;
|
2025-06-21 21:38:42 +08:00
|
|
|
|
// 异步请求更新,立即返回缓存数据
|
2025-08-22 03:41:14 +08:00
|
|
|
|
AsyncHandler::spawn(move || async move {
|
2025-06-21 21:38:42 +08:00
|
|
|
|
let _ = proxy_manager.get_auto_proxy_async().await;
|
|
|
|
|
|
});
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
|
|
|
|
|
let mut map = Mapping::new();
|
|
|
|
|
|
map.insert("enable".into(), current.enable.into());
|
2025-06-21 21:38:42 +08:00
|
|
|
|
map.insert("url".into(), current.url.clone().into());
|
2025-03-01 22:52:43 +08:00
|
|
|
|
|
2025-06-21 21:38:42 +08:00
|
|
|
|
log::debug!(target: "app", "返回自动代理配置(缓存): enable={}, url={}", current.enable, current.url);
|
2025-03-01 22:52:43 +08:00
|
|
|
|
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的主机名,使用调试格式化
|
2025-06-27 23:30:35 +08:00
|
|
|
|
let fallback = format!("{os_string:?}");
|
2025-04-16 10:22:53 +08:00
|
|
|
|
// 去掉可能存在的引号
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|