mirror of
https://github.com/openziti/zrok.git
synced 2025-06-27 05:01:26 +02:00
separate out 'proxy' backend mode from public sharing (#95)
This commit is contained in:
parent
9a8b22e15c
commit
5f29d379b1
@ -32,33 +32,44 @@ type sharePublicCommand struct {
|
|||||||
quiet bool
|
quiet bool
|
||||||
basicAuth []string
|
basicAuth []string
|
||||||
frontendSelection []string
|
frontendSelection []string
|
||||||
|
backendMode string
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSharePublicCommand() *sharePublicCommand {
|
func newSharePublicCommand() *sharePublicCommand {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "public <targetEndpoint>",
|
Use: "public <target>",
|
||||||
Short: "Share a target endpoint publicly",
|
Short: "Share a target resource publicly",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
}
|
}
|
||||||
command := &sharePublicCommand{cmd: cmd}
|
command := &sharePublicCommand{cmd: cmd}
|
||||||
cmd.Flags().BoolVarP(&command.quiet, "quiet", "q", false, "Disable TUI 'chrome' for quiet operation")
|
cmd.Flags().BoolVarP(&command.quiet, "quiet", "q", false, "Disable TUI 'chrome' for quiet operation")
|
||||||
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
|
cmd.Flags().StringArrayVar(&command.basicAuth, "basic-auth", []string{}, "Basic authentication users (<username:password>,...)")
|
||||||
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontends", []string{"public"}, "Selected frontends to use for the share")
|
cmd.Flags().StringArrayVar(&command.frontendSelection, "frontends", []string{"public"}, "Selected frontends to use for the share")
|
||||||
|
cmd.Flags().StringVar(&command.backendMode, "backend-mode", "proxy", "The backend mode {proxy, web}")
|
||||||
cmd.Run = command.run
|
cmd.Run = command.run
|
||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
||||||
targetEndpoint, err := url.Parse(args[0])
|
var target string
|
||||||
if err != nil {
|
|
||||||
if !panicInstead {
|
switch self.backendMode {
|
||||||
showError("invalid target endpoint URL", err)
|
case "proxy":
|
||||||
|
targetEndpoint, err := url.Parse(args[0])
|
||||||
|
if err != nil {
|
||||||
|
if !panicInstead {
|
||||||
|
showError("invalid target endpoint URL", err)
|
||||||
|
}
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
panic(err)
|
if targetEndpoint.Scheme == "" {
|
||||||
}
|
targetEndpoint.Scheme = "https"
|
||||||
if targetEndpoint.Scheme == "" {
|
}
|
||||||
targetEndpoint.Scheme = "https"
|
target = targetEndpoint.String()
|
||||||
|
|
||||||
|
default:
|
||||||
|
showError(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web}", self.backendMode), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.quiet {
|
if !self.quiet {
|
||||||
@ -90,7 +101,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
cfg := &backend.Config{
|
cfg := &backend.Config{
|
||||||
IdentityPath: zif,
|
IdentityPath: zif,
|
||||||
EndpointAddress: targetEndpoint.String(),
|
EndpointAddress: target,
|
||||||
}
|
}
|
||||||
|
|
||||||
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
|
||||||
@ -141,23 +152,22 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
httpProxy, err := backend.NewHTTP(cfg)
|
var bh backendHandler
|
||||||
if err != nil {
|
switch self.backendMode {
|
||||||
ui.Close()
|
case "proxy":
|
||||||
if !panicInstead {
|
bh, err = self.proxyBackendMode(cfg)
|
||||||
showError("unable to create http backend", err)
|
if err != nil {
|
||||||
}
|
ui.Close()
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
if err := httpProxy.Run(); err != nil {
|
|
||||||
if !panicInstead {
|
if !panicInstead {
|
||||||
showError("unable to run http proxy", err)
|
showError("unable to create proxy backend handler", err)
|
||||||
}
|
}
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
default:
|
||||||
|
ui.Close()
|
||||||
|
showError("invalid backend mode", nil)
|
||||||
|
}
|
||||||
|
|
||||||
if !self.quiet {
|
if !self.quiet {
|
||||||
ui.Clear()
|
ui.Clear()
|
||||||
@ -206,7 +216,7 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case <-ticker:
|
case <-ticker:
|
||||||
currentRequests := float64(httpProxy.Requests())
|
currentRequests := float64(bh.Requests()())
|
||||||
deltaRequests := currentRequests - lastRequests
|
deltaRequests := currentRequests - lastRequests
|
||||||
requestData = append(requestData, deltaRequests)
|
requestData = append(requestData, deltaRequests)
|
||||||
lastRequests = currentRequests
|
lastRequests = currentRequests
|
||||||
@ -227,6 +237,21 @@ func (self *sharePublicCommand) run(_ *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *sharePublicCommand) proxyBackendMode(cfg *backend.Config) (backendHandler, error) {
|
||||||
|
httpProxy, err := backend.NewHTTP(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "error creating http proxy backend")
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := httpProxy.Run(); err != nil {
|
||||||
|
logrus.Errorf("error running http proxy backend: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return httpProxy, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (self *sharePublicCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
func (self *sharePublicCommand) destroy(id string, cfg *backend.Config, zrok *rest_client_zrok.Zrok, auth runtime.ClientAuthInfoWriter) {
|
||||||
logrus.Debugf("shutting down '%v'", cfg.Service)
|
logrus.Debugf("shutting down '%v'", cfg.Service)
|
||||||
req := service.NewUnshareParams()
|
req := service.NewUnshareParams()
|
||||||
|
@ -6,6 +6,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type backendHandler interface {
|
||||||
|
Requests() func() int32
|
||||||
|
}
|
||||||
|
|
||||||
func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
|
func mustGetAdminAuth() runtime.ClientAuthInfoWriter {
|
||||||
adminToken := os.Getenv("ZROK_ADMIN_TOKEN")
|
adminToken := os.Getenv("ZROK_ADMIN_TOKEN")
|
||||||
if adminToken == "" {
|
if adminToken == "" {
|
||||||
|
@ -23,7 +23,7 @@ type Config struct {
|
|||||||
|
|
||||||
type httpBind struct {
|
type httpBind struct {
|
||||||
cfg *Config
|
cfg *Config
|
||||||
Requests func() int32
|
requests func() int32
|
||||||
listener edge.Listener
|
listener edge.Listener
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ func NewHTTP(cfg *Config) (*httpBind, error) {
|
|||||||
handler := util.NewProxyHandler(proxy)
|
handler := util.NewProxyHandler(proxy)
|
||||||
return &httpBind{
|
return &httpBind{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
Requests: handler.Requests,
|
requests: handler.Requests,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
handler: handler,
|
handler: handler,
|
||||||
}, nil
|
}, nil
|
||||||
@ -63,6 +63,10 @@ func (self *httpBind) Run() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *httpBind) Requests() func() int32 {
|
||||||
|
return self.requests
|
||||||
|
}
|
||||||
|
|
||||||
func newReverseProxy(target string) (*httputil.ReverseProxy, error) {
|
func newReverseProxy(target string) (*httputil.ReverseProxy, error) {
|
||||||
targetURL, err := url.Parse(target)
|
targetURL, err := url.Parse(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user