godns/hosts.go

189 lines
3.4 KiB
Go
Raw Normal View History

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"
"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 {
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 {
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}
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}
hosts.refresh()
2013-07-26 10:54:19 +00:00
return hosts
}
/*
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) {
2015-02-03 12:32:18 +00:00
2015-10-14 17:08:25 +00:00
var sips []string
var ip net.IP
var ips []net.IP
2015-02-12 06:54:02 +00:00
2015-10-14 17:08:25 +00:00
sips, ok := h.fileHosts.Get(domain)
if !ok {
2015-02-12 06:54:02 +00:00
if h.redisHosts != nil {
2015-10-14 17:08:25 +00:00
sips, ok = h.redisHosts.Get(domain)
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
return ips, (ips != nil)
}
2013-07-26 10:54:19 +00:00
2015-10-14 04:41:08 +00:00
/*
Update hosts records from /etc/hosts file and redis per minute
*/
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
}
2015-10-14 04:41:08 +00:00
type BaseHosts struct {
hosts map[string]string
2013-07-26 10:54:19 +00:00
}
type RedisHosts struct {
redis *redis.Client
key string
hosts map[string]string
}
2015-10-14 17:08:25 +00:00
func (r *RedisHosts) Get(domain string) ([]string, bool) {
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
}
for host, ip := range r.hosts {
2015-10-14 04:41:08 +00:00
if strings.HasPrefix(host, "*.") {
upperLevelDomain := strings.Split(host, "*.")[1]
if strings.HasSuffix(domain, upperLevelDomain) {
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) {
return r.redis.Hset(r.key, domain, []byte(ip))
}
func (r *RedisHosts) Refresh() {
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")
}
}
2013-07-26 10:54:19 +00:00
type FileHosts struct {
file string
hosts map[string]string
}
2015-10-14 17:08:25 +00:00
func (f *FileHosts) Get(domain string) ([]string, bool) {
ip, ok := f.hosts[domain]
if !ok {
return nil, false
}
return []string{ip}, true
2015-02-11 10:01:05 +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
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
}
f.hosts[domain] = ip
2013-07-26 04:06:16 +00:00
}
2015-10-13 17:00:28 +00:00
logger.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
}
2015-10-14 04:41:08 +00:00
match, _ := regexp.MatchString(`^([a-zA-Z0-9\*]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$`, 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
}