From b6040ecb9c7866abb80260c1392feae4aab4af40 Mon Sep 17 00:00:00 2001 From: Robert Willert Date: Wed, 18 Dec 2024 16:07:07 +0100 Subject: [PATCH] allow search domain in global settings --- go.mod | 1 + go.sum | 2 ++ handler/routes.go | 2 +- util/util.go | 55 ++++++++++++++++++++++++----------------------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index b3fd496f..118e0fbc 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( ) require ( + github.com/chmike/domain v1.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-test/deep v1.1.0 // indirect diff --git a/go.sum b/go.sum index 6029c472..efcdecfd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/NicoNex/echotron/v3 v3.39.0 h1:DIOskt7z2oLt6uk3eyM09XJJzDsIJuRlJXZdoE/H/Zs= github.com/NicoNex/echotron/v3 v3.39.0/go.mod h1:7LvjveJmezuUOeaoA3nzQduNlSPQYfq219Z+baKY04Q= +github.com/chmike/domain v1.1.0 h1:615mGyA/ghxvIFBdAaYuB2azxAsUxrpm6Cv5UiL6VPo= +github.com/chmike/domain v1.1.0/go.mod h1:h558M2qGKpYRUxHHNyey6puvXkZBjvjmseOla/d1VGQ= github.com/coreos/bbolt v1.3.1-coreos.6.0.20180223184059-4f5275f4ebbf/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/handler/routes.go b/handler/routes.go index 0591c728..f61eb657 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -1015,7 +1015,7 @@ func GlobalSettingSubmit(db store.IStore) echo.HandlerFunc { c.Bind(&globalSettings) // validate the input dns server list - if util.ValidateIPAddressList(globalSettings.DNSServers) == false { + if !util.ValidateIPAndSearchDomainAddressList(globalSettings.DNSServers) { log.Warnf("Invalid DNS server list input from user: %v", globalSettings.DNSServers) return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Invalid DNS server address"}) } diff --git a/util/util.go b/util/util.go index ac10ac4c..80bafc0f 100644 --- a/util/util.go +++ b/util/util.go @@ -25,6 +25,7 @@ import ( "github.com/skip2/go-qrcode" "golang.org/x/mod/sumdb/dirhash" + "github.com/chmike/domain" externalip "github.com/glendc/go-external-ip" "github.com/labstack/gommon/log" "github.com/rwillert/wireguard-ui/model" @@ -117,10 +118,7 @@ func ContainsCIDR(ipnet1, ipnet2 *net.IPNet) bool { // ValidateCIDR to validate a network CIDR func ValidateCIDR(cidr string) bool { _, _, err := net.ParseCIDR(cidr) - if err != nil { - return false - } - return true + return err == nil } // ValidateCIDRList to validate a list of network CIDR @@ -128,12 +126,12 @@ func ValidateCIDRList(cidrs []string, allowEmpty bool) bool { for _, cidr := range cidrs { if allowEmpty { if len(cidr) > 0 { - if ValidateCIDR(cidr) == false { + if !ValidateCIDR(cidr) { return false } } } else { - if ValidateCIDR(cidr) == false { + if !ValidateCIDR(cidr) { return false } } @@ -143,42 +141,45 @@ func ValidateCIDRList(cidrs []string, allowEmpty bool) bool { // ValidateAllowedIPs to validate allowed ip addresses in CIDR format func ValidateAllowedIPs(cidrs []string) bool { - if ValidateCIDRList(cidrs, false) == false { - return false - } - return true + return ValidateCIDRList(cidrs, false) } // ValidateExtraAllowedIPs to validate extra Allowed ip addresses, allowing empty strings func ValidateExtraAllowedIPs(cidrs []string) bool { - if ValidateCIDRList(cidrs, true) == false { - return false - } - return true + return ValidateCIDRList(cidrs, true) } // ValidateServerAddresses to validate allowed ip addresses in CIDR format func ValidateServerAddresses(cidrs []string) bool { - if ValidateCIDRList(cidrs, false) == false { - return false - } - return true + return ValidateCIDRList(cidrs, false) } // ValidateIPAddress to validate the IPv4 and IPv6 address func ValidateIPAddress(ip string) bool { - if net.ParseIP(ip) == nil { - return false - } - return true + return net.ParseIP(ip) != nil } -// ValidateIPAddressList to validate a list of IPv4 and IPv6 addresses -func ValidateIPAddressList(ips []string) bool { - for _, ip := range ips { - if ValidateIPAddress(ip) == false { - return false +// ValidateDomainName to validate domain name +func ValidateDomainName(name string) bool { + return domain.Check(name) == nil +} + +// ValidateIPAndSearchDomainAddressList to validate a list of IPv4 and IPv6 addresses plus added search domains +func ValidateIPAndSearchDomainAddressList(entries []string) bool { + ip := false + domain := false + for _, entry := range entries { + // ip but not after domain + if ValidateIPAddress(entry) && !domain { + ip = true + continue } + // domain and after ip + if ValidateDomainName(entry) && ip { + domain = true + continue + } + return false } return true }