diff --git a/docker/Dockerfile b/docker/Dockerfile index 62a703a..e9d0b3f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -8,5 +8,6 @@ RUN apk --no-cache add tini ENTRYPOINT ["/sbin/tini", "-g", "--"] CMD ["godns"] +COPY etc/godns.conf /etc/godns.conf COPY godns /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns diff --git a/docker/Dockerfile.arm b/docker/Dockerfile.arm index 901c304..9c4a359 100644 --- a/docker/Dockerfile.arm +++ b/docker/Dockerfile.arm @@ -10,5 +10,6 @@ RUN apk --no-cache add tini ENTRYPOINT ["/sbin/tini", "-g", "--"] CMD ["godns"] +COPY etc/godns.conf /etc/godns.conf COPY godns-arm /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns diff --git a/docker/Dockerfile.arm64 b/docker/Dockerfile.arm64 index 64ff089..63cbe8e 100644 --- a/docker/Dockerfile.arm64 +++ b/docker/Dockerfile.arm64 @@ -10,5 +10,6 @@ RUN apk --no-cache add tini ENTRYPOINT ["/sbin/tini", "-g", "--"] CMD ["godns"] +COPY etc/godns.conf /etc/godns.conf COPY godns-arm64 /usr/local/bin/godns RUN chmod +x /usr/local/bin/godns diff --git a/etc/godns.conf b/etc/godns.conf index 5bd363a..a94be7a 100644 --- a/etc/godns.conf +++ b/etc/godns.conf @@ -16,7 +16,6 @@ port = 53 # Semicolon separate multiple files. server-list-file = "./etc/apple.china.conf;./etc/google.china.conf" resolv-file = "/etc/resolv.conf" -dns-over-https = "https://cloudflare-dns.com/dns-query" timeout = 5 # 5 seconds # The concurrency interval request upstream recursive server # Match the PR15, https://github.com/kenshinx/godns/pull/15 diff --git a/hosts_redis.go b/hosts_redis.go index 79c57f2..09edae9 100644 --- a/hosts_redis.go +++ b/hosts_redis.go @@ -48,7 +48,13 @@ func NewRedisProvider(rc *redis.Client, key string) HostProvider { continue } + rh.mu.RLock() rh.hosts[recordName] = string(b) + rh.mu.RUnlock() + } else if msg.Channel == "godns:remove_record" { + rh.mu.RLock() + delete(rh.hosts, string(msg.Message)) + rh.mu.RUnlock() } else if msg.Channel == keyspaceEvent { rh.Refresh() } diff --git a/resolver.go b/resolver.go index b3a095f..1fbace8 100644 --- a/resolver.go +++ b/resolver.go @@ -36,8 +36,8 @@ type Resolver struct { domain_server *suffixTreeNode config *ResolvSettings - tcpClient *dns.Client - udpClient *dns.Client + tcpClient *dns.Client + udpClient *dns.Client httpsClient *dns.Client } @@ -73,20 +73,20 @@ func NewResolver(c ResolvSettings) *Resolver { timeout := r.Timeout() r.udpClient = &dns.Client{ - Net: "udp", - ReadTimeout: timeout, + Net: "udp", + ReadTimeout: timeout, WriteTimeout: timeout, } r.tcpClient = &dns.Client{ - Net: "tcp", - ReadTimeout: timeout, + Net: "tcp", + ReadTimeout: timeout, WriteTimeout: timeout, } r.httpsClient = &dns.Client{ - Net: "https", - ReadTimeout: timeout, + Net: "https", + ReadTimeout: timeout, WriteTimeout: timeout, } @@ -250,8 +250,8 @@ func (r *Resolver) resolverFor(net, nameserver string) (*dns.Client, error) { } else if strings.HasSuffix(nameserver, ":853") { // TODO We need to set the server name so we can confirm the TLS connection. This may require a rewrite of storing nameservers. return &dns.Client{ - Net: "tcp-tls", - ReadTimeout: r.Timeout(), + Net: "tcp-tls", + ReadTimeout: r.Timeout(), WriteTimeout: r.Timeout(), TLSConfig: &tls.Config{ ServerName: "", diff --git a/server.go b/server.go index 4b8fab0..18f54a2 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "time" "github.com/miekg/dns" + "os" ) type Server struct { @@ -16,42 +17,48 @@ type Server struct { } func (s *Server) Addr() string { + if addr := os.Getenv("SERVER_BIND"); addr != "" { + return addr + } + return net.JoinHostPort(s.host, strconv.Itoa(s.port)) } func (s *Server) Run() { - Handler := NewHandler() + handler := NewHandler() tcpHandler := dns.NewServeMux() - tcpHandler.HandleFunc(".", Handler.DoTCP) + tcpHandler.HandleFunc(".", handler.DoTCP) udpHandler := dns.NewServeMux() - udpHandler.HandleFunc(".", Handler.DoUDP) + udpHandler.HandleFunc(".", handler.DoUDP) - tcpServer := &dns.Server{Addr: s.Addr(), - Net: "tcp", - Handler: tcpHandler, - ReadTimeout: s.rTimeout, - WriteTimeout: s.wTimeout} + tcpServer := &dns.Server{ + Addr: s.Addr(), + Net: "tcp", + Handler: tcpHandler, + ReadTimeout: s.rTimeout, + WriteTimeout: s.wTimeout, + } - udpServer := &dns.Server{Addr: s.Addr(), - Net: "udp", - Handler: udpHandler, - UDPSize: 65535, - ReadTimeout: s.rTimeout, - WriteTimeout: s.wTimeout} + udpServer := &dns.Server{ + Addr: s.Addr(), + Net: "udp", + Handler: udpHandler, + UDPSize: 65535, + ReadTimeout: s.rTimeout, + WriteTimeout: s.wTimeout, + } go s.start(udpServer) go s.start(tcpServer) - } func (s *Server) start(ds *dns.Server) { - logger.Info("Start %s listener on %s", ds.Net, s.Addr()) + err := ds.ListenAndServe() if err != nil { logger.Error("Start %s listener on %s failed:%s", ds.Net, s.Addr(), err.Error()) } - } diff --git a/settings.go b/settings.go index dc4a2e5..dc5a673 100644 --- a/settings.go +++ b/settings.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/BurntSushi/toml" + "strings" ) var ( @@ -59,6 +60,10 @@ type MemcacheSettings struct { } func (s RedisSettings) Addr() string { + if addr := os.Getenv("REDIS_ADDR"); addr != "" { + return addr + } + return s.Host + ":" + strconv.Itoa(s.Port) } @@ -92,10 +97,9 @@ type HostsSettings struct { } func init() { - var configFile string - flag.StringVar(&configFile, "c", "./etc/godns.conf", "Look for godns toml-formatting config file in this directory") + flag.StringVar(&configFile, "c", "/etc/godns.conf", "Look for godns toml-formatting config file in this directory") flag.Parse() if _, err := toml.DecodeFile(configFile, &settings); err != nil { @@ -104,4 +108,12 @@ func init() { os.Exit(1) } + // Env overrides + if cacheBackend := os.Getenv("CACHE_BACKEND"); cacheBackend != "" { + settings.Cache.Backend = cacheBackend + } + + if memcacheServers := os.Getenv("MEMCACHE_SERVERS"); memcacheServers != "" { + settings.Memcache.Servers = strings.Split(memcacheServers, ",") + } }