mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-03 18:53:13 -05:00 
			
		
		
		
	* dnstap: add 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: add setup_test for 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * udnstap: update document and test Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: update setup_test for more coverage Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: add TapMessageWithMetadata function to Dnstap Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: adapt dnstap and forward plugins to use TapMessageWithMetadata Signed-off-by: chenyuheng <chenyuheng99@qq.com> * change TapMessageWithMetadata function Signed-off-by: chenyuheng <chenyuheng99@qq.com> * tab inconsistency fix Signed-off-by: chenyuheng <chenyuheng99@qq.com> * fix replacer to support empty state Signed-off-by: chenyuheng <chenyuheng99@qq.com> * add replacer test for empty status parameter Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: update unit test for 'extra' field Signed-off-by: chenyuheng <chenyuheng99@qq.com> * clean up code Signed-off-by: chenyuheng <chenyuheng99@qq.com> * gofmt fix & static analysis fix Signed-off-by: chenyuheng <chenyuheng99@qq.com> * dnstap: refactor Signed-off-by: chenyuheng <chenyuheng99@qq.com> --------- Signed-off-by: chenyuheng <chenyuheng99@qq.com>
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package dnstap
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
						|
	"github.com/coredns/coredns/request"
 | 
						|
 | 
						|
	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
 | 
						|
	ctx       context.Context
 | 
						|
	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)
 | 
						|
	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)
 | 
						|
	state := request.Request{W: w.ResponseWriter, Req: w.query}
 | 
						|
	w.TapMessageWithMetadata(w.ctx, r, state)
 | 
						|
	return nil
 | 
						|
}
 |