proxyBackend/proxyFrontend -> proxy (#170)

This commit is contained in:
Michael Quigley 2023-04-18 13:36:43 -04:00
parent 1a38aef26d
commit 4c41908ce2
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
6 changed files with 32 additions and 32 deletions

View File

@ -5,7 +5,7 @@ import (
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/proxyFrontend" "github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/rest_client_zrok" "github.com/openziti/zrok/rest_client_zrok"
"github.com/openziti/zrok/rest_client_zrok/share" "github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
@ -88,7 +88,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
} }
logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken) logrus.Infof("allocated frontend '%v'", accessResp.Payload.FrontendToken)
cfg := proxyFrontend.DefaultConfig("backend") cfg := proxy.DefaultFrontendConfig("backend")
cfg.ShrToken = shrToken cfg.ShrToken = shrToken
cfg.Address = cmd.bindAddress cfg.Address = cmd.bindAddress
cfg.RequestsChan = make(chan *endpoints.Request, 1024) cfg.RequestsChan = make(chan *endpoints.Request, 1024)
@ -101,7 +101,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
os.Exit(0) os.Exit(0)
}() }()
frontend, err := proxyFrontend.NewHTTP(cfg) frontend, err := proxy.NewFrontend(cfg)
if err != nil { if err != nil {
if !panicInstead { if !panicInstead {
tui.Error("unable to create private frontend", err) tui.Error("unable to create private frontend", err)

View File

@ -6,7 +6,7 @@ import (
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/proxyBackend" "github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/endpoints/tcpTunnel" "github.com/openziti/zrok/endpoints/tcpTunnel"
"github.com/openziti/zrok/endpoints/webBackend" "github.com/openziti/zrok/endpoints/webBackend"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
@ -145,7 +145,7 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
requestsChan := make(chan *endpoints.Request, 1024) requestsChan := make(chan *endpoints.Request, 1024)
switch cmd.backendMode { switch cmd.backendMode {
case "proxy": case "proxy":
cfg := &proxyBackend.Config{ cfg := &proxy.BackendConfig{
IdentityPath: zif, IdentityPath: zif,
EndpointAddress: target, EndpointAddress: target,
ShrToken: resp.Payload.ShrToken, ShrToken: resp.Payload.ShrToken,
@ -226,8 +226,8 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) {
} }
} }
func (cmd *sharePrivateCommand) proxyBackendMode(cfg *proxyBackend.Config) (endpoints.RequestHandler, error) { func (cmd *sharePrivateCommand) proxyBackendMode(cfg *proxy.BackendConfig) (endpoints.RequestHandler, error) {
be, err := proxyBackend.New(cfg) be, err := proxy.NewBackend(cfg)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error creating http proxy backend") return nil, errors.Wrap(err, "error creating http proxy backend")
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/proxyBackend" "github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/endpoints/webBackend" "github.com/openziti/zrok/endpoints/webBackend"
"github.com/openziti/zrok/model" "github.com/openziti/zrok/model"
"github.com/openziti/zrok/rest_client_zrok" "github.com/openziti/zrok/rest_client_zrok"
@ -142,7 +142,7 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
requestsChan := make(chan *endpoints.Request, 1024) requestsChan := make(chan *endpoints.Request, 1024)
switch cmd.backendMode { switch cmd.backendMode {
case "proxy": case "proxy":
cfg := &proxyBackend.Config{ cfg := &proxy.BackendConfig{
IdentityPath: zif, IdentityPath: zif,
EndpointAddress: target, EndpointAddress: target,
ShrToken: resp.Payload.ShrToken, ShrToken: resp.Payload.ShrToken,
@ -209,8 +209,8 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
} }
} }
func (cmd *sharePublicCommand) proxyBackendMode(cfg *proxyBackend.Config) (endpoints.RequestHandler, error) { func (cmd *sharePublicCommand) proxyBackendMode(cfg *proxy.BackendConfig) (endpoints.RequestHandler, error) {
be, err := proxyBackend.New(cfg) be, err := proxy.NewBackend(cfg)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error creating http proxy backend") return nil, errors.Wrap(err, "error creating http proxy backend")
} }

View File

@ -5,7 +5,7 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
httptransport "github.com/go-openapi/runtime/client" httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/endpoints" "github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/proxyBackend" "github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/endpoints/webBackend" "github.com/openziti/zrok/endpoints/webBackend"
"github.com/openziti/zrok/rest_client_zrok/metadata" "github.com/openziti/zrok/rest_client_zrok/metadata"
"github.com/openziti/zrok/rest_client_zrok/share" "github.com/openziti/zrok/rest_client_zrok/share"
@ -108,7 +108,7 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
requestsChan := make(chan *endpoints.Request, 1024) requestsChan := make(chan *endpoints.Request, 1024)
switch resp.Payload.BackendMode { switch resp.Payload.BackendMode {
case "proxy": case "proxy":
cfg := &proxyBackend.Config{ cfg := &proxy.BackendConfig{
IdentityPath: zif, IdentityPath: zif,
EndpointAddress: target, EndpointAddress: target,
ShrToken: shrToken, ShrToken: shrToken,
@ -187,8 +187,8 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
} }
} }
func (cmd *shareReservedCommand) proxyBackendMode(cfg *proxyBackend.Config) (endpoints.RequestHandler, error) { func (cmd *shareReservedCommand) proxyBackendMode(cfg *proxy.BackendConfig) (endpoints.RequestHandler, error) {
be, err := proxyBackend.New(cfg) be, err := proxy.NewBackend(cfg)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error creating http proxy backend") return nil, errors.Wrap(err, "error creating http proxy backend")
} }

View File

@ -1,4 +1,4 @@
package proxyBackend package proxy
import ( import (
"crypto/tls" "crypto/tls"
@ -16,7 +16,7 @@ import (
"time" "time"
) )
type Config struct { type BackendConfig struct {
IdentityPath string IdentityPath string
EndpointAddress string EndpointAddress string
ShrToken string ShrToken string
@ -25,13 +25,13 @@ type Config struct {
} }
type Backend struct { type Backend struct {
cfg *Config cfg *BackendConfig
requests func() int32 requests func() int32
listener edge.Listener listener edge.Listener
handler http.Handler handler http.Handler
} }
func New(cfg *Config) (*Backend, error) { func NewBackend(cfg *BackendConfig) (*Backend, error) {
options := ziti.ListenOptions{ options := ziti.ListenOptions{
ConnectTimeout: 5 * time.Minute, ConnectTimeout: 5 * time.Minute,
MaxConnections: 64, MaxConnections: 64,
@ -70,7 +70,7 @@ func (b *Backend) Requests() func() int32 {
return b.requests return b.requests
} }
func newReverseProxy(cfg *Config) (*httputil.ReverseProxy, error) { func newReverseProxy(cfg *BackendConfig) (*httputil.ReverseProxy, error) {
targetURL, err := url.Parse(cfg.EndpointAddress) targetURL, err := url.Parse(cfg.EndpointAddress)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,4 +1,4 @@
package proxyFrontend package proxy
import ( import (
"context" "context"
@ -19,28 +19,28 @@ import (
"time" "time"
) )
type Config struct { type FrontendConfig struct {
IdentityName string IdentityName string
ShrToken string ShrToken string
Address string Address string
RequestsChan chan *endpoints.Request RequestsChan chan *endpoints.Request
} }
func DefaultConfig(identityName string) *Config { func DefaultFrontendConfig(identityName string) *FrontendConfig {
return &Config{ return &FrontendConfig{
IdentityName: identityName, IdentityName: identityName,
Address: "0.0.0.0:8080", Address: "0.0.0.0:8080",
} }
} }
type httpFrontend struct { type Frontend struct {
cfg *Config cfg *FrontendConfig
zCtx ziti.Context zCtx ziti.Context
shrToken string shrToken string
handler http.Handler handler http.Handler
} }
func NewHTTP(cfg *Config) (*httpFrontend, error) { func NewFrontend(cfg *FrontendConfig) (*Frontend, error) {
zCfgPath, err := zrokdir.ZitiIdentityFile(cfg.IdentityName) zCfgPath, err := zrokdir.ZitiIdentityFile(cfg.IdentityName)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error getting ziti identity '%v' from zrokdir", cfg.IdentityName) return nil, errors.Wrapf(err, "error getting ziti identity '%v' from zrokdir", cfg.IdentityName)
@ -62,14 +62,14 @@ func NewHTTP(cfg *Config) (*httpFrontend, error) {
proxy.Transport = zTransport proxy.Transport = zTransport
handler := authHandler(cfg.ShrToken, util.NewProxyHandler(proxy), "zrok", cfg, zCtx) handler := authHandler(cfg.ShrToken, util.NewProxyHandler(proxy), "zrok", cfg, zCtx)
return &httpFrontend{ return &Frontend{
cfg: cfg, cfg: cfg,
zCtx: zCtx, zCtx: zCtx,
handler: handler, handler: handler,
}, nil }, nil
} }
func (h *httpFrontend) Run() error { func (h *Frontend) Run() error {
return http.ListenAndServe(h.cfg.Address, h.handler) return http.ListenAndServe(h.cfg.Address, h.handler)
} }
@ -86,7 +86,7 @@ func (zdc *zitiDialContext) Dial(_ context.Context, _ string, addr string) (net.
return conn, nil return conn, nil
} }
func newServiceProxy(cfg *Config, ctx ziti.Context) (*httputil.ReverseProxy, error) { func newServiceProxy(cfg *FrontendConfig, ctx ziti.Context) (*httputil.ReverseProxy, error) {
proxy := serviceTargetProxy(cfg, ctx) proxy := serviceTargetProxy(cfg, ctx)
director := proxy.Director director := proxy.Director
proxy.Director = func(req *http.Request) { proxy.Director = func(req *http.Request) {
@ -111,7 +111,7 @@ func newServiceProxy(cfg *Config, ctx ziti.Context) (*httputil.ReverseProxy, err
return proxy, nil return proxy, nil
} }
func serviceTargetProxy(cfg *Config, ctx ziti.Context) *httputil.ReverseProxy { func serviceTargetProxy(cfg *FrontendConfig, ctx ziti.Context) *httputil.ReverseProxy {
director := func(req *http.Request) { director := func(req *http.Request) {
targetShrToken := cfg.ShrToken targetShrToken := cfg.ShrToken
if svc, found := endpoints.GetRefreshedService(targetShrToken, ctx); found { if svc, found := endpoints.GetRefreshedService(targetShrToken, ctx); found {
@ -144,7 +144,7 @@ func serviceTargetProxy(cfg *Config, ctx ziti.Context) *httputil.ReverseProxy {
return &httputil.ReverseProxy{Director: director} return &httputil.ReverseProxy{Director: director}
} }
func authHandler(shrToken string, handler http.Handler, realm string, cfg *Config, ctx ziti.Context) http.HandlerFunc { func authHandler(shrToken string, handler http.Handler, realm string, cfg *FrontendConfig, ctx ziti.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if svc, found := endpoints.GetRefreshedService(shrToken, ctx); found { if svc, found := endpoints.GetRefreshedService(shrToken, ctx); found {
if cfg, found := svc.Configs[model.ZrokProxyConfig]; found { if cfg, found := svc.Configs[model.ZrokProxyConfig]; found {