From 3be40025e04cc07e8728ab466d080444811eb341 Mon Sep 17 00:00:00 2001 From: kenshinx Date: Thu, 18 May 2017 18:22:15 +0800 Subject: [PATCH] fix: use sld match instead of string suffix match --- hosts.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hosts.go b/hosts.go index 1c6eaee..690c3cc 100644 --- a/hosts.go +++ b/hosts.go @@ -104,16 +104,25 @@ type RedisHosts struct { func (r *RedisHosts) Get(domain string) ([]string, bool) { r.mu.RLock() defer r.mu.RUnlock() + domain = strings.ToLower(domain) ip, ok := r.hosts[domain] if ok { return strings.Split(ip, ","), true } + sld, err := publicsuffix.EffectiveTLDPlusOne(domain) + if err != nil { + return nil, false + } + for host, ip := range r.hosts { if strings.HasPrefix(host, "*.") { - upperLevelDomain := strings.Split(host, "*.")[1] - if strings.HasSuffix(domain, upperLevelDomain) { + old, err := publicsuffix.EffectiveTLDPlusOne(host) + if err != nil { + continue + } + if sld == old { return strings.Split(ip, ","), true } }