mirror of
https://github.com/openziti/zrok.git
synced 2024-11-25 01:23:49 +01:00
solid proxy infrastructure
This commit is contained in:
parent
18769c3230
commit
af3328f333
20
http/http.go
20
http/http.go
@ -1,12 +1,11 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/openziti-test-kitchen/zrok/util"
|
||||
"github.com/openziti/sdk-golang/ziti"
|
||||
"github.com/openziti/sdk-golang/ziti/config"
|
||||
"github.com/pkg/errors"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -24,24 +23,13 @@ func Run(cfg *Config) error {
|
||||
return errors.Wrap(err, "error listening")
|
||||
}
|
||||
|
||||
targetURL, err := url.Parse("http://localhost:3000")
|
||||
proxy, err := util.NewProxy("http://localhost:3000")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing url")
|
||||
return err
|
||||
}
|
||||
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
||||
|
||||
if err := http.Serve(listener, &proxyHandler{proxy: proxy}); err != nil {
|
||||
if err := http.Serve(listener, util.NewProxyHandler(proxy)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type proxyHandler struct {
|
||||
proxy *httputil.ReverseProxy
|
||||
}
|
||||
|
||||
func (self *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
self.proxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
@ -6,8 +6,6 @@ import (
|
||||
"github.com/openziti/sdk-golang/ziti/config"
|
||||
"github.com/pkg/errors"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func Run(cfg *Config) error {
|
||||
@ -20,20 +18,10 @@ func Run(cfg *Config) error {
|
||||
zTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
zTransport.DialContext = zDialCtx.Dial
|
||||
|
||||
targetURL, err := url.Parse("http://zrok")
|
||||
proxy, err := util.NewProxy("http://zrok")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error parsing url")
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
|
||||
}
|
||||
|
43
util/proxy.go
Normal file
43
util/proxy.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user