细节调整及优化(26.8.3)
This commit is contained in:
@@ -7,7 +7,15 @@ use super::{
|
||||
KernelInfo, KernelUpdateInfo, MihomoManager, ProfileMeta, ProxySettings, ProxyStatus,
|
||||
};
|
||||
|
||||
use crate::process_manager::{ProcessInfo, ProcessManager};
|
||||
use crate::process_manager::{ProcessInfo, ProcessManager, ProcessStatus};
|
||||
|
||||
/// 判断 mihomo 进程是否处于运行状态
|
||||
fn mihomo_running(pm: &ProcessManager) -> bool {
|
||||
matches!(
|
||||
pm.get_status("proxy").map(|p| p.status),
|
||||
Some(ProcessStatus::Running)
|
||||
)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
@@ -41,25 +49,33 @@ pub async fn proxy_check_kernel_update(
|
||||
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
|
||||
pub fn proxy_cancel_kernel_install(state: State<'_, MihomoManager>) -> Result<(), String> {
|
||||
state.cancel_kernel_install();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 首次安装内核(与 update_kernel 共用 install_kernel 实现,语义独立便于前端区分场景)
|
||||
/// 前端确认 mihomo 已停止,唤醒等待中的安装流程继续解压替换。
|
||||
/// (下载阶段允许 mihomo 运行以便走系统代理,解压替换前必须停止 mihomo,否则 exe 被占用)
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_install_kernel(
|
||||
pub fn proxy_confirm_install(state: State<'_, MihomoManager>) -> Result<(), String> {
|
||||
state.confirm_install();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 应用内核更新:下载阶段已由下载模块完成,本方法仅做 need_stop → 解压 → 替换。
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn proxy_apply_kernel_update(
|
||||
state: State<'_, MihomoManager>,
|
||||
app: AppHandle,
|
||||
mirror_prefix: Option<String>,
|
||||
zip_path: String,
|
||||
) -> Result<KernelInfo, String> {
|
||||
state.install_kernel(&app, mirror_prefix.unwrap_or_default()).await
|
||||
let path = std::path::PathBuf::from(zip_path);
|
||||
state.apply_kernel_update(&app, path).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -87,12 +103,19 @@ pub fn proxy_start(
|
||||
app: AppHandle,
|
||||
) -> Result<ProcessInfo, String> {
|
||||
let params = state.prepare_for_start(&app)?;
|
||||
pm.start(params)
|
||||
let info = pm.start(params)?;
|
||||
// 手动启动也遵循「启动时自动开启系统代理」设置
|
||||
state.apply_auto_system_proxy();
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn proxy_stop(pm: State<'_, ProcessManager>) -> Result<(), String> {
|
||||
pub fn proxy_stop(state: State<'_, MihomoManager>, pm: State<'_, ProcessManager>) -> Result<(), String> {
|
||||
// 关闭 mihomo 时同步关闭系统代理(若开启),避免系统代理指向已停止的端口导致断网
|
||||
if get_system_proxy_windows() {
|
||||
let _ = state.disable_system_proxy();
|
||||
}
|
||||
pm.stop("proxy")
|
||||
}
|
||||
|
||||
@@ -110,8 +133,65 @@ pub async fn proxy_restart(
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("sleep 失败: {}", e))?;
|
||||
let params = state.prepare_for_start(&app)?;
|
||||
pm.start(params)
|
||||
|
||||
// 启动并等待 API 就绪,带有限重试(共 3 次):
|
||||
// - 冷加载大订阅(首次解析 + geo 下载)可能远超过前端 waitForApi 的 10s 预算,
|
||||
// 这里在命令内等满就绪,避免重启成功后仍被前端误判为「重启失败」导致节点不刷新。
|
||||
// - 偶发的端口未及时释放 / 启动瞬间退出,通过重试自愈。
|
||||
let mut last_err = "mihomo 启动失败".to_string();
|
||||
for _ in 0..3 {
|
||||
match start_and_wait(state.inner(), pm.inner(), &app).await {
|
||||
Ok(info) => return Ok(info),
|
||||
Err(e) => {
|
||||
last_err = e;
|
||||
// 上个实例刚退出,多等一会儿释放端口再重试
|
||||
tauri::async_runtime::spawn_blocking(|| {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1200));
|
||||
})
|
||||
.await
|
||||
.map_err(|x| format!("sleep 失败: {}", x))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 所有启动尝试均失败:mihomo 已停止,同步关闭系统代理(若开启),避免代理指向已停止端口导致断网
|
||||
if get_system_proxy_windows() {
|
||||
let _ = state.disable_system_proxy();
|
||||
}
|
||||
Err(last_err)
|
||||
}
|
||||
|
||||
/// 启动 mihomo 并等待其 HTTP API 就绪(最多 20s)。
|
||||
/// - 若启动后进程立即退出,快速返回(不白等满预算),便于外层尽早重试。
|
||||
/// - 若 pm.start 返回「已在运行中」,说明崩溃监控已抢先拉起进程,同样等待其 API 就绪即可。
|
||||
async fn start_and_wait(
|
||||
state: &MihomoManager,
|
||||
pm: &ProcessManager,
|
||||
app: &AppHandle,
|
||||
) -> Result<ProcessInfo, String> {
|
||||
let params = state.prepare_for_start(app)?;
|
||||
if let Err(e) = pm.start(params) {
|
||||
// "已在运行中" = 崩溃监控已拉起,不算失败;其余为上抛
|
||||
if !e.contains("运行中") {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(20);
|
||||
loop {
|
||||
// 进程已退出且 API 未就绪 → 启动失败(快速失败,交外层重试)
|
||||
if let Some(st) = pm.get_status("proxy") {
|
||||
if !matches!(st.status, ProcessStatus::Running) {
|
||||
return Err("mihomo 启动后立即退出".to_string());
|
||||
}
|
||||
}
|
||||
if state.get_version().await.is_ok() {
|
||||
return pm.get_status("proxy").ok_or_else(|| "mihomo 进程不存在".to_string());
|
||||
}
|
||||
if tokio::time::Instant::now() >= deadline {
|
||||
return Err("mihomo 启动超时,API 无响应".to_string());
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -220,7 +300,12 @@ pub fn proxy_activate_profile(
|
||||
#[specta::specta]
|
||||
pub fn proxy_set_system_proxy(
|
||||
state: State<'_, MihomoManager>,
|
||||
pm: State<'_, ProcessManager>,
|
||||
) -> Result<(), String> {
|
||||
// 停机时禁止开启系统代理:否则系统代理指向已停止的端口,会导致所有网络请求失败
|
||||
if !mihomo_running(&pm) {
|
||||
return Err("mihomo 未运行,无法开启系统代理".into());
|
||||
}
|
||||
let settings = state.load_settings();
|
||||
let addr = format!("127.0.0.1:{}", settings.mixed_port);
|
||||
set_system_proxy_windows(&addr)?;
|
||||
|
||||
Reference in New Issue
Block a user