2024-02-09 01:44:51 +00:00
|
|
|
package synthetic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
|
|
|
"github.com/coredns/coredns/plugin/test"
|
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = clog.NewWithPlugin("synthetic")
|
|
|
|
|
|
|
|
func (s synthetic) Name() string { return "synthetic" }
|
|
|
|
|
|
|
|
type synthetic struct {
|
|
|
|
Next plugin.Handler
|
|
|
|
Config syntheticConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
state := request.Request{W: w, Req: r}
|
|
|
|
log.Debug("Received request for ", state.QName(), " of type ", state.QType())
|
|
|
|
|
|
|
|
// Continue to the next plugin in the chain, and record the response.
|
|
|
|
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
|
|
|
rc, err := plugin.NextOrFailure(s.Name(), s.Next, ctx, rec, r)
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
log.Debug("Next plugin in chain responded with rcode ",
|
|
|
|
rc,
|
|
|
|
" and ",
|
|
|
|
len(r.Answer),
|
|
|
|
" answer(s) for ",
|
|
|
|
state.QName(),
|
|
|
|
)
|
|
|
|
|
2024-02-09 01:44:51 +00:00
|
|
|
// If the next plugin in the chain's recorded response is success, we go with that.
|
2024-02-21 06:26:54 +00:00
|
|
|
if rc == dns.RcodeSuccess && len(rec.Msg.Answer) > 0 {
|
|
|
|
log.Info("Next Plugin's answers are acceptable")
|
2024-02-09 01:44:51 +00:00
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
2024-02-21 06:26:54 +00:00
|
|
|
log.Debug("Injecting Synthetic response")
|
2024-02-09 01:44:51 +00:00
|
|
|
|
|
|
|
switch state.QType() {
|
|
|
|
|
|
|
|
//
|
|
|
|
// Handle AAAA and A requests (Forward Lookup)
|
|
|
|
// Note: on the forward lookup, we're guaranteed to have a match on the domain name
|
|
|
|
//
|
|
|
|
case dns.TypeA, dns.TypeAAAA:
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
// only continue if the first label starts with the prefix (fall back to recorded response)
|
|
|
|
if !strings.HasPrefix(state.Name(), s.Config.prefix) {
|
2024-02-09 01:44:51 +00:00
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
// parse first dns label (now we have: "<prefix><ip>")
|
2024-02-09 01:44:51 +00:00
|
|
|
firstLabel := strings.Split(state.Name(), ".")[0]
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
// trim the prefix (now we have: "<ip>")
|
|
|
|
ipStr := strings.TrimPrefix(firstLabel, s.Config.prefix)
|
2024-02-09 01:44:51 +00:00
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
// attempt to parse as IPv6 or IPv4
|
2024-02-09 01:44:51 +00:00
|
|
|
ip := net.ParseIP(strings.ReplaceAll(ipStr, "-", "."))
|
|
|
|
if ip == nil {
|
|
|
|
ip = net.ParseIP(strings.ReplaceAll(ipStr, "-", ":"))
|
|
|
|
}
|
|
|
|
log.Debug(state.Name(), " parsed to ", ip)
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
// don't continue if we couldn't parse the IP (fall back to recorded responses)
|
2024-02-09 01:44:51 +00:00
|
|
|
if ip == nil {
|
|
|
|
log.Debug("Failed to parse IP from: ", state.QName())
|
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if ip is within the synthetic network
|
|
|
|
var found bool
|
|
|
|
for _, n := range s.Config.net {
|
|
|
|
if n.Contains(ip) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// don't continue if the IP is not in the synthetic network (fall back to recorded response)
|
|
|
|
if !found {
|
|
|
|
log.Debug("IP ", ip, " not found in synthetic network")
|
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle AAAA requests with IPv6 address
|
2024-02-11 07:02:52 +00:00
|
|
|
if ip.To4() == nil && state.QType() == dns.TypeAAAA {
|
2024-02-09 01:44:51 +00:00
|
|
|
log.Debug("Responding to AAAA request for ", state.QName())
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
2024-02-21 06:26:54 +00:00
|
|
|
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
2024-02-09 01:44:51 +00:00
|
|
|
m.Answer = append(m.Answer, &dns.AAAA{Hdr: hdr, AAAA: ip.To16()})
|
|
|
|
w.WriteMsg(m)
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle A requests with IPv4 address
|
|
|
|
if ip.To4() != nil && state.QType() == dns.TypeA {
|
|
|
|
log.Debug("Responding to A request for ", state.QName())
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
2024-02-21 06:26:54 +00:00
|
|
|
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
2024-02-09 01:44:51 +00:00
|
|
|
m.Answer = append(m.Answer, &dns.A{Hdr: hdr, A: ip.To4()})
|
|
|
|
w.WriteMsg(m)
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle A requests with IPv6 address (respond with empty answer and NOERROR)
|
2024-02-11 07:02:52 +00:00
|
|
|
if ip.To4() == nil && state.QType() == dns.TypeA {
|
2024-02-09 01:44:51 +00:00
|
|
|
log.Debug("Responding to A request for ", state.QName(), " with empty answer")
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
|
|
|
w.WriteMsg(m)
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle AAAA requests with IPv4 address (respond with empty answer and NOERROR)
|
|
|
|
if ip.To4() != nil && state.QType() == dns.TypeAAAA {
|
|
|
|
log.Debug("Responding to AAAA request for ", state.QName(), " with empty answer")
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
|
|
|
w.WriteMsg(m)
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got here, we couldn't handle the request (fall back to recorded response)
|
|
|
|
log.Debug("Failed to handle request for ", state.QName())
|
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
|
|
|
|
//
|
|
|
|
// Handle PTR requests (Reverse Lookup)
|
2024-02-21 06:26:54 +00:00
|
|
|
// Note: on the reverse lookup, we're guaranteed to have a match on the network based on the zone file
|
2024-02-09 01:44:51 +00:00
|
|
|
//
|
|
|
|
case dns.TypePTR:
|
|
|
|
ip := inArpaToIp(state.QName())
|
|
|
|
log.Debug("Received PTR request. Parsed ", state.QName(), " to ", ip)
|
|
|
|
|
|
|
|
// don't continue if we couldn't parse the IP (fall back to recorded response)
|
|
|
|
if ip == nil {
|
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// successful reverse lookup
|
2024-02-21 06:26:54 +00:00
|
|
|
forward := ipToDomainName(s.Config.prefix, ip, s.Config.forward)
|
2024-02-09 01:44:51 +00:00
|
|
|
log.Debug("Responding to PTR request for ", state.QName(), " with ", forward)
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetReply(r)
|
2024-02-21 06:26:54 +00:00
|
|
|
hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl}
|
2024-02-09 01:44:51 +00:00
|
|
|
m.Answer = append(m.Answer, &dns.PTR{Hdr: hdr, Ptr: forward})
|
|
|
|
w.WriteMsg(m)
|
|
|
|
return dns.RcodeSuccess, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got here, we couldn't handle the request (fall back to recorded response)
|
|
|
|
log.Debug("synthetic plugin not needed for ", state.QName(), " of type ", state.QType())
|
|
|
|
w.WriteMsg(rec.Msg)
|
|
|
|
return rc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func inArpaToIp(name string) net.IP {
|
|
|
|
ipv4Suffix := ".in-addr.arpa."
|
|
|
|
ipv6Suffix := ".ip6.arpa."
|
|
|
|
|
|
|
|
if idx := strings.Index(name, ipv4Suffix); idx > 6 {
|
|
|
|
name = name[:idx]
|
|
|
|
parts := strings.Split(name, ".")
|
|
|
|
if len(parts) != 4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name = parts[3] + "." + parts[2] + "." + parts[1] + "." + parts[0]
|
|
|
|
return net.ParseIP(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) == 73 && name[63:] == ipv6Suffix {
|
|
|
|
// we can rely on the fact that v6 reverse hostnames have a fixed length
|
|
|
|
|
|
|
|
// read the characters from the hostname into a buffer in reverse
|
|
|
|
v6chars := make([]byte, 32)
|
|
|
|
for i, j := 62, 0; i >= 0; i -= 2 {
|
|
|
|
v6chars[j] = name[i]
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode the characters in the buffer into 16 bytes and return it
|
|
|
|
v6bytes := make([]byte, 16)
|
|
|
|
if _, err := hex.Decode(v6bytes, v6chars); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v6bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 06:26:54 +00:00
|
|
|
func ipToDomainName(prefix string, ip net.IP, zone string) string {
|
2024-02-09 01:44:51 +00:00
|
|
|
if ip == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(zone, ".") {
|
|
|
|
zone = "." + zone
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(zone, ".") {
|
|
|
|
zone = zone + "."
|
|
|
|
}
|
|
|
|
|
|
|
|
sep := ":"
|
|
|
|
if ip.To4() != nil {
|
|
|
|
sep = "."
|
|
|
|
}
|
|
|
|
|
|
|
|
response := strings.Join(strings.Split(ip.String(), sep), "-")
|
2024-02-21 06:26:54 +00:00
|
|
|
return prefix + response + zone
|
2024-02-09 01:44:51 +00:00
|
|
|
}
|