Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added beszel-linux
Binary file not shown.
30 changes: 30 additions & 0 deletions internal/hub/agent_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hub
import (
"context"
"errors"
"log/slog"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -133,9 +134,38 @@ func (acr *agentConnectRequest) verifyWsConn(conn *gws.Conn, fpRecords []ws.Fing
return err
}

// Auto-update system host if the agent's IP has changed
acr.updateSystemHost(fpRecord.SystemId)

return acr.hub.sm.AddWebSocketSystem(fpRecord.SystemId, acr.agentSemVer, wsConn)
}

// updateSystemHost checks if the current connection's IP matches the system's recorded host
// and updates it if they differ.
func (acr *agentConnectRequest) updateSystemHost(systemId string) {
if systemId == "" {
return
}

systemRecord, err := acr.hub.FindRecordById("systems", systemId)
if err != nil {
return
}

currentIP := getRealIP(acr.req)
recordedHost := systemRecord.GetString("host")

// Only update if the IP is valid and different
if currentIP != "" && recordedHost != currentIP {
systemRecord.Set("host", currentIP)
if err := acr.hub.SaveNoValidate(systemRecord); err != nil {
slog.Error("Failed to auto-update agent host", "systemId", systemId, "err", err)
} else {
slog.Info("Auto-updated agent host", "name", systemRecord.GetString("name"), "new_ip", currentIP)
}
}
}

// validateAgentHeaders extracts and validates the token and agent version from HTTP headers.
func (acr *agentConnectRequest) validateAgentHeaders(headers http.Header) (string, string, error) {
token := headers.Get("X-Token")
Expand Down