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 #Toml config file
title = "GODNS" title = "GODNS"
Version = "0.2.1" Version = "0.2.2"
Author = "kenshin, tystuyfzand" Author = "kenshin, tystuyfzand"
debug = false debug = false

View File

@ -4,8 +4,7 @@ import (
"sync" "sync"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"strings" "strings"
"golang.org/x/net/publicsuffix" "os"
"os"
"bufio" "bufio"
"regexp" "regexp"
"github.com/ryanuber/go-glob" "github.com/ryanuber/go-glob"
@ -51,28 +50,22 @@ func (f *FileHosts) Get(domain string) ([]string, bool) {
f.mu.RLock() f.mu.RLock()
defer f.mu.RUnlock() defer f.mu.RUnlock()
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
ip, ok := f.hosts[domain]
if ok { if ip, ok := f.hosts[domain]; ok {
return []string{ip}, true return strings.Split(ip, ","), true
} }
sld, err := publicsuffix.EffectiveTLDPlusOne(domain) if idx := strings.Index(domain, "."); idx != -1 {
if err != nil { wildcard := "*." + domain[strings.Index(domain, ".") + 1:]
return nil, false
if ip, ok := f.hosts[wildcard]; ok {
return strings.Split(ip, ","), true
}
} }
for host, ip := range f.hosts { for host, ip := range f.hosts {
if strings.HasPrefix(host, "*.") { if glob.Glob(host, domain) {
old, err := publicsuffix.EffectiveTLDPlusOne(host) return strings.Split(ip, ","), true
if err != nil {
continue
}
// Don't blindly accept wildcards, match it against string
if sld == old && glob.Glob(host, domain) {
return []string{ip}, true
}
} }
} }

View File

@ -4,8 +4,7 @@ import (
"github.com/hoisie/redis" "github.com/hoisie/redis"
"sync" "sync"
"strings" "strings"
"golang.org/x/net/publicsuffix" "github.com/ryanuber/go-glob"
"github.com/ryanuber/go-glob"
) )
type RedisHosts struct { type RedisHosts struct {
@ -62,8 +61,10 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider {
} else if msg.Channel == "godns:remove_record" { } else if msg.Channel == "godns:remove_record" {
logger.Debug("Record %s was removed", msg.Message) logger.Debug("Record %s was removed", msg.Message)
recordName := string(msg.Message)
rh.mu.Lock() rh.mu.Lock()
delete(rh.hosts, string(msg.Message)) delete(rh.hosts, recordName)
rh.mu.Unlock() rh.mu.Unlock()
} else if msg.Channel == keyspaceEvent { } else if msg.Channel == keyspaceEvent {
logger.Debug("Refreshing redis records due to update") logger.Debug("Refreshing redis records due to update")
@ -82,30 +83,25 @@ func (r *RedisHosts) Get(domain string) ([]string, bool) {
defer r.mu.RUnlock() defer r.mu.RUnlock()
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
ip, ok := r.hosts[domain]
if ok { if ip, ok := r.hosts[domain]; ok {
return strings.Split(ip, ","), true return strings.Split(ip, ","), true
} }
sld, err := publicsuffix.EffectiveTLDPlusOne(domain) if idx := strings.Index(domain, "."); idx != -1 {
if err != nil { wildcard := "*." + domain[strings.Index(domain, ".") + 1:]
return nil, false
if ip, ok := r.hosts[wildcard]; ok {
return strings.Split(ip, ","), true
}
} }
for host, ip := range r.hosts { for host, ip := range r.hosts {
if strings.HasPrefix(host, "*.") { if glob.Glob(host, domain) {
old, err := publicsuffix.EffectiveTLDPlusOne(host) return strings.Split(ip, ","), true
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
}
} }
} }
return nil, false return nil, false
} }

View File

@ -3,7 +3,7 @@ package main
import ( import (
"net" "net"
"regexp" "regexp"
) )
func isDomain(domain string) bool { func isDomain(domain string) bool {
if isIP(domain) { if isIP(domain) {
@ -14,5 +14,5 @@ func isDomain(domain string) bool {
} }
func isIP(ip string) bool { func isIP(ip string) bool {
return (net.ParseIP(ip) != nil) return net.ParseIP(ip) != nil
} }