forked from docker-archive/dnsserver
/
dnsserver.go
202 lines (170 loc) · 5.08 KB
/
dnsserver.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package dnsserver
import (
"fmt"
"net"
"sync"
"github.com/miekg/dns"
)
// Encapsulates the data segment of a SRV record. Priority and Weight are
// always 0 in our SRV records.
type SRVRecord struct {
Port uint16
Host string
}
func (s SRVRecord) Equal(s2 SRVRecord) bool {
return s.Port == s2.Port && s.Host == s2.Host
}
// Struct which describes the DNS server.
type DNSServer struct {
Domain string // using the constructor, this will always end in a '.', making it a FQDN.
aRecords map[string]net.IP // FQDN -> IP
srvRecords map[string][]SRVRecord // service (e.g., _test._tcp) -> SRV
aMutex sync.RWMutex // mutex for A record operations
srvMutex sync.RWMutex // mutex for SRV record operations
}
// Create a new DNS server. Domain is an unqualified domain that will be used
// as the TLD.
func NewDNSServer(domain string) *DNSServer {
return &DNSServer{
Domain: domain + ".",
aRecords: map[string]net.IP{},
srvRecords: map[string][]SRVRecord{},
aMutex: sync.RWMutex{},
srvMutex: sync.RWMutex{},
}
}
// Listen for DNS requests. listenSpec is a dotted-quad + port, e.g.,
// 127.0.0.1:53. This function blocks and only returns when the DNS service is
// no longer functioning.
func (ds *DNSServer) Listen(listenSpec string) error {
return dns.ListenAndServe(listenSpec, "udp", ds)
}
// Convenience function to ensure the fqdn is well-formed, and keeps the
// set/delete interface easy.
func (ds *DNSServer) qualifyHost(host string) string {
return host + "." + ds.Domain
}
// Convenience function to ensure that SRV names are well-formed.
func (ds *DNSServer) qualifySrv(service, protocol string) string {
return fmt.Sprintf("_%s._%s.%s", service, protocol, ds.Domain)
}
// rewrites supplied host entries to use the domain this dns server manages
func (ds *DNSServer) qualifySrvHosts(srvs []SRVRecord) []SRVRecord {
newsrvs := []SRVRecord{}
for _, srv := range srvs {
newsrvs = append(newsrvs, SRVRecord{
Host: ds.qualifyHost(srv.Host),
Port: srv.Port,
})
}
return newsrvs
}
// Receives a FQDN; looks up and supplies the A record.
func (ds *DNSServer) GetA(fqdn string) *dns.A {
ds.aMutex.RLock()
defer ds.aMutex.RUnlock()
val, ok := ds.aRecords[fqdn]
if ok {
return &dns.A{
Hdr: dns.RR_Header{
Name: fqdn,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
// 0 TTL results in UB for DNS resolvers and generally causes problems.
Ttl: 1,
},
A: val,
}
}
return nil
}
// Sets a host to an IP. Note that this is not the FQDN, but a hostname.
func (ds *DNSServer) SetA(host string, ip net.IP) {
ds.aMutex.Lock()
ds.aRecords[ds.qualifyHost(host)] = ip
ds.aMutex.Unlock()
}
// Deletes a host. Note that this is not the FQDN, but a hostname.
func (ds *DNSServer) DeleteA(host string) {
ds.aMutex.Lock()
delete(ds.aRecords, ds.qualifyHost(host))
ds.aMutex.Unlock()
}
// Given a service spec, looks up and returns an array of *dns.SRV objects.
// These must be massaged into the []dns.RR after the fact.
func (ds *DNSServer) GetSRV(spec string) []*dns.SRV {
ds.srvMutex.RLock()
defer ds.srvMutex.RUnlock()
srv, ok := ds.srvRecords[spec]
if ok {
records := []*dns.SRV{}
for _, record := range srv {
srvRecord := &dns.SRV{
Hdr: dns.RR_Header{
Name: spec,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
// 0 TTL results in UB for DNS resolvers and generally causes problems.
Ttl: 1,
},
Priority: 0,
Weight: 0,
Port: record.Port,
Target: record.Host,
}
records = append(records, srvRecord)
}
return records
}
return nil
}
// Sets a SRV with a service and protocol. See SRVRecord for more information
// on what that requires.
func (ds *DNSServer) SetSRV(service, protocol string, srvs []SRVRecord) {
ds.srvMutex.Lock()
ds.srvRecords[ds.qualifySrv(service, protocol)] = ds.qualifySrvHosts(srvs)
ds.srvMutex.Unlock()
}
// Deletes a SRV record based on the service and protocol.
func (ds *DNSServer) DeleteSRV(service, protocol string) {
ds.srvMutex.Lock()
delete(ds.srvRecords, ds.qualifySrv(service, protocol))
ds.srvMutex.Unlock()
}
// Main callback for miekg/dns. Collects information about the query,
// constructs a response, and returns it to the connector.
func (ds *DNSServer) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
m := &dns.Msg{}
m.SetReply(r)
answers := []dns.RR{}
for _, question := range r.Question {
// nil records == not found
switch question.Qtype {
case dns.TypeA:
a := ds.GetA(question.Name)
if a != nil {
answers = append(answers, a)
}
case dns.TypeSRV:
srv := ds.GetSRV(question.Name)
if srv != nil {
for _, record := range srv {
answers = append(answers, record)
}
}
}
}
// If we have no answers, that means we found nothing or didn't get a query
// we can reply to. Reply with no answers so we ensure the query moves on to
// the next server.
if len(answers) == 0 {
m.SetRcode(r, dns.RcodeSuccess)
w.WriteMsg(m)
return
}
// Without these the glibc resolver gets very angry.
m.Authoritative = true
m.RecursionAvailable = true
m.Answer = answers
w.WriteMsg(m)
}