| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | // Package forward implements a forwarding proxy. It caches an upstream net.Conn for some time, so if the same
 | 
					
						
							|  |  |  | // client returns the upstream's Conn will be precached. Depending on how you benchmark this looks to be
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:55:55 +02:00
										 |  |  | // 50% faster than just opening a new connection for every client. It works with UDP and TCP and uses
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | // inband healthchecking.
 | 
					
						
							|  |  |  | package forward
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							|  |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:17 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/debug"
 | 
					
						
							| 
									
										
										
										
											2018-10-09 22:50:30 +03:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	ot "github.com/opentracing/opentracing-go"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 22:50:30 +03:00
										 |  |  | var log = clog.NewWithPlugin("forward")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | // Forward represents a plugin instance that can proxy requests to another (DNS) server. It has a list
 | 
					
						
							|  |  |  | // of proxies each representing one upstream proxy.
 | 
					
						
							|  |  |  | type Forward struct {
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	proxies    []*Proxy
 | 
					
						
							|  |  |  | 	p          Policy
 | 
					
						
							|  |  |  | 	hcInterval time.Duration
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	from    string
 | 
					
						
							|  |  |  | 	ignored []string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tlsConfig     *tls.Config
 | 
					
						
							|  |  |  | 	tlsServerName string
 | 
					
						
							|  |  |  | 	maxfails      uint32
 | 
					
						
							|  |  |  | 	expire        time.Duration
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	opts options // also here for testing
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Next plugin.Handler
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // New returns a new Forward.
 | 
					
						
							|  |  |  | func New() *Forward {
 | 
					
						
							| 
									
										
										
										
											2018-06-21 12:40:19 +02:00
										 |  |  | 	f := &Forward{maxfails: 2, tlsConfig: new(tls.Config), expire: defaultExpire, p: new(random), from: ".", hcInterval: hcInterval}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	return f
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetProxy appends p to the proxy list and starts healthchecking.
 | 
					
						
							|  |  |  | func (f *Forward) SetProxy(p *Proxy) {
 | 
					
						
							|  |  |  | 	f.proxies = append(f.proxies, p)
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	p.start(f.hcInterval)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Len returns the number of configured proxies.
 | 
					
						
							|  |  |  | func (f *Forward) Len() int { return len(f.proxies) }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Name implements plugin.Handler.
 | 
					
						
							|  |  |  | func (f *Forward) Name() string { return "forward" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServeDNS implements plugin.Handler.
 | 
					
						
							|  |  |  | func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state := request.Request{W: w, Req: r}
 | 
					
						
							|  |  |  | 	if !f.match(state) {
 | 
					
						
							|  |  |  | 		return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fails := 0
 | 
					
						
							|  |  |  | 	var span, child ot.Span
 | 
					
						
							| 
									
										
										
										
											2018-04-01 14:23:40 +01:00
										 |  |  | 	var upstreamErr error
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	span = ot.SpanFromContext(ctx)
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	i := 0
 | 
					
						
							| 
									
										
										
										
											2018-05-04 08:47:26 +03:00
										 |  |  | 	list := f.List()
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	deadline := time.Now().Add(defaultTimeout)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 	start := time.Now()
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 	for time.Now().Before(deadline) {
 | 
					
						
							|  |  |  | 		if i >= len(list) {
 | 
					
						
							|  |  |  | 			// reached the end of list, reset to begin
 | 
					
						
							|  |  |  | 			i = 0
 | 
					
						
							|  |  |  | 			fails = 0
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 		proxy := list[i]
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		if proxy.Down(f.maxfails) {
 | 
					
						
							|  |  |  | 			fails++
 | 
					
						
							|  |  |  | 			if fails < len(f.proxies) {
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2019-08-22 22:35:55 +08:00
										 |  |  | 			// All upstream proxies are dead, assume healthcheck is completely broken and randomly
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			// select an upstream to connect to.
 | 
					
						
							|  |  |  | 			r := new(random)
 | 
					
						
							|  |  |  | 			proxy = r.List(f.proxies)[0]
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			HealthcheckBrokenCount.Add(1)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if span != nil {
 | 
					
						
							|  |  |  | 			child = span.Tracer().StartSpan("connect", ot.ChildOf(span.Context()))
 | 
					
						
							|  |  |  | 			ctx = ot.ContextWithSpan(ctx, child)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:18:21 +01:00
										 |  |  | 		var (
 | 
					
						
							|  |  |  | 			ret *dns.Msg
 | 
					
						
							|  |  |  | 			err error
 | 
					
						
							|  |  |  | 		)
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 		opts := f.opts
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:18:21 +01:00
										 |  |  | 		for {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 			ret, err = proxy.Connect(ctx, state, opts)
 | 
					
						
							|  |  |  | 			if err == ErrCachedClosed { // Remote side closed conn, can only happen with TCP.
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 			// Retry with TCP if truncated and prefer_udp configured.
 | 
					
						
							| 
									
										
										
										
											2019-08-17 00:34:12 +08:00
										 |  |  | 			if ret != nil && ret.Truncated && !opts.forceTCP && opts.preferUDP {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 				opts.forceTCP = true
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:18:21 +01:00
										 |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if child != nil {
 | 
					
						
							|  |  |  | 			child.Finish()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		taperr := toDnstap(ctx, proxy.addr, f, state, ret, start)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 14:23:40 +01:00
										 |  |  | 		upstreamErr = err
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		if err != nil {
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 			// Kick off health check to see if *our* upstream is broken.
 | 
					
						
							| 
									
										
										
										
											2018-04-26 09:34:58 +01:00
										 |  |  | 			if f.maxfails != 0 {
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 				proxy.Healthcheck()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			if fails < len(f.proxies) {
 | 
					
						
							|  |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		// Check if the reply is correct; if not return FormErr.
 | 
					
						
							|  |  |  | 		if !state.Match(ret) {
 | 
					
						
							| 
									
										
										
										
											2019-02-18 10:12:14 +03:00
										 |  |  | 			debug.Hexdumpf(ret, "Wrong reply for id: %d, %s %d", ret.Id, state.QName(), state.QType())
 | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-24 13:26:15 +00:00
										 |  |  | 			formerr := new(dns.Msg)
 | 
					
						
							|  |  |  | 			formerr.SetRcode(state.Req, dns.RcodeFormatError)
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 			w.WriteMsg(formerr)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 			return 0, taperr
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2019-03-23 05:45:21 -04:00
										 |  |  | 		return 0, taperr
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 14:23:40 +01:00
										 |  |  | 	if upstreamErr != nil {
 | 
					
						
							|  |  |  | 		return dns.RcodeServerFailure, upstreamErr
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 	return dns.RcodeServerFailure, ErrNoHealthy
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *Forward) match(state request.Request) bool {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 	if !plugin.Name(f.from).Matches(state.Name()) || !f.isAllowedDomain(state.Name()) {
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f *Forward) isAllowedDomain(name string) bool {
 | 
					
						
							|  |  |  | 	if dns.Name(name) == dns.Name(f.from) {
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, ignore := range f.ignored {
 | 
					
						
							|  |  |  | 		if plugin.Name(ignore).Matches(name) {
 | 
					
						
							|  |  |  | 			return false
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return true
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-04 08:47:26 +03:00
										 |  |  | // ForceTCP returns if TCP is forced to be used even when the request comes in over UDP.
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | func (f *Forward) ForceTCP() bool { return f.opts.forceTCP }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PreferUDP returns if UDP is preferred to be used even when the request comes in over TCP.
 | 
					
						
							|  |  |  | func (f *Forward) PreferUDP() bool { return f.opts.preferUDP }
 | 
					
						
							| 
									
										
										
										
											2018-05-04 08:47:26 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | // List returns a set of proxies to be used for this client depending on the policy in f.
 | 
					
						
							| 
									
										
										
										
											2018-05-04 08:47:26 +03:00
										 |  |  | func (f *Forward) List() []*Proxy { return f.p.List(f.proxies) }
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 	// ErrNoHealthy means no healthy proxies left.
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 	ErrNoHealthy = errors.New("no healthy proxies")
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 	// ErrNoForward means no forwarder defined.
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 	ErrNoForward = errors.New("no forwarder defined")
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 	// ErrCachedClosed means cached connection was closed by peer.
 | 
					
						
							| 
									
										
										
										
											2018-05-09 14:41:14 +03:00
										 |  |  | 	ErrCachedClosed = errors.New("cached connection was closed by peer")
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // policy tells forward what policy for selecting upstream it uses.
 | 
					
						
							|  |  |  | type policy int
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	randomPolicy policy = iota
 | 
					
						
							|  |  |  | 	roundRobinPolicy
 | 
					
						
							| 
									
										
										
										
											2018-04-20 01:07:58 -05:00
										 |  |  | 	sequentialPolicy
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | )
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | // options holds various options that can be set.
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | type options struct {
 | 
					
						
							|  |  |  | 	forceTCP  bool
 | 
					
						
							|  |  |  | 	preferUDP bool
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | const defaultTimeout = 5 * time.Second
 |