| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"io"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2016-08-14 12:57:49 -06:00
										 |  |  | 	"sync/atomic"
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											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 {
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	from   string
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	options           Options
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 06:00:50 +08:00
										 |  |  | // Options ...
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | type Options struct {
 | 
					
						
							|  |  |  | 	Ecs []*net.IPNet // EDNS0 CLIENT SUBNET address (v4/v6) to add in CIDR notaton.
 | 
					
						
							| 
									
										
										
										
											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{
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 			from:        "",
 | 
					
						
							|  |  |  | 			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,
 | 
					
						
							| 
									
										
										
										
											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-04-30 15:54:41 +01:00
										 |  |  | 		for _, host := range to {
 | 
					
						
							|  |  |  | 			h, _, err := net.SplitHostPort(host)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				h = host
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if x := net.ParseIP(h); x == nil {
 | 
					
						
							|  |  |  | 				return upstreams, fmt.Errorf("not an IP address: `%s'", h)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		upstream.Hosts = make([]*UpstreamHost, len(to))
 | 
					
						
							|  |  |  | 		for i, host := range to {
 | 
					
						
							|  |  |  | 			uh := &UpstreamHost{
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 				Name:        defaultHostPort(host),
 | 
					
						
							|  |  |  | 				Conns:       0,
 | 
					
						
							|  |  |  | 				Fails:       0,
 | 
					
						
							|  |  |  | 				FailTimeout: upstream.FailTimeout,
 | 
					
						
							|  |  |  | 				Unhealthy:   false,
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +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-18 20:57:35 +00:00
										 |  |  | 							return true
 | 
					
						
							|  |  |  | 						}
 | 
					
						
							|  |  |  | 						return false
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 				}(upstream),
 | 
					
						
							|  |  |  | 				WithoutPathPrefix: upstream.WithoutPathPrefix,
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			upstream.Hosts[i] = uh
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if upstream.HealthCheck.Path != "" {
 | 
					
						
							|  |  |  | 			go upstream.HealthCheckWorker(nil)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		upstreams = append(upstreams, upstream)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return upstreams, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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-04-30 15:54:41 +01:00
										 |  |  | func (u *staticUpstream) Options() Options {
 | 
					
						
							|  |  |  | 	return u.options
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	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{}
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) healthCheck() {
 | 
					
						
							|  |  |  | 	for _, host := range u.Hosts {
 | 
					
						
							| 
									
										
										
										
											2016-04-07 17:42:35 +01:00
										 |  |  | 		port := ""
 | 
					
						
							|  |  |  | 		if u.HealthCheck.Port != "" {
 | 
					
						
							|  |  |  | 			port = ":" + u.HealthCheck.Port
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		hostURL := host.Name + port + u.HealthCheck.Path
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 		if r, err := http.Get(hostURL); err == nil {
 | 
					
						
							|  |  |  | 			io.Copy(ioutil.Discard, r.Body)
 | 
					
						
							|  |  |  | 			r.Body.Close()
 | 
					
						
							|  |  |  | 			host.Unhealthy = r.StatusCode < 200 || r.StatusCode >= 400
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			host.Unhealthy = true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (u *staticUpstream) HealthCheckWorker(stop chan struct{}) {
 | 
					
						
							|  |  |  | 	ticker := time.NewTicker(u.HealthCheck.Interval)
 | 
					
						
							|  |  |  | 	u.healthCheck()
 | 
					
						
							|  |  |  | 	for {
 | 
					
						
							|  |  |  | 		select {
 | 
					
						
							|  |  |  | 		case <-ticker.C:
 | 
					
						
							|  |  |  | 			u.healthCheck()
 | 
					
						
							|  |  |  | 		case <-stop:
 | 
					
						
							|  |  |  | 			// TODO: the library should provide a stop channel and global
 | 
					
						
							|  |  |  | 			// waitgroup to allow goroutines started by plugins a chance
 | 
					
						
							|  |  |  | 			// to clean themselves up.
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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)
 | 
					
						
							|  |  |  | 		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
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | func (u *staticUpstream) IsAllowedPath(name string) bool {
 | 
					
						
							|  |  |  | 	for _, ignoredSubDomain := range u.IgnoredSubDomains {
 | 
					
						
							| 
									
										
										
										
											2016-04-12 22:34:44 +01:00
										 |  |  | 		if dns.Name(name) == dns.Name(u.From()) {
 | 
					
						
							|  |  |  | 			return true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-03-19 16:11:30 +00:00
										 |  |  | 		if middleware.Name(name).Matches(ignoredSubDomain + u.From()) {
 | 
					
						
							|  |  |  | 			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
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func defaultHostPort(s string) string {
 | 
					
						
							|  |  |  | 	_, _, e := net.SplitHostPort(s)
 | 
					
						
							|  |  |  | 	if e == nil {
 | 
					
						
							|  |  |  | 		return s
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return net.JoinHostPort(s, "53")
 | 
					
						
							|  |  |  | }
 |