mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 08:44:14 +01:00
adjust reverse proxy infrastructure to selectively proxy based on host header (#7)
This commit is contained in:
parent
cad9a2bf5b
commit
0b6469b742
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/openziti/sdk-golang/ziti"
|
"github.com/openziti/sdk-golang/ziti"
|
||||||
"github.com/openziti/sdk-golang/ziti/config"
|
"github.com/openziti/sdk-golang/ziti/config"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,10 +19,18 @@ func Run(cfg *Config) error {
|
|||||||
zTransport := http.DefaultTransport.(*http.Transport).Clone()
|
zTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
zTransport.DialContext = zDialCtx.Dial
|
zTransport.DialContext = zDialCtx.Dial
|
||||||
|
|
||||||
proxy, err := util.NewProxy("http://zrok")
|
proxy, err := util.NewServiceProxy(&resolver{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
proxy.Transport = zTransport
|
proxy.Transport = zTransport
|
||||||
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
|
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type resolver struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *resolver) Service(host string) string {
|
||||||
|
logrus.Infof("host = '%v'", host)
|
||||||
|
return "zrok"
|
||||||
|
}
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ProxyServiceResolver interface {
|
||||||
|
Service(host string) string
|
||||||
|
}
|
||||||
|
|
||||||
func NewProxy(target string) (*httputil.ReverseProxy, error) {
|
func NewProxy(target string) (*httputil.ReverseProxy, error) {
|
||||||
targetURL, err := url.Parse(target)
|
targetURL, err := url.Parse(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -30,6 +36,24 @@ func NewProxy(target string) (*httputil.ReverseProxy, error) {
|
|||||||
return proxy, nil
|
return proxy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewServiceProxy(p ProxyServiceResolver) (*httputil.ReverseProxy, error) {
|
||||||
|
proxy := hostTargetReverseProxy(p)
|
||||||
|
director := proxy.Director
|
||||||
|
proxy.Director = func(req *http.Request) {
|
||||||
|
director(req)
|
||||||
|
logrus.Infof("-> %v", req.URL.String())
|
||||||
|
req.Header.Set("X-Proxy", "zrok")
|
||||||
|
}
|
||||||
|
proxy.ModifyResponse = func(resp *http.Response) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
|
logrus.Errorf("error proxying: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return proxy, nil
|
||||||
|
}
|
||||||
|
|
||||||
type proxyHandler struct {
|
type proxyHandler struct {
|
||||||
proxy *httputil.ReverseProxy
|
proxy *httputil.ReverseProxy
|
||||||
}
|
}
|
||||||
@ -41,3 +65,58 @@ func NewProxyHandler(proxy *httputil.ReverseProxy) *proxyHandler {
|
|||||||
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
self.proxy.ServeHTTP(w, r)
|
self.proxy.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hostTargetReverseProxy(r ProxyServiceResolver) *httputil.ReverseProxy {
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
targetSvc := r.Service(req.Host)
|
||||||
|
if target, err := url.Parse(fmt.Sprintf("http://%v", targetSvc)); err == nil {
|
||||||
|
targetQuery := target.RawQuery
|
||||||
|
req.URL.Scheme = target.Scheme
|
||||||
|
req.URL.Host = target.Host
|
||||||
|
req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
|
||||||
|
if targetQuery == "" || req.URL.RawQuery == "" {
|
||||||
|
req.URL.RawQuery = targetQuery + req.URL.RawQuery
|
||||||
|
} else {
|
||||||
|
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
|
||||||
|
}
|
||||||
|
if _, ok := req.Header["User-Agent"]; !ok {
|
||||||
|
// explicitly disable User-Agent so it's not set to default value
|
||||||
|
req.Header.Set("User-Agent", "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &httputil.ReverseProxy{Director: director}
|
||||||
|
}
|
||||||
|
|
||||||
|
func joinURLPath(a, b *url.URL) (path, rawpath string) {
|
||||||
|
if a.RawPath == "" && b.RawPath == "" {
|
||||||
|
return singleJoiningSlash(a.Path, b.Path), ""
|
||||||
|
}
|
||||||
|
// Same as singleJoiningSlash, but uses EscapedPath to determine
|
||||||
|
// whether a slash should be added
|
||||||
|
apath := a.EscapedPath()
|
||||||
|
bpath := b.EscapedPath()
|
||||||
|
|
||||||
|
aslash := strings.HasSuffix(apath, "/")
|
||||||
|
bslash := strings.HasPrefix(bpath, "/")
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case aslash && bslash:
|
||||||
|
return a.Path + b.Path[1:], apath + bpath[1:]
|
||||||
|
case !aslash && !bslash:
|
||||||
|
return a.Path + "/" + b.Path, apath + "/" + bpath
|
||||||
|
}
|
||||||
|
return a.Path + b.Path, apath + bpath
|
||||||
|
}
|
||||||
|
|
||||||
|
func singleJoiningSlash(a, b string) string {
|
||||||
|
aslash := strings.HasSuffix(a, "/")
|
||||||
|
bslash := strings.HasPrefix(b, "/")
|
||||||
|
switch {
|
||||||
|
case aslash && bslash:
|
||||||
|
return a + b[1:]
|
||||||
|
case !aslash && !bslash:
|
||||||
|
return a + "/" + b
|
||||||
|
}
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user