2016-06-06 11:51:56 +08:00
|
|
|
// Package proxy is middleware that proxies HTTP requests.
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2017-04-19 22:56:51 +08:00
|
|
|
"context"
|
2016-06-06 11:51:56 +08:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Proxy represents a middleware instance that can proxy requests.
|
|
|
|
type Proxy struct {
|
|
|
|
Next httpserver.Handler
|
|
|
|
Upstreams []Upstream
|
|
|
|
}
|
|
|
|
|
2016-09-25 06:03:22 +08:00
|
|
|
// Upstream manages a pool of proxy upstream hosts.
|
2016-06-06 11:51:56 +08:00
|
|
|
type Upstream interface {
|
|
|
|
// The path this upstream host should be routed on
|
|
|
|
From() string
|
2016-09-25 06:03:22 +08:00
|
|
|
|
|
|
|
// Selects an upstream host to be routed to. It
|
|
|
|
// should return a suitable upstream host, or nil
|
|
|
|
// if no such hosts are available.
|
2016-07-31 17:04:54 +08:00
|
|
|
Select(*http.Request) *UpstreamHost
|
2016-09-25 06:03:22 +08:00
|
|
|
|
2016-06-06 11:51:56 +08:00
|
|
|
// Checks if subpath is not an ignored path
|
|
|
|
AllowedPath(string) bool
|
2016-09-25 06:03:22 +08:00
|
|
|
|
|
|
|
// Gets how long to try selecting upstream hosts
|
|
|
|
// in the case of cascading failures.
|
|
|
|
GetTryDuration() time.Duration
|
|
|
|
|
|
|
|
// Gets how long to wait between selecting upstream
|
|
|
|
// hosts in the case of cascading failures.
|
|
|
|
GetTryInterval() time.Duration
|
2017-01-12 02:38:14 +08:00
|
|
|
|
|
|
|
// Gets the number of upstream hosts.
|
|
|
|
GetHostCount() int
|
2017-04-03 04:58:15 +08:00
|
|
|
|
|
|
|
// Stops the upstream from proxying requests to shutdown goroutines cleanly.
|
|
|
|
Stop() error
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpstreamHostDownFunc can be used to customize how Down behaves.
|
|
|
|
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
|
|
|
|
|
|
|
// UpstreamHost represents a single proxy upstream
|
|
|
|
type UpstreamHost struct {
|
2017-02-15 23:09:42 +08:00
|
|
|
// This field is read & written to concurrently, so all access must use
|
|
|
|
// atomic operations.
|
2016-10-26 00:45:30 +08:00
|
|
|
Conns int64 // must be first field to be 64-bit aligned on 32-bit systems
|
|
|
|
MaxConns int64
|
2016-06-06 11:51:56 +08:00
|
|
|
Name string // hostname of this upstream host
|
|
|
|
UpstreamHeaders http.Header
|
|
|
|
DownstreamHeaders http.Header
|
2016-10-26 00:45:30 +08:00
|
|
|
FailTimeout time.Duration
|
2016-06-06 11:51:56 +08:00
|
|
|
CheckDown UpstreamHostDownFunc
|
|
|
|
WithoutPathPrefix string
|
2016-10-26 00:45:30 +08:00
|
|
|
ReverseProxy *ReverseProxy
|
|
|
|
Fails int32
|
2017-02-15 23:09:42 +08:00
|
|
|
// This is an int32 so that we can use atomic operations to do concurrent
|
|
|
|
// reads & writes to this value. The default value of 0 indicates that it
|
|
|
|
// is healthy and any non-zero value indicates unhealthy.
|
|
|
|
Unhealthy int32
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Down checks whether the upstream host is down or not.
|
|
|
|
// Down will try to use uh.CheckDown first, and will fall
|
|
|
|
// back to some default criteria if necessary.
|
|
|
|
func (uh *UpstreamHost) Down() bool {
|
|
|
|
if uh.CheckDown == nil {
|
|
|
|
// Default settings
|
2017-02-15 23:09:42 +08:00
|
|
|
return atomic.LoadInt32(&uh.Unhealthy) != 0 || atomic.LoadInt32(&uh.Fails) > 0
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
return uh.CheckDown(uh)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full checks whether the upstream host has reached its maximum connections
|
|
|
|
func (uh *UpstreamHost) Full() bool {
|
2017-02-15 23:09:42 +08:00
|
|
|
return uh.MaxConns > 0 && atomic.LoadInt64(&uh.Conns) >= uh.MaxConns
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Available checks whether the upstream host is available for proxying to
|
|
|
|
func (uh *UpstreamHost) Available() bool {
|
|
|
|
return !uh.Down() && !uh.Full()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP satisfies the httpserver.Handler interface.
|
|
|
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-06-29 08:19:35 +08:00
|
|
|
// start by selecting most specific matching upstream config
|
2015-06-08 01:07:23 +08:00
|
|
|
upstream := p.match(r)
|
|
|
|
if upstream == nil {
|
|
|
|
return p.Next.ServeHTTP(w, r)
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2016-06-29 08:19:35 +08:00
|
|
|
// this replacer is used to fill in header field values
|
2016-07-22 08:18:01 +08:00
|
|
|
replacer := httpserver.NewReplacer(r, nil, "")
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2016-06-29 08:19:35 +08:00
|
|
|
// outreq is the request that makes a roundtrip to the backend
|
2017-04-19 22:56:51 +08:00
|
|
|
outreq, cancel := createUpstreamRequest(w, r)
|
|
|
|
defer cancel()
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2017-01-12 02:38:14 +08:00
|
|
|
// If we have more than one upstream host defined and if retrying is enabled
|
|
|
|
// by setting try_duration to a non-zero value, caddy will try to
|
|
|
|
// retry the request at a different host if the first one failed.
|
|
|
|
//
|
|
|
|
// This requires us to possibly rewind and replay the request body though,
|
|
|
|
// which in turn requires us to buffer the request body first.
|
|
|
|
//
|
|
|
|
// An unbuffered request is usually preferrable, because it reduces latency
|
|
|
|
// as well as memory usage. Furthermore it enables different kinds of
|
|
|
|
// HTTP streaming applications like gRPC for instance.
|
|
|
|
requiresBuffering := upstream.GetHostCount() > 1 && upstream.GetTryDuration() != 0
|
|
|
|
|
|
|
|
if requiresBuffering {
|
|
|
|
body, err := newBufferedBody(outreq.Body)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusBadRequest, errors.New("failed to read downstream request body")
|
|
|
|
}
|
|
|
|
if body != nil {
|
|
|
|
outreq.Body = body
|
|
|
|
}
|
2016-11-01 12:34:39 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 06:30:40 +08:00
|
|
|
// The keepRetrying function will return true if we should
|
|
|
|
// loop and try to select another host, or false if we
|
|
|
|
// should break and stop retrying.
|
2016-06-29 08:19:35 +08:00
|
|
|
start := time.Now()
|
2017-04-19 22:56:51 +08:00
|
|
|
keepRetrying := func(backendErr error) bool {
|
|
|
|
// if downstream has canceled the request, break
|
|
|
|
if backendErr == context.Canceled {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-25 06:30:40 +08:00
|
|
|
// if we've tried long enough, break
|
2016-09-25 11:27:57 +08:00
|
|
|
if time.Since(start) >= upstream.GetTryDuration() {
|
2016-09-25 06:30:40 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// otherwise, wait and try the next available host
|
|
|
|
time.Sleep(upstream.GetTryInterval())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-25 04:22:13 +08:00
|
|
|
var backendErr error
|
2016-09-25 06:03:22 +08:00
|
|
|
for {
|
2016-09-25 06:30:40 +08:00
|
|
|
// since Select() should give us "up" hosts, keep retrying
|
|
|
|
// hosts until timeout (or until we get a nil host).
|
2016-07-31 17:04:54 +08:00
|
|
|
host := upstream.Select(r)
|
2015-06-08 01:07:23 +08:00
|
|
|
if host == nil {
|
2016-09-25 04:22:13 +08:00
|
|
|
if backendErr == nil {
|
|
|
|
backendErr = errors.New("no hosts available upstream")
|
|
|
|
}
|
2017-04-19 22:56:51 +08:00
|
|
|
if !keepRetrying(backendErr) {
|
2016-09-25 06:30:40 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
2015-06-08 01:07:23 +08:00
|
|
|
}
|
|
|
|
if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
|
|
|
|
rr.Replacer.Set("upstream", host.Name)
|
|
|
|
}
|
|
|
|
|
2016-06-29 08:40:07 +08:00
|
|
|
proxy := host.ReverseProxy
|
|
|
|
|
|
|
|
// a backend's name may contain more than just the host,
|
|
|
|
// so we parse it as a URL to try to isolate the host.
|
|
|
|
if nameURL, err := url.Parse(host.Name); err == nil {
|
|
|
|
outreq.Host = nameURL.Host
|
|
|
|
if proxy == nil {
|
2016-08-06 06:41:32 +08:00
|
|
|
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix, http.DefaultMaxIdleConnsPerHost)
|
2016-06-29 08:40:07 +08:00
|
|
|
}
|
2016-07-23 01:33:50 +08:00
|
|
|
|
|
|
|
// use upstream credentials by default
|
|
|
|
if outreq.Header.Get("Authorization") == "" && nameURL.User != nil {
|
|
|
|
pwd, _ := nameURL.User.Password()
|
|
|
|
outreq.SetBasicAuth(nameURL.User.Username(), pwd)
|
|
|
|
}
|
2016-06-29 08:40:07 +08:00
|
|
|
} else {
|
|
|
|
outreq.Host = host.Name
|
|
|
|
}
|
|
|
|
if proxy == nil {
|
|
|
|
return http.StatusInternalServerError, errors.New("proxy for host '" + host.Name + "' is nil")
|
|
|
|
}
|
2016-06-29 08:19:35 +08:00
|
|
|
|
|
|
|
// set headers for request going upstream
|
2015-06-08 01:07:23 +08:00
|
|
|
if host.UpstreamHeaders != nil {
|
2016-06-29 08:19:35 +08:00
|
|
|
// modify headers for request that will be sent to the upstream host
|
2016-07-22 08:18:01 +08:00
|
|
|
mutateHeadersByRules(outreq.Header, host.UpstreamHeaders, replacer)
|
|
|
|
if hostHeaders, ok := outreq.Header["Host"]; ok && len(hostHeaders) > 0 {
|
|
|
|
outreq.Host = hostHeaders[len(hostHeaders)-1]
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
2015-06-08 01:07:23 +08:00
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2016-06-29 08:19:35 +08:00
|
|
|
// prepare a function that will update response
|
|
|
|
// headers coming back downstream
|
2015-06-08 01:07:23 +08:00
|
|
|
var downHeaderUpdateFn respUpdateFn
|
|
|
|
if host.DownstreamHeaders != nil {
|
|
|
|
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer)
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2017-01-12 02:38:14 +08:00
|
|
|
// Before we retry the request we have to make sure
|
|
|
|
// that the body is rewound to it's beginning.
|
|
|
|
if bb, ok := outreq.Body.(*bufferedBody); ok {
|
|
|
|
if err := bb.rewind(); err != nil {
|
|
|
|
return http.StatusInternalServerError, errors.New("unable to rewind downstream request body")
|
|
|
|
}
|
2016-11-01 12:34:39 +08:00
|
|
|
}
|
|
|
|
|
2016-06-29 08:19:35 +08:00
|
|
|
// tell the proxy to serve the request
|
2017-01-12 02:38:14 +08:00
|
|
|
//
|
|
|
|
// NOTE:
|
|
|
|
// The call to proxy.ServeHTTP can theoretically panic.
|
|
|
|
// To prevent host.Conns from getting out-of-sync we thus have to
|
|
|
|
// make sure that it's _always_ correctly decremented afterwards.
|
|
|
|
func() {
|
|
|
|
atomic.AddInt64(&host.Conns, 1)
|
|
|
|
defer atomic.AddInt64(&host.Conns, -1)
|
|
|
|
backendErr = proxy.ServeHTTP(w, outreq, downHeaderUpdateFn)
|
|
|
|
}()
|
2016-06-29 08:19:35 +08:00
|
|
|
|
2016-09-25 06:03:22 +08:00
|
|
|
// if no errors, we're done here
|
2015-06-08 01:07:23 +08:00
|
|
|
if backendErr == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2016-09-25 06:03:22 +08:00
|
|
|
|
2016-11-04 08:25:49 +08:00
|
|
|
if _, ok := backendErr.(httpserver.MaxBytesExceeded); ok {
|
|
|
|
return http.StatusRequestEntityTooLarge, backendErr
|
|
|
|
}
|
|
|
|
|
2016-09-25 06:03:22 +08:00
|
|
|
// failover; remember this failure for some time if
|
|
|
|
// request failure counting is enabled
|
2015-06-08 01:07:23 +08:00
|
|
|
timeout := host.FailTimeout
|
2016-09-25 06:03:22 +08:00
|
|
|
if timeout > 0 {
|
|
|
|
atomic.AddInt32(&host.Fails, 1)
|
|
|
|
go func(host *UpstreamHost, timeout time.Duration) {
|
|
|
|
time.Sleep(timeout)
|
|
|
|
atomic.AddInt32(&host.Fails, -1)
|
|
|
|
}(host, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we've tried long enough, break
|
2017-04-19 22:56:51 +08:00
|
|
|
if !keepRetrying(backendErr) {
|
2016-09-25 06:03:22 +08:00
|
|
|
break
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 04:22:13 +08:00
|
|
|
return http.StatusBadGateway, backendErr
|
2015-06-08 01:07:23 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 06:03:22 +08:00
|
|
|
// match finds the best match for a proxy config based on r.
|
2015-06-08 01:07:23 +08:00
|
|
|
func (p Proxy) match(r *http.Request) Upstream {
|
|
|
|
var u Upstream
|
|
|
|
var longestMatch int
|
|
|
|
for _, upstream := range p.Upstreams {
|
|
|
|
basePath := upstream.From()
|
|
|
|
if !httpserver.Path(r.URL.Path).Matches(basePath) || !upstream.AllowedPath(r.URL.Path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(basePath) > longestMatch {
|
|
|
|
longestMatch = len(basePath)
|
|
|
|
u = upstream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// createUpstremRequest shallow-copies r into a new request
|
|
|
|
// that can be sent upstream.
|
2016-07-22 08:18:01 +08:00
|
|
|
//
|
|
|
|
// Derived from reverseproxy.go in the standard Go httputil package.
|
2017-04-19 22:56:51 +08:00
|
|
|
func createUpstreamRequest(rw http.ResponseWriter, r *http.Request) (*http.Request, context.CancelFunc) {
|
|
|
|
// Original incoming server request may be canceled by the
|
|
|
|
// user or by std lib(e.g. too many idle connections).
|
|
|
|
ctx, cancel := context.WithCancel(r.Context())
|
|
|
|
if cn, ok := rw.(http.CloseNotifier); ok {
|
|
|
|
notifyChan := cn.CloseNotify()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-notifyChan:
|
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
outreq := r.WithContext(ctx) // includes shallow copies of maps, but okay
|
|
|
|
|
2016-10-24 11:10:12 +08:00
|
|
|
// We should set body to nil explicitly if request body is empty.
|
|
|
|
// For server requests the Request Body is always non-nil.
|
|
|
|
if r.ContentLength == 0 {
|
|
|
|
outreq.Body = nil
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
|
2016-12-31 01:13:14 +08:00
|
|
|
// We are modifying the same underlying map from req (shallow
|
|
|
|
// copied above) so we only copy it if necessary.
|
|
|
|
copiedHeaders := false
|
|
|
|
|
|
|
|
// Remove hop-by-hop headers listed in the "Connection" header.
|
|
|
|
// See RFC 2616, section 14.10.
|
|
|
|
if c := outreq.Header.Get("Connection"); c != "" {
|
|
|
|
for _, f := range strings.Split(c, ",") {
|
|
|
|
if f = strings.TrimSpace(f); f != "" {
|
|
|
|
if !copiedHeaders {
|
|
|
|
outreq.Header = make(http.Header)
|
|
|
|
copyHeader(outreq.Header, r.Header)
|
|
|
|
copiedHeaders = true
|
|
|
|
}
|
|
|
|
outreq.Header.Del(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 08:19:35 +08:00
|
|
|
// Remove hop-by-hop headers to the backend. Especially
|
2016-06-06 11:51:56 +08:00
|
|
|
// important is "Connection" because we want a persistent
|
2016-12-31 01:13:14 +08:00
|
|
|
// connection, regardless of what the client sent to us.
|
2016-06-06 11:51:56 +08:00
|
|
|
for _, h := range hopHeaders {
|
|
|
|
if outreq.Header.Get(h) != "" {
|
2016-07-22 08:18:01 +08:00
|
|
|
if !copiedHeaders {
|
|
|
|
outreq.Header = make(http.Header)
|
|
|
|
copyHeader(outreq.Header, r.Header)
|
|
|
|
copiedHeaders = true
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
outreq.Header.Del(h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
|
|
|
// If we aren't the first proxy, retain prior
|
|
|
|
// X-Forwarded-For information as a comma+space
|
|
|
|
// separated list and fold multiple headers into one.
|
|
|
|
if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
|
|
|
|
clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
|
|
|
}
|
|
|
|
outreq.Header.Set("X-Forwarded-For", clientIP)
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:56:51 +08:00
|
|
|
return outreq, cancel
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func createRespHeaderUpdateFn(rules http.Header, replacer httpserver.Replacer) respUpdateFn {
|
|
|
|
return func(resp *http.Response) {
|
2016-07-22 08:18:01 +08:00
|
|
|
mutateHeadersByRules(resp.Header, rules, replacer)
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 08:18:01 +08:00
|
|
|
func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) {
|
|
|
|
for ruleField, ruleValues := range rules {
|
|
|
|
if strings.HasPrefix(ruleField, "+") {
|
|
|
|
for _, ruleValue := range ruleValues {
|
2016-11-17 12:41:53 +08:00
|
|
|
replacement := repl.Replace(ruleValue)
|
|
|
|
if len(replacement) > 0 {
|
|
|
|
headers.Add(strings.TrimPrefix(ruleField, "+"), replacement)
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
2016-07-22 08:18:01 +08:00
|
|
|
} else if strings.HasPrefix(ruleField, "-") {
|
|
|
|
headers.Del(strings.TrimPrefix(ruleField, "-"))
|
|
|
|
} else if len(ruleValues) > 0 {
|
2016-11-17 12:41:53 +08:00
|
|
|
replacement := repl.Replace(ruleValues[len(ruleValues)-1])
|
|
|
|
if len(replacement) > 0 {
|
|
|
|
headers.Set(ruleField, replacement)
|
|
|
|
}
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|