godns/hosts.go

155 lines
2.8 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 15:03:47 +00:00
"errors"
2015-02-03 12:32:18 +00:00
"net"
2013-07-26 10:54:19 +00:00
"os"
"regexp"
"strings"
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 map[string]string
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}
2015-02-03 15:03:47 +00:00
var rc *redis.Client
if rs.Enable {
rc = &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password}
} else {
rc = nil
}
redisHosts := &RedisHosts{rc, hs.RedisKey}
2013-07-26 10:54:19 +00:00
hosts := Hosts{fileHosts.GetAll(), redisHosts}
return hosts
}
/*
1. Resolve hosts file only one times
2013-07-31 02:43:50 +00:00
2. Request redis on every query called, not found performance lose serious yet.
2013-07-26 10:54:19 +00:00
3. Match local /etc/hosts file first, remote redis records second
*/
2015-02-03 12:32:18 +00:00
func (h *Hosts) Get(domain string, family int) (ip net.IP, ok bool) {
var sip string
if sip, ok = h.FileHosts[domain]; !ok {
if sip, ok = h.RedisHosts.Get(domain); !ok {
return nil, false
}
2013-07-26 10:54:19 +00:00
}
2015-02-03 12:32:18 +00:00
switch family {
case _IP4Query:
ip = net.ParseIP(sip).To4()
return ip, (ip != nil)
case _IP6Query:
ip = net.ParseIP(sip).To16()
return ip, (ip != nil)
2013-07-26 10:54:19 +00:00
}
2015-02-03 12:32:18 +00:00
return nil, false
2013-07-26 10:54:19 +00:00
}
func (h *Hosts) GetAll() map[string]string {
m := make(map[string]string)
for domain, ip := range h.RedisHosts.GetAll() {
m[domain] = ip
}
for domain, ip := range h.FileHosts {
m[domain] = ip
}
return m
}
type RedisHosts struct {
redis *redis.Client
key string
}
func (r *RedisHosts) GetAll() map[string]string {
2015-02-03 15:03:47 +00:00
if r.redis == nil {
return map[string]string{}
}
2013-07-26 10:54:19 +00:00
var hosts = make(map[string]string)
r.redis.Hgetall(r.key, hosts)
return hosts
}
func (r *RedisHosts) Get(domain string) (ip string, ok bool) {
2015-02-03 15:03:47 +00:00
if r.redis == nil {
return "", false
}
2013-07-26 10:54:19 +00:00
b, err := r.redis.Hget(r.key, domain)
return string(b), err == nil
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) {
2015-02-03 15:03:47 +00:00
if r.redis == nil {
return false, errors.New("Redis not enabled")
}
2013-07-26 10:54:19 +00:00
return r.redis.Hset(r.key, domain, []byte(ip))
}
type FileHosts struct {
file string
}
func (f *FileHosts) GetAll() map[string]string {
2013-07-26 04:06:16 +00:00
var hosts = make(map[string]string)
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]
if !isDomain(domain) || !isIP(ip) {
2013-07-26 04:06:16 +00:00
continue
}
hosts[domain] = ip
}
return hosts
}
func isDomain(domain string) bool {
if isIP(domain) {
2015-02-03 12:32:18 +00:00
return false
}
match, _ := regexp.MatchString("^[a-zA-Z0-9][a-zA-Z0-9-]", domain)
2013-07-26 04:06:16 +00:00
return match
}
func 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
}