File hosts support widlcard #31
This commit is contained in:
parent
829c7af1bc
commit
5f42911d12
25
hosts.go
25
hosts.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hoisie/redis"
|
"github.com/hoisie/redis"
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hosts struct {
|
type Hosts struct {
|
||||||
|
@ -149,15 +150,33 @@ type FileHosts struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileHosts) Get(domain string) ([]string, bool) {
|
func (f *FileHosts) Get(domain string) ([]string, bool) {
|
||||||
domain = strings.ToLower(domain)
|
|
||||||
f.mu.RLock()
|
f.mu.RLock()
|
||||||
|
defer f.mu.RUnlock()
|
||||||
|
domain = strings.ToLower(domain)
|
||||||
ip, ok := f.hosts[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 nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 []string{ip}, true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
func (f *FileHosts) Refresh() {
|
func (f *FileHosts) Refresh() {
|
||||||
buf, err := os.Open(f.file)
|
buf, err := os.Open(f.file)
|
||||||
|
|
Loading…
Reference in New Issue