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 (
|
|
|
|
"net/http"
|
2015-02-03 14:41:35 +08:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2015-04-01 13:53:39 +08:00
|
|
|
"strings"
|
2015-01-30 13:05:36 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-01-30 08:17:59 +08:00
|
|
|
)
|
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
// Proxy represents a middleware instance that can proxy requests.
|
|
|
|
type Proxy struct {
|
|
|
|
Next middleware.Handler
|
|
|
|
Rules []Rule
|
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
// ServeHTTP satisfies the middleware.Handler interface.
|
|
|
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
for _, rule := range p.Rules {
|
|
|
|
if middleware.Path(r.URL.Path).Matches(rule.From) {
|
|
|
|
var base string
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
if strings.HasPrefix(rule.To, "http") { // includes https
|
|
|
|
// destination includes a scheme! no need to guess
|
|
|
|
base = rule.To
|
|
|
|
} else {
|
|
|
|
// no scheme specified; assume same as request
|
|
|
|
var scheme string
|
|
|
|
if r.TLS == nil {
|
|
|
|
scheme = "http"
|
|
|
|
} else {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
base = scheme + "://" + rule.To
|
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl, err := url.Parse(base)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
r.Host = baseUrl.Host
|
|
|
|
|
|
|
|
// TODO: Construct this before; not during every request, if possible
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(baseUrl)
|
|
|
|
proxy.ServeHTTP(w, r)
|
|
|
|
return 0, nil
|
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
return p.Next.ServeHTTP(w, r)
|
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
// New creates a new instance of proxy middleware.
|
|
|
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
|
|
|
rules, err := parse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-01 13:53:39 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return Proxy{Next: next, Rules: rules}
|
|
|
|
}, nil
|
|
|
|
}
|
2015-01-30 08:17:59 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
func parse(c middleware.Controller) ([]Rule, error) {
|
|
|
|
var rules []Rule
|
2015-02-03 14:41:35 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
for c.Next() {
|
|
|
|
var rule Rule
|
|
|
|
if !c.Args(&rule.From, &rule.To) {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
2015-03-29 06:56:56 +08:00
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
return rules, nil
|
2015-01-30 08:17:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-12 07:24:47 +08:00
|
|
|
type Rule struct {
|
|
|
|
From, To string
|
2015-01-30 08:17:59 +08:00
|
|
|
}
|