From 2517a41ddff49188673798dc19967d591a2316db Mon Sep 17 00:00:00 2001 From: jester Date: Sun, 19 Apr 2026 21:51:00 +0000 Subject: [PATCH] updates 4-19-26 --- internal/files/files.go | 7 --- internal/http/agent.go | 59 ++------------------ internal/minecraft/readiness.go | 19 ++++--- internal/minecraft/status.go | 2 +- internal/mods/installer.go | 12 ++-- internal/mods/metadata.go | 8 +-- internal/provision/devcontainer/common.go | 9 ++- internal/provision/files.go | 5 +- internal/provision/minecraft/fabric_proxy.go | 5 +- internal/provision/provision.go | 4 +- internal/system/process.go | 14 ++--- state/update.json | 6 +- 12 files changed, 42 insertions(+), 108 deletions(-) diff --git a/internal/files/files.go b/internal/files/files.go index 8eb414d..577f152 100644 --- a/internal/files/files.go +++ b/internal/files/files.go @@ -717,13 +717,6 @@ func sourceForPath(root, rel string) *string { return &source } -func min(a, b int) int { - if a < b { - return a - } - return b -} - func normalizeAndCheckVisibleRel(rel string) (string, error) { normalizedRel, err := normalizeRelativePath(rel) if err != nil { diff --git a/internal/http/agent.go b/internal/http/agent.go index 8b5b070..9f469ad 100755 --- a/internal/http/agent.go +++ b/internal/http/agent.go @@ -8,7 +8,6 @@ import ( "net/http" "os" "path/filepath" - "strconv" "strings" "time" @@ -293,7 +292,7 @@ func ensureProvisioned(cfg *state.Config) error { } func ensureDevCodeServer(cfg *state.Config) error { - if cfg == nil || !devCodeServerRequested(*cfg) { + if cfg == nil || !provision.CodeServerRequested(*cfg) { return nil } @@ -316,18 +315,6 @@ func ensureDevCodeServer(cfg *state.Config) error { return nil } -func devCodeServerRequested(cfg state.Config) bool { - if cfg.EnableCodeServer { - return true - } - for _, addon := range cfg.Addons { - if strings.EqualFold(addon, "codeserver") { - return true - } - } - return false -} - /* -------------------------------------------------------------------------- /config — the REAL provisioning trigger (async) @@ -864,49 +851,13 @@ func handleGamePlayers(w http.ResponseWriter, r *http.Request) { return } - ports := make([]int, 0, 3) - propsPath := filepath.Join(provision.ServerDir(*cfg), "server.properties") - if b, err := os.ReadFile(propsPath); err == nil { - lines := strings.Split(string(b), "\n") - for _, l := range lines { - if strings.HasPrefix(l, "server-port=") { - if p, err := strconv.Atoi(strings.TrimPrefix(l, "server-port=")); err == nil && p > 0 { - ports = append(ports, p) - } - break - } - } - } - if len(cfg.Ports) > 0 && cfg.Ports[0] > 0 { - ports = append(ports, cfg.Ports[0]) - } - ports = append(ports, 25565) - - seenPorts := make(map[int]struct{}, len(ports)) - uniqPorts := make([]int, 0, len(ports)) - for _, p := range ports { - if _, ok := seenPorts[p]; ok { - continue - } - seenPorts[p] = struct{}{} - uniqPorts = append(uniqPorts, p) - } - - protocols := []int{mcstatus.ProtocolForVersion(cfg.Version), 767, 765, 763, 762, 754} - seenProtocols := make(map[int]struct{}, len(protocols)) - uniqProtocols := make([]int, 0, len(protocols)) - for _, pr := range protocols { - if _, ok := seenProtocols[pr]; ok { - continue - } - seenProtocols[pr] = struct{}{} - uniqProtocols = append(uniqProtocols, pr) - } + ports := mcstatus.CandidatePorts(*cfg) + protocols := mcstatus.CandidateProtocols(cfg.Version) var status mcstatus.StatusResponse var lastErr error - for _, port := range uniqPorts { - for _, protocol := range uniqProtocols { + for _, port := range ports { + for _, protocol := range protocols { s, err := mcstatus.QueryStatus("127.0.0.1", port, protocol) if err != nil { lastErr = err diff --git a/internal/minecraft/readiness.go b/internal/minecraft/readiness.go index 189b902..c6e8c50 100644 --- a/internal/minecraft/readiness.go +++ b/internal/minecraft/readiness.go @@ -15,9 +15,8 @@ import ( func WaitUntilReady(cfg state.Config, timeout, interval time.Duration) error { start := time.Now() - ports := candidatePorts(cfg) - protocols := []int{ProtocolForVersion(cfg.Version), 767, 765, 763, 762, 754} - protocols = dedupeInts(protocols) + ports := CandidatePorts(cfg) + protocols := CandidateProtocols(cfg.Version) attempt := 0 deadline := start.Add(timeout) @@ -50,14 +49,14 @@ func WaitUntilReady(cfg state.Config, timeout, interval time.Duration) error { } } -func candidatePorts(cfg state.Config) []int { +func CandidatePorts(cfg state.Config) []int { ports := make([]int, 0, 3) propsPath := filepath.Join(provision.ServerDir(cfg), "server.properties") if b, err := os.ReadFile(propsPath); err == nil { - lines := strings.Split(string(b), "\n") - for _, l := range lines { - if strings.HasPrefix(l, "server-port=") { - if p, err := strconv.Atoi(strings.TrimPrefix(l, "server-port=")); err == nil && p > 0 { + lines := strings.SplitSeq(string(b), "\n") + for l := range lines { + if after, ok := strings.CutPrefix(l, "server-port="); ok { + if p, err := strconv.Atoi(after); err == nil && p > 0 { ports = append(ports, p) } break @@ -71,6 +70,10 @@ func candidatePorts(cfg state.Config) []int { return dedupeInts(ports) } +func CandidateProtocols(version string) []int { + return dedupeInts([]int{ProtocolForVersion(version), 767, 765, 763, 762, 754}) +} + func dedupeInts(in []int) []int { seen := make(map[int]struct{}, len(in)) out := make([]int, 0, len(in)) diff --git a/internal/minecraft/status.go b/internal/minecraft/status.go index 5b13b73..000b268 100644 --- a/internal/minecraft/status.go +++ b/internal/minecraft/status.go @@ -45,7 +45,7 @@ func ProtocolForVersion(version string) int { } func QueryStatus(host string, port int, protocol int) (StatusResponse, error) { - addr := fmt.Sprintf("%s:%d", host, port) + addr := net.JoinHostPort(host, fmt.Sprintf("%d", port)) conn, err := net.DialTimeout("tcp", addr, 3*time.Second) if err != nil { return StatusResponse{}, err diff --git a/internal/mods/installer.go b/internal/mods/installer.go index 2bcc74e..ea2b6ad 100644 --- a/internal/mods/installer.go +++ b/internal/mods/installer.go @@ -307,8 +307,8 @@ func validateExpectedHash(v string) error { func normalizeExpectedHash(raw string, expectedLen int, prefix string) (string, error) { v := strings.TrimSpace(strings.ToLower(raw)) - if strings.HasPrefix(v, prefix+":") { - v = strings.TrimPrefix(v, prefix+":") + if after, ok := strings.CutPrefix(v, prefix+":"); ok { + v = after } if len(v) != expectedLen { return "", fmt.Errorf("%s must include %d hex characters", prefix, expectedLen) @@ -407,12 +407,12 @@ func uniqueRemovedPath(dir, filename string) string { } base := filename ext := "" - if strings.HasSuffix(filename, ".disabled") { - base = strings.TrimSuffix(filename, ".disabled") + if before, ok := strings.CutSuffix(filename, ".disabled"); ok { + base = before ext = ".disabled" } - if strings.HasSuffix(base, ".jar") { - base = strings.TrimSuffix(base, ".jar") + if before, ok := strings.CutSuffix(base, ".jar"); ok { + base = before ext = ".jar" + ext } ts := time.Now().UTC().Format("20060102T150405") diff --git a/internal/mods/metadata.go b/internal/mods/metadata.go index 8b60433..e644c85 100644 --- a/internal/mods/metadata.go +++ b/internal/mods/metadata.go @@ -123,8 +123,8 @@ func mergeFabricMetadata(out *jarMetadata, b []byte) { } func mergeModsTOMLMetadata(out *jarMetadata, b []byte) { - lines := strings.Split(string(b), "\n") - for _, raw := range lines { + lines := strings.SplitSeq(string(b), "\n") + for raw := range lines { line := strings.TrimSpace(raw) if line == "" || strings.HasPrefix(line, "#") { continue @@ -182,8 +182,8 @@ func mergeModInfoMap(out *jarMetadata, obj map[string]any) { } func mergePluginYAMLMetadata(out *jarMetadata, b []byte) { - lines := strings.Split(string(b), "\n") - for _, raw := range lines { + lines := strings.SplitSeq(string(b), "\n") + for raw := range lines { line := strings.TrimSpace(raw) if line == "" || strings.HasPrefix(line, "#") { continue diff --git a/internal/provision/devcontainer/common.go b/internal/provision/devcontainer/common.go index 078ea96..2101214 100644 --- a/internal/provision/devcontainer/common.go +++ b/internal/provision/devcontainer/common.go @@ -10,6 +10,7 @@ import ( "os/exec" "os/user" "path/filepath" + "slices" "strconv" "strings" "time" @@ -154,11 +155,9 @@ func ValidateRuntimeSelection(cfg state.Config) error { for _, runtime := range catalog.Runtimes { if strings.EqualFold(runtime.ID, runtimeID) { - for _, candidate := range runtime.Versions { - if candidate == version { - log.Printf("[provision] action=validate_runtime runtime=%s version=%s status=ok", runtimeID, version) - return nil - } + if slices.Contains(runtime.Versions, version) { + log.Printf("[provision] action=validate_runtime runtime=%s version=%s status=ok", runtimeID, version) + return nil } return fmt.Errorf("unsupported %s version: %s", runtimeID, version) } diff --git a/internal/provision/files.go b/internal/provision/files.go index d47da5b..558bf2b 100755 --- a/internal/provision/files.go +++ b/internal/provision/files.go @@ -95,10 +95,7 @@ func WriteStartScript(cfg state.Config) error { } xmx := mem - xms := mem / 2 - if xms < 512 { - xms = 512 - } + xms := max(mem/2, 512) if xms > xmx { xms = xmx } diff --git a/internal/provision/minecraft/fabric_proxy.go b/internal/provision/minecraft/fabric_proxy.go index 349c8ca..69ffc02 100644 --- a/internal/provision/minecraft/fabric_proxy.go +++ b/internal/provision/minecraft/fabric_proxy.go @@ -300,10 +300,7 @@ func parseVersion(raw string) ([]int, error) { } func compareVersionSlices(a, b []int) int { - maxLen := len(a) - if len(b) > maxLen { - maxLen = len(b) - } + maxLen := max(len(b), len(a)) for i := 0; i < maxLen; i++ { ai := 0 diff --git a/internal/provision/provision.go b/internal/provision/provision.go index c818f54..a0bed28 100644 --- a/internal/provision/provision.go +++ b/internal/provision/provision.go @@ -151,7 +151,7 @@ func ProvisionAll(cfg state.Config) error { /* --------------------------------------------------------- ADDONS (OPTIONAL, ROLE-AGNOSTIC) --------------------------------------------------------- */ - if strings.EqualFold(cfg.ContainerType, "dev") && codeServerRequested(cfg) { + if strings.EqualFold(cfg.ContainerType, "dev") && CodeServerRequested(cfg) { seen := false for _, addon := range cfg.Addons { if strings.EqualFold(addon, "codeserver") { @@ -173,7 +173,7 @@ func ProvisionAll(cfg state.Config) error { return nil } -func codeServerRequested(cfg state.Config) bool { +func CodeServerRequested(cfg state.Config) bool { if cfg.EnableCodeServer { return true } diff --git a/internal/system/process.go b/internal/system/process.go index c0771dd..451a046 100755 --- a/internal/system/process.go +++ b/internal/system/process.go @@ -182,10 +182,7 @@ func minecraftHeap(memoryMB int) (xms int, xmx int) { mem = 2048 } xmx = mem - xms = mem / 2 - if xms < 512 { - xms = 512 - } + xms = max(mem/2, 512) if xms > xmx { xms = xmx } @@ -411,8 +408,8 @@ func GetConsolePTY(cfg *state.Config) (*os.File, error) { } func WriteConsoleInput(cfg *state.Config, input string) error { - if strings.HasSuffix(input, "\n") { - input = strings.TrimSuffix(input, "\n") + if before, ok := strings.CutSuffix(input, "\n"); ok { + input = before } payload := []byte(input + "\n") @@ -469,10 +466,7 @@ func buildCrashInfo(cfg *state.Config, waitErr error, startedAt time.Time) *stat exitCode, signal := extractExitDetails(waitErr) uptime := int64(0) if !startedAt.IsZero() { - uptime = int64(time.Since(startedAt).Seconds()) - if uptime < 0 { - uptime = 0 - } + uptime = max(int64(time.Since(startedAt).Seconds()), 0) } logTail := tailLogLines(cfg, 40) diff --git a/state/update.json b/state/update.json index cc0a30e..a745afa 100644 --- a/state/update.json +++ b/state/update.json @@ -1,6 +1,6 @@ { - "status": "error", + "status": "available", "current": "0.0.0-dev", - "error": "Get \"http://zlh-artifacts.internal.zlh:8080/agents/manifest.json\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)", - "checked_at_utc": "2026-04-19T11:18:03Z" + "target": "1.0.68", + "checked_at_utc": "2026-04-19T21:48:03Z" } \ No newline at end of file