pancheri/handler.go

63 lines
1.2 KiB
Go

package pancheri
import (
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
type Handler struct {
C *Config
R *Resolver
A *Authority
}
func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
// figure out how we should resolve this
msg := new(dns.Msg)
msg.SetReply(r)
msg.Authoritative = true
if len(r.Question) != 1 {
msg.Rcode = dns.RcodeFormatError
err := w.WriteMsg(msg)
if err != nil {
logrus.Errorf("error responding: %s", err)
return
}
return
}
question := r.Question[0]
// okay, do we have upstream resolution enabled?
if h.C.Resolver.Enable {
// alright, resolve it with the resolver
answers, rcode, err := h.R.Resolve(question.Name, question.Qtype)
if err != nil {
logrus.Errorf("error resolving: %s", err)
return
}
msg.Rcode = rcode
msg.Answer = append(msg.Answer, answers...)
err = w.WriteMsg(msg)
if err != nil {
logrus.Errorf("error responding: %s", err)
return
}
} else {
// alright, send an nxdomain
if r.RecursionDesired {
msg.RecursionAvailable = true
}
msg.Rcode = dns.RcodeNameError
err := w.WriteMsg(msg)
if err != nil {
logrus.Errorf("error responding: %s", err)
return
}
}
}