mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	Fix the except keyword usage - the config would allow it, but it was
not enforced in the code.
Turns out that **FROM** was also not enforced, fix both, by (basically)
copying the code from Caddy.
Update the README and tests.
Locally test as well, shows that this works:
~~~
.:1053 {
    proxy miek.nl 8.8.8.8:53 {
        except a.miek.nl
    }
    proxy a.miek.nl 8.8.4.4:53
    errors stdout
    log stdout
}
~~~
And gives the desired results, not having a proxy line for `a.miek.nl`
results in a SERVFAIL (as expected).
Fixes #502
		
	
		
			
				
	
	
		
			114 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package proxy
 | |
| 
 | |
| // functions other middleware might want to use to do lookup in the same style as the proxy.
 | |
| 
 | |
| import (
 | |
| 	"sync/atomic"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/miekg/coredns/request"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| // NewLookup create a new proxy with the hosts in host and a Random policy.
 | |
| func NewLookup(hosts []string) Proxy {
 | |
| 	p := Proxy{Next: nil}
 | |
| 
 | |
| 	upstream := &staticUpstream{
 | |
| 		from:        ".",
 | |
| 		Hosts:       make([]*UpstreamHost, len(hosts)),
 | |
| 		Policy:      &Random{},
 | |
| 		Spray:       nil,
 | |
| 		FailTimeout: 10 * time.Second,
 | |
| 		MaxFails:    3, // TODO(miek): disable error checking for simple lookups?
 | |
| 		ex:          newDNSEx(),
 | |
| 	}
 | |
| 
 | |
| 	for i, host := range hosts {
 | |
| 		uh := &UpstreamHost{
 | |
| 			Name:        host,
 | |
| 			Conns:       0,
 | |
| 			Fails:       0,
 | |
| 			FailTimeout: upstream.FailTimeout,
 | |
| 
 | |
| 			Unhealthy: false,
 | |
| 			CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
 | |
| 				return func(uh *UpstreamHost) bool {
 | |
| 					if uh.Unhealthy {
 | |
| 						return true
 | |
| 					}
 | |
| 					fails := atomic.LoadInt32(&uh.Fails)
 | |
| 					if fails >= upstream.MaxFails && upstream.MaxFails != 0 {
 | |
| 						return true
 | |
| 					}
 | |
| 					return false
 | |
| 				}
 | |
| 			}(upstream),
 | |
| 			WithoutPathPrefix: upstream.WithoutPathPrefix,
 | |
| 		}
 | |
| 		upstream.Hosts[i] = uh
 | |
| 	}
 | |
| 	p.Upstreams = &[]Upstream{upstream}
 | |
| 	return p
 | |
| }
 | |
| 
 | |
| // Lookup will use name and type to forge a new message and will send that upstream. It will
 | |
| // set any EDNS0 options correctly so that downstream will be able to process the reply.
 | |
| func (p Proxy) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
 | |
| 	req := new(dns.Msg)
 | |
| 	req.SetQuestion(name, typ)
 | |
| 	state.SizeAndDo(req)
 | |
| 
 | |
| 	state2 := request.Request{W: state.W, Req: req}
 | |
| 
 | |
| 	return p.lookup(state2)
 | |
| }
 | |
| 
 | |
| // Forward forward the request in state as-is. Unlike Lookup that adds EDNS0 suffix to the message.
 | |
| func (p Proxy) Forward(state request.Request) (*dns.Msg, error) {
 | |
| 	return p.lookup(state)
 | |
| }
 | |
| 
 | |
| func (p Proxy) lookup(state request.Request) (*dns.Msg, error) {
 | |
| 	upstream := p.match(state)
 | |
| 	if upstream == nil {
 | |
| 		return nil, errInvalidDomain
 | |
| 	}
 | |
| 	for {
 | |
| 		start := time.Now()
 | |
| 
 | |
| 		// Since Select() should give us "up" hosts, keep retrying
 | |
| 		// hosts until timeout (or until we get a nil host).
 | |
| 		for time.Now().Sub(start) < tryDuration {
 | |
| 			host := upstream.Select()
 | |
| 			if host == nil {
 | |
| 				return nil, errUnreachable
 | |
| 			}
 | |
| 
 | |
| 			// duplicated from proxy.go, but with a twist, we don't write the
 | |
| 			// reply back to the client, we return it and there is no monitoring.
 | |
| 
 | |
| 			atomic.AddInt64(&host.Conns, 1)
 | |
| 
 | |
| 			reply, backendErr := upstream.Exchanger().Exchange(host.Name, state)
 | |
| 
 | |
| 			atomic.AddInt64(&host.Conns, -1)
 | |
| 
 | |
| 			if backendErr == nil {
 | |
| 				return reply, nil
 | |
| 			}
 | |
| 			timeout := host.FailTimeout
 | |
| 			if timeout == 0 {
 | |
| 				timeout = 10 * time.Second
 | |
| 			}
 | |
| 			atomic.AddInt32(&host.Fails, 1)
 | |
| 			go func(host *UpstreamHost, timeout time.Duration) {
 | |
| 				time.Sleep(timeout)
 | |
| 				atomic.AddInt32(&host.Fails, -1)
 | |
| 			}(host, timeout)
 | |
| 		}
 | |
| 		return nil, errUnreachable
 | |
| 	}
 | |
| }
 |