| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2020-02-04 07:59:08 -05:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-07-04 07:54:17 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/debug"
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap"
 | 
					
						
							| 
									
										
										
										
											2021-03-16 08:34:22 -04:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metadata"
 | 
					
						
							| 
									
										
										
										
											2018-10-09 22:50:30 +03:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | 	proxyPkg "github.com/coredns/coredns/plugin/pkg/proxy"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 	ot "github.com/opentracing/opentracing-go"
 | 
					
						
							| 
									
										
										
										
											2021-07-16 14:35:39 +02:00
										 |  |  | 	otext "github.com/opentracing/opentracing-go/ext"
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 22:50:30 +03:00
										 |  |  | var log = clog.NewWithPlugin("forward")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | const (
 | 
					
						
							|  |  |  | 	defaultExpire = 10 * time.Second
 | 
					
						
							|  |  |  | 	hcInterval    = 500 * time.Millisecond
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2020-02-04 07:59:08 -05:00
										 |  |  | 	concurrent int64 // atomic counters need to be first in struct for proper alignment
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | 	proxies    []*proxyPkg.Proxy
 | 
					
						
							| 
									
										
										
										
											2020-05-29 12:30:26 -04:00
										 |  |  | 	p          Policy
 | 
					
						
							| 
									
										
										
										
											2018-02-15 10:21:57 +01:00
										 |  |  | 	hcInterval time.Duration
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	from    string
 | 
					
						
							|  |  |  | 	ignored []string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 17:20:12 +02:00
										 |  |  | 	nextAlternateRcodes []int
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-07 08:37:25 -08:00
										 |  |  | 	tlsConfig                  *tls.Config
 | 
					
						
							|  |  |  | 	tlsServerName              string
 | 
					
						
							|  |  |  | 	maxfails                   uint32
 | 
					
						
							|  |  |  | 	expire                     time.Duration
 | 
					
						
							|  |  |  | 	maxConcurrent              int64
 | 
					
						
							|  |  |  | 	failfastUnhealthyUpstreams bool
 | 
					
						
							| 
									
										
										
										
											2025-09-13 05:45:01 +08:00
										 |  |  | 	failoverRcodes             []int
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | 	opts proxyPkg.Options // also here for testing
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-04 07:59:08 -05:00
										 |  |  | 	// ErrLimitExceeded indicates that a query was rejected because the number of concurrent queries has exceeded
 | 
					
						
							|  |  |  | 	// the maximum allowed (maxConcurrent)
 | 
					
						
							|  |  |  | 	ErrLimitExceeded error
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	tapPlugins []*dnstap.Dnstap // when dnstap plugins are loaded, we use to this to send messages out.
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	Next plugin.Handler
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // New returns a new Forward.
 | 
					
						
							|  |  |  | func New() *Forward {
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | 	f := &Forward{maxfails: 2, tlsConfig: new(tls.Config), expire: defaultExpire, p: new(random), from: ".", hcInterval: hcInterval, opts: proxyPkg.Options{ForceTCP: false, PreferUDP: false, HCRecursionDesired: true, HCDomain: "."}}
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	return f
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetProxy appends p to the proxy list and starts healthchecking.
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | func (f *Forward) SetProxy(p *proxyPkg.Proxy) {
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	f.proxies = append(f.proxies, p)
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 	p.Start(f.hcInterval)
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-04 23:27:47 +07:00
										 |  |  | // SetProxyOptions setup proxy options
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | func (f *Forward) SetProxyOptions(opts proxyPkg.Options) {
 | 
					
						
							| 
									
										
										
										
											2025-04-04 23:27:47 +07:00
										 |  |  | 	f.opts = opts
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-31 03:38:15 +08:00
										 |  |  | // SetTapPlugin appends one or more dnstap plugins to the tap plugin list.
 | 
					
						
							|  |  |  | func (f *Forward) SetTapPlugin(tapPlugin *dnstap.Dnstap) {
 | 
					
						
							|  |  |  | 	f.tapPlugins = append(f.tapPlugins, tapPlugin)
 | 
					
						
							|  |  |  | 	if nextPlugin, ok := tapPlugin.Next.(*dnstap.Dnstap); ok {
 | 
					
						
							|  |  |  | 		f.SetTapPlugin(nextPlugin)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-04 07:59:08 -05:00
										 |  |  | 	if f.maxConcurrent > 0 {
 | 
					
						
							|  |  |  | 		count := atomic.AddInt64(&(f.concurrent), 1)
 | 
					
						
							|  |  |  | 		defer atomic.AddInt64(&(f.concurrent), -1)
 | 
					
						
							|  |  |  | 		if count > f.maxConcurrent {
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 			maxConcurrentRejectCount.Add(1)
 | 
					
						
							| 
									
										
										
										
											2020-12-15 08:02:15 -05:00
										 |  |  | 			return dns.RcodeRefused, f.ErrLimitExceeded
 | 
					
						
							| 
									
										
										
										
											2020-02-04 07:59:08 -05:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 	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()
 | 
					
						
							| 
									
										
										
										
											2024-03-07 21:54:28 +02:00
										 |  |  | 	for time.Now().Before(deadline) && ctx.Err() == nil {
 | 
					
						
							| 
									
										
										
										
											2018-04-11 09:53:08 +03:00
										 |  |  | 		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
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2025-03-07 08:37:25 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			healthcheckBrokenCount.Add(1)
 | 
					
						
							|  |  |  | 			// All upstreams are dead, return servfail if all upstreams are down
 | 
					
						
							|  |  |  | 			if f.failfastUnhealthyUpstreams {
 | 
					
						
							|  |  |  | 				break
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			// assume healthcheck is completely broken and randomly
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			// select an upstream to connect to.
 | 
					
						
							| 
									
										
										
										
											2020-05-29 12:30:26 -04:00
										 |  |  | 			r := new(random)
 | 
					
						
							|  |  |  | 			proxy = r.List(f.proxies)[0]
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if span != nil {
 | 
					
						
							|  |  |  | 			child = span.Tracer().StartSpan("connect", ot.ChildOf(span.Context()))
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 			otext.PeerAddress.Set(child, proxy.Addr())
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 			ctx = ot.ContextWithSpan(ctx, child)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 08:34:22 -04:00
										 |  |  | 		metadata.SetValueFunc(ctx, "forward/upstream", func() string {
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 			return proxy.Addr()
 | 
					
						
							| 
									
										
										
										
											2021-03-16 08:34:22 -04:00
										 |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 16:18:21 +01:00
										 |  |  | 		for {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 			ret, err = proxy.Connect(ctx, state, opts)
 | 
					
						
							| 
									
										
										
										
											2023-07-04 15:35:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | 			if err == proxyPkg.ErrCachedClosed { // Remote side closed conn, can only happen with TCP.
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 				continue
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2018-07-07 14:38:05 +01:00
										 |  |  | 			// Retry with TCP if truncated and prefer_udp configured.
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 			if ret != nil && ret.Truncated && !opts.ForceTCP && opts.PreferUDP {
 | 
					
						
							|  |  |  | 				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()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		if len(f.tapPlugins) != 0 {
 | 
					
						
							| 
									
										
										
										
											2023-08-14 14:01:13 -04:00
										 |  |  | 			toDnstap(ctx, f, proxy.Addr(), state, opts, ret, start)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											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)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 			return 0, nil
 | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 05:45:01 +08:00
										 |  |  | 		// Check if we have a failover Rcode defined, check if we match on the code
 | 
					
						
							|  |  |  | 		tryNext := false
 | 
					
						
							|  |  |  | 		for _, failoverRcode := range f.failoverRcodes {
 | 
					
						
							|  |  |  | 			// if we match, we continue to the next upstream in the list
 | 
					
						
							|  |  |  | 			if failoverRcode == ret.Rcode {
 | 
					
						
							|  |  |  | 				if fails < len(f.proxies) {
 | 
					
						
							|  |  |  | 					tryNext = true
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if tryNext {
 | 
					
						
							|  |  |  | 			fails++
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-01 17:20:12 +02:00
										 |  |  | 		// Check if we have an alternate Rcode defined, check if we match on the code
 | 
					
						
							|  |  |  | 		for _, alternateRcode := range f.nextAlternateRcodes {
 | 
					
						
							|  |  |  | 			if alternateRcode == ret.Rcode && f.Next != nil { // In case we do not have a Next handler, just continue normally
 | 
					
						
							|  |  |  | 				if _, ok := f.Next.(*Forward); ok { // Only continue if the next forwarder is also a Forworder
 | 
					
						
							|  |  |  | 					return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:00:47 +00:00
										 |  |  | 		w.WriteMsg(ret)
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		return 0, nil
 | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | func (f *Forward) ForceTCP() bool { return f.opts.ForceTCP }
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | // PreferUDP returns if UDP is preferred to be used even when the request comes in over TCP.
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | 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.
 | 
					
						
							| 
									
										
										
										
											2025-07-29 00:36:26 +05:30
										 |  |  | func (f *Forward) List() []*proxyPkg.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
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 12:55:51 +00:00
										 |  |  | // Options holds various Options that can be set.
 | 
					
						
							|  |  |  | type Options struct {
 | 
					
						
							|  |  |  | 	// ForceTCP use TCP protocol for upstream DNS request. Has precedence over PreferUDP flag
 | 
					
						
							|  |  |  | 	ForceTCP bool
 | 
					
						
							|  |  |  | 	// PreferUDP use UDP protocol for upstream DNS request.
 | 
					
						
							|  |  |  | 	PreferUDP bool
 | 
					
						
							|  |  |  | 	// HCRecursionDesired sets recursion desired flag for Proxy healthcheck requests
 | 
					
						
							|  |  |  | 	HCRecursionDesired bool
 | 
					
						
							|  |  |  | 	// HCDomain sets domain for Proxy healthcheck requests
 | 
					
						
							|  |  |  | 	HCDomain string
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
											  
											
												Speed up testing (#4239)
* Speed up testing
* make notification run in the background, this recudes the test_readme
time from 18s to 0.10s
* reduce time for zone reload
* TestServeDNSConcurrent remove entirely. This took a whopping 58s for
  ... ? A few minutes staring didn't reveal wth it is actually testing.
  Making values smaller revealed race conditions in the tests. Remove
  entirely.
* Move many interval values to variables so we can reset them to short
  values for the tests.
* test_large_axfr: make the zone smaller. The number used 64K has no
  rational, make it 64/10 to speed up.
* TestProxyThreeWay: use client with shorter timeout
A few random tidbits in other tests.
Total time saved: 177s (almost 3m) - which makes it worthwhile again to
run the test locally:
this branch:
~~~
ok  	github.com/coredns/coredns/test	10.437s
cd plugin; time go t ./...
5,51s user 7,51s system 11,15s elapsed 744%CPU (
~~~
master:
~~~
ok  	github.com/coredns/coredns/test	35.252s
cd plugin; time go t ./...
157,64s user 15,39s system 50,05s elapsed 345%CPU ()
~~~
tests/ -25s
plugins/ -40s
This brings the total on 20s, and another 10s can be saved by fixing
dnstapio. Moving this to 5s would be even better, but 10s is also nice.
Signed-off-by: Miek Gieben <miek@miek.nl>
* Also 0.01
Signed-off-by: Miek Gieben <miek@miek.nl>
											
										 
											2020-10-30 10:27:04 +01:00
										 |  |  | var defaultTimeout = 5 * time.Second
 |