Split the client edit form's AllowedIPs into per-protocol fields

A client attached to both WireGuard and AmneziaWG shared one AllowedIPs
form field with a dynamically-switching label, so its two genuinely
different addresses could never both be shown or edited correctly.
Worse, Update/Create broadcast that one shared value to every attached
wg/awg inbound with no subnet-fit check, so an ordinary edit save could
silently overwrite one protocol's address with the other's -- the same
bug class already fixed for Attach, but reachable from any client edit.

model.Client gains an optional AllowedIPsByInbound map so a caller can
send distinct values per inbound; Update/Create honor it and, when it's
absent, clear a shared value that doesn't fit an AmneziaWG inbound's own
subnet instead of writing it through. A new TunnelAllowedIPsByInbound
read path feeds the real per-inbound address to the client edit form via
GET, which now renders two separate, correctly-labeled fields whenever
both protocols are attached (unchanged single dynamic field otherwise).
This commit is contained in:
Kuzz007
2026-08-04 14:13:20 +03:00
parent 569b7fbc6b
commit 878ee839db
13 changed files with 500 additions and 62 deletions
+29 -2
View File
@@ -126,7 +126,19 @@ func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreate
if err := s.fillProtocolDefaults(&client, inbound); err != nil {
return needRestart, err
}
settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(client, inbound)}})
clientForInbound := client
if ips, ok := client.AllowedIPsByInbound[ibId]; ok {
clientForInbound.AllowedIPs = ips
} else if !addressesFitAmneziaWGInbound(clientForInbound.AllowedIPs, inbound) {
// The shared AllowedIPs value (e.g. from a single-field legacy
// caller) came from a different subnet than this inbound's own --
// clear it so defaultAmneziaWGClients allocates a fresh, correct
// address for THIS inbound instead of persisting an unroutable
// peer. Same reasoning as addressesFitAmneziaWGInbound's own doc
// comment on the Attach path.
clientForInbound.AllowedIPs = nil
}
settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(clientForInbound, inbound)}})
if mErr != nil {
return needRestart, mErr
}
@@ -414,7 +426,22 @@ func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model
if err := s.fillProtocolDefaults(&updated, inbound); err != nil {
return needRestart, err
}
settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(updated, inbound)}})
clientForInbound := updated
if ips, ok := updated.AllowedIPsByInbound[ibId]; ok {
clientForInbound.AllowedIPs = ips
} else if !addressesFitAmneziaWGInbound(clientForInbound.AllowedIPs, inbound) {
// A single shared AllowedIPs field (the common case for a caller
// that never sends AllowedIPsByInbound) must never overwrite an
// inbound it doesn't belong to -- e.g. a client attached to both
// wg and awg saving its wg-labeled address would otherwise get
// that same address silently written into the awg peer config
// too. Clearing it here makes UpdateInboundClient's own
// empty-AllowedIPs carry-forward (see its WireGuard/AmneziaWG
// branch) preserve THIS inbound's existing, correct value
// instead.
clientForInbound.AllowedIPs = nil
}
settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(clientForInbound, inbound)}})
if mErr != nil {
return needRestart, mErr
}