2015-01-30 14:52:18 +08:00
|
|
|
// Package proxy is middleware that proxies requests.
|
2015-01-30 13:05:36 +08:00
|
|
|
package proxy
|
2015-01-30 08:17:59 +08:00
|
|
|
|
|
|
|
import (
|
2015-05-03 13:20:36 +08:00
|
|
|
"errors"
|
2015-01-30 08:17:59 +08:00
|
|
|
"net/http"
|
2015-02-03 14:41:35 +08:00
|
|
|
"net/url"
|
2015-05-03 13:20:36 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2015-05-05 01:04:17 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-30 08:17:59 +08:00
|
|
|
)
|
|
|
|
|
2015-06-08 08:49:17 +08:00
|
|
|
var errUnreachable = errors.New("unreachable backend")
|
2015-05-03 13:20:36 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
// Proxy represents a middleware instance that can proxy requests.
|
|
|
|
type Proxy struct {
|
2015-05-03 13:20:36 +08:00
|
|
|
Next middleware.Handler
|
|
|
|
Upstreams []Upstream
|
2015-04-12 07:24:47 +08:00
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-05-25 08:58:17 +08:00
|
|
|
// Upstream manages a pool of proxy upstream hosts. Select should return a
|
2015-05-03 13:20:36 +08:00
|
|
|
// suitable upstream host, or nil if no such hosts are available.
|
|
|
|
type Upstream interface {
|
2015-05-25 08:58:17 +08:00
|
|
|
// The path this upstream host should be routed on
|
2015-05-05 10:58:18 +08:00
|
|
|
From() string
|
2015-05-03 13:20:36 +08:00
|
|
|
// Selects an upstream host to be routed to.
|
|
|
|
Select() *UpstreamHost
|
2015-11-17 00:22:06 +08:00
|
|
|
// Checks if subpath is not an ignored path
|
|
|
|
IsAllowedPath(string) bool
|
2015-05-03 13:20:36 +08:00
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-05-25 08:58:17 +08:00
|
|
|
// UpstreamHostDownFunc can be used to customize how Down behaves.
|
2015-05-03 13:20:36 +08:00
|
|
|
type UpstreamHostDownFunc func(*UpstreamHost) bool
|
2015-04-12 07:24:47 +08:00
|
|
|
|
2015-05-25 08:58:17 +08:00
|
|
|
// UpstreamHost represents a single proxy upstream
|
2015-05-03 13:20:36 +08:00
|
|
|
type UpstreamHost struct {
|
2015-09-23 06:47:39 +08:00
|
|
|
Conns int64 // must be first field to be 64-bit aligned on 32-bit systems
|
|
|
|
Name string // hostname of this upstream host
|
2015-06-04 21:57:39 +08:00
|
|
|
ReverseProxy *ReverseProxy
|
|
|
|
Fails int32
|
|
|
|
FailTimeout time.Duration
|
|
|
|
Unhealthy bool
|
|
|
|
ExtraHeaders http.Header
|
|
|
|
CheckDown UpstreamHostDownFunc
|
|
|
|
WithoutPathPrefix string
|
2015-05-03 13:20:36 +08:00
|
|
|
}
|
2015-04-12 07:24:47 +08:00
|
|
|
|
2015-05-25 08:58:17 +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.
|
2015-05-03 13:20:36 +08:00
|
|
|
func (uh *UpstreamHost) Down() bool {
|
|
|
|
if uh.CheckDown == nil {
|
|
|
|
// Default settings
|
|
|
|
return uh.Unhealthy || uh.Fails > 0
|
2015-01-30 08:17:59 +08:00
|
|
|
}
|
2015-05-03 13:20:36 +08:00
|
|
|
return uh.CheckDown(uh)
|
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-05-03 13:20:36 +08:00
|
|
|
// ServeHTTP satisfies the middleware.Handler interface.
|
|
|
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
|
|
|
|
for _, upstream := range p.Upstreams {
|
2015-11-17 00:22:06 +08:00
|
|
|
if middleware.Path(r.URL.Path).Matches(upstream.From()) && upstream.IsAllowedPath(r.URL.Path) {
|
2015-05-04 04:33:08 +08:00
|
|
|
var replacer middleware.Replacer
|
|
|
|
start := time.Now()
|
|
|
|
requestHost := r.Host
|
|
|
|
|
2015-05-03 13:20:36 +08:00
|
|
|
// Since Select() should give us "up" hosts, keep retrying
|
|
|
|
// hosts until timeout (or until we get a nil host).
|
|
|
|
for time.Now().Sub(start) < (60 * time.Second) {
|
|
|
|
host := upstream.Select()
|
|
|
|
if host == nil {
|
|
|
|
return http.StatusBadGateway, errUnreachable
|
|
|
|
}
|
|
|
|
proxy := host.ReverseProxy
|
|
|
|
r.Host = host.Name
|
|
|
|
|
2015-05-25 08:58:17 +08:00
|
|
|
if baseURL, err := url.Parse(host.Name); err == nil {
|
|
|
|
r.Host = baseURL.Host
|
2015-05-03 13:20:36 +08:00
|
|
|
if proxy == nil {
|
2015-06-04 21:57:39 +08:00
|
|
|
proxy = NewSingleHostReverseProxy(baseURL, host.WithoutPathPrefix)
|
2015-05-03 13:20:36 +08:00
|
|
|
}
|
|
|
|
} else if proxy == nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
var extraHeaders http.Header
|
|
|
|
if host.ExtraHeaders != nil {
|
|
|
|
extraHeaders = make(http.Header)
|
2015-05-04 04:33:08 +08:00
|
|
|
if replacer == nil {
|
|
|
|
rHost := r.Host
|
|
|
|
r.Host = requestHost
|
2015-07-25 00:11:34 +08:00
|
|
|
replacer = middleware.NewReplacer(r, nil, "")
|
2015-05-04 04:33:08 +08:00
|
|
|
r.Host = rHost
|
|
|
|
}
|
2015-05-03 13:20:36 +08:00
|
|
|
for header, values := range host.ExtraHeaders {
|
|
|
|
for _, value := range values {
|
|
|
|
extraHeaders.Add(header,
|
2015-05-04 04:33:08 +08:00
|
|
|
replacer.Replace(value))
|
2015-05-03 13:20:36 +08:00
|
|
|
if header == "Host" {
|
2015-05-04 04:33:08 +08:00
|
|
|
r.Host = replacer.Replace(value)
|
2015-05-03 13:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 14:41:35 +08:00
|
|
|
|
2015-05-03 13:20:36 +08: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-12 07:24:47 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-29 06:56:56 +08:00
|
|
|
|
2015-05-03 13:20:36 +08:00
|
|
|
return p.Next.ServeHTTP(w, r)
|
2015-01-30 08:17:59 +08:00
|
|
|
}
|