fix: 修复托盘服务控制无效问题
修正 macOS LaunchAgent 服务标签,使用 bootstrap 和 bootout 管理服务,避免 KeepAlive 导致停止后立即重启。 Windows 托盘操作通过 UAC 提权执行 sc.exe,支持普通用户控制管理员安装的 Windows 服务。 显示服务启动、停止和重启失败原因,避免静默丢弃控制命令错误。 其他平台明确返回不支持错误,并完成本机及 Windows、macOS 交叉编译验证。
This commit is contained in:
1 parent
c21a3a33dc
commit
733a536031
4 files changed
+110
-32
No files matched your search
+29
-14
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -2,4 +2,8 @@
|
||||
|
||||
package main
|
||||
|
||||
func serviceAction(_ string) error { return nil }
|
||||
import "fmt"
|
||||
|
||||
func serviceAction(action string) error {
|
||||
return fmt.Errorf("当前平台不支持托盘服务操作: %s", action)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user