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

View file

@ -8,41 +8,46 @@ import (
type Handler struct { type Handler struct {
C *Config C *Config
R *Resolver R *Resolver
A *Authority
} }
func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { func (h *Handler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
// figure out how we should resolve this // figure out how we should resolve this
// is this in our list of authoritative domains?
// TODO
// okay, do we have upstream resolution enabled?
if h.C.Resolver.Enable {
// alright, resolve it with the resolver
msg := new(dns.Msg) msg := new(dns.Msg)
msg.SetReply(r) msg.SetReply(r)
msg.Authoritative = true msg.Authoritative = true
for _, question := range r.Question { if len(r.Question) != 1 {
answers, err := h.R.Resolve(question.Name, question.Qtype) 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 { if err != nil {
logrus.Errorf("error resolving: %s", err) logrus.Errorf("error resolving: %s", err)
return return
} }
msg.Rcode = rcode
msg.Answer = append(msg.Answer, answers...) msg.Answer = append(msg.Answer, answers...)
}
err := w.WriteMsg(msg) err = w.WriteMsg(msg)
if err != nil { if err != nil {
logrus.Errorf("error responding: %s", err) logrus.Errorf("error responding: %s", err)
return return
} }
} else { } else {
// alright, send an nxdomain // alright, send an nxdomain
msg := new(dns.Msg)
msg.SetReply(r)
msg.Authoritative = true
if r.RecursionDesired { if r.RecursionDesired {
msg.RecursionAvailable = true 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{ logrus.WithFields(logrus.Fields{
"domain": domain, "domain": domain,
"qtype": qtype, "qtype": qtype,
@ -33,5 +33,5 @@ func (r *Resolver) Resolve(domain string, qtype uint16) ([]dns.RR, error) {
return nil, err return nil, err
} }
return in.Answer, nil return in.Answer, in.Rcode, nil
} }

View file

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