add option to disable redis
This commit is contained in:
parent
f1687a6040
commit
ccb72deec7
|
@ -37,6 +37,7 @@ maxcount = 0 #If set zero. The Sum of cache itmes will be unlimit.
|
||||||
#If set false, will not query hosts file and redis hosts record
|
#If set false, will not query hosts file and redis hosts record
|
||||||
enable = false
|
enable = false
|
||||||
host-file = "/etc/hosts"
|
host-file = "/etc/hosts"
|
||||||
|
redis-enable = true
|
||||||
redis-key = "godns:hosts"
|
redis-key = "godns:hosts"
|
||||||
ttl = 600
|
ttl = 600
|
||||||
|
|
||||||
|
|
20
hosts.go
20
hosts.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -17,8 +18,14 @@ type Hosts struct {
|
||||||
|
|
||||||
func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
|
func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
|
||||||
fileHosts := &FileHosts{hs.HostsFile}
|
fileHosts := &FileHosts{hs.HostsFile}
|
||||||
redis := &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password}
|
|
||||||
redisHosts := &RedisHosts{redis, hs.RedisKey}
|
var rc *redis.Client
|
||||||
|
if hs.RedisEnable {
|
||||||
|
rc = &redis.Client{Addr: rs.Addr(), Db: rs.DB, Password: rs.Password}
|
||||||
|
} else {
|
||||||
|
rc = nil
|
||||||
|
}
|
||||||
|
redisHosts := &RedisHosts{rc, hs.RedisKey}
|
||||||
|
|
||||||
hosts := Hosts{fileHosts.GetAll(), redisHosts}
|
hosts := Hosts{fileHosts.GetAll(), redisHosts}
|
||||||
return hosts
|
return hosts
|
||||||
|
@ -69,17 +76,26 @@ type RedisHosts struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisHosts) GetAll() map[string]string {
|
func (r *RedisHosts) GetAll() map[string]string {
|
||||||
|
if r.redis == nil {
|
||||||
|
return map[string]string{}
|
||||||
|
}
|
||||||
var hosts = make(map[string]string)
|
var hosts = make(map[string]string)
|
||||||
r.redis.Hgetall(r.key, hosts)
|
r.redis.Hgetall(r.key, hosts)
|
||||||
return hosts
|
return hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisHosts) Get(domain string) (ip string, ok bool) {
|
func (r *RedisHosts) Get(domain string) (ip string, ok bool) {
|
||||||
|
if r.redis == nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
b, err := r.redis.Hget(r.key, domain)
|
b, err := r.redis.Hget(r.key, domain)
|
||||||
return string(b), err == nil
|
return string(b), err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisHosts) Set(domain, ip string) (bool, error) {
|
func (r *RedisHosts) Set(domain, ip string) (bool, error) {
|
||||||
|
if r.redis == nil {
|
||||||
|
return false, errors.New("Redis not enabled")
|
||||||
|
}
|
||||||
return r.redis.Hset(r.key, domain, []byte(ip))
|
return r.redis.Hset(r.key, domain, []byte(ip))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,10 +55,11 @@ type CacheSettings struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type HostsSettings struct {
|
type HostsSettings struct {
|
||||||
Enable bool
|
Enable bool
|
||||||
HostsFile string `toml:"host-file"`
|
HostsFile string `toml:"host-file"`
|
||||||
RedisKey string `toml:"redis-key"`
|
RedisEnable bool `toml:"redis-enable"`
|
||||||
TTL uint32 `toml:"ttl"`
|
RedisKey string `toml:"redis-key"`
|
||||||
|
TTL uint32 `toml:"ttl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
Loading…
Reference in New Issue