Files
ServerKit/agent/internal/setupui/setupui.go
T
Juan Denis 0d76726936 Add native Windows desktop wizard & ARM64 builds
Introduce a native Windows desktop pairing experience and add ARM64 Windows builds/MSI.

Key changes:
- Add native Windows UI support: app.manifest, walk/win deps, icons, header image, and foreground activation helpers.
- Replace the old local HTTP/webview wizard with a native setup UI (remove wizard.html, refactor setupui). Add setupui.Run integration.
- Implement desktop/tray behavior: runDesktop/runTray refactor, single-instance mutex, detached spawn for the pairing wizard, desktop file-backed logging, and MessageBox wrapper for early errors.
- Build/CI updates: add windows/arm64 to build matrix, include -H=windowsgui for windows builds, produce per-arch MSI artifacts, and update release workflow summaries.
- Packaging & resources: add .syso resources for amd64/arm64, MSI build script updates, icon generation script and packaging tweaks, and update .gitignore to ignore dist and MSI outputs.
- Dependency and version bumps: update agent/go.mod/go.sum for walk/win and other indirect deps; bump VERSION to 1.5.4.

These changes enable a native pairing wizard on Windows (no browser/WebView2 required), keep the tray visible while the wizard runs, and add ARM64 support end-to-end (binary and MSI installers).
2026-04-29 15:16:25 -04:00

155 lines
4.4 KiB
Go

// Package setupui shows a small native desktop wizard that walks an operator
// through pairing the agent with a ServerKit panel. The pairing protocol
// itself lives here; the platform-specific window code lives in window_*.go.
package setupui
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/serverkit/agent/internal/config"
"github.com/serverkit/agent/internal/logger"
"github.com/serverkit/agent/internal/metrics"
"github.com/serverkit/agent/internal/pairing"
)
// Run shows the pairing wizard and blocks until the user closes it.
// configPath may be empty to use the default location.
func Run(ctx context.Context, log *logger.Logger, configPath string) error {
if configPath == "" {
configPath = config.DefaultConfigPath()
}
return runWindow(ctx, log.WithComponent("setupui"), configPath)
}
// pairingCallbacks lets the platform-specific window observe state
// transitions in the pairing driver. All callbacks fire on a background
// goroutine; UI code is responsible for marshalling them onto its own thread.
type pairingCallbacks struct {
onEnrolled func(code, formatted string)
onClaimed func(serverName string)
onError func(err error)
}
// runPairing performs the full enroll → wait-for-claim → save-credentials flow.
// It returns when pairing succeeds, the user cancels, or an error occurs.
// All progress is reported via cb.
func runPairing(
ctx context.Context,
log *logger.Logger,
configPath, panelURL, passphrase, displayName string,
cb pairingCallbacks,
) {
panelURL = normalizePanelURL(panelURL)
kp, err := pairing.LoadOrCreate(pairing.DefaultKeyPath())
if err != nil {
cb.onError(fmt.Errorf("load keypair: %w", err))
return
}
collector := metrics.NewCollector(config.MetricsConfig{}, log)
infoCtx, infoCancel := context.WithTimeout(ctx, 8*time.Second)
sysInfo, _ := collector.GetSystemInfo(infoCtx)
infoCancel()
sysMap := buildSystemInfo(sysInfo, displayName)
client := pairing.NewClient(panelURL, log)
enrollCtx, enrollCancel := context.WithTimeout(ctx, 30*time.Second)
enrollResp, err := client.Enroll(enrollCtx, pairing.EnrollRequest{
Pubkey: kp.PublicKeyHex(),
Passphrase: passphrase,
MachineID: config.MachineID(),
SystemInfo: sysMap,
})
enrollCancel()
if err != nil {
cb.onError(fmt.Errorf("enroll: %w", err))
return
}
cb.onEnrolled(enrollResp.PairCode, enrollResp.PairCodeFormatted)
creds, err := client.WaitForClaim(ctx, func(code, formatted, _ string) {
cb.onEnrolled(code, formatted)
})
if err != nil {
if ctx.Err() != nil {
return // user cancelled
}
cb.onError(fmt.Errorf("waiting for claim: %w", err))
return
}
if err := saveCredentials(configPath, panelURL, creds); err != nil {
cb.onError(fmt.Errorf("save credentials: %w", err))
return
}
startServiceIfInstalled()
cb.onClaimed(creds.Name)
}
func normalizePanelURL(u string) string {
u = strings.TrimSpace(u)
if !strings.HasPrefix(u, "http://") && !strings.HasPrefix(u, "https://") {
u = "https://" + u
}
return strings.TrimSuffix(u, "/")
}
func buildSystemInfo(sysInfo *metrics.SystemInfo, displayName string) map[string]interface{} {
m := map[string]interface{}{}
if sysInfo != nil {
m["hostname"] = sysInfo.Hostname
m["os"] = sysInfo.OS
m["platform"] = sysInfo.Platform
m["platform_version"] = sysInfo.PlatformVersion
m["architecture"] = sysInfo.Architecture
m["cpu_cores"] = sysInfo.CPUCores
m["total_memory"] = sysInfo.TotalMemory
m["total_disk"] = sysInfo.TotalDisk
} else {
hostname, _ := os.Hostname()
m["hostname"] = hostname
m["os"] = runtime.GOOS
m["architecture"] = runtime.GOARCH
}
if displayName = strings.TrimSpace(displayName); displayName != "" {
m["display_name"] = displayName
}
return m
}
func saveCredentials(configPath, panelURL string, creds *pairing.Credentials) error {
cfg, err := config.Load(configPath)
if err != nil {
cfg = config.Default()
}
wsURL := strings.TrimSuffix(panelURL, "/")
wsURL = strings.Replace(wsURL, "https://", "wss://", 1)
wsURL = strings.Replace(wsURL, "http://", "ws://", 1)
cfg.Server.URL = wsURL + "/agent"
cfg.Agent.ID = creds.AgentID
cfg.Agent.Name = creds.Name
cfg.Auth.APIKey = creds.APIKey
cfg.Auth.APISecret = creds.APISecret
if configPath == "" {
configPath = config.DefaultConfigPath()
}
if err := os.MkdirAll(filepath.Dir(configPath), 0700); err != nil {
return err
}
if err := cfg.Save(configPath); err != nil {
return err
}
return cfg.SaveCredentials()
}