fix: use sld match instead of string suffix match

This commit is contained in:
kenshinx 2017-05-18 18:22:15 +08:00
parent 5f42911d12
commit 3be40025e0

View File

@ -104,16 +104,25 @@ type RedisHosts struct {
func (r *RedisHosts) Get(domain string) ([]string, bool) { func (r *RedisHosts) Get(domain string) ([]string, bool) {
r.mu.RLock() r.mu.RLock()
defer r.mu.RUnlock() defer r.mu.RUnlock()
domain = strings.ToLower(domain) domain = strings.ToLower(domain)
ip, ok := r.hosts[domain] ip, ok := r.hosts[domain]
if ok { if ok {
return strings.Split(ip, ","), true return strings.Split(ip, ","), true
} }
sld, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
return nil, false
}
for host, ip := range r.hosts { for host, ip := range r.hosts {
if strings.HasPrefix(host, "*.") { if strings.HasPrefix(host, "*.") {
upperLevelDomain := strings.Split(host, "*.")[1] old, err := publicsuffix.EffectiveTLDPlusOne(host)
if strings.HasSuffix(domain, upperLevelDomain) { if err != nil {
continue
}
if sld == old {
return strings.Split(ip, ","), true return strings.Split(ip, ","), true
} }
} }