| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Package middleware provides some types and functions common among middleware.
 | 
					
						
							|  |  |  | package middleware
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 	"errors"
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 	ot "github.com/opentracing/opentracing-go"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type (
 | 
					
						
							|  |  |  | 	// Middleware is the middle layer which represents the traditional
 | 
					
						
							|  |  |  | 	// idea of middleware: it chains one Handler to the next by being
 | 
					
						
							|  |  |  | 	// passed the next Handler in the chain.
 | 
					
						
							|  |  |  | 	Middleware func(Handler) Handler
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Handler is like dns.Handler except ServeDNS may return an rcode
 | 
					
						
							|  |  |  | 	// and/or error.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// If ServeDNS writes to the response body, it should return a status
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:11 +00:00
										 |  |  | 	// code. If the status code is not one of the following:
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:11 +00:00
										 |  |  | 	// * SERVFAIL (dns.RcodeServerFailure)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:11 +00:00
										 |  |  | 	// * REFUSED (dns.RecodeRefused)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:11 +00:00
										 |  |  | 	// * FORMERR (dns.RcodeFormatError)
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-04-04 15:45:11 +00:00
										 |  |  | 	// * NOTIMP (dns.RcodeNotImplemented)
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// CoreDNS assumes *no* reply has yet been written. All other response
 | 
					
						
							|  |  |  | 	// codes signal other handlers above it that the response message is
 | 
					
						
							|  |  |  | 	// already written, and that they should not write to it also.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	// If ServeDNS encounters an error, it should return the error value
 | 
					
						
							|  |  |  | 	// so it can be logged by designated error-handling middleware.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// If writing a response after calling another ServeDNS method, the
 | 
					
						
							|  |  |  | 	// returned rcode SHOULD be used when writing the response.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// If handling errors after calling another ServeDNS method, the
 | 
					
						
							|  |  |  | 	// returned error value SHOULD be logged or handled accordingly.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// Otherwise, return values should be propagated down the middleware
 | 
					
						
							|  |  |  | 	// chain by returning them unchanged.
 | 
					
						
							|  |  |  | 	Handler interface {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 		ServeDNS(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 		Name() string
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// HandlerFunc is a convenience type like dns.HandlerFunc, except
 | 
					
						
							|  |  |  | 	// ServeDNS returns an rcode and an error. See Handler
 | 
					
						
							|  |  |  | 	// documentation for more information.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | 	HandlerFunc func(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServeDNS implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-03-19 07:18:57 +00:00
										 |  |  | func (f HandlerFunc) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	return f(ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (f HandlerFunc) Name() string { return "handlerfunc" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | // Error returns err with 'middleware/name: ' prefixed to it.
 | 
					
						
							|  |  |  | func Error(name string, err error) error { return fmt.Errorf("%s/%s: %s", "middleware", name, err) }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | // NextOrFailure calls next.ServeDNS when next is not nill, otherwise it will return, a ServerFailure
 | 
					
						
							|  |  |  | // and a nil error.
 | 
					
						
							|  |  |  | func NextOrFailure(name string, next Handler, ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 	if next != nil {
 | 
					
						
							| 
									
										
										
										
											2017-01-23 15:40:47 -05:00
										 |  |  | 		if span := ot.SpanFromContext(ctx); span != nil {
 | 
					
						
							|  |  |  | 			child := span.Tracer().StartSpan(next.Name(), ot.ChildOf(span.Context()))
 | 
					
						
							|  |  |  | 			defer child.Finish()
 | 
					
						
							|  |  |  | 			ctx = ot.ContextWithSpan(ctx, child)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 		return next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dns.RcodeServerFailure, Error(name, errors.New("no next middleware found"))
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | // Namespace is the namespace used for the metrics.
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | const Namespace = "coredns"
 |