Fix wildcard matching
This commit is contained in:
parent
37c3ebb57e
commit
67fef871c2
|
@ -1,6 +1,6 @@
|
|||
#Toml config file
|
||||
title = "GODNS"
|
||||
Version = "0.2.1"
|
||||
Version = "0.2.2"
|
||||
Author = "kenshin, tystuyfzand"
|
||||
|
||||
debug = false
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue