2015-05-03 00:20:36 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-05-04 21:58:18 -05:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-05-03 00:20:36 -05:00
|
|
|
"time"
|
2015-05-24 19:58:17 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/config/parse"
|
2015-05-03 00:20:36 -05:00
|
|
|
)
|
|
|
|
|
2015-05-30 13:34:54 -05:00
|
|
|
var (
|
|
|
|
supportedPolicies map[string]func() Policy = make(map[string]func() Policy)
|
2015-06-01 12:23:57 -05:00
|
|
|
proxyHeaders http.Header = make(http.Header)
|
2015-05-30 13:34:54 -05:00
|
|
|
)
|
2015-05-28 17:56:11 -05:00
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
type staticUpstream struct {
|
|
|
|
from string
|
2015-05-03 00:20:36 -05:00
|
|
|
Hosts HostPool
|
|
|
|
Policy Policy
|
|
|
|
|
|
|
|
FailTimeout time.Duration
|
|
|
|
MaxFails int32
|
|
|
|
HealthCheck struct {
|
|
|
|
Path string
|
|
|
|
Interval time.Duration
|
|
|
|
}
|
2015-06-03 13:06:24 -05:00
|
|
|
Without string
|
2015-05-03 00:20:36 -05:00
|
|
|
}
|
|
|
|
|
2015-05-24 19:58:17 -05:00
|
|
|
// NewStaticUpstreams parses the configuration input and sets up
|
2015-05-04 21:58:18 -05:00
|
|
|
// static upstreams for the proxy middleware.
|
|
|
|
func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
|
|
|
|
var upstreams []Upstream
|
|
|
|
for c.Next() {
|
|
|
|
upstream := &staticUpstream{
|
|
|
|
from: "",
|
|
|
|
Hosts: nil,
|
|
|
|
Policy: &Random{},
|
|
|
|
FailTimeout: 10 * time.Second,
|
|
|
|
MaxFails: 1,
|
|
|
|
}
|
2015-05-30 13:34:54 -05:00
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
if !c.Args(&upstream.from) {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
to := c.RemainingArgs()
|
|
|
|
if len(to) == 0 {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "policy":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
2015-05-28 17:56:11 -05:00
|
|
|
|
2015-05-28 18:53:54 -05:00
|
|
|
if policyCreateFunc, ok := supportedPolicies[c.Val()]; ok {
|
|
|
|
upstream.Policy = policyCreateFunc()
|
|
|
|
} else {
|
2015-05-04 21:58:18 -05:00
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
case "fail_timeout":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
if dur, err := time.ParseDuration(c.Val()); err == nil {
|
|
|
|
upstream.FailTimeout = dur
|
|
|
|
} else {
|
|
|
|
return upstreams, err
|
|
|
|
}
|
|
|
|
case "max_fails":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
if n, err := strconv.Atoi(c.Val()); err == nil {
|
|
|
|
upstream.MaxFails = int32(n)
|
|
|
|
} else {
|
|
|
|
return upstreams, err
|
|
|
|
}
|
|
|
|
case "health_check":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
upstream.HealthCheck.Path = c.Val()
|
|
|
|
upstream.HealthCheck.Interval = 30 * time.Second
|
|
|
|
if c.NextArg() {
|
|
|
|
if dur, err := time.ParseDuration(c.Val()); err == nil {
|
|
|
|
upstream.HealthCheck.Interval = dur
|
|
|
|
} else {
|
|
|
|
return upstreams, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "proxy_header":
|
|
|
|
var header, value string
|
|
|
|
if !c.Args(&header, &value) {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
2015-06-01 12:23:57 -05:00
|
|
|
proxyHeaders.Add(header, value)
|
2015-05-30 13:34:54 -05:00
|
|
|
case "websocket":
|
2015-06-01 12:23:57 -05:00
|
|
|
proxyHeaders.Add("Connection", "{>Connection}")
|
|
|
|
proxyHeaders.Add("Upgrade", "{>Upgrade}")
|
2015-06-03 13:06:24 -05:00
|
|
|
case "without":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return upstreams, c.ArgErr()
|
|
|
|
}
|
|
|
|
upstream.Without = c.Val()
|
2015-05-04 21:58:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
upstream.Hosts = make([]*UpstreamHost, len(to))
|
|
|
|
for i, host := range to {
|
|
|
|
if !strings.HasPrefix(host, "http") {
|
|
|
|
host = "http://" + host
|
|
|
|
}
|
|
|
|
uh := &UpstreamHost{
|
|
|
|
Name: host,
|
|
|
|
Conns: 0,
|
|
|
|
Fails: 0,
|
|
|
|
FailTimeout: upstream.FailTimeout,
|
|
|
|
Unhealthy: false,
|
|
|
|
ExtraHeaders: proxyHeaders,
|
|
|
|
CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
|
|
|
|
return func(uh *UpstreamHost) bool {
|
|
|
|
if uh.Unhealthy {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if uh.Fails >= upstream.MaxFails &&
|
|
|
|
upstream.MaxFails != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}(upstream),
|
2015-06-03 13:06:24 -05:00
|
|
|
Without: upstream.Without,
|
2015-05-04 21:58:18 -05:00
|
|
|
}
|
2015-05-24 19:58:17 -05:00
|
|
|
if baseURL, err := url.Parse(uh.Name); err == nil {
|
2015-06-03 13:06:24 -05:00
|
|
|
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.Without)
|
2015-05-04 21:58:18 -05:00
|
|
|
} else {
|
|
|
|
return upstreams, err
|
|
|
|
}
|
|
|
|
upstream.Hosts[i] = uh
|
|
|
|
}
|
|
|
|
|
|
|
|
if upstream.HealthCheck.Path != "" {
|
|
|
|
go upstream.HealthCheckWorker(nil)
|
|
|
|
}
|
|
|
|
upstreams = append(upstreams, upstream)
|
|
|
|
}
|
|
|
|
return upstreams, nil
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:56:11 -05:00
|
|
|
// RegisterPolicy adds a custom policy to the proxy.
|
2015-05-28 18:53:54 -05:00
|
|
|
func RegisterPolicy(name string, policy func() Policy) {
|
|
|
|
supportedPolicies[name] = policy
|
2015-05-28 17:56:11 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
func (u *staticUpstream) From() string {
|
|
|
|
return u.from
|
2015-05-03 00:20:36 -05:00
|
|
|
}
|
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
func (u *staticUpstream) healthCheck() {
|
2015-05-03 00:20:36 -05:00
|
|
|
for _, host := range u.Hosts {
|
2015-05-24 19:58:17 -05:00
|
|
|
hostURL := host.Name + u.HealthCheck.Path
|
|
|
|
if r, err := http.Get(hostURL); err == nil {
|
2015-05-03 00:20:36 -05:00
|
|
|
io.Copy(ioutil.Discard, r.Body)
|
|
|
|
r.Body.Close()
|
|
|
|
host.Unhealthy = r.StatusCode < 200 || r.StatusCode >= 400
|
|
|
|
} else {
|
|
|
|
host.Unhealthy = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
func (u *staticUpstream) HealthCheckWorker(stop chan struct{}) {
|
2015-05-03 00:20:36 -05:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:58:18 -05:00
|
|
|
func (u *staticUpstream) Select() *UpstreamHost {
|
2015-05-03 00:20:36 -05:00
|
|
|
pool := u.Hosts
|
|
|
|
if len(pool) == 1 {
|
|
|
|
if pool[0].Down() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pool[0]
|
|
|
|
}
|
|
|
|
allDown := true
|
|
|
|
for _, host := range pool {
|
|
|
|
if !host.Down() {
|
|
|
|
allDown = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if allDown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Policy == nil {
|
|
|
|
return (&Random{}).Select(pool)
|
|
|
|
}
|
2015-05-24 19:58:17 -05:00
|
|
|
return u.Policy.Select(pool)
|
2015-05-03 00:20:36 -05:00
|
|
|
}
|