From 5f42911d12934ff84f987ee8f1f3cb63a291ca4a Mon Sep 17 00:00:00 2001 From: kenshinx Date: Thu, 18 May 2017 18:10:33 +0800 Subject: [PATCH] File hosts support widlcard #31 --- hosts.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/hosts.go b/hosts.go index cdc251f..1c6eaee 100644 --- a/hosts.go +++ b/hosts.go @@ -10,6 +10,7 @@ import ( "time" "github.com/hoisie/redis" + "golang.org/x/net/publicsuffix" ) type Hosts struct { @@ -149,14 +150,32 @@ type FileHosts struct { } func (f *FileHosts) Get(domain string) ([]string, bool) { - domain = strings.ToLower(domain) f.mu.RLock() + defer f.mu.RUnlock() + domain = strings.ToLower(domain) ip, ok := f.hosts[domain] - f.mu.RUnlock() - if !ok { + if ok { + return []string{ip}, true + } + + sld, err := publicsuffix.EffectiveTLDPlusOne(domain) + if err != nil { return nil, false } - return []string{ip}, true + + for host, ip := range f.hosts { + if strings.HasPrefix(host, "*.") { + old, err := publicsuffix.EffectiveTLDPlusOne(host) + if err != nil { + continue + } + if sld == old { + return []string{ip}, true + } + } + } + + return nil, false } func (f *FileHosts) Refresh() {