server: replace client metadata with IP address in registry (#5118)

This commit is contained in:
fatedier
2026-01-09 11:07:19 +08:00
committed by GitHub
parent 479e9f50c2
commit 1245f8804e
9 changed files with 48 additions and 85 deletions

View File

@@ -2,7 +2,6 @@ package server
import (
"fmt"
"maps"
"sync"
"time"
)
@@ -14,7 +13,7 @@ type ClientInfo struct {
ClientID string
RunID string
Hostname string
Metas map[string]string
IP string
FirstConnectedAt time.Time
LastConnectedAt time.Time
DisconnectedAt time.Time
@@ -37,7 +36,7 @@ func NewClientRegistry() *ClientRegistry {
}
// Register stores/updates metadata for a client and returns the registry key plus whether it conflicts with an online client.
func (cr *ClientRegistry) Register(user, clientID, runID, hostname string, metas map[string]string) (key string, conflict bool) {
func (cr *ClientRegistry) Register(user, clientID, runID, hostname, remoteAddr string) (key string, conflict bool) {
if runID == "" {
return "", false
}
@@ -72,7 +71,7 @@ func (cr *ClientRegistry) Register(user, clientID, runID, hostname string, metas
info.RunID = runID
info.Hostname = hostname
info.Metas = metas
info.IP = remoteAddr
if info.FirstConnectedAt.IsZero() {
info.FirstConnectedAt = now
}
@@ -113,9 +112,7 @@ func (cr *ClientRegistry) List() []ClientInfo {
result := make([]ClientInfo, 0, len(cr.clients))
for _, info := range cr.clients {
cp := *info
cp.Metas = maps.Clone(info.Metas)
result = append(result, cp)
result = append(result, *info)
}
return result
}
@@ -129,9 +126,7 @@ func (cr *ClientRegistry) GetByKey(key string) (ClientInfo, bool) {
if !ok {
return ClientInfo{}, false
}
cp := *info
cp.Metas = maps.Clone(info.Metas)
return cp, true
return *info, true
}
func (cr *ClientRegistry) composeClientKey(user, id string) string {

View File

@@ -94,16 +94,16 @@ type serverInfoResp struct {
}
type clientInfoResp struct {
Key string `json:"key"`
User string `json:"user"`
ClientID string `json:"clientId"`
RunID string `json:"runId"`
Hostname string `json:"hostname"`
Metas map[string]string `json:"metas,omitempty"`
FirstConnectedAt int64 `json:"firstConnectedAt"`
LastConnectedAt int64 `json:"lastConnectedAt"`
DisconnectedAt int64 `json:"disconnectedAt,omitempty"`
Online bool `json:"online"`
Key string `json:"key"`
User string `json:"user"`
ClientID string `json:"clientID"`
RunID string `json:"runID"`
Hostname string `json:"hostname"`
ClientIP string `json:"clientIP,omitempty"`
FirstConnectedAt int64 `json:"firstConnectedAt"`
LastConnectedAt int64 `json:"lastConnectedAt"`
DisconnectedAt int64 `json:"disconnectedAt,omitempty"`
Online bool `json:"online"`
}
// /healthz
@@ -531,7 +531,7 @@ func buildClientInfoResp(info ClientInfo) clientInfoResp {
ClientID: info.ClientID,
RunID: info.RunID,
Hostname: info.Hostname,
Metas: info.Metas,
ClientIP: info.IP,
FirstConnectedAt: toUnix(info.FirstConnectedAt),
LastConnectedAt: toUnix(info.LastConnectedAt),
Online: info.Online,

View File

@@ -615,7 +615,11 @@ func (svr *Service) RegisterControl(ctlConn net.Conn, loginMsg *msg.Login, inter
oldCtl.WaitClosed()
}
_, conflict := svr.clientRegistry.Register(loginMsg.User, loginMsg.ClientID, loginMsg.RunID, loginMsg.Hostname, loginMsg.Metas)
remoteAddr := ctlConn.RemoteAddr().String()
if host, _, err := net.SplitHostPort(remoteAddr); err == nil {
remoteAddr = host
}
_, conflict := svr.clientRegistry.Register(loginMsg.User, loginMsg.ClientID, loginMsg.RunID, loginMsg.Hostname, remoteAddr)
if conflict {
svr.ctlManager.Del(loginMsg.RunID, ctl)
ctl.Close()