| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Package plugin provides some types and functions common among plugin.
 | 
					
						
							|  |  |  | package plugin
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-12-01 11:15:05 +00:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type (
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Plugin is a middle layer which represents the traditional
 | 
					
						
							|  |  |  | 	// idea of plugin: it chains one Handler to the next by being
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// passed the next Handler in the chain.
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	Plugin func(Handler) Handler
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// 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
 | 
					
						
							| 
									
										
										
										
											2019-06-17 15:01:06 -04:00
										 |  |  | 	// code. CoreDNS assumes *no* reply has yet been written if the status
 | 
					
						
							|  |  |  | 	// code is 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)
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2019-06-17 15:01:06 -04:00
										 |  |  | 	// 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
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// so it can be logged by designated error-handling plugin.
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	// 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.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Otherwise, return values should be propagated down the plugin
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// 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" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Error returns err with 'plugin/name: ' prefixed to it.
 | 
					
						
							|  |  |  | func Error(name string, err error) error { return fmt.Errorf("%s/%s: %s", "plugin", name, err) }
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-05 14:59:24 +01:00
										 |  |  | // NextOrFailure calls next.ServeDNS when next is not nil, otherwise it will return, a ServerFailure and a nil error.
 | 
					
						
							| 
									
										
										
										
											2017-09-24 03:59:04 -07:00
										 |  |  | func NextOrFailure(name string, next Handler, ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { // nolint: golint
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | 	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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	return dns.RcodeServerFailure, Error(name, errors.New("no next plugin found"))
 | 
					
						
							| 
									
										
										
										
											2016-12-20 18:58:05 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 07:39:57 -07:00
										 |  |  | // ClientWrite returns true if the response has been written to the client.
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | // Each plugin to adhere to this protocol.
 | 
					
						
							| 
									
										
										
										
											2017-08-07 07:39:57 -07:00
										 |  |  | func ClientWrite(rcode int) bool {
 | 
					
						
							|  |  |  | 	switch rcode {
 | 
					
						
							|  |  |  | 	case dns.RcodeServerFailure:
 | 
					
						
							|  |  |  | 		fallthrough
 | 
					
						
							|  |  |  | 	case dns.RcodeRefused:
 | 
					
						
							|  |  |  | 		fallthrough
 | 
					
						
							|  |  |  | 	case dns.RcodeFormatError:
 | 
					
						
							|  |  |  | 		fallthrough
 | 
					
						
							|  |  |  | 	case dns.RcodeNotImplemented:
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-11-27 22:34:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TimeBuckets is based on Prometheus client_golang prometheus.DefBuckets
 | 
					
						
							| 
									
										
										
										
											2017-12-05 19:51:55 +01:00
										 |  |  | var TimeBuckets = prometheus.ExponentialBuckets(0.00025, 2, 16) // from 0.25ms to 8 seconds
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ErrOnce is returned when a plugin doesn't support multiple setups per server.
 | 
					
						
							|  |  |  | var ErrOnce = errors.New("this plugin can only be used once per Server Block")
 |