pancheri/handler.go

63 lines
1.2 KiB
Go
Raw Normal View History

package pancheri
import (
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
type Handler struct {
C *Config
R *Resolver
2023-10-03 00:50:21 +00:00
A *Authority
}
func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
// figure out how we should resolve this
2023-10-03 00:50:21 +00:00
msg := new(dns.Msg)
msg.SetReply(r)
msg.Authoritative = true
2023-10-03 00:50:21 +00:00
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
2023-10-03 00:50:21 +00:00
answers, rcode, err := h.R.Resolve(question.Name, question.Qtype)
if err != nil {
logrus.Errorf("error resolving: %s", err)
return
}
2023-10-03 00:50:21 +00:00
msg.Rcode = rcode
msg.Answer = append(msg.Answer, answers...)
2023-10-03 00:50:21 +00:00
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
}
}
}