2023-10-01 21:54:22 +00:00
|
|
|
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
|
2023-10-01 21:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-01 21:54:22 +00:00
|
|
|
|
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]
|
2023-10-01 21:54:22 +00:00
|
|
|
|
|
|
|
// 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-01 21:54:22 +00:00
|
|
|
}
|
2023-10-03 00:50:21 +00:00
|
|
|
msg.Rcode = rcode
|
|
|
|
msg.Answer = append(msg.Answer, answers...)
|
2023-10-01 21:54:22 +00:00
|
|
|
|
2023-10-03 00:50:21 +00:00
|
|
|
err = w.WriteMsg(msg)
|
2023-10-01 21:54:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|