性能优化
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
//! 代理模块 Tauri 命令层。
|
||||
|
||||
use tauri::{AppHandle, State};
|
||||
|
||||
use super::system_proxy::{clear_system_proxy_windows, get_system_proxy_windows, set_system_proxy_windows};
|
||||
use super::{
|
||||
KernelInfo, KernelUpdateInfo, MihomoManager, ProfileMeta, ProxySettings, ProxyStatus,
|
||||
};
|
||||
|
||||
use crate::process_manager::{ProcessInfo, ProcessManager};
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_get_settings(state: State<'_, MihomoManager>) -> ProxySettings {
|
||||
state.load_settings()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_save_settings(
|
||||
state: State<'_, MihomoManager>,
|
||||
settings: ProxySettings,
|
||||
) -> Result<(), String> {
|
||||
state.save_settings(&settings)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_kernel_info(
|
||||
state: State<'_, MihomoManager>,
|
||||
app: AppHandle,
|
||||
) -> Result<KernelInfo, String> {
|
||||
state.prepare_kernel(&app)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_check_kernel_update(
|
||||
state: State<'_, MihomoManager>,
|
||||
) -> Result<KernelUpdateInfo, String> {
|
||||
state.check_kernel_update().await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_update_kernel(
|
||||
state: State<'_, MihomoManager>,
|
||||
app: AppHandle,
|
||||
mirror_prefix: Option<String>,
|
||||
) -> Result<KernelInfo, String> {
|
||||
state.install_kernel(&app, mirror_prefix.unwrap_or_default()).await
|
||||
}
|
||||
|
||||
/// 首次安装内核(与 update_kernel 共用 install_kernel 实现,语义独立便于前端区分场景)
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_install_kernel(
|
||||
state: State<'_, MihomoManager>,
|
||||
app: AppHandle,
|
||||
mirror_prefix: Option<String>,
|
||||
) -> Result<KernelInfo, String> {
|
||||
state.install_kernel(&app, mirror_prefix.unwrap_or_default()).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_status(pm: State<'_, ProcessManager>) -> ProxyStatus {
|
||||
match pm.get_status("proxy") {
|
||||
Some(p) => ProxyStatus {
|
||||
running: matches!(p.status, crate::process_manager::ProcessStatus::Running),
|
||||
pid: p.pid,
|
||||
restart_count: p.restart_count,
|
||||
},
|
||||
None => ProxyStatus {
|
||||
running: false,
|
||||
pid: None,
|
||||
restart_count: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_start(
|
||||
state: State<'_, MihomoManager>,
|
||||
pm: State<'_, ProcessManager>,
|
||||
app: AppHandle,
|
||||
) -> Result<ProcessInfo, String> {
|
||||
let params = state.prepare_for_start(&app)?;
|
||||
pm.start(params)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_stop(pm: State<'_, ProcessManager>) -> Result<(), String> {
|
||||
pm.stop("proxy")
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_restart(
|
||||
state: State<'_, MihomoManager>,
|
||||
pm: State<'_, ProcessManager>,
|
||||
app: AppHandle,
|
||||
) -> Result<ProcessInfo, String> {
|
||||
let _ = pm.stop("proxy");
|
||||
// 等待 TCP 端口释放(Windows 上 kill 后端口释放有延迟),在阻塞线程池中 sleep 避免阻塞主线程
|
||||
tauri::async_runtime::spawn_blocking(|| {
|
||||
std::thread::sleep(std::time::Duration::from_millis(800));
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("sleep 失败: {}", e))?;
|
||||
let params = state.prepare_for_start(&app)?;
|
||||
pm.start(params)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn proxy_version(state: State<'_, MihomoManager>) -> Result<serde_json::Value, String> {
|
||||
state.get_version().await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn proxy_get_proxies(state: State<'_, MihomoManager>) -> Result<serde_json::Value, String> {
|
||||
state.get_proxies().await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_select_proxy(
|
||||
state: State<'_, MihomoManager>,
|
||||
group: String,
|
||||
name: String,
|
||||
) -> Result<(), String> {
|
||||
state.select_proxy(&group, &name).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_test_delay(
|
||||
state: State<'_, MihomoManager>,
|
||||
name: String,
|
||||
url: Option<String>,
|
||||
timeout: Option<u32>,
|
||||
) -> Result<u32, String> {
|
||||
state
|
||||
.test_delay(
|
||||
&name,
|
||||
url.as_deref().unwrap_or("https://www.gstatic.com/generate_204"),
|
||||
timeout.unwrap_or(5000),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn proxy_get_connections(
|
||||
state: State<'_, MihomoManager>,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
state.get_connections().await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_close_connection(
|
||||
state: State<'_, MihomoManager>,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
state.close_connection(&id).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn proxy_patch_configs(
|
||||
state: State<'_, MihomoManager>,
|
||||
body: serde_json::Value,
|
||||
) -> Result<(), String> {
|
||||
state.patch_configs(body).await
|
||||
}
|
||||
|
||||
// ---------- 订阅 ----------
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_import_profile(
|
||||
state: State<'_, MihomoManager>,
|
||||
url: String,
|
||||
name: String,
|
||||
) -> Result<ProfileMeta, String> {
|
||||
state.import_profile(&url, &name).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_update_profile(
|
||||
state: State<'_, MihomoManager>,
|
||||
id: String,
|
||||
) -> Result<ProfileMeta, String> {
|
||||
state.update_profile(&id).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_delete_profile(
|
||||
state: State<'_, MihomoManager>,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
state.delete_profile(&id)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_activate_profile(
|
||||
state: State<'_, MihomoManager>,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
state.activate_profile(&id)
|
||||
}
|
||||
|
||||
// ---------- 系统代理 ----------
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_set_system_proxy(
|
||||
state: State<'_, MihomoManager>,
|
||||
) -> Result<(), String> {
|
||||
let settings = state.load_settings();
|
||||
let addr = format!("127.0.0.1:{}", settings.mixed_port);
|
||||
set_system_proxy_windows(&addr)?;
|
||||
let mut settings = settings;
|
||||
settings.system_proxy = true;
|
||||
state.save_settings(&settings)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_clear_system_proxy(
|
||||
state: State<'_, MihomoManager>,
|
||||
) -> Result<(), String> {
|
||||
clear_system_proxy_windows()?;
|
||||
let mut settings = state.load_settings();
|
||||
settings.system_proxy = false;
|
||||
state.save_settings(&settings)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_get_system_proxy() -> bool {
|
||||
get_system_proxy_windows()
|
||||
}
|
||||
Reference in New Issue
Block a user