godns/handler.go

115 lines
2.4 KiB
Go
Raw Normal View History

2013-07-23 11:10:38 +00:00
package main
import (
"github.com/miekg/dns"
)
2013-07-24 10:29:38 +00:00
type Question struct {
qname string
qtype string
qclass string
}
func (q *Question) String() string {
return q.qname + " " + q.qclass + " " + q.qtype
}
2013-07-23 11:10:38 +00:00
type GODNSHandler struct {
resolver *Resolver
2013-07-24 10:29:38 +00:00
cache Cache
2013-07-23 11:10:38 +00:00
}
func NewHandler() *GODNSHandler {
var (
clientConfig *dns.ClientConfig
cacheConfig CacheSettings
2013-07-24 10:29:38 +00:00
resolver *Resolver
cache Cache
2013-07-23 11:10:38 +00:00
)
resolvConfig := settings.ResolvConfig
clientConfig, err := dns.ClientConfigFromFile(resolvConfig.ResolvFile)
if err != nil {
logger.Printf(":%s is not a valid resolv.conf file\n", resolvConfig.ResolvFile)
logger.Println(err)
panic(err)
}
clientConfig.Timeout = resolvConfig.Timeout
2013-07-24 10:29:38 +00:00
resolver = &Resolver{clientConfig}
2013-07-23 11:10:38 +00:00
cacheConfig = settings.Cache
2013-07-24 10:29:38 +00:00
switch cacheConfig.Backend {
case "memory":
cache = &MemoryCache{
2013-07-24 14:40:18 +00:00
backend: make(map[string]*dns.Msg),
expire: cacheConfig.Expire,
maxcount: cacheConfig.Maxcount,
2013-07-24 10:29:38 +00:00
}
case "redis":
2013-07-24 14:40:18 +00:00
// cache = &MemoryCache{
// backend: make(map[string]*dns.Msg),
// serializer: new(JsonSerializer),
// expire: cacheConfig.Expire,
// maxcount: cacheConfig.Maxcount,
// }
panic("Redis cache backend not implement yet")
2013-07-24 10:29:38 +00:00
default:
logger.Printf("Invalid cache backend %s", cacheConfig.Backend)
panic("Invalid cache backend")
}
2013-07-23 11:10:38 +00:00
return &GODNSHandler{resolver, cache}
}
func (h *GODNSHandler) do(net string, w dns.ResponseWriter, req *dns.Msg) {
2013-07-23 16:37:38 +00:00
q := req.Question[0]
Q := Question{q.Name, dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
2013-07-23 11:10:38 +00:00
2013-07-23 16:37:38 +00:00
Debug("Question: %s", Q.String())
2013-07-23 11:10:38 +00:00
2013-07-24 10:29:38 +00:00
key := KeyGen(Q)
// Only query cache when qtype == 'A' , qclass == 'IN'
if q.Qtype == dns.TypeA && q.Qclass == dns.ClassINET {
mesg, err := h.cache.Get(key)
if err != nil {
Debug("%s didn't hit cache: %s", Q.String(), err)
} else {
Debug("%s hit cache", Q.String())
mesg.Id = req.Id
2013-07-24 10:48:43 +00:00
w.WriteMsg(mesg)
2013-07-24 10:29:38 +00:00
return
}
}
2013-07-23 16:37:38 +00:00
mesg, err := h.resolver.Lookup(net, req)
2013-07-23 11:10:38 +00:00
2013-07-24 02:52:59 +00:00
if err != nil {
Debug("%s", err)
dns.HandleFailed(w, req)
return
}
w.WriteMsg(mesg)
2013-07-24 10:29:38 +00:00
if q.Qtype == dns.TypeA && q.Qclass == dns.ClassINET {
err = h.cache.Set(key, mesg)
if err != nil {
Debug("Set %s cache failed: %s", Q.String(), err.Error())
}
Debug("Insert %s into cache", Q.String())
}
2013-07-23 11:10:38 +00:00
}
func (h *GODNSHandler) DoTCP(w dns.ResponseWriter, req *dns.Msg) {
h.do("tcp", w, req)
}
func (h *GODNSHandler) DoUDP(w dns.ResponseWriter, req *dns.Msg) {
h.do("udp", w, req)
}