forked from foxcpp/go-mockdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
418 lines (370 loc) · 9.02 KB
/
server.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package mockdns
import (
"context"
"errors"
"log"
"net"
"os"
"strings"
"time"
"github.com/miekg/dns"
)
// Server is the wrapper that binds Resolver to the DNS server implementation
// from github.com/miekg/dns. This allows it to be used as a replacement
// resolver for testing code that doesn't support DNS callbacks. See PatchNet.
type Server struct {
r Resolver
stopped bool
tcpServ dns.Server
udpServ dns.Server
Log Logger
Authoritative bool
}
type Logger interface {
Printf(f string, args ...interface{})
}
func NewServer(zones map[string]Zone, authoritative bool) (*Server, error) {
return NewServerWithLogger(zones, log.New(os.Stderr, "mockdns server: ", log.LstdFlags), authoritative)
}
func NewServerWithLogger(zones map[string]Zone, l Logger, authoritative bool) (*Server, error) {
s := &Server{
r: Resolver{
Zones: zones,
},
tcpServ: dns.Server{Addr: "127.0.0.1:0", Net: "tcp"},
udpServ: dns.Server{Addr: "127.0.0.1:0", Net: "udp"},
Log: l,
Authoritative: authoritative,
}
tcpL, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
return nil, err
}
// Note we bind TCP on automatic port first since it is more likely to be
// already used. Then we bind UDP on the same port, hoping it is
// not taken. We avoid using different ports for TCP and UDP since
// some applications do not support using a different TCP/UDP ports
// for DNS.
pconn, err := net.ListenPacket("udp4", tcpL.Addr().String())
if err != nil {
return nil, err
}
s.tcpServ.Listener = tcpL
s.tcpServ.Handler = s
s.udpServ.PacketConn = pconn
s.udpServ.Handler = s
go s.tcpServ.ActivateAndServe()
go s.udpServ.ActivateAndServe()
return s, nil
}
func (s *Server) writeErr(w dns.ResponseWriter, reply *dns.Msg, err error) {
reply.Rcode = dns.RcodeServerFailure
reply.RecursionAvailable = false
reply.Answer = nil
reply.Extra = nil
if dnsErr, ok := err.(*net.DNSError); ok {
if isNotFound(dnsErr) {
reply.Rcode = dns.RcodeNameError
reply.RecursionAvailable = true
reply.Ns = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: dnsErr.Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 9999,
},
Ns: "localhost.",
Mbox: "hostmaster.localhost.",
Serial: 1,
Refresh: 900,
Retry: 900,
Expire: 1800,
Minttl: 60,
},
}
}
} else {
s.Log.Printf("lookup error: %v", err)
}
w.WriteMsg(reply)
}
func mkCname(name, cname string) *dns.CNAME {
return &dns.CNAME{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeCNAME,
Class: dns.ClassINET,
Ttl: 9999,
},
Target: cname,
}
}
func splitTXT(s string) []string {
const maxLen = 255
parts := make([]string, 0, len(s)/maxLen+1)
min := func(i, j int) int {
if i < j {
return i
}
return j
}
for i := 0; i < len(s)/maxLen+1; i++ {
p := s[i*maxLen : min(len(s), (i+1)*maxLen)]
parts = append(parts, p)
}
return parts
}
// ServeDNS implements miekg/dns.Handler. It responds with values from underlying
// Resolver object.
func (s *Server) ServeDNS(w dns.ResponseWriter, m *dns.Msg) {
reply := new(dns.Msg)
if m.MsgHdr.Opcode != dns.OpcodeQuery {
reply.SetRcode(m, dns.RcodeRefused)
if err := w.WriteMsg(reply); err != nil {
s.Log.Printf("WriteMsg: %v", err)
}
return
}
reply.SetReply(m)
reply.RecursionAvailable = true
if s.Authoritative {
reply.Authoritative = true
reply.RecursionAvailable = false
}
q := m.Question[0]
qname := strings.ToLower(dns.Fqdn(q.Name))
if q.Qclass != dns.ClassINET {
reply.SetRcode(m, dns.RcodeNotImplemented)
if err := w.WriteMsg(reply); err != nil {
s.Log.Printf("WriteMsg: %v", err)
}
return
}
qnameZone, ok := s.r.Zones[qname]
if !ok {
s.writeErr(w, reply, notFound(qname))
return
}
// This does the lookup twice (including lookup* below).
// TODO: Avoid this.
ad, rname, _, err := s.r.targetZone(qname)
if err != nil {
s.writeErr(w, reply, err)
return
}
reply.AuthenticatedData = ad
if rname != qname {
reply.Answer = append(reply.Answer, mkCname(qname, rname))
}
switch q.Qtype {
case dns.TypeA:
_, addrs, err := s.r.lookupA(context.Background(), qname)
if err != nil {
s.writeErr(w, reply, err)
return
}
for _, addr := range addrs {
parsed := net.ParseIP(addr)
if parsed == nil {
panic("ServeDNS: malformed IP in records")
}
reply.Answer = append(reply.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 9999,
},
A: parsed,
})
}
case dns.TypeAAAA:
_, addrs, err := s.r.lookupAAAA(context.Background(), q.Name)
if err != nil {
s.writeErr(w, reply, err)
return
}
for _, addr := range addrs {
parsed := net.ParseIP(addr)
if parsed == nil {
panic("ServeDNS: malformed IP in records")
}
reply.Answer = append(reply.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET,
Ttl: 9999,
},
AAAA: parsed,
})
}
case dns.TypeMX:
_, mxs, err := s.r.lookupMX(context.Background(), q.Name)
if err != nil {
s.writeErr(w, reply, err)
return
}
for _, mx := range mxs {
reply.Answer = append(reply.Answer, &dns.MX{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeMX,
Class: dns.ClassINET,
Ttl: 9999,
},
Preference: mx.Pref,
Mx: mx.Host,
})
}
case dns.TypeNS:
cname, nss, err := s.r.lookupNS(context.Background(), q.Name)
if err != nil {
s.writeErr(w, reply, err)
return
}
if cname != "" {
reply.Answer = append(reply.Answer, mkCname(q.Name, cname))
}
for _, ns := range nss {
reply.Answer = append(reply.Answer, &dns.NS{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeNS,
Class: dns.ClassINET,
Ttl: 9999,
},
Ns: ns.Host,
})
}
case dns.TypeSRV:
_, srvs, err := s.r.lookupSRV(context.Background(), q.Name)
if err != nil {
s.writeErr(w, reply, err)
return
}
for _, srv := range srvs {
reply.Answer = append(reply.Answer, &dns.SRV{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeSRV,
Class: dns.ClassINET,
Ttl: 9999,
},
Priority: srv.Priority,
Port: srv.Port,
Target: srv.Target,
})
}
case dns.TypeCNAME:
reply.AuthenticatedData = qnameZone.AD
case dns.TypeTXT:
_, txts, err := s.r.lookupTXT(context.Background(), q.Name)
if err != nil {
s.writeErr(w, reply, err)
return
}
for _, txt := range txts {
reply.Answer = append(reply.Answer, &dns.TXT{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypeTXT,
Class: dns.ClassINET,
Ttl: 9999,
},
Txt: splitTXT(txt),
})
}
case dns.TypePTR:
rzone, ok := s.r.Zones[q.Name]
if !ok {
s.writeErr(w, reply, notFound(q.Name))
return
}
for _, name := range rzone.PTR {
reply.Answer = append(reply.Answer, &dns.PTR{
Hdr: dns.RR_Header{
Name: rname,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: 9999,
},
Ptr: name,
})
}
case dns.TypeSOA:
reply.Answer = []dns.RR{
&dns.SOA{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeSOA,
Class: dns.ClassINET,
Ttl: 9999,
},
Ns: "localhost.",
Mbox: "hostmaster.localhost.",
Serial: 1,
Refresh: 900,
Retry: 900,
Expire: 1800,
Minttl: 60,
},
}
default:
rzone, ok := s.r.Zones[q.Name]
if !ok {
s.writeErr(w, reply, notFound(q.Name))
return
}
reply.Answer = append(reply.Answer, rzone.Misc[dns.Type(q.Qtype)]...)
}
s.Log.Printf("DNS TRACE %v", reply.String())
if err := w.WriteMsg(reply); err != nil {
s.Log.Printf("WriteMsg: %v", err)
}
}
// LocalAddr returns the local endpoint used by the server. It will always be
// *net.UDPAddr, however it is also usable for TCP connections.
func (s *Server) LocalAddr() net.Addr {
return s.udpServ.PacketConn.LocalAddr()
}
// PatchNet configures net.Resolver instance to use this Server object.
//
// Use UnpatchNet to revert changes.
func (s *Server) PatchNet(r *net.Resolver) {
r.PreferGo = true
r.Dial = func(ctx context.Context, network, addr string) (net.Conn, error) {
if s.stopped {
return nil, errors.New("Patched resolver is used after Server.Close")
}
dialer := net.Dialer{
// This is localhost, it is either running or not. Fail quickly if
// we can't connect.
Timeout: 1 * time.Second,
}
switch network {
case "udp", "udp4", "udp6":
return dialer.DialContext(ctx, "udp4", s.udpServ.PacketConn.LocalAddr().String())
case "tcp", "tcp4", "tcp6":
return dialer.DialContext(ctx, "tcp4", s.tcpServ.Listener.Addr().String())
default:
panic("PatchNet.Dial: unknown network")
}
}
}
func UnpatchNet(r *net.Resolver) {
r.PreferGo = false
r.Dial = nil
}
// Resolver returns the underlying Resolver object that can be used directly
// to access Zones content.
func (s *Server) Resolver() *Resolver {
return &s.r
}
func (s *Server) Close() error {
s.tcpServ.Shutdown()
s.udpServ.Shutdown()
s.stopped = true
return nil
}