2020-05-26 20:53:10 +02:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-02-22 17:55:08 +02:00
|
|
|
"strings"
|
2020-05-26 20:53:10 +02:00
|
|
|
|
2021-02-22 17:55:08 +02:00
|
|
|
"github.com/gorilla/mux"
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
2021-03-31 11:30:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
|
2020-09-29 18:44:42 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
2020-05-26 20:53:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProxyErrorHandler is a function that will be used to render error pages when
|
|
|
|
// HTTP proxies fail to connect to upstream servers.
|
|
|
|
type ProxyErrorHandler func(http.ResponseWriter, *http.Request, error)
|
|
|
|
|
|
|
|
// NewProxy creates a new multiUpstreamProxy that can serve requests directed to
|
|
|
|
// multiple upstreams.
|
2021-03-31 11:30:42 +02:00
|
|
|
func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, writer pagewriter.Writer) (http.Handler, error) {
|
2020-05-26 20:53:10 +02:00
|
|
|
m := &multiUpstreamProxy{
|
2021-02-22 17:55:08 +02:00
|
|
|
serveMux: mux.NewRouter(),
|
2020-05-26 20:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, upstream := range upstreams {
|
|
|
|
if upstream.Static {
|
|
|
|
m.registerStaticResponseHandler(upstream)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(upstream.URI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing URI for upstream %q: %w", upstream.ID, err)
|
|
|
|
}
|
|
|
|
switch u.Scheme {
|
|
|
|
case fileScheme:
|
|
|
|
m.registerFileServer(upstream, u)
|
|
|
|
case httpScheme, httpsScheme:
|
2021-03-31 11:30:42 +02:00
|
|
|
m.registerHTTPUpstreamProxy(upstream, u, sigData, writer)
|
2020-05-26 20:53:10 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown scheme for upstream %q: %q", upstream.ID, u.Scheme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiUpstreamProxy will serve requests directed to multiple upstream servers
|
|
|
|
// registered in the serverMux.
|
|
|
|
type multiUpstreamProxy struct {
|
2021-02-22 17:55:08 +02:00
|
|
|
serveMux *mux.Router
|
2020-05-26 20:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerHTTP handles HTTP requests.
|
|
|
|
func (m *multiUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
m.serveMux.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerStaticResponseHandler registers a static response handler with at the given path.
|
|
|
|
func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
|
2020-08-31 12:22:10 +02:00
|
|
|
logger.Printf("mapping path %q => static response %d", upstream.Path, derefStaticCode(upstream.StaticCode))
|
2021-02-22 17:55:08 +02:00
|
|
|
m.registerSimpleHandler(upstream.Path, newStaticResponseHandler(upstream.ID, upstream.StaticCode))
|
2020-05-26 20:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerFileServer registers a new fileServer based on the configuration given.
|
|
|
|
func (m *multiUpstreamProxy) registerFileServer(upstream options.Upstream, u *url.URL) {
|
|
|
|
logger.Printf("mapping path %q => file system %q", upstream.Path, u.Path)
|
2021-02-22 17:55:08 +02:00
|
|
|
m.registerSimpleHandler(upstream.Path, newFileServer(upstream.ID, upstream.Path, u.Path))
|
2020-05-26 20:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerHTTPUpstreamProxy registers a new httpUpstreamProxy based on the configuration given.
|
2021-03-31 11:30:42 +02:00
|
|
|
func (m *multiUpstreamProxy) registerHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, writer pagewriter.Writer) {
|
2020-05-26 20:53:10 +02:00
|
|
|
logger.Printf("mapping path %q => upstream %q", upstream.Path, upstream.URI)
|
2021-02-22 17:55:08 +02:00
|
|
|
m.registerSimpleHandler(upstream.Path, newHTTPUpstreamProxy(upstream, u, sigData, writer.ProxyErrorHandler))
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerSimpleHandler maintains the behaviour of the go standard serveMux
|
|
|
|
// by ensuring any path with a trailing `/` matches all paths under that prefix.
|
|
|
|
func (m *multiUpstreamProxy) registerSimpleHandler(path string, handler http.Handler) {
|
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
m.serveMux.PathPrefix(path).Handler(handler)
|
|
|
|
} else {
|
|
|
|
m.serveMux.Path(path).Handler(handler)
|
|
|
|
}
|
2020-05-26 20:53:10 +02:00
|
|
|
}
|