mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-04 01:09:36 +02:00
proxy: use middleware to manage request flow
proxy: remove duplicate error handling in New proxy: remove routeConfigs in favor of using gorilla/mux proxy: add proxy specific middleware proxy: no longer need to use middleware / handler to check if valid route. Can use build in 404 mux. internal/middleware: add cors bypass middleware Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
parent
70c5553d3c
commit
782ffbeb3e
17 changed files with 834 additions and 839 deletions
31
internal/httputil/proxy.go
Normal file
31
internal/httputil/proxy.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package httputil // import "github.com/pomerium/pomerium/internal/httputil"
|
||||
|
||||
import (
|
||||
stdlog "log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
)
|
||||
|
||||
// HeaderForwardHost is the header key the identifies the originating
|
||||
// IP addresses of a client connecting to a web server through an HTTP proxy
|
||||
// or a load balancer.
|
||||
const HeaderForwardHost = "X-Forwarded-Host"
|
||||
|
||||
// NewReverseProxy returns a new ReverseProxy that routes
|
||||
// URLs to the scheme, host, and base path provided in target,
|
||||
// rewrites the Host header, and sets `X-Forwarded-Host`.
|
||||
func NewReverseProxy(target *url.URL) *httputil.ReverseProxy {
|
||||
reverseProxy := httputil.NewSingleHostReverseProxy(target)
|
||||
sublogger := log.With().Str("reverse-proxy", target.Host).Logger()
|
||||
reverseProxy.ErrorLog = stdlog.New(&log.StdLogWrapper{Logger: &sublogger}, "", 0)
|
||||
director := reverseProxy.Director
|
||||
reverseProxy.Director = func(req *http.Request) {
|
||||
req.Header.Add(HeaderForwardHost, req.Host)
|
||||
director(req)
|
||||
req.Host = target.Host
|
||||
}
|
||||
return reverseProxy
|
||||
}
|
35
internal/httputil/proxy_test.go
Normal file
35
internal/httputil/proxy_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package httputil // import "github.com/pomerium/pomerium/internal/httputil"
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewReverseProxy(t *testing.T) {
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
hostname, _, _ := net.SplitHostPort(r.Host)
|
||||
w.Write([]byte(hostname))
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
backendURL, _ := url.Parse(backend.URL)
|
||||
backendHostname, backendPort, _ := net.SplitHostPort(backendURL.Host)
|
||||
backendHost := net.JoinHostPort(backendHostname, backendPort)
|
||||
proxyURL, _ := url.Parse(backendURL.Scheme + "://" + backendHost + "/")
|
||||
|
||||
proxyHandler := NewReverseProxy(proxyURL)
|
||||
frontend := httptest.NewServer(proxyHandler)
|
||||
defer frontend.Close()
|
||||
|
||||
getReq, _ := http.NewRequest("GET", frontend.URL, nil)
|
||||
res, _ := http.DefaultClient.Do(getReq)
|
||||
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
||||
if g, e := string(bodyBytes), backendHostname; g != e {
|
||||
t.Errorf("got body %q; expected %q", g, e)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package httputil
|
||||
package httputil // import "github.com/pomerium/pomerium/internal/httputil"
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue