From 7f2f6db8426e69ae5b1c261b66853f80526d7c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=B4=E5=A4=A9?= Date: Wed, 4 Mar 2026 14:56:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B6=88=E9=99=A4=20Win=20=E9=97=AA?= =?UTF-8?q?=E7=BB=88=E7=AB=AF=20+=20=E6=9B=BF=E6=8D=A2=E6=85=A2=20CLI=20?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E4=B8=BA=E7=9B=B4=E6=8E=A5=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 闪终端修复: - extensions.rs: 提取 cftunnel_cmd() 助手,Windows 自动加 CREATE_NO_WINDOW;get_cftunnel_status/action/logs 全部改用 CLI 替换: - config.rs reload_gateway / restart_gateway: Windows/Linux 改调 service::restart_service(),删除慢 openclaw gateway restart CLI 调用 - config.rs uninstall_gateway: Windows 改用直接 taskkill, Linux 改用 pkill,不再调用 openclaw gateway stop CLI --- src-tauri/src/commands/config.rs | 39 +++++++++++++--------------- src-tauri/src/commands/extensions.rs | 22 +++++++++++++--- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index bebe0a1..6299f41 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -625,7 +625,7 @@ fn get_uid() -> Result { /// 重载 Gateway 服务 /// macOS: launchctl kickstart -k -/// Windows/Linux: openclaw gateway restart +/// Windows/Linux: 直接通过进程管理重启(不走慢 CLI) #[tauri::command] pub async fn reload_gateway() -> Result { #[cfg(target_os = "macos")] @@ -646,23 +646,10 @@ pub async fn reload_gateway() -> Result { } #[cfg(not(target_os = "macos"))] { - use crate::utils::openclaw_command_async; - let cli_check = openclaw_command_async().arg("--version").output().await; - match cli_check { - Ok(o) if o.status.success() => {} - _ => return Err("openclaw CLI 未安装,无法重载 Gateway".into()), - } - let output = openclaw_command_async() - .args(["gateway", "restart"]) - .output() + // 直接调用服务管理(进程级别),避免慢 CLI 调用 + crate::commands::service::restart_service("ai.openclaw.gateway".into()) .await - .map_err(|e| format!("重载失败: {e}"))?; - if output.status.success() { - Ok("Gateway 已重载".to_string()) - } else { - let stderr = String::from_utf8_lossy(&output.stderr); - Err(format!("重载失败: {stderr}")) - } + .map(|_| "Gateway 已重载".to_string()) } } @@ -839,7 +826,8 @@ pub async fn install_gateway() -> Result { /// 卸载 Gateway 服务 /// macOS: launchctl bootout + 删除 plist -/// Windows/Linux: openclaw gateway stop +/// Windows: 直接 taskkill +/// Linux: pkill #[tauri::command] pub fn uninstall_gateway() -> Result { #[cfg(target_os = "macos")] @@ -859,10 +847,19 @@ pub fn uninstall_gateway() -> Result { fs::remove_file(&plist).map_err(|e| format!("删除 plist 失败: {e}"))?; } } - #[cfg(not(target_os = "macos"))] + #[cfg(target_os = "windows")] { - // Windows/Linux: 停止 Gateway 服务 - let _ = openclaw_command().args(["gateway", "stop"]).output(); + // 直接杀死 gateway 相关的 node.exe 进程,不走慢 CLI + let _ = Command::new("taskkill") + .args(["/f", "/im", "node.exe", "/fi", "WINDOWTITLE eq openclaw*"]) + .creation_flags(0x08000000) + .output(); + } + #[cfg(target_os = "linux")] + { + let _ = Command::new("pkill") + .args(["-f", "openclaw.*gateway"]) + .output(); } Ok("Gateway 服务已卸载".to_string()) diff --git a/src-tauri/src/commands/extensions.rs b/src-tauri/src/commands/extensions.rs index 7f88556..67d5b19 100644 --- a/src-tauri/src/commands/extensions.rs +++ b/src-tauri/src/commands/extensions.rs @@ -98,6 +98,20 @@ fn cftunnel_bin() -> String { } } +/// 创建 cftunnel 命令,Windows 上自动隐藏窗口 +fn cftunnel_cmd(bin: &str) -> Command { + #[cfg(target_os = "windows")] + { + let mut cmd = Command::new(bin); + cmd.creation_flags(0x08000000); + cmd + } + #[cfg(not(target_os = "windows"))] + { + Command::new(bin) + } +} + /// 检测 cftunnel 进程是否在运行(平台相关的补充检测) fn check_cftunnel_process() -> Option<(Option, bool)> { #[cfg(target_os = "macos")] @@ -178,7 +192,7 @@ pub fn get_cftunnel_status() -> Result { result.insert("installed".into(), Value::Bool(true)); // 获取状态(单次 CLI 调用) - if let Ok(out) = Command::new(&bin).arg("status").output() { + if let Ok(out) = cftunnel_cmd(&bin).arg("status").output() { let text = String::from_utf8_lossy(&out.stdout); let status = parse_cftunnel_status(&text); // 从 status 输出中提取版本号(如果有) @@ -204,7 +218,7 @@ pub fn get_cftunnel_status() -> Result { } // 获取路由列表 - if let Ok(out) = Command::new(&bin).arg("list").output() { + if let Ok(out) = cftunnel_cmd(&bin).arg("list").output() { let text = String::from_utf8_lossy(&out.stdout); let routes = parse_cftunnel_routes(&text); result.insert("routes".into(), Value::Array(routes)); @@ -222,7 +236,7 @@ pub fn cftunnel_action(action: String) -> Result<(), String> { "restart" => vec!["restart"], _ => return Err(format!("不支持的操作: {action}")), }; - let output = Command::new(&bin) + let output = cftunnel_cmd(&bin) .args(&args) .output() .map_err(|e| format!("执行 cftunnel {action} 失败: {e}"))?; @@ -238,7 +252,7 @@ pub fn cftunnel_action(action: String) -> Result<(), String> { pub fn get_cftunnel_logs(lines: Option) -> Result { let bin = cftunnel_bin(); let n = lines.unwrap_or(20).to_string(); - let output = Command::new(&bin) + let output = cftunnel_cmd(&bin) .args(["logs", "--tail", &n]) .output() .map_err(|e| format!("读取 cftunnel 日志失败: {e}"))?;