resolver work
This commit is contained in:
parent
19d67c2209
commit
6998394403
5 changed files with 41 additions and 23 deletions
5
authority.go
Normal file
5
authority.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package pancheri
|
||||
|
||||
type Authority struct {
|
||||
Zones map[string]*Zone
|
||||
}
|
|
@ -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,
|
||||
|
|
33
handler.go
33
handler.go
|
@ -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
|
||||
|
||||
// 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.SetReply(r)
|
||||
msg.Authoritative = true
|
||||
|
||||
for _, question := range r.Question {
|
||||
answers, err := h.R.Resolve(question.Name, question.Qtype)
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue