solid proxy infrastructure

This commit is contained in:
Michael Quigley 2022-07-21 16:01:39 -04:00
parent 18769c3230
commit af3328f333
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 50 additions and 31 deletions

View File

@ -1,12 +1,11 @@
package http package http
import ( import (
"github.com/openziti-test-kitchen/zrok/util"
"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"
"net/http" "net/http"
"net/http/httputil"
"net/url"
"time" "time"
) )
@ -24,24 +23,13 @@ func Run(cfg *Config) error {
return errors.Wrap(err, "error listening") return errors.Wrap(err, "error listening")
} }
targetURL, err := url.Parse("http://localhost:3000") proxy, err := util.NewProxy("http://localhost:3000")
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing url") return err
} }
if err := http.Serve(listener, util.NewProxyHandler(proxy)); err != nil {
proxy := httputil.NewSingleHostReverseProxy(targetURL)
if err := http.Serve(listener, &proxyHandler{proxy: proxy}); err != nil {
return err return err
} }
return nil return nil
} }
type proxyHandler struct {
proxy *httputil.ReverseProxy
}
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.proxy.ServeHTTP(w, r)
}

View File

@ -6,8 +6,6 @@ import (
"github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors" "github.com/pkg/errors"
"net/http" "net/http"
"net/http/httputil"
"net/url"
) )
func Run(cfg *Config) error { func Run(cfg *Config) error {
@ -20,20 +18,10 @@ 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
targetURL, err := url.Parse("http://zrok") proxy, err := util.NewProxy("http://zrok")
if err != nil { if err != nil {
return errors.Wrap(err, "error parsing url") return err
} }
proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy.Transport = zTransport proxy.Transport = zTransport
return http.ListenAndServe(cfg.Address, &proxyHandler{proxy: proxy}) return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
}
type proxyHandler struct {
proxy *httputil.ReverseProxy
}
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.proxy.ServeHTTP(w, r)
} }

43
util/proxy.go Normal file
View File

@ -0,0 +1,43 @@
package util
import (
"github.com/sirupsen/logrus"
"net/http"
"net/http/httputil"
"net/url"
)
func NewProxy(target string) (*httputil.ReverseProxy, error) {
targetURL, err := url.Parse(target)
if err != nil {
return nil, err
}
proxy := httputil.NewSingleHostReverseProxy(targetURL)
director := proxy.Director
proxy.Director = func(req *http.Request) {
director(req)
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 {
proxy *httputil.ReverseProxy
}
func NewProxyHandler(proxy *httputil.ReverseProxy) *proxyHandler {
return &proxyHandler{proxy}
}
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logrus.Infof("proxying from: %v", r.RequestURI)
self.proxy.ServeHTTP(w, r)
}