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 }