resolver work

This commit is contained in:
core 2023-10-02 20:50:21 -04:00
parent 19d67c2209
commit 6998394403
Signed by: core
GPG key ID: FDBF740DADDCEECF
5 changed files with 41 additions and 23 deletions

5
authority.go Normal file
View file

@ -0,0 +1,5 @@
package pancheri
type Authority struct {
Zones map[string]*Zone
}

View file

@ -31,15 +31,23 @@ func main() {
}
logrus.SetLevel(c.Logging.Level)
zones := make(map[string]*pancheri.Zone, len(c.Zone.LoadFiles))
for _, zonefile := range c.Zone.LoadFiles {
logrus.WithFields(logrus.Fields{
"file": zonefile,
}).Info("loading zone")
_, err := pancheri.LoadZone(zonefile)
zone, err := pancheri.LoadZone(zonefile)
if err != nil {
logrus.Errorf("failed to load zone %s: %s", zonefile, err)
os.Exit(1)
}
authoritativeZone := "." + zone.Root + "."
logrus.WithFields(logrus.Fields{
"rsha": zone.ReducedHash,
"root": authoritativeZone,
}).Info("zone loaded")
zones[authoritativeZone] = zone
}
logrus.WithFields(logrus.Fields{
@ -60,6 +68,9 @@ func main() {
handler := pancheri.Handler{
C: c,
R: r,
A: &pancheri.Authority{
Zones: zones,
},
}
server := &dns.Server{
Addr: c.Server.Host + ":" + c.Server.Port,

View file

@ -8,41 +8,46 @@ import (
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
// is this in our list of authoritative domains?
// TODO
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
msg := new(dns.Msg)
msg.SetReply(r)
msg.Authoritative = true
for _, question := range r.Question {
answers, err := h.R.Resolve(question.Name, question.Qtype)
if err != nil {
logrus.Errorf("error resolving: %s", err)
return
}
msg.Answer = append(msg.Answer, answers...)
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)
err = w.WriteMsg(msg)
if err != nil {
logrus.Errorf("error responding: %s", err)
return
}
} else {
// alright, send an nxdomain
msg := new(dns.Msg)
msg.SetReply(r)
msg.Authoritative = true
if r.RecursionDesired {
msg.RecursionAvailable = true
}

View file

@ -17,7 +17,7 @@ func NewResolver(upstream string) *Resolver {
}
}
func (r *Resolver) Resolve(domain string, qtype uint16) ([]dns.RR, error) {
func (r *Resolver) Resolve(domain string, qtype uint16) ([]dns.RR, int, error) {
logrus.WithFields(logrus.Fields{
"domain": domain,
"qtype": qtype,
@ -33,5 +33,5 @@ func (r *Resolver) Resolve(domain string, qtype uint16) ([]dns.RR, error) {
return nil, err
}
return in.Answer, nil
return in.Answer, in.Rcode, nil
}

View file

@ -4,7 +4,6 @@ import (
"crypto/sha256"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"net"
"os"
@ -156,7 +155,5 @@ func LoadZone(path string) (*Zone, error) {
}
}
logrus.Debugf("%+v", zone)
return &zone, nil
}