forked from coredns/coredns
/
writer.go
79 lines (66 loc) · 1.77 KB
/
writer.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
// Package taprw takes a query and intercepts the response.
// It will log both after the response is written.
package taprw
import (
"fmt"
"time"
"github.com/coredns/coredns/plugin/dnstap/msg"
tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns"
)
// SendOption stores the flag to indicate whether a certain DNSTap message to
// be sent out or not.
type SendOption struct {
Cq bool
Cr bool
}
// Tapper is what ResponseWriter needs to log to dnstap.
type Tapper interface {
TapMessage(*tap.Message)
Pack() bool
}
// ResponseWriter captures the client response and logs the query to dnstap.
// Single request use.
// SendOption configures Dnstap to selectively send Dnstap messages. Default is send all.
type ResponseWriter struct {
QueryEpoch time.Time
Query *dns.Msg
dns.ResponseWriter
Tapper
Send *SendOption
dnstapErr error
}
// DnstapError check if a dnstap error occurred during Write and returns it.
func (w *ResponseWriter) DnstapError() error {
return w.dnstapErr
}
// WriteMsg writes back the response to the client and THEN works on logging the request
// and response to dnstap.
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) (writeErr error) {
writeErr = w.ResponseWriter.WriteMsg(resp)
writeEpoch := time.Now()
b := msg.New().Time(w.QueryEpoch).Addr(w.RemoteAddr())
if w.Send == nil || w.Send.Cq {
if w.Pack() {
b.Msg(w.Query)
}
if m, err := b.ToClientQuery(); err != nil {
w.dnstapErr = fmt.Errorf("client query: %s", err)
} else {
w.TapMessage(m)
}
}
if w.Send == nil || w.Send.Cr {
if writeErr == nil {
if w.Pack() {
b.Msg(resp)
}
if m, err := b.Time(writeEpoch).ToClientResponse(); err != nil {
w.dnstapErr = fmt.Errorf("client response: %s", err)
} else {
w.TapMessage(m)
}
}
}
return writeErr
}