Skip to content

Commit 87dff69

Browse files
authored
Merge pull request #17 from rwillert/searchdomain2
allow search domain in global settings
2 parents aa984ca + b6040ec commit 87dff69

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
)
2626

2727
require (
28+
github.com/chmike/domain v1.1.0 // indirect
2829
github.com/go-playground/locales v0.14.1 // indirect
2930
github.com/go-playground/universal-translator v0.18.1 // indirect
3031
github.com/go-test/deep v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/NicoNex/echotron/v3 v3.39.0 h1:DIOskt7z2oLt6uk3eyM09XJJzDsIJuRlJXZdoE/H/Zs=
22
github.com/NicoNex/echotron/v3 v3.39.0/go.mod h1:7LvjveJmezuUOeaoA3nzQduNlSPQYfq219Z+baKY04Q=
3+
github.com/chmike/domain v1.1.0 h1:615mGyA/ghxvIFBdAaYuB2azxAsUxrpm6Cv5UiL6VPo=
4+
github.com/chmike/domain v1.1.0/go.mod h1:h558M2qGKpYRUxHHNyey6puvXkZBjvjmseOla/d1VGQ=
35
github.com/coreos/bbolt v1.3.1-coreos.6.0.20180223184059-4f5275f4ebbf/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
46
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

handler/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ func GlobalSettingSubmit(db store.IStore) echo.HandlerFunc {
10151015
c.Bind(&globalSettings)
10161016

10171017
// validate the input dns server list
1018-
if util.ValidateIPAddressList(globalSettings.DNSServers) == false {
1018+
if !util.ValidateIPAndSearchDomainAddressList(globalSettings.DNSServers) {
10191019
log.Warnf("Invalid DNS server list input from user: %v", globalSettings.DNSServers)
10201020
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Invalid DNS server address"})
10211021
}

util/util.go

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
118119
func 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
127125
func 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
145143
func 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
153148
func 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
161153
func 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
169158
func 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

Comments
 (0)