2013-07-26 04:06:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-07-26 10:54:19 +00:00
|
|
|
"bufio"
|
2015-02-03 12:32:18 +00:00
|
|
|
"net"
|
2013-07-26 10:54:19 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2016-09-06 04:02:20 +00:00
|
|
|
"sync"
|
2015-02-12 06:09:49 +00:00
|
|
|
"time"
|
2015-02-03 12:32:18 +00:00
|
|
|
|
|
|
|
"github.com/hoisie/redis"
|
2017-05-18 10:10:33 +00:00
|
|
|
"golang.org/x/net/publicsuffix"
|
2018-07-01 03:08:29 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2013-07-26 04:06:16 +00:00
|
|
|
)
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
type Hosts struct {
|
2018-07-01 03:08:29 +00:00
|
|
|
providers []HostProvider
|
2016-09-18 04:04:21 +00:00
|
|
|
refreshInterval time.Duration
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
type HostProvider interface {
|
|
|
|
Get(domain string) ([]string, bool)
|
|
|
|
Refresh()
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
|
2018-07-01 03:08:29 +00:00
|
|
|
providers := []HostProvider{
|
|
|
|
NewFileProvider(hs.HostsFile),
|
2016-09-06 04:02:20 +00:00
|
|
|
}
|
2015-02-03 15:03:47 +00:00
|
|
|
|
|
|
|
if hs.RedisEnable {
|
2015-02-11 10:01:05 +00:00
|
|
|
rc := &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password}
|
2018-07-01 03:08:29 +00:00
|
|
|
|
|
|
|
providers = append(providers, NewRedisProvider(rc, hs.RedisKey))
|
2015-02-03 15:03:47 +00:00
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
return Hosts{providers, time.Second * time.Duration(hs.RefreshInterval)}
|
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
func (h *Hosts) refresh() {
|
|
|
|
ticker := time.NewTicker(h.refreshInterval)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// Force a refresh every refreshInterval
|
|
|
|
for _, provider := range h.providers {
|
|
|
|
provider.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
<-ticker.C
|
|
|
|
}
|
|
|
|
}()
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-10-14 04:41:08 +00:00
|
|
|
Match local /etc/hosts file first, remote redis records second
|
2013-07-26 10:54:19 +00:00
|
|
|
*/
|
2015-10-14 17:08:25 +00:00
|
|
|
func (h *Hosts) Get(domain string, family int) ([]net.IP, bool) {
|
|
|
|
var sips []string
|
2018-07-01 03:08:29 +00:00
|
|
|
var ok bool
|
2015-10-14 17:08:25 +00:00
|
|
|
var ip net.IP
|
|
|
|
var ips []net.IP
|
2015-02-12 06:54:02 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
for _, provider := range h.providers {
|
|
|
|
sips, ok = provider.Get(domain)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
break
|
2015-02-12 06:54:02 +00:00
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
2015-02-03 12:32:18 +00:00
|
|
|
|
2015-10-14 17:08:25 +00:00
|
|
|
if sips == nil {
|
2015-02-12 06:54:02 +00:00
|
|
|
return nil, false
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 17:08:25 +00:00
|
|
|
for _, sip := range sips {
|
|
|
|
switch family {
|
|
|
|
case _IP4Query:
|
|
|
|
ip = net.ParseIP(sip).To4()
|
|
|
|
case _IP6Query:
|
|
|
|
ip = net.ParseIP(sip).To16()
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ip != nil {
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
2015-02-12 06:54:02 +00:00
|
|
|
}
|
2015-10-14 17:08:25 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
return ips, ips != nil
|
2015-02-12 06:09:49 +00:00
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
type RedisHosts struct {
|
|
|
|
HostProvider
|
|
|
|
|
|
|
|
redis *redis.Client
|
|
|
|
key string
|
|
|
|
hosts map[string]string
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedisProvider(rc *redis.Client, key string) HostProvider {
|
|
|
|
rh := &RedisHosts{
|
|
|
|
redis: rc,
|
|
|
|
key: key,
|
|
|
|
hosts: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use pubsub to listen for key update events
|
2015-02-12 06:09:49 +00:00
|
|
|
go func() {
|
2018-07-01 03:08:29 +00:00
|
|
|
sub := make(chan string, 2)
|
|
|
|
sub <- "godns:update"
|
|
|
|
sub <- "godns:update_record"
|
|
|
|
messages := make(chan redis.Message, 0)
|
|
|
|
go rc.Subscribe(sub, nil, nil, nil, messages)
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
for {
|
2018-07-01 03:08:29 +00:00
|
|
|
msg := <- messages
|
|
|
|
|
|
|
|
if msg.Channel == "godns:update" {
|
|
|
|
rh.Refresh()
|
|
|
|
} else if msg.Channel == "godns:update_record" {
|
|
|
|
recordName := string(msg.Message)
|
|
|
|
|
|
|
|
b, err := rc.Hget(key, recordName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rh.hosts[recordName] = string(b)
|
2015-02-12 06:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
return rh
|
2015-10-14 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 17:08:25 +00:00
|
|
|
func (r *RedisHosts) Get(domain string) ([]string, bool) {
|
2016-09-06 04:02:20 +00:00
|
|
|
r.mu.RLock()
|
2017-05-18 09:29:56 +00:00
|
|
|
defer r.mu.RUnlock()
|
2017-05-18 10:22:15 +00:00
|
|
|
|
2017-05-18 09:29:56 +00:00
|
|
|
domain = strings.ToLower(domain)
|
2015-10-14 17:08:25 +00:00
|
|
|
ip, ok := r.hosts[domain]
|
2015-10-14 04:41:08 +00:00
|
|
|
if ok {
|
2015-10-14 17:08:25 +00:00
|
|
|
return strings.Split(ip, ","), true
|
2015-10-14 04:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 10:22:15 +00:00
|
|
|
sld, err := publicsuffix.EffectiveTLDPlusOne(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2015-10-14 16:40:34 +00:00
|
|
|
for host, ip := range r.hosts {
|
2015-10-14 04:41:08 +00:00
|
|
|
if strings.HasPrefix(host, "*.") {
|
2017-05-18 10:22:15 +00:00
|
|
|
old, err := publicsuffix.EffectiveTLDPlusOne(host)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sld == old {
|
2015-10-14 17:08:25 +00:00
|
|
|
return strings.Split(ip, ","), true
|
2015-10-14 04:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-14 17:08:25 +00:00
|
|
|
return nil, false
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func (r *RedisHosts) Set(domain, ip string) (bool, error) {
|
2016-09-06 04:02:20 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2016-02-15 04:34:24 +00:00
|
|
|
return r.redis.Hset(r.key, strings.ToLower(domain), []byte(ip))
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
func (r *RedisHosts) Refresh() {
|
2016-09-06 04:02:20 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.clear()
|
2015-10-13 17:00:28 +00:00
|
|
|
err := r.redis.Hgetall(r.key, r.hosts)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("Update hosts records from redis failed %s", err)
|
|
|
|
} else {
|
|
|
|
logger.Debug("Update hosts records from redis")
|
|
|
|
}
|
2015-02-12 06:09:49 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 04:02:20 +00:00
|
|
|
func (r *RedisHosts) clear() {
|
|
|
|
r.hosts = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
type FileHosts struct {
|
2018-07-01 03:08:29 +00:00
|
|
|
HostProvider
|
|
|
|
|
2015-10-14 16:40:34 +00:00
|
|
|
file string
|
|
|
|
hosts map[string]string
|
2016-09-06 04:02:20 +00:00
|
|
|
mu sync.RWMutex
|
2015-10-14 16:40:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-14 17:08:25 +00:00
|
|
|
func (f *FileHosts) Get(domain string) ([]string, bool) {
|
2016-09-06 04:02:20 +00:00
|
|
|
f.mu.RLock()
|
2017-05-18 10:10:33 +00:00
|
|
|
defer f.mu.RUnlock()
|
|
|
|
domain = strings.ToLower(domain)
|
2015-10-14 17:08:25 +00:00
|
|
|
ip, ok := f.hosts[domain]
|
2017-05-18 10:10:33 +00:00
|
|
|
if ok {
|
|
|
|
return []string{ip}, true
|
|
|
|
}
|
|
|
|
|
|
|
|
sld, err := publicsuffix.EffectiveTLDPlusOne(domain)
|
|
|
|
if err != nil {
|
2015-10-14 17:08:25 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
2017-05-18 10:10:33 +00:00
|
|
|
|
|
|
|
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
|
2015-02-11 10:01:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
func (f *FileHosts) Refresh() {
|
2013-07-26 10:54:19 +00:00
|
|
|
buf, err := os.Open(f.file)
|
|
|
|
if err != nil {
|
2015-10-13 17:00:28 +00:00
|
|
|
logger.Warn("Update hosts records from file failed %s", err)
|
|
|
|
return
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
2015-06-12 04:04:23 +00:00
|
|
|
defer buf.Close()
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2016-09-06 04:02:20 +00:00
|
|
|
f.mu.Lock()
|
|
|
|
defer f.mu.Unlock()
|
|
|
|
|
2015-10-20 06:01:41 +00:00
|
|
|
f.clear()
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
scanner := bufio.NewScanner(buf)
|
2013-07-26 04:06:16 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
|
|
line := scanner.Text()
|
|
|
|
line = strings.TrimSpace(line)
|
2017-07-10 10:26:48 +00:00
|
|
|
line = strings.Replace(line, "\t", " ", -1)
|
2013-07-26 04:06:16 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") || line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sli := strings.Split(line, " ")
|
|
|
|
|
|
|
|
if len(sli) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := sli[0]
|
2018-02-01 07:39:47 +00:00
|
|
|
if !isIP(ip) {
|
2013-07-26 04:06:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-10 10:26:48 +00:00
|
|
|
// 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 i := 1; i <= len(sli)-1; i++ {
|
|
|
|
domain := strings.TrimSpace(sli[i])
|
|
|
|
if domain == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f.hosts[strings.ToLower(domain)] = ip
|
|
|
|
}
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
2017-07-10 10:26:48 +00:00
|
|
|
logger.Debug("update hosts records from %s, total %d records.", f.file, len(f.hosts))
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 06:01:41 +00:00
|
|
|
func (f *FileHosts) clear() {
|
|
|
|
f.hosts = make(map[string]string)
|
|
|
|
}
|