1
0

client_registry.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package server
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. // ClientInfo captures metadata about a connected frpc instance.
  8. type ClientInfo struct {
  9. Key string
  10. User string
  11. ClientID string
  12. RunID string
  13. Hostname string
  14. IP string
  15. FirstConnectedAt time.Time
  16. LastConnectedAt time.Time
  17. DisconnectedAt time.Time
  18. Online bool
  19. }
  20. // ClientRegistry keeps track of active clients keyed by "{user}.{clientID}" (or runID if clientID is empty).
  21. // Entries without an explicit clientID are removed on disconnect to avoid stale offline records.
  22. type ClientRegistry struct {
  23. mu sync.RWMutex
  24. clients map[string]*ClientInfo
  25. runIndex map[string]string
  26. }
  27. func NewClientRegistry() *ClientRegistry {
  28. return &ClientRegistry{
  29. clients: make(map[string]*ClientInfo),
  30. runIndex: make(map[string]string),
  31. }
  32. }
  33. // Register stores/updates metadata for a client and returns the registry key plus whether it conflicts with an online client.
  34. func (cr *ClientRegistry) Register(user, clientID, runID, hostname, remoteAddr string) (key string, conflict bool) {
  35. if runID == "" {
  36. return "", false
  37. }
  38. effectiveID := clientID
  39. if effectiveID == "" {
  40. effectiveID = runID
  41. }
  42. key = cr.composeClientKey(user, effectiveID)
  43. enforceUnique := clientID != ""
  44. now := time.Now()
  45. cr.mu.Lock()
  46. defer cr.mu.Unlock()
  47. info, exists := cr.clients[key]
  48. if enforceUnique && exists && info.Online && info.RunID != "" && info.RunID != runID {
  49. return key, true
  50. }
  51. if !exists {
  52. info = &ClientInfo{
  53. Key: key,
  54. User: user,
  55. ClientID: clientID,
  56. FirstConnectedAt: now,
  57. }
  58. cr.clients[key] = info
  59. } else if info.RunID != "" {
  60. delete(cr.runIndex, info.RunID)
  61. }
  62. info.RunID = runID
  63. info.Hostname = hostname
  64. info.IP = remoteAddr
  65. if info.FirstConnectedAt.IsZero() {
  66. info.FirstConnectedAt = now
  67. }
  68. info.LastConnectedAt = now
  69. info.DisconnectedAt = time.Time{}
  70. info.Online = true
  71. cr.runIndex[runID] = key
  72. return key, false
  73. }
  74. // MarkOfflineByRunID marks the client as offline when the corresponding control disconnects.
  75. func (cr *ClientRegistry) MarkOfflineByRunID(runID string) {
  76. cr.mu.Lock()
  77. defer cr.mu.Unlock()
  78. key, ok := cr.runIndex[runID]
  79. if !ok {
  80. return
  81. }
  82. if info, ok := cr.clients[key]; ok && info.RunID == runID {
  83. if info.ClientID == "" {
  84. delete(cr.clients, key)
  85. } else {
  86. info.RunID = ""
  87. info.Online = false
  88. now := time.Now()
  89. info.DisconnectedAt = now
  90. }
  91. }
  92. delete(cr.runIndex, runID)
  93. }
  94. // List returns a snapshot of all known clients.
  95. func (cr *ClientRegistry) List() []ClientInfo {
  96. cr.mu.RLock()
  97. defer cr.mu.RUnlock()
  98. result := make([]ClientInfo, 0, len(cr.clients))
  99. for _, info := range cr.clients {
  100. result = append(result, *info)
  101. }
  102. return result
  103. }
  104. // GetByKey retrieves a client by its composite key ({user}.{clientID} or runID fallback).
  105. func (cr *ClientRegistry) GetByKey(key string) (ClientInfo, bool) {
  106. cr.mu.RLock()
  107. defer cr.mu.RUnlock()
  108. info, ok := cr.clients[key]
  109. if !ok {
  110. return ClientInfo{}, false
  111. }
  112. return *info, true
  113. }
  114. func (cr *ClientRegistry) composeClientKey(user, id string) string {
  115. switch {
  116. case user == "":
  117. return id
  118. case id == "":
  119. return user
  120. default:
  121. return fmt.Sprintf("%s.%s", user, id)
  122. }
  123. }