2020-10-12 19:10:35 +02:00
|
|
|
package dnstap
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-14 14:01:13 -04:00
|
|
|
"context"
|
2020-10-12 19:10:35 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/coredns/coredns/plugin/dnstap/msg"
|
2023-08-14 14:01:13 -04:00
|
|
|
"github.com/coredns/coredns/request"
|
2021-01-24 18:28:49 +01:00
|
|
|
|
2020-10-12 19:10:35 +02:00
|
|
|
tap "github.com/dnstap/golang-dnstap"
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ResponseWriter captures the client response and logs the query to dnstap.
|
|
|
|
|
type ResponseWriter struct {
|
2020-11-05 14:37:16 +01:00
|
|
|
queryTime time.Time
|
|
|
|
|
query *dns.Msg
|
2023-08-14 14:01:13 -04:00
|
|
|
ctx context.Context
|
2020-10-12 19:10:35 +02:00
|
|
|
dns.ResponseWriter
|
2024-04-27 04:08:47 +09:00
|
|
|
*Dnstap
|
2020-10-12 19:10:35 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
// WriteMsg writes back the response to the client and THEN works on logging the request and response to dnstap.
|
2020-10-12 19:10:35 +02:00
|
|
|
func (w *ResponseWriter) WriteMsg(resp *dns.Msg) error {
|
|
|
|
|
err := w.ResponseWriter.WriteMsg(resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r := new(tap.Message)
|
2020-11-05 14:37:16 +01:00
|
|
|
msg.SetQueryTime(r, w.queryTime)
|
2020-10-12 19:10:35 +02:00
|
|
|
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)
|
2023-08-14 14:01:13 -04:00
|
|
|
state := request.Request{W: w.ResponseWriter, Req: w.query}
|
|
|
|
|
w.TapMessageWithMetadata(w.ctx, r, state)
|
2020-10-12 19:10:35 +02:00
|
|
|
return nil
|
|
|
|
|
}
|