Fix wildcard matching

This commit is contained in:
Tyler 2018-08-31 21:27:26 -04:00
parent 37c3ebb57e
commit 67fef871c2
4 changed files with 31 additions and 42 deletions

View File

@ -1,6 +1,6 @@
#Toml config file
title = "GODNS"
Version = "0.2.1"
Version = "0.2.2"
Author = "kenshin, tystuyfzand"
debug = false

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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
}