mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	middleware/proxy: dnstap (#786)
* experimental dnstap support into proxy * proxy reports dnstap errors * refactoring * add a message builder for less dnstap code * msg lint * context * proxy by DNS: dnstap comments * TapBuilder * resolves conflict * dnstap into ServeDNS * testing * more tests * `go lint` * doc update
This commit is contained in:
		| @@ -4,22 +4,40 @@ import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
|  | ||||
| 	"golang.org/x/net/context" | ||||
|  | ||||
| 	"github.com/coredns/coredns/middleware" | ||||
| 	"github.com/coredns/coredns/middleware/dnstap/msg" | ||||
| 	"github.com/coredns/coredns/middleware/dnstap/taprw" | ||||
|  | ||||
| 	tap "github.com/dnstap/golang-dnstap" | ||||
| 	"github.com/miekg/dns" | ||||
| 	"golang.org/x/net/context" | ||||
| ) | ||||
|  | ||||
| // Dnstap is the dnstap handler. | ||||
| type Dnstap struct { | ||||
| 	Next middleware.Handler | ||||
| 	Out  io.Writer | ||||
| 	Pack bool | ||||
| } | ||||
|  | ||||
| type ( | ||||
| 	// Tapper is implemented by the Context passed by the dnstap handler. | ||||
| 	Tapper interface { | ||||
| 		TapMessage(*tap.Message) error | ||||
| 		TapBuilder() msg.Builder | ||||
| 	} | ||||
| 	tapContext struct { | ||||
| 		context.Context | ||||
| 		Dnstap | ||||
| 	} | ||||
| ) | ||||
|  | ||||
| // TapperFromContext will return a Tapper if the dnstap middleware is enabled. | ||||
| func TapperFromContext(ctx context.Context) (t Tapper) { | ||||
| 	t, _ = ctx.(Tapper) | ||||
| 	return | ||||
| } | ||||
|  | ||||
| func tapMessageTo(w io.Writer, m *tap.Message) error { | ||||
| 	frame, err := msg.Marshal(m) | ||||
| 	if err != nil { | ||||
| @@ -29,15 +47,22 @@ func tapMessageTo(w io.Writer, m *tap.Message) error { | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // TapMessage implements Tapper. | ||||
| func (h Dnstap) TapMessage(m *tap.Message) error { | ||||
| 	return tapMessageTo(h.Out, m) | ||||
| } | ||||
|  | ||||
| // TapBuilder implements Tapper. | ||||
| func (h Dnstap) TapBuilder() msg.Builder { | ||||
| 	return msg.Builder{Full: h.Pack} | ||||
| } | ||||
|  | ||||
| // 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 := &taprw.ResponseWriter{ResponseWriter: w, Taper: &h, Query: r, Pack: h.Pack} | ||||
| 	rw := &taprw.ResponseWriter{ResponseWriter: w, Tapper: &h, Query: r} | ||||
| 	rw.QueryEpoch() | ||||
|  | ||||
| 	code, err := middleware.NextOrFailure(h.Name(), h.Next, ctx, rw, r) | ||||
| 	code, err := middleware.NextOrFailure(h.Name(), h.Next, tapContext{ctx, h}, rw, r) | ||||
| 	if err != nil { | ||||
| 		// ignore dnstap errors | ||||
| 		return code, err | ||||
| @@ -49,4 +74,6 @@ func (h Dnstap) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) | ||||
|  | ||||
| 	return code, nil | ||||
| } | ||||
|  | ||||
| // Name returns dnstap. | ||||
| func (h Dnstap) Name() string { return "dnstap" } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user