2013-07-23 11:10:38 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-25 17:43:02 +00:00
|
|
|
|
"crypto/md5"
|
|
|
|
|
"fmt"
|
2020-01-25 18:48:26 +00:00
|
|
|
|
"github.com/miekg/dns"
|
2020-01-25 17:43:02 +00:00
|
|
|
|
"meow.tf/joker/godns/cache"
|
|
|
|
|
"meow.tf/joker/godns/hosts"
|
|
|
|
|
"meow.tf/joker/godns/log"
|
|
|
|
|
"meow.tf/joker/godns/resolver"
|
|
|
|
|
"meow.tf/joker/godns/utils"
|
2015-10-13 17:00:28 +00:00
|
|
|
|
"net"
|
2013-07-24 16:09:07 +00:00
|
|
|
|
"time"
|
2013-07-23 11:10:38 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
|
type Handler struct {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
resolver *resolver.Resolver
|
|
|
|
|
cache, negCache cache.Cache
|
|
|
|
|
hosts hosts.Hosts
|
2013-07-23 11:10:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
|
func NewHandler(r *resolver.Resolver, resolverCache, negCache cache.Cache, h hosts.Hosts) *Handler {
|
|
|
|
|
return &Handler{r, resolverCache, negCache, h}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 03:38:22 +00:00
|
|
|
|
func (h *Handler) do(network string, w dns.ResponseWriter, req *dns.Msg) {
|
2013-07-23 16:37:38 +00:00
|
|
|
|
q := req.Question[0]
|
2020-02-08 03:38:22 +00:00
|
|
|
|
question := resolver.Question{Name: utils.UnFqdn(q.Name), Type: q.Qtype, Class: dns.ClassToString[q.Qclass]}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
2015-10-13 17:00:28 +00:00
|
|
|
|
var remote net.IP
|
2020-02-08 03:38:22 +00:00
|
|
|
|
|
|
|
|
|
switch t := w.RemoteAddr().(type) {
|
|
|
|
|
case *net.TCPAddr:
|
|
|
|
|
remote = t.IP
|
|
|
|
|
case *net.UDPAddr:
|
|
|
|
|
remote = t.IP
|
|
|
|
|
default:
|
|
|
|
|
return
|
2015-10-13 17:00:28 +00:00
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
2020-02-08 03:38:22 +00:00
|
|
|
|
log.Info("%s lookup %s", remote, question.String())
|
2015-02-03 12:32:18 +00:00
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
|
// Query hosts
|
2020-02-08 03:38:22 +00:00
|
|
|
|
if h.hosts != nil {
|
|
|
|
|
if vals, ttl, ok := h.hosts.Get(question.Type, question.Name); ok {
|
2013-07-26 10:54:19 +00:00
|
|
|
|
m := new(dns.Msg)
|
|
|
|
|
m.SetReply(req)
|
2015-02-03 12:32:18 +00:00
|
|
|
|
|
2020-02-08 03:38:22 +00:00
|
|
|
|
hdr := dns.RR_Header{
|
|
|
|
|
Name: q.Name,
|
|
|
|
|
Rrtype: question.Type,
|
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
|
Ttl: uint32(ttl / time.Second),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch question.Type {
|
|
|
|
|
case dns.TypeA:
|
|
|
|
|
for _, val := range vals {
|
|
|
|
|
m.Answer = append(m.Answer, &dns.A{Hdr: hdr, A: net.ParseIP(val).To4()})
|
2015-10-14 17:08:25 +00:00
|
|
|
|
}
|
2020-02-08 03:38:22 +00:00
|
|
|
|
case dns.TypeAAAA:
|
|
|
|
|
for _, val := range vals {
|
|
|
|
|
m.Answer = append(m.Answer, &dns.AAAA{Hdr: hdr, AAAA: net.ParseIP(val).To16()})
|
2015-02-03 12:32:18 +00:00
|
|
|
|
}
|
2020-02-08 03:38:22 +00:00
|
|
|
|
case dns.TypeCNAME:
|
|
|
|
|
for _, val := range vals {
|
|
|
|
|
m.Answer = append(m.Answer, &dns.CNAME{Hdr: hdr, Target: val})
|
2015-10-14 17:08:25 +00:00
|
|
|
|
}
|
2015-02-03 12:32:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 10:54:19 +00:00
|
|
|
|
w.WriteMsg(m)
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Debug("%s found in hosts file", question.Name)
|
2013-07-26 10:54:19 +00:00
|
|
|
|
return
|
2015-02-04 06:37:48 +00:00
|
|
|
|
} else {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Debug("%s didn't found in hosts file", question.Name)
|
2013-07-26 10:54:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
|
key := KeyGen(question)
|
2020-02-08 03:38:22 +00:00
|
|
|
|
|
|
|
|
|
mesg, err := h.cache.Get(key)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if mesg, err = h.negCache.Get(key); err != nil {
|
|
|
|
|
log.Debug("%s didn't hit cache", question.String())
|
2013-07-24 10:29:38 +00:00
|
|
|
|
} else {
|
2020-02-08 03:38:22 +00:00
|
|
|
|
log.Debug("%s hit negative cache", question.String())
|
|
|
|
|
dns.HandleFailed(w, req)
|
2013-07-24 10:29:38 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-08 03:38:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
log.Debug("%s hit cache", question.String())
|
|
|
|
|
// we need this copy against concurrent modification of Id
|
|
|
|
|
msg := *mesg
|
|
|
|
|
msg.Id = req.Id
|
|
|
|
|
w.WriteMsg(&msg)
|
|
|
|
|
return
|
2013-07-24 10:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 03:38:22 +00:00
|
|
|
|
mesg, err = h.resolver.Lookup(network, req)
|
2013-07-23 11:10:38 +00:00
|
|
|
|
|
2013-07-24 02:52:59 +00:00
|
|
|
|
if err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Warn("Resolve query error %s", err)
|
2013-07-24 02:52:59 +00:00
|
|
|
|
dns.HandleFailed(w, req)
|
2015-02-12 20:30:16 +00:00
|
|
|
|
|
|
|
|
|
// cache the failure, too!
|
|
|
|
|
if err = h.negCache.Set(key, nil); err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Warn("Set %s negative cache failed: %v", question.String(), err)
|
2015-02-12 20:30:16 +00:00
|
|
|
|
}
|
2013-07-24 02:52:59 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteMsg(mesg)
|
|
|
|
|
|
2020-02-08 03:38:22 +00:00
|
|
|
|
if len(mesg.Answer) > 0 {
|
2013-07-24 10:29:38 +00:00
|
|
|
|
err = h.cache.Set(key, mesg)
|
|
|
|
|
if err != nil {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Warn("Set %s cache failed: %s", question.String(), err.Error())
|
2013-07-24 10:29:38 +00:00
|
|
|
|
}
|
2020-01-25 17:43:02 +00:00
|
|
|
|
log.Debug("Insert %s into cache", question.String())
|
2013-07-24 10:29:38 +00:00
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
|
func (h *Handler) Bind(net string) func(w dns.ResponseWriter, req *dns.Msg) {
|
2020-01-25 17:43:02 +00:00
|
|
|
|
return func(w dns.ResponseWriter, req *dns.Msg) {
|
|
|
|
|
h.do(net, w, req)
|
|
|
|
|
}
|
2013-07-23 11:10:38 +00:00
|
|
|
|
}
|
2013-07-26 04:06:16 +00:00
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
|
func KeyGen(q resolver.Question) string {
|
|
|
|
|
h := md5.New()
|
|
|
|
|
h.Write([]byte(q.String()))
|
|
|
|
|
x := h.Sum(nil)
|
|
|
|
|
key := fmt.Sprintf("%x", x)
|
|
|
|
|
return key
|
2013-07-26 10:54:19 +00:00
|
|
|
|
}
|