From 733a536031daa5b058f94fda68463ef04409420e Mon Sep 17 00:00:00 2001 From: Christ Date: Tue, 11 Aug 2026 21:56:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=98=E7=9B=98?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=8E=A7=E5=88=B6=E6=97=A0=E6=95=88=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正 macOS LaunchAgent 服务标签,使用 bootstrap 和 bootout 管理服务,避免 KeepAlive 导致停止后立即重启。 Windows 托盘操作通过 UAC 提权执行 sc.exe,支持普通用户控制管理员安装的 Windows 服务。 显示服务启动、停止和重启失败原因,避免静默丢弃控制命令错误。 其他平台明确返回不支持错误,并完成本机及 Windows、macOS 交叉编译验证。 --- cmd/tray/main.go | 43 +++++++++++++++++--------- cmd/tray/service_control_darwin.go | 45 +++++++++++++++++++++++---- cmd/tray/service_control_other.go | 6 +++- cmd/tray/service_control_windows.go | 48 ++++++++++++++++++++++------- 4 files changed, 110 insertions(+), 32 deletions(-) diff --git a/cmd/tray/main.go b/cmd/tray/main.go index 17208ae..a4ab008 100644 --- a/cmd/tray/main.go +++ b/cmd/tray/main.go @@ -49,17 +49,17 @@ func onReady() { }() go func() { for range startItem.ClickedCh { - _ = serviceAction("start") + runServiceAction(statusItem, "启动服务", "start") } }() go func() { for range stopItem.ClickedCh { - _ = serviceAction("stop") + runServiceAction(statusItem, "停止服务", "stop") } }() go func() { for range restartItem.ClickedCh { - _ = serviceAction("restart") + runServiceAction(statusItem, "重启服务", "restart") } }() go func() { @@ -94,21 +94,36 @@ func installBrowser() error { func refreshStatus(item *systray.MenuItem) { client := &http.Client{Timeout: 2 * time.Second} for { - status, err := readHealth(client) - if err != nil { - item.SetTitle("服务状态:未运行") - systray.SetTooltip("闲鱼管家:后台服务未运行") - } else if status.Status == "ok" && status.Database == "ok" { - item.SetTitle("服务状态:运行正常") - systray.SetTooltip("闲鱼管家:运行正常") - } else { - item.SetTitle("服务状态:异常") - systray.SetTooltip("闲鱼管家:数据库或服务异常") - } + refreshStatusOnce(item, client) time.Sleep(5 * time.Second) } } +func refreshStatusOnce(item *systray.MenuItem, client *http.Client) { + status, err := readHealth(client) + if err != nil { + item.SetTitle("服务状态:未运行") + systray.SetTooltip("闲鱼管家:后台服务未运行") + } else if status.Status == "ok" && status.Database == "ok" { + item.SetTitle("服务状态:运行正常") + systray.SetTooltip("闲鱼管家:运行正常") + } else { + item.SetTitle("服务状态:异常") + systray.SetTooltip("闲鱼管家:数据库或服务异常") + } +} + +func runServiceAction(statusItem *systray.MenuItem, actionName, action string) { + statusItem.SetTitle("服务状态:" + actionName + "中") + if err := serviceAction(action); err != nil { + statusItem.SetTitle("服务状态:操作失败") + systray.SetTooltip(fmt.Sprintf("闲鱼管家:%s失败:%v", actionName, err)) + return + } + + statusItem.SetTitle("服务状态:检查中") +} + func readHealth(client *http.Client) (healthResponse, error) { resp, err := client.Get(serviceURL + "/health") if err != nil { diff --git a/cmd/tray/service_control_darwin.go b/cmd/tray/service_control_darwin.go index 5032ee2..b6a11a0 100644 --- a/cmd/tray/service_control_darwin.go +++ b/cmd/tray/service_control_darwin.go @@ -6,20 +6,53 @@ import ( "fmt" "os" "os/exec" + "path/filepath" + "strings" ) func serviceAction(action string) error { - label := envOr("XIANYU_SERVICE_NAME", "com.christ.ydisks-xianyu-helper") + label := envOr("XIANYU_SERVICE_NAME", "com.christ.ydisks-xianyu-helper.server") uid := fmt.Sprint(os.Getuid()) - target := "gui/" + uid + "/" + label + domain := "gui/" + uid + target := domain + "/" + label + home, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("获取用户目录失败: %w", err) + } + plistPath := filepath.Join(home, "Library", "LaunchAgents", label+".plist") switch action { case "start": - return exec.Command("launchctl", "kickstart", target).Run() + if err := launchctl("print", target); err != nil { + if err := launchctl("bootstrap", domain, plistPath); err != nil { + return err + } + } + return launchctl("kickstart", target) case "stop": - return exec.Command("launchctl", "kill", "SIGTERM", target).Run() + if err := launchctl("print", target); err != nil { + return nil + } + return launchctl("bootout", target) case "restart": - return exec.Command("launchctl", "kickstart", "-k", target).Run() + _ = launchctl("bootout", target) + if err := launchctl("bootstrap", domain, plistPath); err != nil { + return err + } + return launchctl("kickstart", target) default: - return nil + return fmt.Errorf("未知服务操作: %s", action) } } + +func launchctl(args ...string) error { + cmd := exec.Command("launchctl", args...) + output, err := cmd.CombinedOutput() + if err != nil { + message := strings.TrimSpace(string(output)) + if message == "" { + return fmt.Errorf("launchctl %s 失败: %w", strings.Join(args, " "), err) + } + return fmt.Errorf("launchctl %s 失败: %s", strings.Join(args, " "), message) + } + return nil +} diff --git a/cmd/tray/service_control_other.go b/cmd/tray/service_control_other.go index 3a7d879..f45419f 100644 --- a/cmd/tray/service_control_other.go +++ b/cmd/tray/service_control_other.go @@ -2,4 +2,8 @@ package main -func serviceAction(_ string) error { return nil } +import "fmt" + +func serviceAction(action string) error { + return fmt.Errorf("当前平台不支持托盘服务操作: %s", action) +} diff --git a/cmd/tray/service_control_windows.go b/cmd/tray/service_control_windows.go index 6b8459d..3625251 100644 --- a/cmd/tray/service_control_windows.go +++ b/cmd/tray/service_control_windows.go @@ -2,19 +2,45 @@ package main -import "os/exec" +import ( + "fmt" + "os/exec" + "strings" +) func serviceAction(action string) error { name := envOr("XIANYU_SERVICE_NAME", "YdisksXianyuHelper") - switch action { - case "start", "stop": - return exec.Command("sc.exe", action, name).Run() - case "restart": - if err := exec.Command("sc.exe", "stop", name).Run(); err != nil { - return err - } - return exec.Command("sc.exe", "start", name).Run() - default: - return nil + if action != "start" && action != "stop" && action != "restart" { + return fmt.Errorf("未知服务操作: %s", action) } + + quotedName := strings.ReplaceAll(name, "'", "''") + script := fmt.Sprintf(`$ErrorActionPreference = 'Stop' +function Invoke-Sc([string] $verb) { + $process = Start-Process -FilePath "$env:SystemRoot\System32\sc.exe" -ArgumentList @($verb, '%s') -Verb RunAs -WindowStyle Hidden -Wait -PassThru + if ($null -eq $process) { exit 1 } + return $process.ExitCode +} +$exitCode = 0 +switch ('%s') { + 'start' { $exitCode = Invoke-Sc 'start' } + 'stop' { $exitCode = Invoke-Sc 'stop' } + 'restart' { + $stopCode = Invoke-Sc 'stop' + if ($stopCode -ne 0 -and $stopCode -ne 1062) { exit $stopCode } + $exitCode = Invoke-Sc 'start' + } +} +exit $exitCode`, quotedName, action) + + cmd := exec.Command("powershell.exe", "-NoLogo", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-Command", script) + output, err := cmd.CombinedOutput() + if err != nil { + message := strings.TrimSpace(string(output)) + if message == "" { + return fmt.Errorf("Windows 服务操作 %s 失败: %w", action, err) + } + return fmt.Errorf("Windows 服务操作 %s 失败: %s", action, message) + } + return nil }