fix(amneziawg): let an emptied forwardedPorts withdraw a client's forwards

UpdateInboundClient carried the stored forwardedPorts forward whenever
the incoming value was empty, and Client.ForwardedPorts was a plain
omitempty string, so clearing the field in the client editor, as its
hint says to, reported success while the settings kept the old ports
and the host kept relaying them to the client's device.

The field is now a pointer, as KeepAlive is: nil (not sent) keeps the
stored spec and "" withdraws it. The client record follows the live
spec on every update, and a one-time seeder fills records that still
hold their creation value from the inbound settings, so the editor no
longer posts an empty field that would drop forwards added later.
This commit is contained in:
MHSanaei
2026-11-01 08:55:38 +01:00
parent 0972ac31eb
commit a9191cb468
19 changed files with 304 additions and 31 deletions
+13 -9
View File
@@ -61,7 +61,7 @@ func validateAmneziaWGClientWrite(inbound *model.Inbound, clients []model.Client
inst := amneziawg.Instance{Listen: inbound.Listen, IPv6Enabled: settings.Server.IPv6Enabled}
for _, client := range clients {
inst.Peers = append(inst.Peers, amneziawg.Peer{
AllowedIPs: client.AllowedIPs, ForwardedPorts: client.ForwardedPorts,
AllowedIPs: client.AllowedIPs, ForwardedPorts: client.ForwardedPortsSpec(),
})
}
if err := amneziawgkern.ValidateInstanceFeatures(inst, settings.Server.RouteThroughXray); err != nil {
@@ -537,7 +537,7 @@ func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model
}
}
if oldInbound.Protocol == model.AmneziaWG {
if hit := inboundSvc.checkForwardedPortsConflict(portCtx, client.ForwardedPorts); hit != "" {
if hit := inboundSvc.checkForwardedPortsConflict(portCtx, client.ForwardedPortsSpec()); hit != "" {
return false, common.NewError("amneziawg: forwardedPorts collides with", hit)
}
}
@@ -607,7 +607,7 @@ func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model
return pErr
}
for i := range clients {
if hit := inboundSvc.checkForwardedPortsConflict(txPortCtx, clients[i].ForwardedPorts); hit != "" {
if hit := inboundSvc.checkForwardedPortsConflict(txPortCtx, clients[i].ForwardedPortsSpec()); hit != "" {
return common.NewError("amneziawg: forwardedPorts collides with", hit)
}
}
@@ -845,8 +845,8 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
if clients[0].KeepAlive == nil {
clients[0].KeepAlive = old.KeepAlive
}
// AWG partial edits preserve forwarding specs; kernel mode rejects legacy specs.
if oldInbound.Protocol == model.AmneziaWG && clients[0].ForwardedPorts == "" {
// AWG partial edits (nil: the field was not sent) keep the stored spec; "" withdraws it.
if oldInbound.Protocol == model.AmneziaWG && clients[0].ForwardedPorts == nil {
clients[0].ForwardedPorts = old.ForwardedPorts
}
}
@@ -858,7 +858,7 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
if err != nil {
return false, err
}
if hit := inboundSvc.checkForwardedPortsConflict(portCtx, clients[0].ForwardedPorts); hit != "" {
if hit := inboundSvc.checkForwardedPortsConflict(portCtx, clients[0].ForwardedPortsSpec()); hit != "" {
return false, common.NewError("amneziawg: forwardedPorts collides with", hit)
}
}
@@ -911,10 +911,14 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
if clients[0].KeepAlive != nil {
newMap["keepAlive"] = clients[0].KeepAlive
}
if oldInbound.Protocol == model.AmneziaWG && clients[0].ForwardedPorts != "" {
newMap["forwardedPorts"] = clients[0].ForwardedPorts
if oldInbound.Protocol == model.AmneziaWG && clients[0].ForwardedPortsSpec() != "" {
newMap["forwardedPorts"] = clients[0].ForwardedPortsSpec()
}
}
// Withdrawn forwards are stored as no spec at all, whatever the protocol.
if clients[0].ForwardedPortsSpec() == "" {
delete(newMap, "forwardedPorts")
}
if oldClientMap != nil && sameClientConfigExceptUpdatedAt(oldClientMap, newMap) {
if v, ok2 := oldClientMap["updated_at"]; ok2 {
newMap["updated_at"] = v
@@ -995,7 +999,7 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
if pErr != nil {
return pErr
}
if hit := inboundSvc.checkForwardedPortsConflict(txPortCtx, clients[0].ForwardedPorts); hit != "" {
if hit := inboundSvc.checkForwardedPortsConflict(txPortCtx, clients[0].ForwardedPortsSpec()); hit != "" {
return common.NewError("amneziawg: forwardedPorts collides with", hit)
}
}