mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-17 02:25:02 +03:00
fix(amneziawg): validate peer subnets before enabling kernel mode (#6965)
A kernel-mode AmneziaWG inbound routes and masquerades only its interface subnet, so peers whose AllowedIPs lay outside it (a restored backup, a changed subnet) handshook but got no internet. Enabling kernel mode, saving an active kernel inbound, and adding, enabling or re-addressing a client now refuse out-of-subnet peers with the offending client named. Legacy stray peers still start and can still be edited or disabled.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -31,7 +32,7 @@ func droppedClientNeedsRestart() bool {
|
||||
return on
|
||||
}
|
||||
|
||||
func validateAmneziaWGClientWrite(inbound *model.Inbound, clients []model.Client) error {
|
||||
func validateAmneziaWGClientWrite(inbound *model.Inbound, clients []model.Client, previousEmail string) error {
|
||||
if inbound.Protocol != model.AmneziaWG {
|
||||
return nil
|
||||
}
|
||||
@@ -59,15 +60,47 @@ func validateAmneziaWGClientWrite(inbound *model.Inbound, clients []model.Client
|
||||
return nil
|
||||
}
|
||||
inst := amneziawg.Instance{Listen: inbound.Listen, IPv6Enabled: settings.Server.IPv6Enabled}
|
||||
v4Base, _, err := defaultAmneziaWGSubnetBases(inbound.Settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inst.Address = []string{v4Base}
|
||||
for _, client := range clients {
|
||||
inst.Peers = append(inst.Peers, amneziawg.Peer{
|
||||
AllowedIPs: client.AllowedIPs, ForwardedPorts: client.ForwardedPortsSpec(),
|
||||
Email: client.Email, AllowedIPs: client.AllowedIPs, ForwardedPorts: client.ForwardedPortsSpec(),
|
||||
})
|
||||
}
|
||||
if err := amneziawgkern.ValidateInstanceFeatures(inst, settings.Server.RouteThroughXray); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
if !inbound.Enable {
|
||||
return nil
|
||||
}
|
||||
inst.Peers = nil
|
||||
for _, client := range clients {
|
||||
if !client.Enable {
|
||||
continue
|
||||
}
|
||||
allowedIPs := client.AllowedIPs
|
||||
for _, old := range settings.Clients {
|
||||
if previousEmail == "" || !strings.EqualFold(old.Email, previousEmail) {
|
||||
continue
|
||||
}
|
||||
if len(allowedIPs) == 0 {
|
||||
allowedIPs = old.AllowedIPs
|
||||
}
|
||||
// Preserve legacy addresses on active peers; validate newly added
|
||||
// addresses, or every address when enabling a disabled peer.
|
||||
if old.Enable {
|
||||
allowedIPs = slices.DeleteFunc(slices.Clone(allowedIPs), func(ip string) bool {
|
||||
return slices.Contains(old.AllowedIPs, ip)
|
||||
})
|
||||
}
|
||||
break
|
||||
}
|
||||
inst.Peers = append(inst.Peers, amneziawg.Peer{Email: client.Email, AllowedIPs: allowedIPs})
|
||||
}
|
||||
return amneziawgkern.ValidatePeerSubnets(inst)
|
||||
}
|
||||
|
||||
func sameClientConfigExceptUpdatedAt(a, b map[string]any) bool {
|
||||
@@ -482,7 +515,7 @@ func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := validateAmneziaWGClientWrite(oldInbound, clients); err != nil {
|
||||
if err := validateAmneziaWGClientWrite(oldInbound, clients, ""); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -850,7 +883,7 @@ func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *mo
|
||||
clients[0].ForwardedPorts = old.ForwardedPorts
|
||||
}
|
||||
}
|
||||
if err := validateAmneziaWGClientWrite(oldInbound, clients); err != nil {
|
||||
if err := validateAmneziaWGClientWrite(oldInbound, clients, oldEmail); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if oldInbound.Protocol == model.AmneziaWG {
|
||||
|
||||
Reference in New Issue
Block a user