| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | // functions other middleware might want to use to do lookup in the same style as the proxy.
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"sync/atomic"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-12 21:30:08 +01:00
										 |  |  | // New create a new proxy with the hosts in host and a Random policy.
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | func New(hosts []string) Proxy {
 | 
					
						
							| 
									
										
										
										
											2016-10-12 12:46:35 +01:00
										 |  |  | 	p := Proxy{Next: nil, Client: newClient()}
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	upstream := &staticUpstream{
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 		from:        "",
 | 
					
						
							|  |  |  | 		Hosts:       make([]*UpstreamHost, len(hosts)),
 | 
					
						
							|  |  |  | 		Policy:      &Random{},
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 		Spray:       nil,
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 		FailTimeout: 10 * time.Second,
 | 
					
						
							|  |  |  | 		MaxFails:    1,
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, host := range hosts {
 | 
					
						
							|  |  |  | 		uh := &UpstreamHost{
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 			Name:        host,
 | 
					
						
							|  |  |  | 			Conns:       0,
 | 
					
						
							|  |  |  | 			Fails:       0,
 | 
					
						
							|  |  |  | 			FailTimeout: upstream.FailTimeout,
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Unhealthy: false,
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 			CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
 | 
					
						
							|  |  |  | 				return func(uh *UpstreamHost) bool {
 | 
					
						
							|  |  |  | 					if uh.Unhealthy {
 | 
					
						
							|  |  |  | 						return true
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 					fails := atomic.LoadInt32(&uh.Fails)
 | 
					
						
							|  |  |  | 					if fails >= upstream.MaxFails && upstream.MaxFails != 0 {
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 						return true
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					return false
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}(upstream),
 | 
					
						
							|  |  |  | 			WithoutPathPrefix: upstream.WithoutPathPrefix,
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		upstream.Hosts[i] = uh
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	p.Upstreams = []Upstream{upstream}
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:22:23 +00:00
										 |  |  | 	return p
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | // Lookup will use name and type to forge a new message and will send that upstream. It will
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | // set any EDNS0 options correctly so that downstream will be able to process the reply.
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | func (p Proxy) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 	req := new(dns.Msg)
 | 
					
						
							| 
									
										
										
										
											2016-10-30 15:54:16 +00:00
										 |  |  | 	req.SetQuestion(name, typ)
 | 
					
						
							| 
									
										
										
										
											2016-04-09 16:17:53 +01:00
										 |  |  | 	state.SizeAndDo(req)
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 	return p.lookup(state, req)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | // Forward forward the request in state as-is. Unlike Lookup that adds EDNS0 suffix to the message.
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (p Proxy) Forward(state request.Request) (*dns.Msg, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-25 20:26:42 +00:00
										 |  |  | 	return p.lookup(state, state.Req)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (p Proxy) lookup(state request.Request, r *dns.Msg) (*dns.Msg, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 	for _, upstream := range p.Upstreams {
 | 
					
						
							|  |  |  | 		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()
 | 
					
						
							| 
									
										
										
										
											2016-11-10 12:58:40 +00:00
										 |  |  | 			println(host.Name)
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 			if host == nil {
 | 
					
						
							|  |  |  | 				return nil, errUnreachable
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 			// duplicated from proxy.go, but with a twist, we don't write the
 | 
					
						
							|  |  |  | 			// reply back to the client, we return it.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 			atomic.AddInt64(&host.Conns, 1)
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			reply, backendErr := p.Client.ServeDNS(state.W, r, host)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 			atomic.AddInt64(&host.Conns, -1)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 			if backendErr == nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:21:29 +00:00
										 |  |  | 				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
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil, errUnreachable
 | 
					
						
							|  |  |  | }
 |