From 67fef871c295b60258162992b5ea81c15fc3e282 Mon Sep 17 00:00:00 2001 From: Tyler Date: Fri, 31 Aug 2018 21:27:26 -0400 Subject: [PATCH] Fix wildcard matching --- etc/godns.example.conf | 2 +- hosts_file.go | 31 ++++++++++++------------------- hosts_redis.go | 34 +++++++++++++++------------------- utils.go | 6 +++--- 4 files changed, 31 insertions(+), 42 deletions(-) diff --git a/etc/godns.example.conf b/etc/godns.example.conf index 6827fd5..287eaaf 100644 --- a/etc/godns.example.conf +++ b/etc/godns.example.conf @@ -1,6 +1,6 @@ #Toml config file title = "GODNS" -Version = "0.2.1" +Version = "0.2.2" Author = "kenshin, tystuyfzand" debug = false diff --git a/hosts_file.go b/hosts_file.go index 71ae464..ab65245 100644 --- a/hosts_file.go +++ b/hosts_file.go @@ -4,8 +4,7 @@ import ( "sync" "github.com/fsnotify/fsnotify" "strings" - "golang.org/x/net/publicsuffix" - "os" + "os" "bufio" "regexp" "github.com/ryanuber/go-glob" @@ -51,28 +50,22 @@ func (f *FileHosts) Get(domain string) ([]string, bool) { f.mu.RLock() defer f.mu.RUnlock() domain = strings.ToLower(domain) - ip, ok := f.hosts[domain] - if ok { - return []string{ip}, true + + if ip, ok := f.hosts[domain]; ok { + return strings.Split(ip, ","), true } - sld, err := publicsuffix.EffectiveTLDPlusOne(domain) - if err != nil { - return nil, false + if idx := strings.Index(domain, "."); idx != -1 { + wildcard := "*." + domain[strings.Index(domain, ".") + 1:] + + if ip, ok := f.hosts[wildcard]; ok { + return strings.Split(ip, ","), true + } } for host, ip := range f.hosts { - if strings.HasPrefix(host, "*.") { - old, err := publicsuffix.EffectiveTLDPlusOne(host) - - if err != nil { - continue - } - - // Don't blindly accept wildcards, match it against string - if sld == old && glob.Glob(host, domain) { - return []string{ip}, true - } + if glob.Glob(host, domain) { + return strings.Split(ip, ","), true } } diff --git a/hosts_redis.go b/hosts_redis.go index effaf27..db4cee7 100644 --- a/hosts_redis.go +++ b/hosts_redis.go @@ -4,8 +4,7 @@ import ( "github.com/hoisie/redis" "sync" "strings" - "golang.org/x/net/publicsuffix" - "github.com/ryanuber/go-glob" + "github.com/ryanuber/go-glob" ) type RedisHosts struct { @@ -62,8 +61,10 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider { } else if msg.Channel == "godns:remove_record" { logger.Debug("Record %s was removed", msg.Message) + recordName := string(msg.Message) + rh.mu.Lock() - delete(rh.hosts, string(msg.Message)) + delete(rh.hosts, recordName) rh.mu.Unlock() } else if msg.Channel == keyspaceEvent { logger.Debug("Refreshing redis records due to update") @@ -82,30 +83,25 @@ func (r *RedisHosts) Get(domain string) ([]string, bool) { defer r.mu.RUnlock() domain = strings.ToLower(domain) - ip, ok := r.hosts[domain] - if ok { + + if ip, ok := r.hosts[domain]; ok { return strings.Split(ip, ","), true } - sld, err := publicsuffix.EffectiveTLDPlusOne(domain) - if err != nil { - return nil, false + if idx := strings.Index(domain, "."); idx != -1 { + wildcard := "*." + domain[strings.Index(domain, ".") + 1:] + + if ip, ok := r.hosts[wildcard]; ok { + return strings.Split(ip, ","), true + } } for host, ip := range r.hosts { - if strings.HasPrefix(host, "*.") { - old, err := publicsuffix.EffectiveTLDPlusOne(host) - - if err != nil { - continue - } - - // Don't blindly accept wildcards, match it against string - if sld == old && glob.Glob(host, domain) { - return strings.Split(ip, ","), true - } + if glob.Glob(host, domain) { + return strings.Split(ip, ","), true } } + return nil, false } diff --git a/utils.go b/utils.go index 1993d59..6e9bb61 100644 --- a/utils.go +++ b/utils.go @@ -3,7 +3,7 @@ package main import ( "net" "regexp" -) + ) func isDomain(domain string) bool { if isIP(domain) { @@ -14,5 +14,5 @@ func isDomain(domain string) bool { } func isIP(ip string) bool { - return (net.ParseIP(ip) != nil) -} + return net.ParseIP(ip) != nil +} \ No newline at end of file