@@ -25,6 +25,7 @@ import (
2525 "github.com/skip2/go-qrcode"
2626 "golang.org/x/mod/sumdb/dirhash"
2727
28+ "github.com/chmike/domain"
2829 externalip "github.com/glendc/go-external-ip"
2930 "github.com/labstack/gommon/log"
3031 "github.com/rwillert/wireguard-ui/model"
@@ -117,23 +118,20 @@ func ContainsCIDR(ipnet1, ipnet2 *net.IPNet) bool {
117118// ValidateCIDR to validate a network CIDR
118119func ValidateCIDR (cidr string ) bool {
119120 _ , _ , err := net .ParseCIDR (cidr )
120- if err != nil {
121- return false
122- }
123- return true
121+ return err == nil
124122}
125123
126124// ValidateCIDRList to validate a list of network CIDR
127125func ValidateCIDRList (cidrs []string , allowEmpty bool ) bool {
128126 for _ , cidr := range cidrs {
129127 if allowEmpty {
130128 if len (cidr ) > 0 {
131- if ValidateCIDR (cidr ) == false {
129+ if ! ValidateCIDR (cidr ) {
132130 return false
133131 }
134132 }
135133 } else {
136- if ValidateCIDR (cidr ) == false {
134+ if ! ValidateCIDR (cidr ) {
137135 return false
138136 }
139137 }
@@ -143,42 +141,45 @@ func ValidateCIDRList(cidrs []string, allowEmpty bool) bool {
143141
144142// ValidateAllowedIPs to validate allowed ip addresses in CIDR format
145143func ValidateAllowedIPs (cidrs []string ) bool {
146- if ValidateCIDRList (cidrs , false ) == false {
147- return false
148- }
149- return true
144+ return ValidateCIDRList (cidrs , false )
150145}
151146
152147// ValidateExtraAllowedIPs to validate extra Allowed ip addresses, allowing empty strings
153148func ValidateExtraAllowedIPs (cidrs []string ) bool {
154- if ValidateCIDRList (cidrs , true ) == false {
155- return false
156- }
157- return true
149+ return ValidateCIDRList (cidrs , true )
158150}
159151
160152// ValidateServerAddresses to validate allowed ip addresses in CIDR format
161153func ValidateServerAddresses (cidrs []string ) bool {
162- if ValidateCIDRList (cidrs , false ) == false {
163- return false
164- }
165- return true
154+ return ValidateCIDRList (cidrs , false )
166155}
167156
168157// ValidateIPAddress to validate the IPv4 and IPv6 address
169158func ValidateIPAddress (ip string ) bool {
170- if net .ParseIP (ip ) == nil {
171- return false
172- }
173- return true
159+ return net .ParseIP (ip ) != nil
174160}
175161
176- // ValidateIPAddressList to validate a list of IPv4 and IPv6 addresses
177- func ValidateIPAddressList (ips []string ) bool {
178- for _ , ip := range ips {
179- if ValidateIPAddress (ip ) == false {
180- return false
162+ // ValidateDomainName to validate domain name
163+ func ValidateDomainName (name string ) bool {
164+ return domain .Check (name ) == nil
165+ }
166+
167+ // ValidateIPAndSearchDomainAddressList to validate a list of IPv4 and IPv6 addresses plus added search domains
168+ func ValidateIPAndSearchDomainAddressList (entries []string ) bool {
169+ ip := false
170+ domain := false
171+ for _ , entry := range entries {
172+ // ip but not after domain
173+ if ValidateIPAddress (entry ) && ! domain {
174+ ip = true
175+ continue
181176 }
177+ // domain and after ip
178+ if ValidateDomainName (entry ) && ip {
179+ domain = true
180+ continue
181+ }
182+ return false
182183 }
183184 return true
184185}
0 commit comments