agent v0.3.15: mode proxy auto/manuel, correction auto-update et conservation systray, animation UI update

This commit is contained in:
EduBox Dev
2026-06-28 19:53:19 +00:00
parent 33d89c66c0
commit adab165274
13 changed files with 444 additions and 55 deletions
+70 -25
View File
@@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
@@ -27,15 +28,30 @@ type AgentVersionInfo struct {
} `json:"downloadUrls"`
}
// httpBaseURL converts a WebSocket server URL into the corresponding HTTP(S)
// base URL, stripping the /api/websocket path if present.
func httpBaseURL(serverURL string) string {
u := serverURL
switch {
case strings.HasPrefix(u, "wss://"):
u = "https://" + strings.TrimPrefix(u, "wss://")
case strings.HasPrefix(u, "ws://"):
u = "http://" + strings.TrimPrefix(u, "ws://")
}
u = strings.TrimSuffix(u, "/api/websocket/")
u = strings.TrimSuffix(u, "/api/websocket")
return strings.TrimSuffix(u, "/")
}
// checkForUpdate fetches the latest agent version from the server and compares
// it with the running binary's version.
func checkForUpdate(serverBaseURL string) (*AgentVersionInfo, bool, error) {
if serverBaseURL == "" {
func checkForUpdate(cfg *AgentConfig) (*AgentVersionInfo, bool, error) {
if cfg == nil || cfg.Server == "" {
return nil, false, fmt.Errorf("no server URL configured")
}
wsURL := strings.TrimSuffix(serverBaseURL, "/api/websocket")
url := wsURL + "/api/agent/version"
client := &http.Client{Timeout: 30 * time.Second}
url := httpBaseURL(cfg.Server) + "/api/agent/version"
client := httpClientWithProxy(cfg)
client.Timeout = 30 * time.Second
resp, err := client.Get(url)
if err != nil {
return nil, false, err
@@ -60,7 +76,7 @@ func checkForUpdate(serverBaseURL string) (*AgentVersionInfo, bool, error) {
}
// downloadUpdate downloads the new agent binary to the update directory.
func downloadUpdate(dataDir, downloadURL string) (string, error) {
func downloadUpdate(cfg *AgentConfig, dataDir, downloadURL string) (string, error) {
updateDir := filepath.Join(dataDir, "update")
if err := os.MkdirAll(updateDir, 0755); err != nil {
return "", err
@@ -72,7 +88,8 @@ func downloadUpdate(dataDir, downloadURL string) (string, error) {
dest := filepath.Join(updateDir, "studioE5-agent-new"+ext)
log.Printf("Downloading update from %s to %s", downloadURL, dest)
resp, err := http.Get(downloadURL)
client := httpClientWithProxy(cfg)
resp, err := client.Get(downloadURL)
if err != nil {
return "", err
}
@@ -99,35 +116,62 @@ func downloadUpdate(dataDir, downloadURL string) (string, error) {
return dest, nil
}
// formatArgsForShell returns the given arguments as a safely quoted string
// suitable for embedding in shell/PowerShell scripts.
func formatArgsForShell(args []string) string {
if len(args) == 0 {
return ""
}
quoted := make([]string, len(args))
for i, a := range args {
quoted[i] = strconv.Quote(a)
}
return strings.Join(quoted, " ")
}
// applyUpdate replaces the running binary with the downloaded one using an
// external helper script, then exits the current process.
// external helper script, then exits the current process. The new process is
// started with the same arguments as the current one so that tray/console mode
// is preserved.
func applyUpdate(currentPath, newPath, dataDir string) error {
pid := os.Getpid()
restartArgs := os.Args[1:]
switch runtime.GOOS {
case "windows":
return applyUpdateWindows(currentPath, newPath, dataDir, pid)
return applyUpdateWindows(currentPath, newPath, dataDir, pid, restartArgs)
default:
return applyUpdateUnix(currentPath, newPath, dataDir, pid)
return applyUpdateUnix(currentPath, newPath, dataDir, pid, restartArgs)
}
}
func applyUpdateWindows(currentPath, newPath, dataDir string, pid int) error {
func applyUpdateWindows(currentPath, newPath, dataDir string, pid int, restartArgs []string) error {
scriptPath := filepath.Join(dataDir, "update", "apply-update.ps1")
argsList := formatArgsForShell(restartArgs)
if argsList == "" {
argsList = ""
} else {
argsList = "$startArgs = @(" + argsList + ")"
}
script := fmt.Sprintf(`$old = "%s"
$new = "%s"
$pid = %d
$args = '-no-tray', '-data-dir', '%s'
Wait-Process -Id $pid -ErrorAction SilentlyContinue
$targetPid = %d
%s
Wait-Process -Id $targetPid -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
Move-Item -Path $new -Destination $old -Force
Start-Process -FilePath $old -ArgumentList $args -WindowStyle Hidden
`, currentPath, newPath, pid, dataDir)
if ($startArgs) {
Start-Process -FilePath $old -ArgumentList $startArgs -WindowStyle Hidden
} else {
Start-Process -FilePath $old -WindowStyle Hidden
}
`, currentPath, newPath, pid, argsList)
if err := os.WriteFile(scriptPath, []byte(script), 0644); err != nil {
return err
}
cmd := exec.Command("powershell.exe", "-ExecutionPolicy", "Bypass", "-File", scriptPath)
cmd := exec.Command("powershell.exe", "-ExecutionPolicy", "Bypass", "-WindowStyle", "Hidden", "-File", scriptPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
hideWindow(cmd)
if err := cmd.Start(); err != nil {
return err
}
@@ -136,8 +180,9 @@ Start-Process -FilePath $old -ArgumentList $args -WindowStyle Hidden
return nil
}
func applyUpdateUnix(currentPath, newPath, dataDir string, pid int) error {
func applyUpdateUnix(currentPath, newPath, dataDir string, pid int, restartArgs []string) error {
scriptPath := filepath.Join(dataDir, "update", "apply-update.sh")
argsList := formatArgsForShell(restartArgs)
script := fmt.Sprintf(`#!/bin/bash
set -e
old="%s"
@@ -147,8 +192,8 @@ while kill -0 "$pid" 2>/dev/null; do sleep 1; done
sleep 2
mv "$new" "$old"
chmod +x "$old"
nohup "$old" -no-tray -data-dir "%s" >/dev/null 2>&1 &
`, currentPath, newPath, pid, dataDir)
nohup "$old" %s >/dev/null 2>&1 &
`, currentPath, newPath, pid, argsList)
if err := os.WriteFile(scriptPath, []byte(script), 0755); err != nil {
return err
}
@@ -164,8 +209,8 @@ nohup "$old" -no-tray -data-dir "%s" >/dev/null 2>&1 &
}
// startAgentUpdate performs the full update flow: download + replace + restart.
func startAgentUpdate(dataDir, serverBaseURL string) error {
info, available, err := checkForUpdate(serverBaseURL)
func startAgentUpdate(cfg *AgentConfig, dataDir string) error {
info, available, err := checkForUpdate(cfg)
if err != nil {
return fmt.Errorf("update check failed: %w", err)
}
@@ -192,7 +237,7 @@ func startAgentUpdate(dataDir, serverBaseURL string) error {
if downloadURL == "" {
return fmt.Errorf("no download URL for %s", runtime.GOOS)
}
newPath, err := downloadUpdate(dataDir, downloadURL)
newPath, err := downloadUpdate(cfg, dataDir, downloadURL)
if err != nil {
return fmt.Errorf("download failed: %w", err)
}
@@ -206,9 +251,9 @@ func startAgentUpdate(dataDir, serverBaseURL string) error {
}
// updateCheckerLoop periodically checks for agent updates and notifies the UI.
func updateCheckerLoop(dataDir, serverBaseURL string) {
func updateCheckerLoop(cfg *AgentConfig, dataDir string) {
for {
info, available, err := checkForUpdate(serverBaseURL)
info, available, err := checkForUpdate(cfg)
if err == nil && available && info != nil {
log.Printf("Agent update available: %s (current: %s)", info.Version, version)
setServerAgentVersion(info.Version)