-
Notifications
You must be signed in to change notification settings - Fork 0
/
rreflector.go
83 lines (67 loc) · 1.73 KB
/
rreflector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package rreflector
import (
"context"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
"strings"
)
var log = clog.NewWithPlugin("rreflector")
type rreflectorHandler struct {
host string
}
func newRReflectorHandler() *rreflectorHandler {
return &rreflectorHandler{host: ""}
}
func (h *rreflectorHandler) ServeDNS(_ context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{
Req: r,
W: w,
}
name := state.Name()
log.Infof("parsing qname %s", name)
parts := strings.Split(name[:len(name)-2], ".")
arpaHost := parts[len(parts)-2]
log.Debugf("arpa host: %s", arpaHost)
addrParts := parts[:len(parts)-2]
log.Debugf("address parts: %v", addrParts)
a := &dns.Msg{}
a.SetReply(r)
a.Authoritative = true
var joiner string
switch arpaHost {
case "ip6":
joiner = ":"
log.Debug("detected ipv6 lookup")
case "in-addr":
joiner = "."
log.Debug("detected ipv4 lookup")
default:
log.Error("rreflector plugin called for non-rDNS lookup")
a.Rcode = dns.RcodeNameError
w.WriteMsg(a)
return dns.RcodeNameError, nil
}
if h.host != "" {
joiner = "."
}
var reversedAddrParts []string
for i := len(addrParts) - 1; i >= 0; i-- {
reversedAddrParts = append(reversedAddrParts, addrParts[i])
}
log.Infof("original address: %s", strings.Join(reversedAddrParts, joiner))
ptr := new(dns.PTR)
ptr.Hdr = dns.RR_Header{
Name: state.QName(),
Rrtype: dns.TypePTR,
Class: state.QClass(),
}
ptr.Ptr = strings.Join(reversedAddrParts, joiner) + "." + h.host + "."
a.Answer = []dns.RR{ptr}
a.Rcode = dns.RcodeSuccess
w.WriteMsg(a)
return dns.RcodeSuccess, nil
}
func (h *rreflectorHandler) Name() string {
return "rreflector"
}