| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package registry |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sync" |
| 20 | "time" |
| 21 |
| 22 | "k8s.io/utils/clock" |
| 23 | ) |
| 24 |
| 25 | // ClientInfo captures metadata about a connected frpc instance. |
| 26 | type ClientInfo struct { |
| 27 | Key string |
| 28 | User string |
| 29 | RawClientID string |
| 30 | RunID string |
| 31 | Hostname string |
| 32 | IP string |
| 33 | Version string |
| 34 | WireProtocol string |
| 35 | FirstConnectedAt time.Time |
| 36 | LastConnectedAt time.Time |
| 37 | DisconnectedAt time.Time |
| 38 | Online bool |
| 39 | } |
| 40 |
| 41 | // ClientRegistry keeps track of active clients keyed by "{user}.{clientID}" (runID fallback when raw clientID is empty). |
| 42 | // Entries without an explicit raw clientID are removed on disconnect to avoid stale offline records. |
| 43 | type ClientRegistry struct { |
| 44 | mu sync.RWMutex |
| 45 | clients map[string]*ClientInfo |
| 46 | runIndex map[string]string |
| 47 | clock clock.PassiveClock |
| 48 | } |
| 49 |
| 50 | func NewClientRegistry() *ClientRegistry { |
| 51 | return newClientRegistryWithClock(clock.RealClock{}) |
| 52 | } |
| 53 |
| 54 | func newClientRegistryWithClock(clk clock.PassiveClock) *ClientRegistry { |
| 55 | if clk == nil { |
| 56 | clk = clock.RealClock{} |
| 57 | } |
| 58 | return &ClientRegistry{ |
| 59 | clients: make(map[string]*ClientInfo), |
| 60 | runIndex: make(map[string]string), |
| 61 | clock: clk, |
| 62 | } |
| 63 | } |
| 64 |
| 65 | // Register stores/updates metadata for a client and returns the registry key plus whether it conflicts with an online client. |
| 66 | func (cr *ClientRegistry) Register(user, rawClientID, runID, hostname, version, remoteAddr, wireProtocol string) (key string, conflict bool) { |
| 67 | if runID == "" { |
| 68 | return "", false |
| 69 | } |
| 70 |
| 71 | effectiveID := rawClientID |
| 72 | if effectiveID == "" { |
| 73 | effectiveID = runID |
| 74 | } |
| 75 | key = cr.composeClientKey(user, effectiveID) |
| 76 | enforceUnique := rawClientID != "" |
| 77 |
| 78 | now := cr.clock.Now() |
| 79 | cr.mu.Lock() |
| 80 | defer cr.mu.Unlock() |
| 81 |
| 82 | info, exists := cr.clients[key] |
| 83 | if enforceUnique && exists && info.Online && info.RunID != "" && info.RunID != runID { |
| 84 | return key, true |
| 85 | } |
| 86 |
| 87 | if !exists { |
| 88 | info = &ClientInfo{ |
| 89 | Key: key, |
| 90 | User: user, |
| 91 | FirstConnectedAt: now, |
| 92 | } |
| 93 | cr.clients[key] = info |
| 94 | } else if info.RunID != "" { |
| 95 | delete(cr.runIndex, info.RunID) |
| 96 | } |
| 97 |
| 98 | info.RawClientID = rawClientID |
| 99 | info.RunID = runID |
| 100 | info.Hostname = hostname |
| 101 | info.IP = remoteAddr |
| 102 | info.Version = version |
| 103 | info.WireProtocol = wireProtocol |
| 104 | if info.FirstConnectedAt.IsZero() { |
| 105 | info.FirstConnectedAt = now |
| 106 | } |
| 107 | info.LastConnectedAt = now |
| 108 | info.DisconnectedAt = time.Time{} |
| 109 | info.Online = true |
| 110 |
| 111 | cr.runIndex[runID] = key |
| 112 | return key, false |
| 113 | } |
| 114 |
| 115 | // MarkOfflineByRunID marks the client as offline when the corresponding control disconnects. |
| 116 | func (cr *ClientRegistry) MarkOfflineByRunID(runID string) { |
| 117 | cr.mu.Lock() |
| 118 | defer cr.mu.Unlock() |
| 119 |
| 120 | key, ok := cr.runIndex[runID] |
| 121 | if !ok { |
| 122 | return |
| 123 | } |
| 124 | if info, ok := cr.clients[key]; ok && info.RunID == runID { |
| 125 | if info.RawClientID == "" { |
| 126 | delete(cr.clients, key) |
| 127 | } else { |
| 128 | info.RunID = "" |
| 129 | info.Online = false |
| 130 | now := cr.clock.Now() |
| 131 | info.DisconnectedAt = now |
| 132 | } |
| 133 | } |
| 134 | delete(cr.runIndex, runID) |
| 135 | } |
| 136 |
| 137 | // List returns a snapshot of all known clients. |
| 138 | func (cr *ClientRegistry) List() []ClientInfo { |
| 139 | cr.mu.RLock() |
| 140 | defer cr.mu.RUnlock() |
| 141 |
| 142 | result := make([]ClientInfo, 0, len(cr.clients)) |
| 143 | for _, info := range cr.clients { |
| 144 | result = append(result, *info) |
| 145 | } |
| 146 | return result |
| 147 | } |
| 148 |
| 149 | // GetByKey retrieves a client by its composite key ({user}.{clientID} with runID fallback). |
| 150 | func (cr *ClientRegistry) GetByKey(key string) (ClientInfo, bool) { |
| 151 | cr.mu.RLock() |
| 152 | defer cr.mu.RUnlock() |
| 153 |
| 154 | info, ok := cr.clients[key] |
| 155 | if !ok { |
| 156 | return ClientInfo{}, false |
| 157 | } |
| 158 | return *info, true |
| 159 | } |
| 160 |
| 161 | // ClientID returns the resolved client identifier for external use. |
| 162 | func (info ClientInfo) ClientID() string { |
| 163 | if info.RawClientID != "" { |
| 164 | return info.RawClientID |
| 165 | } |
| 166 | return info.RunID |
| 167 | } |
| 168 |
| 169 | func (cr *ClientRegistry) composeClientKey(user, id string) string { |
| 170 | switch { |
| 171 | case user == "": |
| 172 | return id |
| 173 | case id == "": |
| 174 | return user |
| 175 | default: |
| 176 | return fmt.Sprintf("%s.%s", user, id) |
| 177 | } |
| 178 | } |