fix(client): stop duplicate client entries accumulating in inbound settings

Adding a user to multi-node inbounds could leave 3-6 identical entries
in one inbound's settings.clients array: addInboundClient appended
incoming clients unconditionally, and the duplicate-email precheck
exempts a matching subId (so one identity can span several inbounds),
so a retried or raced add of the same client re-appended it to an
inbound that already carried it - on the master and, since nodes run
the same code, on every node, whose snapshot adoption then copied the
duplicates back verbatim. The normalized clients/client_inbounds tables
stayed clean (unique constraints), which is why the phantom rows only
showed in settings-driven views like the Detach clients modal, where
duplicate React keys also broke the selection counter.

Three layers: addInboundClient now skips incoming clients whose email
is already on the target inbound (idempotent re-adds instead of
duplication), node snapshot adoption collapses duplicate emails before
writing the central row, and an idempotent startup repair rewrites any
inbound whose settings still carry duplicates from older builds.

Closes #5770
This commit is contained in:
MHSanaei
2026-07-05 21:17:25 +02:00
parent 9d1a21b484
commit 5a7b3b7370
6 changed files with 321 additions and 5 deletions
+39 -5
View File
@@ -319,12 +319,46 @@ func (s *ClientService) addInboundClient(inboundSvc *InboundService, data *model
return false, err
}
if oldInbound.Protocol == model.WireGuard {
existing, gcErr := inboundSvc.GetClients(oldInbound)
if gcErr != nil {
return false, gcErr
existingClients, err := inboundSvc.GetClients(oldInbound)
if err != nil {
return false, err
}
// A client already on this inbound is skipped instead of appended again:
// checkEmailsExistForClients exempts a matching subId so one identity can
// live on several inbounds, which let retried or raced adds duplicate the
// same email inside a single settings array (#5770). clients and
// interfaceClients are parsed from the same data.Settings array, so they
// stay index-aligned while filtering.
if len(existingClients) > 0 && len(clients) > 0 {
existingEmails := make(map[string]struct{}, len(existingClients))
for _, c := range existingClients {
if c.Email != "" {
existingEmails[strings.ToLower(c.Email)] = struct{}{}
}
}
if dErr := defaultWireguardClients(existing, clients, interfaceClients); dErr != nil {
keptClients := make([]model.Client, 0, len(clients))
keptWire := make([]any, 0, len(interfaceClients))
for i, c := range clients {
if c.Email != "" {
if _, dup := existingEmails[strings.ToLower(c.Email)]; dup {
continue
}
}
keptClients = append(keptClients, c)
if i < len(interfaceClients) {
keptWire = append(keptWire, interfaceClients[i])
}
}
if len(keptClients) == 0 {
return false, nil
}
clients = keptClients
interfaceClients = keptWire
}
if oldInbound.Protocol == model.WireGuard {
if dErr := defaultWireguardClients(existingClients, clients, interfaceClients); dErr != nil {
return false, dErr
}
}