-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
netbox.go
175 lines (147 loc) · 4.65 KB
/
netbox.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
// Copyright 2020 Oz Tiram <oz.tiram@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netbox
import (
"context"
"net"
"net/http"
"strings"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/fall"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
// 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("netbox")
type Netbox struct {
Url string
Token string
Next plugin.Handler
TTL time.Duration
Fall fall.F
Zones []string
Client *http.Client
}
// constants to match IP address family used by NetBox
const (
familyIP4 = 4
familyIP6 = 6
)
// ServeDNS implements the plugin.Handler interface
func (n *Netbox) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
var (
ips []net.IP
domains []string
err error
)
state := request.Request{W: w, Req: r}
// only handle zones we are configured to respond for
zone := plugin.Zones(n.Zones).Matches(state.Name())
if zone == "" {
return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r)
}
qname := state.Name()
// Export metric with the server label set to the current
// server handling the request.
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
var answers []dns.RR
// check record type here and bail out if not A, AAAA or PTR
switch state.QType() {
case dns.TypeA:
ips, err = n.query(strings.TrimRight(qname, "."), familyIP4)
answers = a(qname, uint32(n.TTL.Seconds()), ips)
case dns.TypeAAAA:
ips, err = n.query(strings.TrimRight(qname, "."), familyIP6)
answers = aaaa(qname, uint32(n.TTL.Seconds()), ips)
case dns.TypePTR:
domains, err = n.queryreverse(qname)
answers = ptr(qname, uint32(n.TTL.Seconds()), domains)
default:
// always fallthrough if configured
if n.Fall.Through(qname) {
return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r)
}
// otherwise return SERVFAIL here without fallthrough
return dnserror(dns.RcodeServerFailure, state, err)
}
if err != nil {
// return SERVFAIL here without fallthrough
return dnserror(dns.RcodeServerFailure, state, err)
}
if len(answers) == 0 {
if n.Fall.Through(qname) {
return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r)
} else {
return dnserror(dns.RcodeNameError, state, nil)
}
}
// create DNS response
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.Answer = answers
// send response back to client
_ = w.WriteMsg(m)
// signal response sent back to client
return dns.RcodeSuccess, nil
}
// Name implements the Handler interface.
func (n *Netbox) Name() string { return "netbox" }
// a takes a slice of net.IPs and returns a slice of A RRs.
func a(zone string, ttl uint32, ips []net.IP) []dns.RR {
answers := make([]dns.RR, len(ips))
for i, ip := range ips {
r := new(dns.A)
r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
r.A = ip
answers[i] = r
}
return answers
}
// aaaa takes a slice of net.IPs and returns a slice of AAAA RRs.
func aaaa(zone string, ttl uint32, ips []net.IP) []dns.RR {
answers := make([]dns.RR, len(ips))
for i, ip := range ips {
r := new(dns.AAAA)
r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttl}
r.AAAA = ip
answers[i] = r
}
return answers
}
// ptr takes a slice of strings and returns a slice of PTR RRs.
func ptr(zone string, ttl uint32, domains []string) []dns.RR {
answers := make([]dns.RR, len(domains))
for i, domain := range domains {
r := new(dns.PTR)
r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: ttl}
r.Ptr = domain
answers[i] = r
}
return answers
}
// dnserror writes a DNS error response back to the client. Based on plugin.BackendError
func dnserror(rcode int, state request.Request, err error) (int, error) {
m := new(dns.Msg)
m.SetRcode(state.Req, rcode)
m.Authoritative = true
// send response
_ = state.W.WriteMsg(m)
// return success as the rcode to signal we have written to the client.
return dns.RcodeSuccess, err
}