2017-07-24 23:12:50 +02:00
|
|
|
package dnstap
|
|
|
|
|
|
|
|
import (
|
2018-04-22 08:34:35 +01:00
|
|
|
"context"
|
2018-03-01 03:19:01 +01:00
|
|
|
"time"
|
2017-07-24 23:12:50 +02:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2021-01-21 02:00:27 -07: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-01 12:41:41 +02:00
|
|
|
// Dnstap is the dnstap handler.
|
2017-07-24 23:12:50 +02:00
|
|
|
type Dnstap struct {
|
2017-09-14 09:36:06 +01:00
|
|
|
Next plugin.Handler
|
2020-11-05 14:37:16 +01:00
|
|
|
io tapper
|
2018-03-01 03:19:01 +01:00
|
|
|
|
2020-10-12 19:10:35 +02:00
|
|
|
// IncludeRawMessage will include the raw DNS message into the dnstap messages if true.
|
|
|
|
IncludeRawMessage bool
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:10:35 +02:00
|
|
|
// TapMessage sends the message m to the dnstap interface.
|
2018-08-02 00:58:23 +02:00
|
|
|
func (h Dnstap) TapMessage(m *tap.Message) {
|
2018-03-01 03:19:01 +01:00
|
|
|
t := tap.Dnstap_MESSAGE
|
2022-07-10 20:06:33 +02:00
|
|
|
h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m})
|
2017-09-01 12:41:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:00:27 -07:00
|
|
|
func (h Dnstap) tapQuery(w dns.ResponseWriter, query *dns.Msg, queryTime time.Time) {
|
|
|
|
q := new(tap.Message)
|
|
|
|
msg.SetQueryTime(q, queryTime)
|
|
|
|
msg.SetQueryAddress(q, w.RemoteAddr())
|
|
|
|
|
|
|
|
if h.IncludeRawMessage {
|
|
|
|
buf, _ := query.Pack()
|
|
|
|
q.QueryMessage = buf
|
|
|
|
}
|
|
|
|
msg.SetType(q, tap.Message_CLIENT_QUERY)
|
|
|
|
h.TapMessage(q)
|
|
|
|
}
|
|
|
|
|
2017-09-01 12:41:41 +02:00
|
|
|
// ServeDNS logs the client query and response to dnstap and passes the dnstap Context.
|
2017-07-24 23:12:50 +02:00
|
|
|
func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
2020-10-12 19:10:35 +02:00
|
|
|
rw := &ResponseWriter{
|
2018-03-01 03:19:01 +01:00
|
|
|
ResponseWriter: w,
|
2020-10-12 19:10:35 +02:00
|
|
|
Dnstap: h,
|
2020-11-05 14:37:16 +01:00
|
|
|
query: r,
|
|
|
|
queryTime: time.Now(),
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:00:27 -07:00
|
|
|
// The query tap message should be sent before sending the query to the
|
|
|
|
// forwarder. Otherwise, the tap messages will come out out of order.
|
|
|
|
h.tapQuery(w, r, rw.queryTime)
|
|
|
|
|
2020-10-12 19:10:35 +02:00
|
|
|
return plugin.NextOrFailure(h.Name(), h.Next, ctx, rw, r)
|
2017-07-24 23:12:50 +02:00
|
|
|
}
|
2017-09-01 12:41:41 +02:00
|
|
|
|
2020-10-12 19:10:35 +02:00
|
|
|
// Name implements the plugin.Plugin interface.
|
2017-07-24 23:12:50 +02:00
|
|
|
func (h Dnstap) Name() string { return "dnstap" }
|