diff --git a/godns.conf b/godns.conf index 3afcc5c..ae700e9 100644 --- a/godns.conf +++ b/godns.conf @@ -40,9 +40,9 @@ maxcount = 0 #If set zero. The Sum of cache itmes will be unlimit. [hosts] #If set false, will not query hosts file and redis hosts record -enable = false +enable = true host-file = "/etc/hosts" -redis-enable = true +redis-enable = false redis-key = "godns:hosts" ttl = 600 diff --git a/handler.go b/handler.go index b1b3fe3..0b02170 100644 --- a/handler.go +++ b/handler.go @@ -1,6 +1,7 @@ package main import ( + "net" "time" "github.com/miekg/dns" @@ -85,7 +86,13 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) { q := req.Question[0] Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]} - Debug("Question: %s", Q.String()) + var remote net.IP + if Net == "tcp" { + remote = w.RemoteAddr().(*net.TCPAddr).IP + } else { + remote = w.RemoteAddr().(*net.UDPAddr).IP + } + logger.Info("%s lookup %s", remote, Q.String()) IPQuery := h.isIPQuery(q) @@ -117,10 +124,10 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) { } w.WriteMsg(m) - Debug("%s found in hosts file", Q.qname) + logger.Debug("%s found in hosts file", Q.qname) return } else { - Debug("%s didn't found in hosts file", Q.qname) + logger.Debug("%s didn't found in hosts file", Q.qname) } } @@ -130,14 +137,14 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) { mesg, err := h.cache.Get(key) if err != nil { if mesg, err = h.negCache.Get(key); err != nil { - Debug("%s didn't hit cache: %s", Q.String(), err) + logger.Debug("%s didn't hit cache", Q.String()) } else { - Debug("%s hit negative cache", Q.String()) + logger.Debug("%s hit negative cache", Q.String()) dns.HandleFailed(w, req) return } } else { - Debug("%s hit cache", Q.String()) + logger.Debug("%s hit cache", Q.String()) // we need this copy against concurrent modification of Id msg := *mesg msg.Id = req.Id @@ -149,12 +156,12 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) { mesg, err := h.resolver.Lookup(Net, req) if err != nil { - Debug("%s", err) + logger.Warn("Resolve query error %s", err) dns.HandleFailed(w, req) // cache the failure, too! if err = h.negCache.Set(key, nil); err != nil { - Debug("Set %s negative cache failed: %v", Q.String(), err) + logger.Warn("Set %s negative cache failed: %v", Q.String(), err) } return } @@ -164,9 +171,9 @@ func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) { if IPQuery > 0 && len(mesg.Answer) > 0 { err = h.cache.Set(key, mesg) if err != nil { - Debug("Set %s cache failed: %s", Q.String(), err.Error()) + logger.Warn("Set %s cache failed: %s", Q.String(), err.Error()) } - Debug("Insert %s into cache", Q.String()) + logger.Debug("Insert %s into cache", Q.String()) } } diff --git a/hosts.go b/hosts.go index 87bef5e..1796700 100644 --- a/hosts.go +++ b/hosts.go @@ -90,8 +90,12 @@ func (r *RedisHosts) Set(domain, ip string) (bool, error) { } func (r *RedisHosts) Refresh() { - r.redis.Hgetall(r.key, r.hosts) - Debug("update hosts records from redis") + 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") + } } type FileHosts struct { @@ -107,7 +111,8 @@ func (f *FileHosts) Get(domain string) (ip string, ok bool) { func (f *FileHosts) Refresh() { buf, err := os.Open(f.file) if err != nil { - panic("Can't open " + f.file) + logger.Warn("Update hosts records from file failed %s", err) + return } defer buf.Close() @@ -138,7 +143,7 @@ func (f *FileHosts) Refresh() { f.hosts[domain] = ip } - Debug("update hosts records from %s", f.file) + logger.Debug("update hosts records from %s", f.file) } func (f *FileHosts) isDomain(domain string) bool { diff --git a/resolver.go b/resolver.go index cc59dcd..c91f12d 100644 --- a/resolver.go +++ b/resolver.go @@ -41,15 +41,15 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error defer wg.Done() r, rtt, err := c.Exchange(req, nameserver) if err != nil { - Debug("%s socket error on %s", qname, nameserver) - Debug("error:%s", err.Error()) + logger.Warn("%s socket error on %s", qname, nameserver) + logger.Warn("error:%s", err.Error()) return } if r != nil && r.Rcode != dns.RcodeSuccess { - Debug("%s failed to get an valid answer on %s", qname, nameserver) + logger.Warn("%s failed to get an valid answer on %s", qname, nameserver) return } - Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt) + logger.Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt) select { case res <- r: default: