mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-24 00:24:06 +03:00
Validate the client IP allocation
This commit is contained in:
parent
15703b9185
commit
e52ffaf686
2 changed files with 58 additions and 10 deletions
40
util/util.go
40
util/util.go
|
@ -268,3 +268,43 @@ func GetAvailableIP(cidr string, allocatedList []string) (string, error) {
|
|||
|
||||
return "", errors.New("No more available ip address")
|
||||
}
|
||||
|
||||
// ValidateIPAllocation to validate the list of client's ip allocation
|
||||
// They must have a correct format and available in serverAddresses space
|
||||
func ValidateIPAllocation(serverAddresses []string, ipAllocatedList []string, ipAllocationList []string) (bool, error) {
|
||||
for _, clientCIDR := range ipAllocationList {
|
||||
ip, _, _ := net.ParseCIDR(clientCIDR)
|
||||
|
||||
// clientCIDR must be in CIDR format
|
||||
if ip == nil {
|
||||
return false, fmt.Errorf("Invalid ip allocation input %s. Must be in CIDR format", clientCIDR)
|
||||
}
|
||||
|
||||
// return false immediately if the ip is already in use (in ipAllocatedList)
|
||||
for _, item := range ipAllocatedList {
|
||||
if item == ip.String() {
|
||||
return false, fmt.Errorf("IP %s already allocated", ip)
|
||||
}
|
||||
}
|
||||
|
||||
// even if it is not in use, we still need to check if it
|
||||
// belongs to a network of the server.
|
||||
var isValid bool = false
|
||||
for _, serverCIDR := range serverAddresses {
|
||||
_, serverNet, _ := net.ParseCIDR(serverCIDR)
|
||||
if serverNet.Contains(ip) {
|
||||
isValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// current ip allocation is valid, check the next one
|
||||
if isValid {
|
||||
continue
|
||||
} else {
|
||||
return false, fmt.Errorf("IP %s does not belong to any network addresses of WireGuard server", ip)
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue