Fix pubsub, documentation, version

This commit is contained in:
Tyler 2018-08-05 04:48:26 -04:00
parent 3457be7942
commit aae18bc0c9
10 changed files with 92 additions and 22 deletions

3
.gitignore vendored
View File

@ -1 +1,4 @@
/bin /bin
godns
godns.log
etc/godns.conf

View File

@ -65,6 +65,6 @@ func (m *RedisCache) Remove(key string) error {
} }
func (m *RedisCache) Full() bool { func (m *RedisCache) Full() bool {
// memcache is never full (LRU) // redis is never full (LRU)
return false return false
} }

View File

@ -8,6 +8,6 @@ RUN apk --no-cache add tini
ENTRYPOINT ["/sbin/tini", "-g", "--"] ENTRYPOINT ["/sbin/tini", "-g", "--"]
CMD ["godns"] CMD ["godns"]
COPY etc/godns.conf /etc/godns.conf COPY etc/godns.example.conf /etc/godns.conf
COPY godns /usr/local/bin/godns COPY godns /usr/local/bin/godns
RUN chmod +x /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns

View File

@ -10,6 +10,6 @@ RUN apk --no-cache add tini
ENTRYPOINT ["/sbin/tini", "-g", "--"] ENTRYPOINT ["/sbin/tini", "-g", "--"]
CMD ["godns"] CMD ["godns"]
COPY etc/godns.conf /etc/godns.conf COPY etc/godns.example.conf /etc/godns.conf
COPY godns-arm /usr/local/bin/godns COPY godns-arm /usr/local/bin/godns
RUN chmod +x /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns

View File

@ -10,6 +10,6 @@ RUN apk --no-cache add tini
ENTRYPOINT ["/sbin/tini", "-g", "--"] ENTRYPOINT ["/sbin/tini", "-g", "--"]
CMD ["godns"] CMD ["godns"]
COPY etc/godns.conf /etc/godns.conf COPY etc/godns.example.conf /etc/godns.conf
COPY godns-arm64 /usr/local/bin/godns COPY godns-arm64 /usr/local/bin/godns
RUN chmod +x /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns

View File

@ -14,17 +14,16 @@ port = 53
[resolv] [resolv]
# Domain-specific nameservers configuration, formatting keep compatible with Dnsmasq # Domain-specific nameservers configuration, formatting keep compatible with Dnsmasq
# Semicolon separate multiple files. # Semicolon separate multiple files.
resolv-file = "/etc/resolv.conf" resolv-file = "etc/resolv.conf"
timeout = 5 # 5 seconds timeout = 5 # 5 seconds
# The concurrency interval request upstream recursive server # The concurrency interval request upstream recursive server
# Match the PR15, https://github.com/kenshinx/godns/pull/15 # Match the PR15, https://github.com/kenshinx/godns/pull/15
interval = 200 # 200 milliseconds interval = 200 # 200 milliseconds
setedns0 = false #Support for larger UDP DNS responses setedns0 = false #Support for larger UDP DNS responses
[redis] [redis]
enable = true enable = true
host = "127.0.0.1" host = "192.168.1.71"
port = 6379 port = 6379
db = 0 db = 0
password ="" password =""
@ -35,7 +34,7 @@ servers = ["127.0.0.1:11211"]
[log] [log]
stdout = true stdout = true
file = "./godns.log" file = "./godns.log"
level = "INFO" #DEBUG | INFO |NOTICE | WARN | ERROR level = "DEBUG" #DEBUG | INFO |NOTICE | WARN | ERROR
@ -50,9 +49,9 @@ 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 = true enable = true
host-file = "/etc/hosts" host-file = "/etc/hosts"
redis-enable = false redis-enable = true
redis-key = "godns:hosts" redis-key = "godns:hosts"
ttl = 600 ttl = 600
refresh-interval = 5 # 5 seconds refresh-interval = 60 # 5 seconds

56
etc/godns.example.conf Normal file
View File

@ -0,0 +1,56 @@
#Toml config file
title = "GODNS"
version = "0.2.0"
author = "kenshin, tystuyfzand"
debug = false
[server]
host = ""
port = 53
[resolv]
# Domain-specific nameservers configuration, formatting keep compatible with Dnsmasq
# Semicolon separate multiple files.
resolv-file = "/etc/resolv.conf"
timeout = 5 # 5 seconds
# The concurrency interval request upstream recursive server
# Match the PR15, https://github.com/kenshinx/godns/pull/15
interval = 200 # 200 milliseconds
# When defined, this is preferred over regular DNS. This requires a resolver to be active besides this, only for the initial lookup.
# A hosts file entry will suffice as well.
# dns-over-https = "https://cloudflare-dns.com/dns-query"
setedns0 = false #Support for larger UDP DNS responses
[redis]
enable = true
host = "127.0.0.1"
port = 6379
db = 0
password =""
[memcache]
servers = ["127.0.0.1:11211"]
[log]
stdout = true
file = "./godns.log"
level = "INFO" #DEBUG | INFO |NOTICE | WARN | ERROR
[cache]
# backend option [memory|memcache|redis]
backend = "memory"
expire = 600 # 10 minutes
maxcount = 0 #If set zero. The Sum of cache items will be unlimit.
[hosts]
#If set false, will not query hosts file and redis hosts record
enable = true
host-file = "/etc/hosts"
redis-enable = false
redis-key = "godns:hosts"
ttl = 600
# Refresh interval can be high since we have automatic updating via push and fsnotify
refresh-interval = 300

View File

@ -32,7 +32,9 @@ func NewHosts(hs HostsSettings, rs RedisSettings) Hosts {
h := Hosts{providers, time.Second * time.Duration(hs.RefreshInterval)} h := Hosts{providers, time.Second * time.Duration(hs.RefreshInterval)}
h.refresh() if h.refreshInterval > 0 {
h.refresh()
}
return h return h
} }

View File

@ -23,11 +23,14 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider {
hosts: make(map[string]string), hosts: make(map[string]string),
} }
// Force an initial refresh
rh.Refresh()
// Use pubsub to listen for key update events // Use pubsub to listen for key update events
go func() { go func() {
keyspaceEvent := "__keyspace@0__:" + key keyspaceEvent := "__keyspace@0__:" + key
sub := make(chan string, 2) sub := make(chan string, 3)
sub <- keyspaceEvent sub <- keyspaceEvent
sub <- "godns:update" sub <- "godns:update"
sub <- "godns:update_record" sub <- "godns:update_record"
@ -38,6 +41,7 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider {
msg := <- messages msg := <- messages
if msg.Channel == "godns:update" { if msg.Channel == "godns:update" {
logger.Debug("Refreshing redis records due to update")
rh.Refresh() rh.Refresh()
} else if msg.Channel == "godns:update_record" { } else if msg.Channel == "godns:update_record" {
recordName := string(msg.Message) recordName := string(msg.Message)
@ -45,17 +49,23 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider {
b, err := rc.Hget(key, recordName) b, err := rc.Hget(key, recordName)
if err != nil { if err != nil {
logger.Warn("Record %s does not exist, but was updated", recordName)
continue continue
} }
rh.mu.RLock() logger.Debug("Record %s was updated to %s", recordName, string(b))
rh.mu.Lock()
rh.hosts[recordName] = string(b) rh.hosts[recordName] = string(b)
rh.mu.RUnlock() rh.mu.Unlock()
} else if msg.Channel == "godns:remove_record" { } else if msg.Channel == "godns:remove_record" {
rh.mu.RLock() logger.Debug("Record %s was removed", msg.Message)
rh.mu.Lock()
delete(rh.hosts, string(msg.Message)) delete(rh.hosts, string(msg.Message))
rh.mu.RUnlock() rh.mu.Unlock()
} else if msg.Channel == keyspaceEvent { } else if msg.Channel == keyspaceEvent {
logger.Debug("Refreshing redis records due to update")
rh.Refresh() rh.Refresh()
} }
} }

View File

@ -23,8 +23,8 @@ var LogLevelMap = map[string]int{
} }
type Settings struct { type Settings struct {
Version string Version string `toml:"version"`
Debug bool Debug bool `toml:"debug"`
Server DNSServerSettings `toml:"server"` Server DNSServerSettings `toml:"server"`
ResolvConfig ResolvSettings `toml:"resolv"` ResolvConfig ResolvSettings `toml:"resolv"`
Redis RedisSettings `toml:"redis"` Redis RedisSettings `toml:"redis"`
@ -35,9 +35,9 @@ type Settings struct {
} }
type ResolvSettings struct { type ResolvSettings struct {
Timeout int Timeout int `toml:"timeout" env:"RESOLV_TIMEOUT"`
Interval int Interval int `toml:"interval" env:"RESOLV_INTERVAL"`
SetEDNS0 bool SetEDNS0 bool `toml:"setedns0" env:"RESOLV_EDNS0"`
ServerListFile string `toml:"server-list-file" env:"SERVER_LIST_FILE"` ServerListFile string `toml:"server-list-file" env:"SERVER_LIST_FILE"`
ResolvFile string `toml:"resolv-file" env:"RESOLV_FILE"` ResolvFile string `toml:"resolv-file" env:"RESOLV_FILE"`
DOHServer string `toml:"dns-over-https" env:"DNS_HTTPS_SERVER"` DOHServer string `toml:"dns-over-https" env:"DNS_HTTPS_SERVER"`
@ -64,7 +64,7 @@ func (s RedisSettings) Addr() string {
} }
type LogSettings struct { type LogSettings struct {
Stdout bool Stdout bool `toml:"stdout" env:"LOG_STDOUT"`
File string `toml:"file" env:"LOG_FILE"` File string `toml:"file" env:"LOG_FILE"`
Level string `toml:"level" env:"LOG_LEVEL"` Level string `toml:"level" env:"LOG_LEVEL"`
} }