2020-01-25 17:43:02 +00:00
|
|
|
package hosts
|
2018-07-01 15:34:01 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-25 18:48:26 +00:00
|
|
|
"github.com/go-redis/redis/v7"
|
2019-09-26 04:43:17 +00:00
|
|
|
"github.com/ryanuber/go-glob"
|
2020-01-25 17:43:02 +00:00
|
|
|
"meow.tf/joker/godns/log"
|
2018-07-01 15:34:01 +00:00
|
|
|
"strings"
|
2019-09-26 04:43:17 +00:00
|
|
|
"sync"
|
2020-01-25 18:48:26 +00:00
|
|
|
"time"
|
2018-07-01 15:34:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedisHosts struct {
|
2020-01-25 18:48:26 +00:00
|
|
|
Provider
|
2018-07-01 15:34:01 +00:00
|
|
|
|
|
|
|
redis *redis.Client
|
|
|
|
key string
|
|
|
|
hosts map[string]string
|
|
|
|
mu sync.RWMutex
|
2020-01-25 18:48:26 +00:00
|
|
|
ttl time.Duration
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func NewRedisProvider(rc *redis.Client, key string, ttl time.Duration) Provider {
|
2018-07-01 15:34:01 +00:00
|
|
|
rh := &RedisHosts{
|
|
|
|
redis: rc,
|
2019-09-26 04:43:17 +00:00
|
|
|
key: key,
|
2018-07-01 15:34:01 +00:00
|
|
|
hosts: make(map[string]string),
|
2020-01-25 18:48:26 +00:00
|
|
|
ttl: ttl,
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 08:48:26 +00:00
|
|
|
// Force an initial refresh
|
|
|
|
rh.Refresh()
|
|
|
|
|
2018-07-01 15:34:01 +00:00
|
|
|
return rh
|
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func (r *RedisHosts) Get(domain string) ([]string, time.Duration, bool) {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Debug("Checking redis provider for %s", domain)
|
2018-08-05 04:16:15 +00:00
|
|
|
|
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 {
|
2020-01-25 18:48:26 +00:00
|
|
|
return strings.Split(ip, ","), r.ttl, true
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-01 01:27:26 +00:00
|
|
|
if idx := strings.Index(domain, "."); idx != -1 {
|
2019-09-26 04:43:17 +00:00
|
|
|
wildcard := "*." + domain[strings.Index(domain, ".")+1:]
|
2018-09-01 01:27:26 +00:00
|
|
|
|
|
|
|
if ip, ok := r.hosts[wildcard]; ok {
|
2020-01-25 18:48:26 +00:00
|
|
|
return strings.Split(ip, ","), r.ttl, true
|
2018-09-01 01:27:26 +00:00
|
|
|
}
|
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) {
|
2020-01-25 18:48:26 +00:00
|
|
|
return strings.Split(ip, ","), r.ttl, true
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-01 01:27:26 +00:00
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
return nil, time.Duration(0), false
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) Set(domain, ip string) (bool, error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
2020-01-25 18:48:26 +00:00
|
|
|
return r.redis.HSet(r.key, strings.ToLower(domain), []byte(ip)).Result()
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) Refresh() {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.clear()
|
2020-01-25 18:48:26 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
r.hosts, err = r.redis.HGetAll(r.key).Result()
|
2018-07-01 15:34:01 +00:00
|
|
|
if err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Warn("Update hosts records from redis failed %s", err)
|
2018-07-01 15:34:01 +00:00
|
|
|
} else {
|
2020-01-25 17:43:02 +00:00
|
|
|
log.Debug("Update hosts records from redis")
|
2018-07-01 15:34:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisHosts) clear() {
|
|
|
|
r.hosts = make(map[string]string)
|
|
|
|
}
|