Validate AllowedIPs from user input

This commit is contained in:
Khanh Ngo 2020-04-19 10:46:43 +07:00
parent fe57993240
commit 7ae4dd12dd
No known key found for this signature in database
GPG key ID: D5FAA6A16150E49E
5 changed files with 55 additions and 14 deletions

View file

@ -2,6 +2,7 @@ package util
import (
"fmt"
"net"
"strings"
"github.com/ngoduykhanh/wireguard-ui/model"
@ -38,3 +39,22 @@ func BuildClientConfig(client model.Client) string {
return strConfig
}
// ValidateCIDR to validate an network CIDR
func ValidateCIDR(cidr string) bool {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
return true
}
// ValidateAllowedIPs to validate allowed ip addresses in CIDR format.
func ValidateAllowedIPs(cidrs []string) bool {
for _, cidr := range cidrs {
if ValidateCIDR(cidr) == false {
return false
}
}
return true
}