mirror of
https://github.com/openziti/zrok.git
synced 2024-11-24 17:13:51 +01:00
httputil.ReverseProxy version skeleton
This commit is contained in:
parent
fe7e3acbca
commit
18769c3230
59
http/http.go
59
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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user