From 6698cb98d7633ef3d2fb79fa984758fc932d7b19 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 10 Aug 2022 15:15:35 -0400 Subject: [PATCH] proxy lint --- proxy/config.go | 6 --- proxy/proxy.go | 108 +++++++++++++++++++++++++++++++++++++++++++++- proxy/service.go | 89 -------------------------------------- proxy/zdialctx.go | 23 ---------- 4 files changed, 106 insertions(+), 120 deletions(-) delete mode 100644 proxy/config.go delete mode 100644 proxy/service.go delete mode 100644 proxy/zdialctx.go diff --git a/proxy/config.go b/proxy/config.go deleted file mode 100644 index 7599fafd..00000000 --- a/proxy/config.go +++ /dev/null @@ -1,6 +0,0 @@ -package proxy - -type Config struct { - IdentityPath string - Address string -} diff --git a/proxy/proxy.go b/proxy/proxy.go index 411d848c..2aa1cccf 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,15 +1,25 @@ package proxy import ( + "context" + "fmt" "github.com/openziti-test-kitchen/zrok/util" "github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti/config" "github.com/pkg/errors" "github.com/sirupsen/logrus" + "net" "net/http" + "net/http/httputil" + "net/url" "strings" ) +type Config struct { + IdentityPath string + Address string +} + func Run(cfg *Config) error { zCfg, err := config.NewFromFile(cfg.IdentityPath) if err != nil { @@ -28,8 +38,7 @@ func Run(cfg *Config) error { return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy)) } -type resolver struct { -} +type resolver struct{} func (r *resolver) Service(host string) string { logrus.Debugf("host = '%v'", host) @@ -39,3 +48,98 @@ func (r *resolver) Service(host string) string { } return "zrok" } + +type ZitiDialContext struct { + Context ziti.Context +} + +func (self *ZitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) { + service := strings.Split(addr, ":")[0] // ignore :port (we get passed 'host:port') + _, found := self.Context.GetService(service) + if !found { + logrus.Infof("service '%v' not cached; refreshing", service) + if err := self.Context.RefreshServices(); err != nil { + return nil, errors.Wrap(err, "error refreshing services") + } + } + return self.Context.Dial(service) +} + +type ProxyServiceResolver interface { + Service(host string) string +} + +func NewServiceProxy(p ProxyServiceResolver) (*httputil.ReverseProxy, error) { + proxy := hostTargetReverseProxy(p) + director := proxy.Director + proxy.Director = func(req *http.Request) { + director(req) + logrus.Debugf("-> %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 +} + +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", "") + } + } else { + logrus.Errorf("error proxying: %v", err) + } + } + 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 +} diff --git a/proxy/service.go b/proxy/service.go deleted file mode 100644 index 97d2fb91..00000000 --- a/proxy/service.go +++ /dev/null @@ -1,89 +0,0 @@ -package proxy - -import ( - "fmt" - "github.com/sirupsen/logrus" - "net/http" - "net/http/httputil" - "net/url" - "strings" -) - -type ProxyServiceResolver interface { - Service(host string) string -} - -func NewServiceProxy(p ProxyServiceResolver) (*httputil.ReverseProxy, error) { - proxy := hostTargetReverseProxy(p) - director := proxy.Director - proxy.Director = func(req *http.Request) { - director(req) - logrus.Debugf("-> %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 -} - -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", "") - } - } else { - logrus.Errorf("error proxying: %v", err) - } - } - 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 -} diff --git a/proxy/zdialctx.go b/proxy/zdialctx.go deleted file mode 100644 index 824ac3af..00000000 --- a/proxy/zdialctx.go +++ /dev/null @@ -1,23 +0,0 @@ -package proxy - -import ( - "context" - "github.com/openziti/sdk-golang/ziti" - "github.com/sirupsen/logrus" - "net" - "strings" -) - -type ZitiDialContext struct { - Context ziti.Context -} - -func (self *ZitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) { - service := strings.Split(addr, ":")[0] // ignore :port (we get passed 'host:port') - _, found := self.Context.GetService(service) - if !found { - logrus.Infof("service '%v' not cached; refreshing", service) - self.Context.RefreshServices() - } - return self.Context.Dial(service) -}