coredns/plugin/dnstap/writer.go
Miek Gieben 123da4c844
plugin/dnstap: remove config struct (#4258)
* plugin/dnstap: remove config struct

this struct is an uneeded intermidiate to get a dnstap it can be
removed. Remove the dnstapio subpkg: it's also not needed. Make *many*
functions and structs private now that we can.

Signed-off-by: Miek Gieben <miek@miek.nl>

* correct logging

Signed-off-by: Miek Gieben <miek@miek.nl>
2020-11-05 14:37:16 +01:00

51 lines
1.1 KiB
Go

package dnstap
import (
"time"
"github.com/coredns/coredns/plugin/dnstap/msg"
tap "github.com/dnstap/golang-dnstap"
"github.com/miekg/dns"
)
// ResponseWriter captures the client response and logs the query to dnstap.
type ResponseWriter struct {
queryTime time.Time
query *dns.Msg
dns.ResponseWriter
Dnstap
}
// 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) error {
err := w.ResponseWriter.WriteMsg(resp)
q := new(tap.Message)
msg.SetQueryTime(q, w.queryTime)
msg.SetQueryAddress(q, w.RemoteAddr())
if w.IncludeRawMessage {
buf, _ := w.query.Pack()
q.QueryMessage = buf
}
msg.SetType(q, tap.Message_CLIENT_QUERY)
w.TapMessage(q)
if err != nil {
return err
}
r := new(tap.Message)
msg.SetQueryTime(r, w.queryTime)
msg.SetResponseTime(r, time.Now())
msg.SetQueryAddress(r, w.RemoteAddr())
if w.IncludeRawMessage {
buf, _ := resp.Pack()
r.ResponseMessage = buf
}
msg.SetType(r, tap.Message_CLIENT_RESPONSE)
w.TapMessage(r)
return nil
}