feat: 增加桌面端安装包与分支专用 CI/CD

拆分后台服务、浏览器安装器和桌面托盘控制器,支持 Windows Service、macOS LaunchAgent 以及 Linux systemd 部署。

新增 Windows Inno Setup 安装器、macOS Intel/Apple Silicon 通用 pkg、Linux 多架构安装脚本,并在安装过程中自动准备 Playwright Chromium。

新增仅 codex/desktop-packaging 分支触发的桌面端 CI/CD,覆盖 Go 测试、交叉编译和 Windows/macOS/Linux 安装包构建;保持现有 Docker 工作流不变。

补充数据密钥持久化、托盘图标、安装文档及相关测试。
This commit is contained in:
Christ committed 2026-08-11 19:52:34 +08:00
1 parent 1204655170
commit a99ea5c20d
25 files changed
+1141 -81

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
package main
import (
"bytes"
"encoding/binary"
"image"
"image/color"
"image/png"
"runtime"
)
// trayIconBytes 返回一个不依赖外部文件的图标,避免安装器还要额外管理图标路径。
// Windows 的 Shell_NotifyIcon 需要 ICO;macOS/Linux 可以直接使用 PNG。
func trayIconBytes() []byte {
const size = 32
img := image.NewRGBA(image.Rect(0, 0, size, size))
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
dx, dy := x-size/2, y-size/2
if dx*dx+dy*dy <= 14*14 {
img.SetRGBA(x, y, color.RGBA{R: 37, G: 99, B: 235, A: 255})
}
if abs(x-y) <= 2 || abs(x+y-(size-1)) <= 2 {
if dx*dx+dy*dy <= 10*10 {
img.SetRGBA(x, y, color.RGBA{R: 255, G: 255, B: 255, A: 255})
}
}
}
}
var pngData bytes.Buffer
_ = png.Encode(&pngData, img)
if runtime.GOOS != "windows" {
return pngData.Bytes()
}
return pngToICO(pngData.Bytes(), size)
}
func pngToICO(data []byte, size int) []byte {
const headerSize = 6
const entrySize = 16
result := make([]byte, headerSize+entrySize+len(data))
// ICONDIR: reserved, type=icon, image count=1.
binary.LittleEndian.PutUint16(result[0:2], 0)
binary.LittleEndian.PutUint16(result[2:4], 1)
binary.LittleEndian.PutUint16(result[4:6], 1)
entry := result[headerSize : headerSize+entrySize]
entry[0] = byte(size)
entry[1] = byte(size)
entry[2] = 0
entry[3] = 0
binary.LittleEndian.PutUint16(entry[4:6], 1)
binary.LittleEndian.PutUint16(entry[6:8], 32)
binary.LittleEndian.PutUint32(entry[8:12], uint32(len(data)))
binary.LittleEndian.PutUint32(entry[12:16], headerSize+entrySize)
copy(result[headerSize+entrySize:], data)
return result
}
func abs(value int) int {
if value < 0 {
return -value
}
return value
}
+140
View File
@@ -0,0 +1,140 @@
// Command tray provides the small desktop controller for the background server.
// The server remains a separate process managed by the operating system.
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
"fyne.io/systray"
)
type healthResponse struct {
Status string `json:"status"`
Database string `json:"database"`
}
var serviceURL = strings.TrimRight(envOr("XIANYU_SERVICE_URL", "http://127.0.0.1:8080"), "/")
func main() {
systray.Run(onReady, func() {})
}
func onReady() {
systray.SetTitle("闲鱼管家")
systray.SetIcon(trayIconBytes())
systray.SetTooltip("闲鱼管家服务")
statusItem := systray.AddMenuItem("服务状态:检查中", "读取后台服务状态")
openItem := systray.AddMenuItem("打开管理页面", "在默认浏览器打开管理页面")
systray.AddSeparator()
startItem := systray.AddMenuItem("启动服务", "启动后台服务")
stopItem := systray.AddMenuItem("停止服务", "停止后台服务")
restartItem := systray.AddMenuItem("重启服务", "重启后台服务")
browserItem := systray.AddMenuItem("安装/修复 Chromium", "准备 Playwright Chromium")
systray.AddSeparator()
quitItem := systray.AddMenuItem("退出托盘", "只退出菜单栏控制器")
go refreshStatus(statusItem)
go func() {
for range openItem.ClickedCh {
_ = openURL(serviceURL)
}
}()
go func() {
for range startItem.ClickedCh {
_ = serviceAction("start")
}
}()
go func() {
for range stopItem.ClickedCh {
_ = serviceAction("stop")
}
}()
go func() {
for range restartItem.ClickedCh {
_ = serviceAction("restart")
}
}()
go func() {
for range browserItem.ClickedCh {
_ = installBrowser()
}
}()
go func() {
for range quitItem.ClickedCh {
systray.Quit()
}
}()
}
func installBrowser() error {
path := strings.TrimSpace(os.Getenv("XIANYU_BROWSER_INSTALL"))
if path == "" {
executable, err := os.Executable()
if err != nil {
return err
}
path = executable
if runtime.GOOS == "darwin" {
path = strings.Replace(path, "/Contents/MacOS/xianyu-tray", "/Contents/Helpers/browser-install", 1)
} else {
path = strings.Replace(path, "xianyu-tray.exe", "browser-install.exe", 1)
}
}
return exec.Command(path).Run()
}
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("闲鱼管家:数据库或服务异常")
}
time.Sleep(5 * time.Second)
}
}
func readHealth(client *http.Client) (healthResponse, error) {
resp, err := client.Get(serviceURL + "/health")
if err != nil {
return healthResponse{}, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return healthResponse{}, fmt.Errorf("health status %d", resp.StatusCode)
}
var health healthResponse
if err := json.NewDecoder(resp.Body).Decode(&health); err != nil {
return healthResponse{}, err
}
return health, nil
}
func openURL(url string) error {
if runtime.GOOS == "windows" {
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
}
return exec.Command("open", url).Start()
}
func envOr(name, fallback string) string {
if value := strings.TrimSpace(os.Getenv(name)); value != "" {
return value
}
return fallback
}
+25
View File
@@ -0,0 +1,25 @@
//go:build darwin
package main
import (
"fmt"
"os"
"os/exec"
)
func serviceAction(action string) error {
label := envOr("XIANYU_SERVICE_NAME", "com.christ.ydisks-xianyu-helper")
uid := fmt.Sprint(os.Getuid())
target := "gui/" + uid + "/" + label
switch action {
case "start":
return exec.Command("launchctl", "kickstart", target).Run()
case "stop":
return exec.Command("launchctl", "kill", "SIGTERM", target).Run()
case "restart":
return exec.Command("launchctl", "kickstart", "-k", target).Run()
default:
return nil
}
}
+5
View File
@@ -0,0 +1,5 @@
//go:build !windows && !darwin
package main
func serviceAction(_ string) error { return nil }
+20
View File
@@ -0,0 +1,20 @@
//go:build windows
package main
import "os/exec"
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
}
}