2015-01-30 01:52:18 -05:00
|
|
|
// Package proxy is middleware that proxies requests.
|
2015-01-30 00:05:36 -05:00
|
|
|
package proxy
|
2015-01-29 19:17:59 -05:00
|
|
|
|
|
|
|
import (
|
2015-05-03 00:20:36 -05:00
|
|
|
"errors"
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"net"
|
2015-01-29 19:17:59 -05:00
|
|
|
"net/http"
|
2015-02-03 01:41:35 -05:00
|
|
|
"net/url"
|
2015-05-03 00:20:36 -05:00
|
|
|
"regexp"
|
2015-04-01 00:53:39 -05:00
|
|
|
"strings"
|
2015-05-03 00:20:36 -05:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2015-01-29 19:17:59 -05:00
|
|
|
)
|
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
var errUnreachable = errors.New("Unreachable backend")
|
|
|
|
|
2015-04-11 18:24:47 -05:00
|
|
|
// Proxy represents a middleware instance that can proxy requests.
|
|
|
|
type Proxy struct {
|
2015-05-03 00:20:36 -05:00
|
|
|
Next middleware.Handler
|
|
|
|
Upstreams []Upstream
|
2015-04-11 18:24:47 -05:00
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
// An upstream manages a pool of proxy upstream hosts. Select should return a
|
|
|
|
// suitable upstream host, or nil if no such hosts are available.
|
|
|
|
type Upstream interface {
|
|
|
|
// The path this upstream host should be routed on
|
|
|
|
From() string
|
|
|
|
// Selects an upstream host to be routed to.
|
|
|
|
Select() *UpstreamHost
|
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
2015-04-11 18:24:47 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
// An UpstreamHost represents a single proxy upstream
|
|
|
|
type UpstreamHost struct {
|
|
|
|
Name string
|
|
|
|
ReverseProxy *ReverseProxy
|
|
|
|
Conns int64
|
|
|
|
Fails int32
|
|
|
|
FailTimeout time.Duration
|
|
|
|
Unhealthy bool
|
|
|
|
ExtraHeaders http.Header
|
|
|
|
CheckDown UpstreamHostDownFunc
|
|
|
|
}
|
2015-04-11 18:24:47 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
func (uh *UpstreamHost) Down() bool {
|
|
|
|
if uh.CheckDown == nil {
|
|
|
|
// Default settings
|
|
|
|
return uh.Unhealthy || uh.Fails > 0
|
2015-01-29 19:17:59 -05:00
|
|
|
}
|
2015-05-03 00:20:36 -05:00
|
|
|
return uh.CheckDown(uh)
|
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
//https://github.com/mgutz/str
|
|
|
|
var tRe = regexp.MustCompile(`([\-\[\]()*\s])`)
|
|
|
|
var tRe2 = regexp.MustCompile(`\$`)
|
|
|
|
var openDelim = tRe2.ReplaceAllString(tRe.ReplaceAllString("{{", "\\$1"), "\\$")
|
|
|
|
var closDelim = tRe2.ReplaceAllString(tRe.ReplaceAllString("}}", "\\$1"), "\\$")
|
|
|
|
var templateDelim = regexp.MustCompile(openDelim + `(.+?)` + closDelim)
|
|
|
|
|
|
|
|
type requestVars struct {
|
|
|
|
Host string
|
|
|
|
RemoteIp string
|
|
|
|
Scheme string
|
|
|
|
Upstream string
|
|
|
|
UpstreamHost string
|
2015-04-11 18:24:47 -05:00
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
func templateWithDelimiters(s string, vars requestVars) string {
|
|
|
|
matches := templateDelim.FindAllStringSubmatch(s, -1)
|
|
|
|
for _, submatches := range matches {
|
|
|
|
match := submatches[0]
|
|
|
|
key := submatches[1]
|
|
|
|
found := true
|
|
|
|
repl := ""
|
|
|
|
switch key {
|
|
|
|
case "http_host":
|
|
|
|
repl = vars.Host
|
|
|
|
case "remote_addr":
|
|
|
|
repl = vars.RemoteIp
|
|
|
|
case "scheme":
|
|
|
|
repl = vars.Scheme
|
|
|
|
case "upstream":
|
|
|
|
repl = vars.Upstream
|
|
|
|
case "upstream_host":
|
|
|
|
repl = vars.UpstreamHost
|
|
|
|
default:
|
|
|
|
found = false
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
s = strings.Replace(s, match, repl, -1)
|
|
|
|
}
|
2015-04-11 18:24:47 -05:00
|
|
|
}
|
2015-05-03 00:20:36 -05:00
|
|
|
return s
|
2015-04-11 18:24:47 -05:00
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
// ServeHTTP satisfies the middleware.Handler interface.
|
|
|
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
|
|
|
|
for _, upstream := range p.Upstreams {
|
|
|
|
if middleware.Path(r.URL.Path).Matches(upstream.From()) {
|
|
|
|
vars := requestVars{
|
|
|
|
Host: r.Host,
|
|
|
|
Scheme: "http",
|
|
|
|
}
|
|
|
|
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
|
|
|
vars.RemoteIp = clientIP
|
|
|
|
}
|
|
|
|
if fFor := r.Header.Get("X-Forwarded-For"); fFor != "" {
|
|
|
|
vars.RemoteIp = fFor
|
|
|
|
}
|
|
|
|
if r.TLS != nil {
|
|
|
|
vars.Scheme = "https"
|
|
|
|
}
|
|
|
|
// Since Select() should give us "up" hosts, keep retrying
|
|
|
|
// hosts until timeout (or until we get a nil host).
|
|
|
|
start := time.Now()
|
|
|
|
for time.Now().Sub(start) < (60 * time.Second) {
|
|
|
|
host := upstream.Select()
|
|
|
|
if host == nil {
|
|
|
|
return http.StatusBadGateway, errUnreachable
|
|
|
|
}
|
|
|
|
proxy := host.ReverseProxy
|
|
|
|
vars.Upstream = host.Name
|
|
|
|
r.Host = host.Name
|
|
|
|
|
|
|
|
if baseUrl, err := url.Parse(host.Name); err == nil {
|
|
|
|
vars.UpstreamHost = baseUrl.Host
|
|
|
|
if proxy == nil {
|
|
|
|
proxy = NewSingleHostReverseProxy(baseUrl)
|
|
|
|
}
|
|
|
|
} else if proxy == nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
var extraHeaders http.Header
|
|
|
|
if host.ExtraHeaders != nil {
|
|
|
|
extraHeaders = make(http.Header)
|
|
|
|
for header, values := range host.ExtraHeaders {
|
|
|
|
for _, value := range values {
|
|
|
|
extraHeaders.Add(header,
|
|
|
|
templateWithDelimiters(value, vars))
|
|
|
|
if header == "Host" {
|
|
|
|
r.Host = templateWithDelimiters(value, vars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 01:41:35 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
atomic.AddInt64(&host.Conns, 1)
|
|
|
|
backendErr := proxy.ServeHTTP(w, r, extraHeaders)
|
|
|
|
atomic.AddInt64(&host.Conns, -1)
|
|
|
|
if backendErr == nil {
|
|
|
|
return 0, 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 http.StatusBadGateway, errUnreachable
|
2015-04-11 18:24:47 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-28 17:56:56 -05:00
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
return p.Next.ServeHTTP(w, r)
|
2015-01-29 19:17:59 -05:00
|
|
|
}
|
|
|
|
|
2015-05-03 00:20:36 -05:00
|
|
|
// New creates a new instance of proxy middleware.
|
|
|
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
|
|
|
if upstreams, err := newStaticUpstreams(c); err == nil {
|
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return Proxy{Next: next, Upstreams: upstreams}
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-29 19:17:59 -05:00
|
|
|
}
|