forked from Eun/coredns-ipecho
/
ipecho.go
187 lines (165 loc) · 4.34 KB
/
ipecho.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ipecho
import (
"fmt"
"log"
"net"
"strings"
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
"golang.org/x/net/context"
)
type ipecho struct {
Next plugin.Handler
Config *config
}
// ServeDNS implements the middleware.Handler interface.
func (p ipecho) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
if p.echoIP(w, r) {
return dns.RcodeSuccess, nil
}
return plugin.NextOrFailure(p.Name(), p.Next, ctx, w, r)
}
// Name implements the Handler interface.
func (ipecho) Name() string {
return "IPEcho"
}
func (p *ipecho) echoIP(w dns.ResponseWriter, r *dns.Msg) bool {
if len(r.Question) <= 0 {
return false
}
var rrs []dns.RR
for i := 0; i < len(r.Question); i++ {
question := r.Question[i]
if question.Qclass != dns.ClassINET {
continue
}
switch question.Qtype {
case dns.TypeAAAA:
for _, record := range p.handleAAAA(question) {
rrs = append(rrs, record)
}
case dns.TypePTR:
for _, record := range p.handlePTR(question) {
rrs = append(rrs, record)
}
}
}
if len(rrs) > 0 {
if p.Config.Debug {
log.Printf("[ipecho] Answering with %d rr's\n", len(rrs))
}
m := new(dns.Msg)
m.SetReply(r)
m.Answer = rrs
w.WriteMsg(m)
return true
}
return false
}
func (p *ipecho) handlePTR(question dns.Question) (rrs []dns.RR) {
if !strings.HasSuffix(question.Name, "ip6.arpa.") {
log.Printf("not a ip6.arpa PTR query")
return
}
ipv6str := question.Name[:len(question.Name)-9 /* ip6.arpa. */ -1 /* trailing dot */]
hexslice := strings.Split(ipv6str, ".")
for i, j := 0, len(hexslice) - 1; i < j; i, j = i + 1, j - 1 {
hexslice[i] = hexslice[j]
}
quads := make([]string, 8)
for i, j := 0, 4; i < len(quads); i, j = i + 1, j + 4 {
quads[i] = strings.Join(hexslice[i * 4 : j], "")
}
ip := net.ParseIP(strings.Join(quads, ":"))
if ip == nil {
panic("parsed IP is invalid")
}
if !p.Config.network.Contains(ip) {
log.Println("refusing to return PTR since IP is not part of network")
return
}
hostname := fmt.Sprintf("%s%s.%s", p.Config.Prefix, strings.Join(quads[4:], ""), p.Config.Zones[0])
rrs = append(rrs, &dns.PTR{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: p.Config.TTL,
},
Ptr: dns.Fqdn(hostname),
})
return
}
func (p *ipecho) handleAAAA(question dns.Question) (rrs []dns.RR) {
ip := p.parseIP(&question)
if ip == nil {
if p.Config.Debug {
log.Printf("[ipecho] Parsed IP of '%s' is nil\n", question.Name)
}
return
}
if ip4 := ip.To4(); ip4 == nil {
if p.Config.Debug {
log.Printf("[ipecho] Parsed IP of '%s' is an IPv6 address\n", question.Name)
}
rrs = append(rrs, &dns.AAAA{
Hdr: dns.RR_Header{
Name: question.Name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: p.Config.TTL,
},
AAAA: ip,
})
}
return rrs
}
func (p *ipecho) parseIP(question *dns.Question) net.IP {
if p.Config.Debug {
log.Printf("[ipecho] Query for '%s'", question.Name)
}
for _, zone := range p.Config.Zones {
if !strings.HasSuffix(strings.ToLower(question.Name), zone) {
continue
}
subdomain := question.Name[:len(question.Name)-len(zone)]
if len(subdomain) <= 0 {
if p.Config.Debug {
log.Printf("[ipecho] Query ('%s') has no subdomain\n", question.Name)
}
return nil
}
subdomain = strings.Trim(subdomain, ".")
if p.Config.Prefix != "" && !strings.HasPrefix(strings.ToLower(subdomain), p.Config.Prefix) {
if p.Config.Debug {
log.Printf("[ipecho] Query ('%s') prefix does not match\n", question.Name)
}
return nil
}
ipv6nick := subdomain[len(p.Config.Prefix):]
if len(ipv6nick) != 16 {
log.Printf("[ipecho] ipv6 nick is longer than expected")
return nil
}
var quads [4]string
var quad string
for i := 0; len(ipv6nick) >= 4; i++ {
quad, ipv6nick = ipv6nick[0:4], ipv6nick[4:]
log.Println(quad, ipv6nick)
quads[i] = quad
}
var ipv6addr string
ones, _ := p.Config.network.Mask.Size();
if ones == 64 {
ipv6addr = p.Config.Network[:len(p.Config.Network) - 1 /* :: */] + strings.Join(quads[:], ":")
} else {
ipv6addr = p.Config.Network + strings.Join(quads[:], ":")
}
log.Println(ipv6addr)
return net.ParseIP(ipv6addr)
}
if p.Config.Debug {
log.Printf("[ipecho] Query ('%s') does not have a matching zone (%s)\n", question.Name, strings.Join(p.Config.Zones, ", "))
}
return nil
}