summaryrefslogtreecommitdiff
path: root/att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go
diff options
context:
space:
mode:
Diffstat (limited to 'att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go')
-rw-r--r--att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go b/att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go
new file mode 100644
index 0000000..c3598bc
--- /dev/null
+++ b/att script/3 (v6 DDoS)/code/辅助权威服务器/plugin/dnstap/dnstap.go
@@ -0,0 +1,60 @@
+package dnstap
+
+import (
+ "context"
+ "ohmydns2/plugin"
+ "ohmydns2/plugin/dnstap/msg"
+ "time"
+
+ tap "github.com/dnstap/golang-dnstap"
+ "github.com/miekg/dns"
+)
+
+// Dnstap is the dnstap handler.
+type Dnstap struct {
+ Next plugin.Handler
+ io tapper
+
+ // IncludeRawMessage will include the raw DNS message into the dnstap messages if true.
+ IncludeRawMessage bool
+ Identity []byte
+ Version []byte
+}
+
+// TapMessage sends the message m to the dnstap interface.
+func (h Dnstap) TapMessage(m *tap.Message) {
+ t := tap.Dnstap_MESSAGE
+ h.io.Dnstap(&tap.Dnstap{Type: &t, Message: m, Identity: h.Identity, Version: h.Version})
+}
+
+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)
+}
+
+// ServeDNS logs the client query and response to dnstap and passes the dnstap Context.
+func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
+ rw := &ResponseWriter{
+ ResponseWriter: w,
+ Dnstap: h,
+ query: r,
+ queryTime: time.Now(),
+ }
+
+ // 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)
+
+ return plugin.NextOrFailure(h.Name(), h.Next, ctx, rw, r)
+}
+
+// Name implements the plugin.Plugin interface.
+func (h Dnstap) Name() string { return "dnstap" }