From 18769c32308abc11834b0784e062fba137d481a8 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 21 Jul 2022 15:46:14 -0400 Subject: [PATCH] httputil.ReverseProxy version skeleton --- http/http.go | 59 ++++++++++++-------------------------------- proxy/proxy.go | 66 +++++++++++++------------------------------------- 2 files changed, 33 insertions(+), 92 deletions(-) diff --git a/http/http.go b/http/http.go index 1511fc55..2fe1b655 100644 --- a/http/http.go +++ b/http/http.go @@ -1,14 +1,12 @@ package http import ( - "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" - "io" "net/http" + "net/http/httputil" + "net/url" "time" ) @@ -26,49 +24,24 @@ func Run(cfg *Config) error { return errors.Wrap(err, "error listening") } - if err := http.Serve(listener, &handler{}); err != nil { + targetURL, err := url.Parse("http://localhost:3000") + if err != nil { + return errors.Wrap(err, "error parsing url") + } + + proxy := httputil.NewSingleHostReverseProxy(targetURL) + + if err := http.Serve(listener, &proxyHandler{proxy: proxy}); err != nil { return err } return nil } -type handler struct{} - -func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - logrus.Warnf("handling request from [%v]", r.RemoteAddr) - - r.Host = "localhost:3000" - r.URL.Host = "localhost:3000" - r.URL.Scheme = "http" - r.RequestURI = "" - logrus.Info(util.DumpHeaders(r.Header, true)) - - logrus.Infof("forwarding to: %v [%v]", r.Method, r.URL) - rr, err := http.DefaultClient.Do(r) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = fmt.Fprint(w, err) - return - } - w.WriteHeader(rr.StatusCode) - logrus.Infof("response: %v", rr.Status) - - // forward headers - for k, v := range rr.Header { - for _, vi := range v { - w.Header().Add(k, vi) - } - } - logrus.Info(util.DumpHeaders(w.Header(), false)) - - // copy body - n, err := io.Copy(w, rr.Body) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = fmt.Fprint(w, err) - return - } - - logrus.Infof("proxied [%d] bytes", n) +type proxyHandler struct { + proxy *httputil.ReverseProxy +} + +func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + self.proxy.ServeHTTP(w, r) } diff --git a/proxy/proxy.go b/proxy/proxy.go index c8323fa6..f40c0da4 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,14 +1,13 @@ package proxy import ( - "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" - "io" "net/http" + "net/http/httputil" + "net/url" ) func Run(cfg *Config) error { @@ -17,55 +16,24 @@ func Run(cfg *Config) error { return errors.Wrap(err, "error loading config") } zCtx := ziti.NewContextWithConfig(zCfg) - handler := &handler{ - zCfg: zCfg, - zCtx: zCtx, - } - - return http.ListenAndServe(cfg.Address, handler) -} - -type handler struct { - zCfg *config.Config - zCtx ziti.Context -} - -func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - logrus.Warnf("handling request from [%v]", r.RemoteAddr) - - r.Host = "zrok" - r.URL.Host = "zrok" - r.URL.Scheme = "http" - r.RequestURI = "" - logrus.Info(util.DumpHeaders(r.Header, true)) - - logrus.Infof("forwarding to: %v [%v]", r.Method, r.URL) - zDialCtx := util.ZitiDialContext{Context: self.zCtx} + zDialCtx := util.ZitiDialContext{Context: zCtx} zTransport := http.DefaultTransport.(*http.Transport).Clone() zTransport.DialContext = zDialCtx.Dial - zClient := &http.Client{Transport: zTransport} - rr, err := zClient.Do(r) + + targetURL, err := url.Parse("http://zrok") if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = fmt.Fprint(w, err) - return - } - w.WriteHeader(rr.StatusCode) - logrus.Infof("response: %v", rr.Status) - - // forward headers - for k, v := range rr.Header { - for _, vi := range v { - w.Header().Add(k, vi) - } - } - logrus.Info(util.DumpHeaders(w.Header(), false)) - - // copy body - n, err := io.Copy(w, rr.Body) - if err != nil { - panic(err) + return errors.Wrap(err, "error parsing url") } - logrus.Infof("proxied [%d] bytes", n) + proxy := httputil.NewSingleHostReverseProxy(targetURL) + proxy.Transport = zTransport + return http.ListenAndServe(cfg.Address, &proxyHandler{proxy: proxy}) +} + +type proxyHandler struct { + proxy *httputil.ReverseProxy +} + +func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + self.proxy.ServeHTTP(w, r) }