| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/dnstap/msg"
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	dns.ResponseWriter
 | 
					
						
							|  |  |  | 	Dnstap
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							|  |  |  | 	w.TapMessage(r)
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |