2017-07-24 23:12:50 +02:00
|
|
|
// Package taprw takes a query and intercepts the response.
|
|
|
|
|
// It will log both after the response is written.
|
|
|
|
|
package taprw
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2017-10-25 19:46:41 +01:00
|
|
|
"time"
|
2017-07-24 23:12:50 +02:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin/dnstap/msg"
|
2017-07-24 23:12:50 +02:00
|
|
|
|
|
|
|
|
tap "github.com/dnstap/golang-dnstap"
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
2017-09-29 13:38:01 -07:00
|
|
|
// SendOption stores the flag to indicate whether a certain DNSTap message to
|
|
|
|
|
// be sent out or not.
|
|
|
|
|
type SendOption struct {
|
|
|
|
|
Cq bool
|
|
|
|
|
Cr bool
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
// Tapper is what ResponseWriter needs to log to dnstap.
|
|
|
|
|
type Tapper interface {
|
2017-07-24 23:12:50 +02:00
|
|
|
TapMessage(m *tap.Message) error
|
2017-09-01 12:41:41 +02:00
|
|
|
TapBuilder() msg.Builder
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
// ResponseWriter captures the client response and logs the query to dnstap.
|
2017-07-24 23:12:50 +02:00
|
|
|
// Single request use.
|
2017-09-29 13:38:01 -07:00
|
|
|
// SendOption configures Dnstap to selectively send Dnstap messages. Default is send all.
|
2017-07-24 23:12:50 +02:00
|
|
|
type ResponseWriter struct {
|
2017-09-01 12:41:41 +02:00
|
|
|
queryEpoch uint64
|
|
|
|
|
Query *dns.Msg
|
2017-07-24 23:12:50 +02:00
|
|
|
dns.ResponseWriter
|
2017-09-01 12:41:41 +02:00
|
|
|
Tapper
|
2017-09-29 13:38:01 -07:00
|
|
|
err error
|
|
|
|
|
Send *SendOption
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
// DnstapError check if a dnstap error occurred during Write and returns it.
|
2017-07-24 23:12:50 +02:00
|
|
|
func (w ResponseWriter) DnstapError() error {
|
|
|
|
|
return w.err
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-25 19:46:41 +01:00
|
|
|
// SetQueryEpoch sets the query epoch as reported by dnstap.
|
|
|
|
|
func (w *ResponseWriter) SetQueryEpoch() {
|
|
|
|
|
w.queryEpoch = uint64(time.Now().Unix())
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
// WriteMsg writes back the response to the client and THEN works on logging the request
|
2017-07-24 23:12:50 +02:00
|
|
|
// and response to dnstap.
|
2017-09-01 12:41:41 +02:00
|
|
|
// Dnstap errors are to be checked by DnstapError.
|
|
|
|
|
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) (writeErr error) {
|
|
|
|
|
writeErr = w.ResponseWriter.WriteMsg(resp)
|
2017-10-25 19:46:41 +01:00
|
|
|
writeEpoch := uint64(time.Now().Unix())
|
2017-07-24 23:12:50 +02:00
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
b := w.TapBuilder()
|
|
|
|
|
b.TimeSec = w.queryEpoch
|
2017-07-24 23:12:50 +02:00
|
|
|
|
2017-09-29 13:38:01 -07:00
|
|
|
if w.Send == nil || w.Send.Cq {
|
2017-09-01 12:41:41 +02:00
|
|
|
if err := func() (err error) {
|
2017-09-29 13:38:01 -07:00
|
|
|
err = b.AddrMsg(w.ResponseWriter.RemoteAddr(), w.Query)
|
|
|
|
|
if err != nil {
|
2017-09-01 12:41:41 +02:00
|
|
|
return
|
|
|
|
|
}
|
2017-09-29 13:38:01 -07:00
|
|
|
return w.TapMessage(b.ToClientQuery())
|
2017-09-01 12:41:41 +02:00
|
|
|
}(); err != nil {
|
2017-09-29 13:38:01 -07:00
|
|
|
w.err = fmt.Errorf("client query: %s", err)
|
|
|
|
|
// don't forget to call DnstapError later
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 13:38:01 -07:00
|
|
|
if w.Send == nil || w.Send.Cr {
|
|
|
|
|
if writeErr == nil {
|
|
|
|
|
if err := func() (err error) {
|
|
|
|
|
b.TimeSec = writeEpoch
|
|
|
|
|
if err = b.Msg(resp); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return w.TapMessage(b.ToClientResponse())
|
|
|
|
|
}(); err != nil {
|
|
|
|
|
w.err = fmt.Errorf("client response: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-01 12:41:41 +02:00
|
|
|
return
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|