| 1 |
// Package dnstest allows for easy testing of DNS clients against a test server. But it can also be |
| 2 |
// useful in production servers. |
| 3 |
package dnstest |
| 4 |
|
| 5 |
import ( |
| 6 |
"net" |
| 7 |
"time" |
| 8 |
|
| 9 |
"codeberg.org/miekg/dns" |
| 10 |
) |
| 11 |
|
| 12 |
// Recorder is a type of ResponseWriter that captures the the message written to it. It will never perform an |
| 13 |
// actual write. This effectively an [io.Discard] writer, and it's the caller's responsibility to write to the |
| 14 |
// original dns.ResponseWriter. Usage in a handler: |
| 15 |
// |
| 16 |
// rw := dnstest.NewRecorder(w) |
| 17 |
// ServeDNS(ctx, rw, r) |
| 18 |
// io.Copy(w, rw.Msg) // work on the original writer |
| 19 |
// |
| 20 |
// The msg is not unpacked during the Write, if you need this a rw.Msg.Unpack() is needed. |
| 21 |
// |
| 22 |
// Due to how we handle UDP sockets, it's impossible (hard?) to make the recorder write to the wrapped writer, |
| 23 |
// so this must still be done separately, as shown above. |
| 24 |
type Recorder struct { |
| 25 |
// In msg.go WriteTo we use the socket and then do a WriteMsgUDP to get the session right, this bypasses |
| 26 |
// the io.Writer stuff, thereby breaking the possibility to write here. |
| 27 |
w dns.ResponseWriter |
| 28 |
Msg *dns.Msg // Msg contains the last message written. |
| 29 |
Start time.Time // Time when the recorder was created. |
| 30 |
} |
| 31 |
|
| 32 |
var _ dns.ResponseWriter = &Recorder{} |
| 33 |
|
| 34 |
// NewRecorder makes and returns a new Recorder that wraps the given ResponseWriter. Start time set to now. |
| 35 |
func NewRecorder(w dns.ResponseWriter) *Recorder { return &Recorder{w: w, Start: time.Now()} } |
| 36 |
|
| 37 |
// NewTestRecorder returns a new Recorder that wraps a [dnstest.ResponseWriter]. This is a shortcut for |
| 38 |
// |
| 39 |
// rec := dnstest.NewRecorder(&dnstest.ResponseWriter{}) |
| 40 |
// |
| 41 |
// which is useful in tests. |
| 42 |
func NewTestRecorder() *Recorder { return NewRecorder(&ResponseWriter{}) } |
| 43 |
|
| 44 |
// NewTestRecorder6 works like [NewTestRecorder], but for IPv6. |
| 45 |
func NewTestRecorder6() *Recorder { return NewRecorder(&ResponseWriter6{}) } |
| 46 |
|
| 47 |
// MultiRecorder is a recorder that can record multiple messages written to it. Msg contains the last message |
| 48 |
// written to it. None of the messages are Unpacked. |
| 49 |
type MultiRecorder struct { |
| 50 |
*Recorder |
| 51 |
Msgs []*dns.Msg // Msgs contains all messages written to this recorder. |
| 52 |
} |
| 53 |
|
| 54 |
// NewMultiRecorder makes and returns a new MultiRecorder that wraps the given ResponseWriter. See |
| 55 |
// [NewRecorder]. |
| 56 |
func NewMultiRecorder(w dns.ResponseWriter) *MultiRecorder { |
| 57 |
return &MultiRecorder{Recorder: NewRecorder(w)} |
| 58 |
} |
| 59 |
|
| 60 |
func (m *MultiRecorder) Write(b []byte) (int, error) { |
| 61 |
m.Msgs = append(m.Msgs, &dns.Msg{Data: make([]byte, len(b)-2)}) |
| 62 |
return m.Recorder.Write(b) |
| 63 |
} |
| 64 |
|
| 65 |
func (r *Recorder) Write(b []byte) (int, error) { |
| 66 |
// See [Msg.WriteTo] that defaults to TCP. |
| 67 |
r.Msg = &dns.Msg{Data: make([]byte, len(b)-2)} |
| 68 |
copy(r.Msg.Data, b[2:]) |
| 69 |
return len(b), nil |
| 70 |
} |
| 71 |
|
| 72 |
// Implement the net.Conn interface. |
| 73 |
func (r *Recorder) Read(b []byte) (int, error) { return len(b), nil } |
| 74 |
func (r *Recorder) SetDeadline(time.Time) error { return nil } |
| 75 |
func (r *Recorder) SetReadDeadline(time.Time) error { return nil } |
| 76 |
func (r *Recorder) SetWriteDeadline(time.Time) error { return nil } |
| 77 |
|
| 78 |
func (r *Recorder) Conn() net.Conn { |
| 79 |
// because of this everything defaults to "TCP" |
| 80 |
return r // we are a net.Conn ourselves |
| 81 |
} |
| 82 |
|
| 83 |
func (r *Recorder) Hijack() { |
| 84 |
if r.w != nil { |
| 85 |
r.w.Hijack() |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func (r *Recorder) Session() *dns.Session { |
| 90 |
if r.w != nil { |
| 91 |
return r.w.Session() |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
func (r *Recorder) Close() error { |
| 97 |
if r.w != nil { |
| 98 |
return r.w.Close() |
| 99 |
} |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
func (r *Recorder) LocalAddr() net.Addr { |
| 104 |
if r.w != nil { |
| 105 |
return r.w.LocalAddr() |
| 106 |
} |
| 107 |
return nil |
| 108 |
} |
| 109 |
|
| 110 |
func (r *Recorder) RemoteAddr() net.Addr { |
| 111 |
if r.w != nil { |
| 112 |
return r.w.RemoteAddr() |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |