httputil.ReverseProxy version skeleton

This commit is contained in:
Michael Quigley 2022-07-21 15:46:14 -04:00
parent fe7e3acbca
commit 18769c3230
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 33 additions and 92 deletions

View File

@ -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)
}

View File

@ -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)
}