godns/handler.go

190 lines
3.9 KiB
Go
Raw Normal View History

2013-07-23 11:10:38 +00:00
package main
import (
"net"
2013-12-12 12:31:26 +00:00
"sync"
2013-07-24 16:09:07 +00:00
"time"
2015-02-03 12:32:18 +00:00
"github.com/miekg/dns"
2013-07-23 11:10:38 +00:00
)
2015-02-03 12:32:18 +00:00
const (
notIPQuery = 0
_IP4Query = 4
_IP6Query = 6
)
2015-02-11 09:11:32 +00:00
type Question struct {
qname string
qtype string
qclass string
}
2013-07-24 10:29:38 +00:00
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-26 10:54:19 +00:00
hosts Hosts
2013-12-12 12:31:26 +00:00
mu *sync.Mutex
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 16:09:07 +00:00
Backend: make(map[string]Mesg),
Expire: time.Duration(cacheConfig.Expire) * time.Second,
Maxcount: cacheConfig.Maxcount,
2013-12-12 12:31:26 +00:00
mu: new(sync.RWMutex),
2013-07-24 10:29:38 +00:00
}
case "redis":
2013-07-24 14:40:18 +00:00
// cache = &MemoryCache{
2013-07-24 16:09:07 +00:00
// Backend: make(map[string]*dns.Msg),
// Expire: time.Duration(cacheConfig.Expire) * time.Second,
// Serializer: new(JsonSerializer),
// Maxcount: cacheConfig.Maxcount,
2013-07-24 14:40:18 +00:00
// }
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-26 10:54:19 +00:00
hosts := NewHosts(settings.Hosts, settings.Redis)
2013-12-12 12:31:26 +00:00
return &GODNSHandler{resolver, cache, hosts, new(sync.Mutex)}
2013-07-23 11:10:38 +00:00
}
2013-07-26 10:54:19 +00:00
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
2013-07-23 16:37:38 +00:00
q := req.Question[0]
2013-07-26 10:54:19 +00:00
Q := Question{UnFqdn(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
2015-02-03 12:32:18 +00:00
IPQuery := h.isIPQuery(q)
2013-07-26 10:54:19 +00:00
// Query hosts
2015-02-03 12:32:18 +00:00
if settings.Hosts.Enable && IPQuery > 0 {
if sip, ok := h.hosts.Get(Q.qname); ok {
var ip net.IP
2013-07-26 10:54:19 +00:00
m := new(dns.Msg)
m.SetReply(req)
2015-02-03 12:32:18 +00:00
switch IPQuery {
case _IP4Query:
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: settings.Hosts.TTL,
}
ip = net.ParseIP(sip).To4()
2015-02-03 12:32:18 +00:00
a := &dns.A{rr_header, ip}
m.Answer = append(m.Answer, a)
case _IP6Query:
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: settings.Hosts.TTL,
}
ip = net.ParseIP(sip).To16()
2015-02-03 12:32:18 +00:00
aaaa := &dns.AAAA{rr_header, ip}
m.Answer = append(m.Answer, aaaa)
}
2013-07-26 10:54:19 +00:00
w.WriteMsg(m)
2015-02-04 06:37:48 +00:00
Debug("%s found in hosts file", Q.qname)
2013-07-26 10:54:19 +00:00
return
2015-02-04 06:37:48 +00:00
} else {
Debug("%s didn't found in hosts file", Q.qname)
2013-07-26 10:54:19 +00:00
}
}
2015-02-11 09:11:32 +00:00
// Only query cache when qtype == 'A'|'AAAA' , qclass == 'IN'
2013-07-26 10:54:19 +00:00
key := KeyGen(Q)
2015-02-03 12:32:18 +00:00
if IPQuery > 0 {
2013-07-24 10:29:38 +00:00
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())
2013-12-12 12:31:26 +00:00
h.mu.Lock()
mesg.Id = req.Id
2013-07-24 10:48:43 +00:00
w.WriteMsg(mesg)
2013-12-12 12:31:26 +00:00
h.mu.Unlock()
2013-07-24 10:29:38 +00:00
return
}
}
2013-07-26 10:54:19 +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)
2015-02-03 15:20:36 +00:00
if IPQuery > 0 && len(mesg.Answer) > 0 {
2013-07-24 10:29:38 +00:00
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)
}
2013-07-26 04:06:16 +00:00
2015-02-03 12:32:18 +00:00
func (h *GODNSHandler) isIPQuery(q dns.Question) int {
if q.Qclass != dns.ClassINET {
return notIPQuery
}
switch q.Qtype {
case dns.TypeA:
return _IP4Query
case dns.TypeAAAA:
return _IP6Query
default:
return notIPQuery
}
2013-07-26 04:06:16 +00:00
}
2013-07-26 10:54:19 +00:00
func UnFqdn(s string) string {
if dns.IsFqdn(s) {
return s[:len(s)-1]
}
return s
}