2018-07-01 15:34:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-09-26 04:43:17 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2018-08-31 03:03:39 +00:00
|
|
|
"github.com/ryanuber/go-glob"
|
2019-09-26 04:43:17 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-07-01 15:34:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileHosts struct {
|
|
|
|
HostProvider
|
|
|
|
|
|
|
|
file string
|
|
|
|
hosts map[string]string
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileProvider(file string) HostProvider {
|
|
|
|
fp := &FileHosts{
|
|
|
|
file: file,
|
|
|
|
hosts: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
|
|
|
|
// Use fsnotify to notify us of host file changes
|
|
|
|
if err == nil {
|
|
|
|
watcher.Add(file)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
e := <- watcher.Events
|
|
|
|
|
|
|
|
if e.Op == fsnotify.Write {
|
|
|
|
fp.Refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileHosts) Get(domain string) ([]string, bool) {
|
2018-08-05 04:16:15 +00:00
|
|
|
logger.Debug("Checking file provider for %s", domain)
|
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
if ip, ok := f.hosts[domain]; ok {
|
|
|
|
return strings.Split(ip, ","), true
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
if idx := strings.Index(domain, "."); idx != -1 {
|
|
|
|
wildcard := "*." + domain[strings.Index(domain, ".") + 1:]
|
2018-08-31 03:03:39 +00:00
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
if ip, ok := f.hosts[wildcard]; ok {
|
|
|
|
return strings.Split(ip, ","), true
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 03:03:39 +00:00
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
for host, ip := range f.hosts {
|
|
|
|
if glob.Glob(host, domain) {
|
|
|
|
return strings.Split(ip, ","), true
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hostRegexp = regexp.MustCompile("^(.*?)\\s+(.*)$")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f *FileHosts) Refresh() {
|
|
|
|
buf, err := os.Open(f.file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("Update hosts records from file failed %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer buf.Close()
|
|
|
|
|
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
|
|
|
f.clear()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(buf)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") || line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m := hostRegexp.FindStringSubmatch(line)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := m[1]
|
|
|
|
|
|
|
|
if !isIP(ip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
domains := strings.Split(m[2], " ")
|
|
|
|
|
|
|
|
// Would have multiple columns of domain in line.
|
|
|
|
// Such as "127.0.0.1 localhost localhost.domain" on linux.
|
|
|
|
// The domains may not strict standard, like "local" so don't check with f.isDomain(domain).
|
|
|
|
for _, domain := range domains {
|
|
|
|
domain = strings.TrimSpace(domain)
|
|
|
|
|
|
|
|
if domain == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f.hosts[strings.ToLower(domain)] = ip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Debug("update hosts records from %s, total %d records.", f.file, len(f.hosts))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileHosts) clear() {
|
|
|
|
f.hosts = make(map[string]string)
|
|
|
|
}
|