Initial restructure of server
This commit is contained in:
111
hosts/hosts.go
Normal file
111
hosts/hosts.go
Normal file
@ -0,0 +1,111 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"meow.tf/joker/godns/log"
|
||||
"meow.tf/joker/godns/settings"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/hoisie/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
notIPQuery = 0
|
||||
_IP4Query = 4
|
||||
_IP6Query = 6
|
||||
)
|
||||
|
||||
type Hosts interface {
|
||||
Get(domain string, family int) ([]net.IP, bool)
|
||||
TTL() uint32
|
||||
}
|
||||
|
||||
type ProviderList struct {
|
||||
settings settings.HostsSettings
|
||||
providers []HostProvider
|
||||
refreshInterval time.Duration
|
||||
}
|
||||
|
||||
type HostProvider interface {
|
||||
Get(domain string) ([]string, bool)
|
||||
Refresh()
|
||||
}
|
||||
|
||||
func NewHosts(hs settings.HostsSettings, rs settings.RedisSettings) Hosts {
|
||||
providers := []HostProvider{
|
||||
NewFileProvider(hs.HostsFile),
|
||||
}
|
||||
|
||||
if hs.RedisEnable {
|
||||
log.Info("Redis is enabled: %s", rs.Addr())
|
||||
|
||||
rc := &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password}
|
||||
|
||||
providers = append(providers, NewRedisProvider(rc, hs.RedisKey))
|
||||
}
|
||||
|
||||
h := &ProviderList{hs, providers, time.Second * time.Duration(hs.RefreshInterval)}
|
||||
|
||||
if h.refreshInterval > 0 {
|
||||
h.refresh()
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *ProviderList) refresh() {
|
||||
ticker := time.NewTicker(h.refreshInterval)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
// Force a refresh every refreshInterval
|
||||
for _, provider := range h.providers {
|
||||
provider.Refresh()
|
||||
}
|
||||
|
||||
<-ticker.C
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
/*
|
||||
Match local /etc/hosts file first, remote redis records second
|
||||
*/
|
||||
func (h *ProviderList) Get(domain string, family int) ([]net.IP, bool) {
|
||||
var sips []string
|
||||
var ok bool
|
||||
var ip net.IP
|
||||
var ips []net.IP
|
||||
|
||||
for _, provider := range h.providers {
|
||||
sips, ok = provider.Get(domain)
|
||||
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if sips == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return ips, ips != nil
|
||||
}
|
||||
|
||||
func (h *ProviderList) TTL() uint32 {
|
||||
return h.settings.TTL
|
||||
}
|
136
hosts/hosts_file.go
Normal file
136
hosts/hosts_file.go
Normal file
@ -0,0 +1,136 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/ryanuber/go-glob"
|
||||
"meow.tf/joker/godns/log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type FileHosts struct {
|
||||
HostProvider
|
||||
|
||||
file string
|
||||
hosts map[string]string
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (f *FileHosts) Get(domain string) ([]string, bool) {
|
||||
log.Debug("Checking file provider for %s", domain)
|
||||
|
||||
f.mu.RLock()
|
||||
defer f.mu.RUnlock()
|
||||
domain = strings.ToLower(domain)
|
||||
|
||||
if ip, ok := f.hosts[domain]; ok {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
|
||||
if idx := strings.Index(domain, "."); idx != -1 {
|
||||
wildcard := "*." + domain[strings.Index(domain, ".") + 1:]
|
||||
|
||||
if ip, ok := f.hosts[wildcard]; ok {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
}
|
||||
|
||||
for host, ip := range f.hosts {
|
||||
if glob.Glob(host, domain) {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
var (
|
||||
hostRegexp = regexp.MustCompile("^(.*?)\\s+(.*)$")
|
||||
)
|
||||
|
||||
func (f *FileHosts) Refresh() {
|
||||
buf, err := os.Open(f.file)
|
||||
|
||||
if err != nil {
|
||||
log.Warn("Update hosts records from file failed %s", err)
|
||||
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())
|
||||
|
||||
if strings.HasPrefix(line, "#") || line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
m := hostRegexp.FindStringSubmatch(line)
|
||||
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := m[1]
|
||||
|
||||
if !isIP(ip) {
|
||||
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
|
||||
}
|
||||
|
||||
f.hosts[strings.ToLower(domain)] = ip
|
||||
}
|
||||
}
|
||||
log.Debug("update hosts records from %s, total %d records.", f.file, len(f.hosts))
|
||||
}
|
||||
|
||||
func (f *FileHosts) clear() {
|
||||
f.hosts = make(map[string]string)
|
||||
}
|
129
hosts/hosts_redis.go
Normal file
129
hosts/hosts_redis.go
Normal file
@ -0,0 +1,129 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"github.com/hoisie/redis"
|
||||
"github.com/ryanuber/go-glob"
|
||||
"meow.tf/joker/godns/log"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
// Force an initial refresh
|
||||
rh.Refresh()
|
||||
|
||||
// Use pubsub to listen for key update events
|
||||
go func() {
|
||||
keyspaceEvent := "__keyspace@0__:" + key
|
||||
|
||||
sub := make(chan string, 3)
|
||||
sub <- keyspaceEvent
|
||||
sub <- "godns:update"
|
||||
sub <- "godns:update_record"
|
||||
messages := make(chan redis.Message, 0)
|
||||
go rc.Subscribe(sub, nil, nil, nil, messages)
|
||||
|
||||
for {
|
||||
msg := <-messages
|
||||
|
||||
if msg.Channel == "godns:update" {
|
||||
log.Debug("Refreshing redis records due to update")
|
||||
rh.Refresh()
|
||||
} else if msg.Channel == "godns:update_record" {
|
||||
recordName := string(msg.Message)
|
||||
|
||||
b, err := rc.Hget(key, recordName)
|
||||
|
||||
if err != nil {
|
||||
log.Warn("Record %s does not exist, but was updated", recordName)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug("Record %s was updated to %s", recordName, string(b))
|
||||
|
||||
rh.mu.Lock()
|
||||
rh.hosts[recordName] = string(b)
|
||||
rh.mu.Unlock()
|
||||
} else if msg.Channel == "godns:remove_record" {
|
||||
log.Debug("Record %s was removed", msg.Message)
|
||||
|
||||
recordName := string(msg.Message)
|
||||
|
||||
rh.mu.Lock()
|
||||
delete(rh.hosts, recordName)
|
||||
rh.mu.Unlock()
|
||||
} else if msg.Channel == keyspaceEvent {
|
||||
log.Debug("Refreshing redis records due to update")
|
||||
rh.Refresh()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return rh
|
||||
}
|
||||
|
||||
func (r *RedisHosts) Get(domain string) ([]string, bool) {
|
||||
log.Debug("Checking redis provider for %s", domain)
|
||||
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
domain = strings.ToLower(domain)
|
||||
|
||||
if ip, ok := r.hosts[domain]; ok {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
|
||||
if idx := strings.Index(domain, "."); idx != -1 {
|
||||
wildcard := "*." + domain[strings.Index(domain, ".")+1:]
|
||||
|
||||
if ip, ok := r.hosts[wildcard]; ok {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
}
|
||||
|
||||
for host, ip := range r.hosts {
|
||||
if glob.Glob(host, domain) {
|
||||
return strings.Split(ip, ","), true
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (r *RedisHosts) Set(domain, ip string) (bool, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.redis.Hset(r.key, strings.ToLower(domain), []byte(ip))
|
||||
}
|
||||
|
||||
func (r *RedisHosts) Refresh() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.clear()
|
||||
err := r.redis.Hgetall(r.key, r.hosts)
|
||||
if err != nil {
|
||||
log.Warn("Update hosts records from redis failed %s", err)
|
||||
} else {
|
||||
log.Debug("Update hosts records from redis")
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedisHosts) clear() {
|
||||
r.hosts = make(map[string]string)
|
||||
}
|
Reference in New Issue
Block a user