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"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-02-12 06:09:49 +00:00
|
|
|
"time"
|
2015-02-03 12:32:18 +00:00
|
|
|
|
|
|
|
"github.com/hoisie/redis"
|
2013-07-26 04:06:16 +00:00
|
|
|
)
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
type Hosts struct {
|
2015-02-12 06:09:49 +00:00
|
|
|
fileHosts *FileHosts
|
|
|
|
redisHosts *RedisHosts
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
|
2015-02-12 06:09:49 +00:00
|
|
|
fileHosts := &FileHosts{hs.HostsFile, make(map[string]string)}
|
2015-02-03 15:03:47 +00:00
|
|
|
|
2015-02-11 10:01:05 +00:00
|
|
|
var redisHosts *RedisHosts
|
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}
|
2015-02-12 06:09:49 +00:00
|
|
|
redisHosts = &RedisHosts{rc, hs.RedisKey, make(map[string]string)}
|
2015-02-03 15:03:47 +00:00
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2015-02-11 10:01:05 +00:00
|
|
|
hosts := Hosts{fileHosts, redisHosts}
|
2015-02-12 06:09:49 +00:00
|
|
|
hosts.refresh()
|
2013-07-26 10:54:19 +00:00
|
|
|
return hosts
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-02-12 06:09:49 +00:00
|
|
|
1. Match local /etc/hosts file first, remote redis records second
|
|
|
|
2. Fetch hosts records from /etc/hosts file and redis per minute
|
2013-07-26 10:54:19 +00:00
|
|
|
*/
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
func (h *Hosts) Get(domain string) (ip string, ok bool) {
|
2015-02-03 12:32:18 +00:00
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
if ip, ok = h.fileHosts.Get(domain); ok {
|
|
|
|
return
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
2015-02-03 12:32:18 +00:00
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
if h.redisHosts != nil {
|
|
|
|
ip, ok = h.redisHosts.Get(domain)
|
|
|
|
return
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
return ip, false
|
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
func (h *Hosts) refresh() {
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
h.fileHosts.Refresh()
|
|
|
|
if h.redisHosts != nil {
|
|
|
|
h.redisHosts.Refresh()
|
|
|
|
}
|
|
|
|
<-ticker.C
|
|
|
|
}
|
|
|
|
}()
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RedisHosts struct {
|
|
|
|
redis *redis.Client
|
|
|
|
key string
|
2015-02-12 06:09:49 +00:00
|
|
|
hosts map[string]string
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) Get(domain string) (ip string, ok bool) {
|
2015-02-12 06:09:49 +00:00
|
|
|
ip, ok = r.hosts[domain]
|
|
|
|
return
|
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) {
|
|
|
|
return r.redis.Hset(r.key, domain, []byte(ip))
|
|
|
|
}
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
func (r *RedisHosts) Refresh() {
|
|
|
|
r.redis.Hgetall(r.key, r.hosts)
|
|
|
|
Debug("update hosts records from redis")
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
type FileHosts struct {
|
2015-02-12 06:09:49 +00:00
|
|
|
file string
|
|
|
|
hosts map[string]string
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 10:01:05 +00:00
|
|
|
func (f *FileHosts) Get(domain string) (ip string, ok bool) {
|
2015-02-12 06:09:49 +00:00
|
|
|
ip, ok = f.hosts[domain]
|
2015-02-11 10:01:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
panic("Can't open " + f.file)
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(buf)
|
2013-07-26 04:06:16 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
|
|
|
line := scanner.Text()
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") || line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sli := strings.Split(line, " ")
|
|
|
|
if len(sli) == 1 {
|
|
|
|
sli = strings.Split(line, "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sli) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := sli[len(sli)-1]
|
|
|
|
ip := sli[0]
|
2013-07-26 10:54:19 +00:00
|
|
|
if !f.isDomain(domain) || !f.isIP(ip) {
|
2013-07-26 04:06:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-02-12 06:09:49 +00:00
|
|
|
f.hosts[domain] = ip
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
2015-02-12 06:09:49 +00:00
|
|
|
Debug("update hosts records from %s", f.file)
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func (f *FileHosts) isDomain(domain string) bool {
|
2015-02-03 12:32:18 +00:00
|
|
|
if f.isIP(domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
match, _ := regexp.MatchString("^[a-zA-Z0-9][a-zA-Z0-9-]", domain)
|
2013-07-26 04:06:16 +00:00
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
func (f *FileHosts) isIP(ip string) bool {
|
2015-02-03 12:32:18 +00:00
|
|
|
return (net.ParseIP(ip) != nil)
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|