do proper testing
This commit is contained in:
+25
-23
@@ -13,22 +13,15 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Define log to be a logger with the plugin name in it. This way we can just use log.Info and
|
||||
// friends to log.
|
||||
var log = clog.NewWithPlugin("synthetic")
|
||||
|
||||
// Name implements the Handler interface.
|
||||
func (s synthetic) Name() string { return "synthetic" }
|
||||
|
||||
// synthetic is an synthetic plugin to show how to write a plugin.
|
||||
type synthetic struct {
|
||||
Next plugin.Handler
|
||||
Config syntheticConfig
|
||||
}
|
||||
|
||||
// ServeDNS implements the plugin.Handler interface.
|
||||
//
|
||||
// This method gets called when synthetic is used in a Server
|
||||
func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
|
||||
// Create a new state for this request. This is used to store state and allows us to pass this
|
||||
@@ -39,12 +32,21 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||
rc, err := plugin.NextOrFailure(s.Name(), s.Next, ctx, rec, r)
|
||||
|
||||
log.Debug("Next plugin in chain responded with rcode ",
|
||||
rc,
|
||||
" and ",
|
||||
len(r.Answer),
|
||||
" answer(s) for ",
|
||||
state.QName(),
|
||||
)
|
||||
|
||||
// If the next plugin in the chain's recorded response is success, we go with that.
|
||||
if rc == dns.RcodeNameError {
|
||||
log.Debug("Next plugin in chain responded with acceptable response ", rc, " for ", state.QName())
|
||||
if rc == dns.RcodeSuccess && len(rec.Msg.Answer) > 0 {
|
||||
log.Info("Next Plugin's answers are acceptable")
|
||||
w.WriteMsg(rec.Msg)
|
||||
return rc, err
|
||||
}
|
||||
log.Debug("Injecting Synthetic response")
|
||||
|
||||
switch state.QType() {
|
||||
|
||||
@@ -54,26 +56,26 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
//
|
||||
case dns.TypeA, dns.TypeAAAA:
|
||||
|
||||
// only continue if the first label starts with "ip-" (fall back to recorded response)
|
||||
if !strings.HasPrefix(state.Name(), "ip-") {
|
||||
// only continue if the first label starts with the prefix (fall back to recorded response)
|
||||
if !strings.HasPrefix(state.Name(), s.Config.prefix) {
|
||||
w.WriteMsg(rec.Msg)
|
||||
return rc, err
|
||||
}
|
||||
|
||||
// parse first dns label (now we have: "ip-<ip>")
|
||||
// parse first dns label (now we have: "<prefix><ip>")
|
||||
firstLabel := strings.Split(state.Name(), ".")[0]
|
||||
|
||||
// trim "ip-" prefix (now we have: "<ip>")
|
||||
ipStr := strings.TrimPrefix(firstLabel, "ip-")
|
||||
// trim the prefix (now we have: "<ip>")
|
||||
ipStr := strings.TrimPrefix(firstLabel, s.Config.prefix)
|
||||
|
||||
// attempt to parse as IPv4 or IPv6
|
||||
// attempt to parse as IPv6 or IPv4
|
||||
ip := net.ParseIP(strings.ReplaceAll(ipStr, "-", "."))
|
||||
if ip == nil {
|
||||
ip = net.ParseIP(strings.ReplaceAll(ipStr, "-", ":"))
|
||||
}
|
||||
log.Debug(state.Name(), " parsed to ", ip)
|
||||
|
||||
// don't continue if we couldn't parse the IP (fall back to recorded respones)
|
||||
// don't continue if we couldn't parse the IP (fall back to recorded responses)
|
||||
if ip == nil {
|
||||
log.Debug("Failed to parse IP from: ", state.QName())
|
||||
w.WriteMsg(rec.Msg)
|
||||
@@ -100,7 +102,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
log.Debug("Responding to AAAA request for ", state.QName())
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()}
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
||||
m.Answer = append(m.Answer, &dns.AAAA{Hdr: hdr, AAAA: ip.To16()})
|
||||
w.WriteMsg(m)
|
||||
return dns.RcodeSuccess, nil
|
||||
@@ -111,7 +113,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
log.Debug("Responding to A request for ", state.QName())
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()}
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
||||
m.Answer = append(m.Answer, &dns.A{Hdr: hdr, A: ip.To4()})
|
||||
w.WriteMsg(m)
|
||||
return dns.RcodeSuccess, nil
|
||||
@@ -142,7 +144,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
|
||||
//
|
||||
// Handle PTR requests (Reverse Lookup)
|
||||
// Note: on the forward lookup, we're guaranteed to have a match on the network
|
||||
// Note: on the reverse lookup, we're guaranteed to have a match on the network based on the zone file
|
||||
//
|
||||
case dns.TypePTR:
|
||||
ip := inArpaToIp(state.QName())
|
||||
@@ -155,11 +157,11 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
}
|
||||
|
||||
// successful reverse lookup
|
||||
forward := ipToDomainName(ip, s.Config.forward)
|
||||
forward := ipToDomainName(s.Config.prefix, ip, s.Config.forward)
|
||||
log.Debug("Responding to PTR request for ", state.QName(), " with ", forward)
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()}
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
||||
m.Answer = append(m.Answer, &dns.PTR{Hdr: hdr, Ptr: forward})
|
||||
w.WriteMsg(m)
|
||||
return dns.RcodeSuccess, nil
|
||||
@@ -209,7 +211,7 @@ func inArpaToIp(name string) net.IP {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ipToDomainName(ip net.IP, zone string) string {
|
||||
func ipToDomainName(prefix string, ip net.IP, zone string) string {
|
||||
if ip == nil {
|
||||
return ""
|
||||
}
|
||||
@@ -228,5 +230,5 @@ func ipToDomainName(ip net.IP, zone string) string {
|
||||
}
|
||||
|
||||
response := strings.Join(strings.Split(ip.String(), sep), "-")
|
||||
return "ip-" + response + zone
|
||||
return prefix + response + zone
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user