| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"io"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 	"log"
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"net/http"
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 	"net/url"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | 	"sync"
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/dnsutil"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/pkg/tls"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	"github.com/mholt/caddy/caddyfile"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	supportedPolicies = make(map[string]func() Policy)
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type staticUpstream struct {
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | 	from string
 | 
					
						
							|  |  |  | 	stop chan struct{}  // Signals running goroutines to stop.
 | 
					
						
							|  |  |  | 	wg   sync.WaitGroup // Used to wait for running goroutines to stop.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	Hosts  HostPool
 | 
					
						
							|  |  |  | 	Policy Policy
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 	Spray  Policy
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	FailTimeout time.Duration
 | 
					
						
							|  |  |  | 	MaxFails    int32
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 	Future      time.Duration
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	HealthCheck struct {
 | 
					
						
							|  |  |  | 		Path     string
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		Port     string
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		Interval time.Duration
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	WithoutPathPrefix string
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	IgnoredSubDomains []string
 | 
					
						
							| 
									
										
										
										
											2017-02-06 19:32:48 +00:00
										 |  |  | 	ex                Exchanger
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewStaticUpstreams parses the configuration input and sets up
 | 
					
						
							|  |  |  | // static upstreams for the proxy middleware.
 | 
					
						
							| 
									
										
										
										
											2016-09-16 23:49:35 -07:00
										 |  |  | func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	var upstreams []Upstream
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		upstream := &staticUpstream{
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:01:16 +00:00
										 |  |  | 			from:        ".",
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | 			stop:        make(chan struct{}),
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 			Hosts:       nil,
 | 
					
						
							|  |  |  | 			Policy:      &Random{},
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 			Spray:       nil,
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 			FailTimeout: 10 * time.Second,
 | 
					
						
							|  |  |  | 			MaxFails:    1,
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 			Future:      60 * time.Second,
 | 
					
						
							| 
									
										
										
										
											2017-02-06 19:32:48 +00:00
										 |  |  | 			ex:          newDNSEx(),
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if !c.Args(&upstream.from) {
 | 
					
						
							|  |  |  | 			return upstreams, c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		to := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(to) == 0 {
 | 
					
						
							|  |  |  | 			return upstreams, c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:52:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// process the host list, substituting in any nameservers in files
 | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 		toHosts, err := dnsutil.ParseHostPortOrFile(to...)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return upstreams, err
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			if err := parseBlock(c, upstream); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				return upstreams, err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-22 10:52:10 -04:00
										 |  |  | 		upstream.Hosts = make([]*UpstreamHost, len(toHosts))
 | 
					
						
							|  |  |  | 		for i, host := range toHosts {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			uh := &UpstreamHost{
 | 
					
						
							| 
									
										
										
										
											2016-11-24 16:57:20 +01:00
										 |  |  | 				Name:        host,
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 				Conns:       0,
 | 
					
						
							|  |  |  | 				Fails:       0,
 | 
					
						
							|  |  |  | 				FailTimeout: upstream.FailTimeout,
 | 
					
						
							| 
									
										
										
										
											2016-10-08 14:46:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 				CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
 | 
					
						
							|  |  |  | 					return func(uh *UpstreamHost) bool {
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						down := false
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 20:37:43 +01:00
										 |  |  | 						uh.checkMu.Lock()
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 						until := uh.OkUntil
 | 
					
						
							|  |  |  | 						uh.checkMu.Unlock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if !until.IsZero() && time.Now().After(until) {
 | 
					
						
							|  |  |  | 							down = true
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 						}
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						fails := atomic.LoadInt32(&uh.Fails)
 | 
					
						
							|  |  |  | 						if fails >= upstream.MaxFails && upstream.MaxFails != 0 {
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 							down = true
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 						}
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 						return down
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 					}
 | 
					
						
							|  |  |  | 				}(upstream),
 | 
					
						
							|  |  |  | 				WithoutPathPrefix: upstream.WithoutPathPrefix,
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			upstream.Hosts[i] = uh
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if upstream.HealthCheck.Path != "" {
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | 			upstream.wg.Add(1)
 | 
					
						
							|  |  |  | 			go func() {
 | 
					
						
							|  |  |  | 				defer upstream.wg.Done()
 | 
					
						
							|  |  |  | 				upstream.HealthCheckWorker(upstream.stop)
 | 
					
						
							|  |  |  | 			}()
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		upstreams = append(upstreams, upstream)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return upstreams, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | // Stop sends a signal to all goroutines started by this staticUpstream to exit
 | 
					
						
							|  |  |  | // and waits for them to finish before returning.
 | 
					
						
							|  |  |  | func (u *staticUpstream) Stop() error {
 | 
					
						
							|  |  |  | 	close(u.stop)
 | 
					
						
							|  |  |  | 	u.wg.Wait()
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // RegisterPolicy adds a custom policy to the proxy.
 | 
					
						
							|  |  |  | func RegisterPolicy(name string, policy func() Policy) {
 | 
					
						
							|  |  |  | 	supportedPolicies[name] = policy
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) From() string {
 | 
					
						
							|  |  |  | 	return u.from
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-16 23:49:35 -07:00
										 |  |  | func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	switch c.Val() {
 | 
					
						
							|  |  |  | 	case "policy":
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		policyCreateFunc, ok := supportedPolicies[c.Val()]
 | 
					
						
							|  |  |  | 		if !ok {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		u.Policy = policyCreateFunc()
 | 
					
						
							|  |  |  | 	case "fail_timeout":
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		dur, err := time.ParseDuration(c.Val())
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		u.FailTimeout = dur
 | 
					
						
							|  |  |  | 	case "max_fails":
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		n, err := strconv.Atoi(c.Val())
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		u.MaxFails = int32(n)
 | 
					
						
							|  |  |  | 	case "health_check":
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		var err error
 | 
					
						
							|  |  |  | 		u.HealthCheck.Path, u.HealthCheck.Port, err = net.SplitHostPort(c.Val())
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return err
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		u.HealthCheck.Interval = 30 * time.Second
 | 
					
						
							|  |  |  | 		if c.NextArg() {
 | 
					
						
							|  |  |  | 			dur, err := time.ParseDuration(c.Val())
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			u.HealthCheck.Interval = dur
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 			u.Future = 2 * dur
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// set a minimum of 3 seconds
 | 
					
						
							|  |  |  | 			if u.Future < (3 * time.Second) {
 | 
					
						
							|  |  |  | 				u.Future = 3 * time.Second
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	case "without":
 | 
					
						
							|  |  |  | 		if !c.NextArg() {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		u.WithoutPathPrefix = c.Val()
 | 
					
						
							|  |  |  | 	case "except":
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 		ignoredDomains := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(ignoredDomains) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 		for i := 0; i < len(ignoredDomains); i++ {
 | 
					
						
							|  |  |  | 			ignoredDomains[i] = strings.ToLower(dns.Fqdn(ignoredDomains[i]))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		u.IgnoredSubDomains = ignoredDomains
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 	case "spray":
 | 
					
						
							|  |  |  | 		u.Spray = &Spray{}
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 	case "protocol":
 | 
					
						
							|  |  |  | 		encArgs := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(encArgs) == 0 {
 | 
					
						
							|  |  |  | 			return c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		switch encArgs[0] {
 | 
					
						
							|  |  |  | 		case "dns":
 | 
					
						
							| 
									
										
										
										
											2017-03-14 21:32:21 +00:00
										 |  |  | 			if len(encArgs) > 1 {
 | 
					
						
							|  |  |  | 				if encArgs[1] == "force_tcp" {
 | 
					
						
							|  |  |  | 					opts := Options{ForceTCP: true}
 | 
					
						
							|  |  |  | 					u.ex = newDNSExWithOption(opts)
 | 
					
						
							|  |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return fmt.Errorf("only force_tcp allowed as parameter to dns")
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			} else {
 | 
					
						
							|  |  |  | 				u.ex = newDNSEx()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 		case "https_google":
 | 
					
						
							| 
									
										
										
										
											2017-02-06 19:32:48 +00:00
										 |  |  | 			boot := []string{"8.8.8.8:53", "8.8.4.4:53"}
 | 
					
						
							|  |  |  | 			if len(encArgs) > 2 && encArgs[1] == "bootstrap" {
 | 
					
						
							|  |  |  | 				boot = encArgs[2:]
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			u.ex = newGoogle("", boot) // "" for default in google.go
 | 
					
						
							| 
									
										
										
										
											2017-02-14 22:20:20 -05:00
										 |  |  | 		case "grpc":
 | 
					
						
							|  |  |  | 			if len(encArgs) == 2 && encArgs[1] == "insecure" {
 | 
					
						
							|  |  |  | 				u.ex = newGrpcClient(nil, u)
 | 
					
						
							|  |  |  | 				return nil
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			tls, err := tls.NewTLSConfigFromArgs(encArgs[1:]...)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			u.ex = newGrpcClient(tls, u)
 | 
					
						
							| 
									
										
										
										
											2017-01-15 08:12:58 +00:00
										 |  |  | 		default:
 | 
					
						
							|  |  |  | 			return fmt.Errorf("%s: %s", errInvalidProtocol, encArgs[0])
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	default:
 | 
					
						
							|  |  |  | 		return c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | // This was moved into a thread so that each host could throw a health
 | 
					
						
							|  |  |  | // check at the same time.  The reason for this is that if we are checking
 | 
					
						
							|  |  |  | // 3 hosts, and the first one is gone, and we spend minutes timing out to
 | 
					
						
							|  |  |  | // fail it, we would not have been doing any other health checks in that
 | 
					
						
							|  |  |  | // time.  So we now have a per-host lock and a threaded health check.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // We use the Checking bool to avoid concurrent checks against the same
 | 
					
						
							|  |  |  | // host; if one is taking a long time, the next one will find a check in
 | 
					
						
							|  |  |  | // progress and simply return before trying.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // We are carefully avoiding having the mutex locked while we check,
 | 
					
						
							|  |  |  | // otherwise checks will back up, potentially a lot of them if a host is
 | 
					
						
							|  |  |  | // absent for a long time.  This arrangement makes checks quickly see if
 | 
					
						
							|  |  |  | // they are the only one running and abort otherwise.
 | 
					
						
							|  |  |  | func healthCheckUrl(nextTs time.Time, host *UpstreamHost) {
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 	// lock for our bool check.  We don't just defer the unlock because
 | 
					
						
							|  |  |  | 	// we don't want the lock held while http.Get runs
 | 
					
						
							|  |  |  | 	host.checkMu.Lock()
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 	// are we mid check?  Don't run another one
 | 
					
						
							|  |  |  | 	if host.Checking {
 | 
					
						
							|  |  |  | 		host.checkMu.Unlock()
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	host.Checking = true
 | 
					
						
							|  |  |  | 	host.checkMu.Unlock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//log.Printf("[DEBUG] Healthchecking %s, nextTs is %s\n", url, nextTs.Local())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// fetch that url.  This has been moved into a go func because
 | 
					
						
							|  |  |  | 	// when the remote host is not merely not serving, but actually
 | 
					
						
							|  |  |  | 	// absent, then tcp syn timeouts can be very long, and so one
 | 
					
						
							|  |  |  | 	// fetch could last several check intervals
 | 
					
						
							|  |  |  | 	if r, err := http.Get(host.CheckUrl); err == nil {
 | 
					
						
							|  |  |  | 		io.Copy(ioutil.Discard, r.Body)
 | 
					
						
							|  |  |  | 		r.Body.Close()
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 		if r.StatusCode < 200 || r.StatusCode >= 400 {
 | 
					
						
							|  |  |  | 			log.Printf("[WARNING] Host %s health check returned HTTP code %d\n",
 | 
					
						
							|  |  |  | 				host.Name, r.StatusCode)
 | 
					
						
							|  |  |  | 			nextTs = time.Unix(0, 0)
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 	} else {
 | 
					
						
							|  |  |  | 		log.Printf("[WARNING] Host %s health check probe failed: %v\n", host.Name, err)
 | 
					
						
							|  |  |  | 		nextTs = time.Unix(0, 0)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 	host.checkMu.Lock()
 | 
					
						
							|  |  |  | 	host.Checking = false
 | 
					
						
							|  |  |  | 	host.OkUntil = nextTs
 | 
					
						
							|  |  |  | 	host.checkMu.Unlock()
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-03-17 00:20:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | func (u *staticUpstream) healthCheck() {
 | 
					
						
							|  |  |  | 	for _, host := range u.Hosts {
 | 
					
						
							| 
									
										
										
										
											2017-04-24 20:37:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 		if host.CheckUrl == "" {
 | 
					
						
							|  |  |  | 			var hostName, checkPort string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// The DNS server might be an HTTP server.  If so, extract its name.
 | 
					
						
							|  |  |  | 			ret, err := url.Parse(host.Name)
 | 
					
						
							|  |  |  | 			if err == nil && len(ret.Host) > 0 {
 | 
					
						
							|  |  |  | 				hostName = ret.Host
 | 
					
						
							| 
									
										
										
										
											2017-03-17 00:20:55 -07:00
										 |  |  | 			} else {
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 				hostName = host.Name
 | 
					
						
							| 
									
										
										
										
											2017-03-16 14:10:54 -07:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Extract the port number from the parsed server name.
 | 
					
						
							|  |  |  | 			checkHostName, checkPort, err := net.SplitHostPort(hostName)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				checkHostName = hostName
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if u.HealthCheck.Port != "" {
 | 
					
						
							|  |  |  | 				checkPort = u.HealthCheck.Port
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			host.CheckUrl = "http://" + net.JoinHostPort(checkHostName, checkPort) + u.HealthCheck.Path
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-30 10:13:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// calculate this before the get
 | 
					
						
							|  |  |  | 		nextTs := time.Now().Add(u.Future)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// locks/bools should prevent requests backing up
 | 
					
						
							|  |  |  | 		go healthCheckUrl(nextTs, host)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) HealthCheckWorker(stop chan struct{}) {
 | 
					
						
							|  |  |  | 	ticker := time.NewTicker(u.HealthCheck.Interval)
 | 
					
						
							|  |  |  | 	u.healthCheck()
 | 
					
						
							|  |  |  | 	for {
 | 
					
						
							|  |  |  | 		select {
 | 
					
						
							|  |  |  | 		case <-ticker.C:
 | 
					
						
							|  |  |  | 			u.healthCheck()
 | 
					
						
							|  |  |  | 		case <-stop:
 | 
					
						
							| 
									
										
										
										
											2017-04-26 10:58:14 +01:00
										 |  |  | 			ticker.Stop()
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) Select() *UpstreamHost {
 | 
					
						
							|  |  |  | 	pool := u.Hosts
 | 
					
						
							|  |  |  | 	if len(pool) == 1 {
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 		if pool[0].Down() && u.Spray == nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return pool[0]
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	allDown := true
 | 
					
						
							|  |  |  | 	for _, host := range pool {
 | 
					
						
							|  |  |  | 		if !host.Down() {
 | 
					
						
							|  |  |  | 			allDown = false
 | 
					
						
							|  |  |  | 			break
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if allDown {
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 		if u.Spray == nil {
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return u.Spray.Select(pool)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if u.Policy == nil {
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 		h := (&Random{}).Select(pool)
 | 
					
						
							| 
									
										
										
										
											2017-06-08 16:06:14 +01:00
										 |  |  | 		if h != nil {
 | 
					
						
							|  |  |  | 			return h
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 		if h == nil && u.Spray == nil {
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return u.Spray.Select(pool)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	h := u.Policy.Select(pool)
 | 
					
						
							|  |  |  | 	if h != nil {
 | 
					
						
							|  |  |  | 		return h
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if u.Spray == nil {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-07-04 21:13:28 +01:00
										 |  |  | 	return u.Spray.Select(pool)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:01:16 +00:00
										 |  |  | func (u *staticUpstream) IsAllowedDomain(name string) bool {
 | 
					
						
							| 
									
										
										
										
											2017-02-07 21:03:17 +00:00
										 |  |  | 	if dns.Name(name) == dns.Name(u.From()) {
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:01:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 21:03:17 +00:00
										 |  |  | 	for _, ignoredSubDomain := range u.IgnoredSubDomains {
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:01:16 +00:00
										 |  |  | 		if middleware.Name(ignoredSubDomain).Matches(name) {
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 			return false
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	return true
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-02-06 19:32:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) Exchanger() Exchanger { return u.ex }
 |