2018-07-01 15:34:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hoisie/redis"
|
|
|
|
"sync"
|
|
|
|
"strings"
|
2018-09-01 01:27:26 +00:00
|
|
|
"github.com/ryanuber/go-glob"
|
2018-07-01 15:34:01 +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),
|
|
|
|
}
|
|
|
|
|
2018-08-05 08:48:26 +00:00
|
|
|
// Force an initial refresh
|
|
|
|
rh.Refresh()
|
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
// Use pubsub to listen for key update events
|
|
|
|
go func() {
|
|
|
|
keyspaceEvent := "__keyspace@0__:" + key
|
|
|
|
|
2018-08-05 08:48:26 +00:00
|
|
|
sub := make(chan string, 3)
|
2018-07-01 15:34:01 +00:00
|
|
|
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" {
|
2018-08-05 08:48:26 +00:00
|
|
|
logger.Debug("Refreshing redis records due to update")
|
2018-07-01 15:34:01 +00:00
|
|
|
rh.Refresh()
|
|
|
|
} else if msg.Channel == "godns:update_record" {
|
|
|
|
recordName := string(msg.Message)
|
|
|
|
|
|
|
|
b, err := rc.Hget(key, recordName)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-08-05 08:48:26 +00:00
|
|
|
logger.Warn("Record %s does not exist, but was updated", recordName)
|
2018-07-01 15:34:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-05 08:48:26 +00:00
|
|
|
logger.Debug("Record %s was updated to %s", recordName, string(b))
|
|
|
|
|
|
|
|
rh.mu.Lock()
|
2018-07-01 15:34:01 +00:00
|
|
|
rh.hosts[recordName] = string(b)
|
2018-08-05 08:48:26 +00:00
|
|
|
rh.mu.Unlock()
|
2018-07-03 05:35:20 +00:00
|
|
|
} else if msg.Channel == "godns:remove_record" {
|
2018-08-05 08:48:26 +00:00
|
|
|
logger.Debug("Record %s was removed", msg.Message)
|
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
recordName := string(msg.Message)
|
|
|
|
|
2018-08-05 08:48:26 +00:00
|
|
|
rh.mu.Lock()
|
2018-09-01 01:27:26 +00:00
|
|
|
delete(rh.hosts, recordName)
|
2018-08-05 08:48:26 +00:00
|
|
|
rh.mu.Unlock()
|
2018-07-01 15:34:01 +00:00
|
|
|
} else if msg.Channel == keyspaceEvent {
|
2018-08-05 08:48:26 +00:00
|
|
|
logger.Debug("Refreshing redis records due to update")
|
2018-07-01 15:34:01 +00:00
|
|
|
rh.Refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) Get(domain string) ([]string, bool) {
|
2018-08-05 04:16:15 +00:00
|
|
|
logger.Debug("Checking redis provider for %s", domain)
|
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
r.mu.RLock()
|
|
|
|
defer r.mu.RUnlock()
|
|
|
|
|
|
|
|
domain = strings.ToLower(domain)
|
2018-09-01 01:27:26 +00:00
|
|
|
|
|
|
|
if ip, ok := r.hosts[domain]; ok {
|
2018-07-01 15:34:01 +00:00
|
|
|
return strings.Split(ip, ","), true
|
|
|
|
}
|
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
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
|
|
|
|
}
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for host, ip := range r.hosts {
|
2018-09-01 01:27:26 +00:00
|
|
|
if glob.Glob(host, domain) {
|
|
|
|
return strings.Split(ip, ","), true
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 01:27:26 +00:00
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
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 {
|
|
|
|
logger.Warn("Update hosts records from redis failed %s", err)
|
|
|
|
} else {
|
|
|
|
logger.Debug("Update hosts records from redis")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) clear() {
|
|
|
|
r.hosts = make(map[string]string)
|
|
|
|
}
|