2020-01-25 17:43:02 +00:00
|
|
|
package hosts
|
2018-07-01 15:34:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-02-08 03:38:22 +00:00
|
|
|
"errors"
|
2019-09-26 04:43:17 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2020-02-08 03:38:22 +00:00
|
|
|
"github.com/miekg/dns"
|
2018-08-31 03:03:39 +00:00
|
|
|
"github.com/ryanuber/go-glob"
|
2021-04-15 03:42:24 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-01-25 17:51:20 +00:00
|
|
|
"meow.tf/joker/godns/utils"
|
2019-09-26 04:43:17 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-01-25 18:48:26 +00:00
|
|
|
"time"
|
2018-07-01 15:34:01 +00:00
|
|
|
)
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
var (
|
|
|
|
errUnsupportedType = errors.New("unsupported type")
|
|
|
|
errRecordNotFound = errors.New("record not found")
|
|
|
|
errUnsupportedOperation = errors.New("unsupported operation")
|
|
|
|
)
|
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
type FileHosts struct {
|
2020-01-25 18:48:26 +00:00
|
|
|
Provider
|
2018-07-01 15:34:01 +00:00
|
|
|
|
|
|
|
file string
|
2021-04-15 03:42:24 +00:00
|
|
|
hosts map[string]Host
|
2018-07-01 15:34:01 +00:00
|
|
|
mu sync.RWMutex
|
2020-01-25 18:48:26 +00:00
|
|
|
ttl time.Duration
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func NewFileProvider(file string, ttl time.Duration) Provider {
|
2018-07-01 15:34:01 +00:00
|
|
|
fp := &FileHosts{
|
|
|
|
file: file,
|
2021-04-15 03:42:24 +00:00
|
|
|
hosts: make(map[string]Host),
|
2020-01-25 18:48:26 +00:00
|
|
|
ttl: ttl,
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-15 04:41:06 +00:00
|
|
|
func (f *FileHosts) List() (HostMap, error) {
|
|
|
|
return nil, errUnsupportedOperation
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
func (f *FileHosts) Get(queryType uint16, domain string) (*Host, error) {
|
2021-04-15 05:04:58 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"queryType": dns.TypeToString[queryType],
|
|
|
|
"question": domain,
|
|
|
|
}).Debug("Checking file provider")
|
2020-02-08 03:38:22 +00:00
|
|
|
|
|
|
|
// Does not support CNAME/TXT/etc
|
|
|
|
if queryType != dns.TypeA && queryType != dns.TypeAAAA {
|
2021-04-15 03:42:24 +00:00
|
|
|
return nil, errUnsupportedType
|
2020-02-08 03:38:22 +00:00
|
|
|
}
|
2018-08-05 04:16:15 +00:00
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
f.mu.RLock()
|
|
|
|
defer f.mu.RUnlock()
|
|
|
|
domain = strings.ToLower(domain)
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if host, ok := f.hosts[domain]; ok {
|
|
|
|
return &host, nil
|
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
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if host, ok := f.hosts[wildcard]; ok {
|
|
|
|
return &host, nil
|
2018-09-01 01:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 03:03:39 +00:00
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
for hostname, host := range f.hosts {
|
|
|
|
if glob.Glob(hostname, domain) {
|
|
|
|
return &host, nil
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
return nil, errRecordNotFound
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hostRegexp = regexp.MustCompile("^(.*?)\\s+(.*)$")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f *FileHosts) Refresh() {
|
|
|
|
buf, err := os.Open(f.file)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-15 05:04:58 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"file": f.file,
|
|
|
|
"error": err,
|
|
|
|
}).Warn("Hosts update from file failed")
|
2018-07-01 15:34:01 +00:00
|
|
|
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())
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if line == "" || line[0] == '#' {
|
2018-07-01 15:34:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
m := hostRegexp.FindStringSubmatch(line)
|
|
|
|
|
|
|
|
if m == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := m[1]
|
|
|
|
|
2020-01-25 17:51:20 +00:00
|
|
|
if !utils.IsIP(ip) {
|
2018-07-01 15:34:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
f.hosts[strings.ToLower(domain)] = Host{Values: []string{ip}}
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-15 05:04:58 +00:00
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"file": f.file,
|
|
|
|
"count": len(f.hosts),
|
|
|
|
}).Debug("Updated hosts records")
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileHosts) clear() {
|
2021-04-15 03:42:24 +00:00
|
|
|
f.hosts = make(map[string]Host)
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|