diff --git a/cmd/zrok/sharePublic.go b/cmd/zrok/sharePublic.go index c598af5f..ae80ea9a 100644 --- a/cmd/zrok/sharePublic.go +++ b/cmd/zrok/sharePublic.go @@ -4,7 +4,6 @@ import ( "fmt" tea "github.com/charmbracelet/bubbletea" "github.com/openziti/zrok/endpoints" - "github.com/openziti/zrok/endpoints/caddyf" "github.com/openziti/zrok/endpoints/proxy" "github.com/openziti/zrok/environment" "github.com/openziti/zrok/environment/env_core" @@ -169,13 +168,13 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) { }() case "caddy": - cfg := &caddyf.BackendConfig{ + cfg := &proxy.CaddyfileBackendConfig{ CaddyfilePath: target, Shr: shr, Requests: requests, } - be, err := caddyf.NewBackend(cfg) + be, err := proxy.NewCaddyfileBackend(cfg) if err != nil { if !panicInstead { tui.Error("unable to create caddy backend", err) diff --git a/endpoints/caddyf/backend.go b/endpoints/caddyf/backend.go deleted file mode 100644 index 7f1d8193..00000000 --- a/endpoints/caddyf/backend.go +++ /dev/null @@ -1,51 +0,0 @@ -package caddyf - -import ( - "github.com/caddyserver/caddy/v2" - "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" - "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" - "github.com/openziti/zrok/endpoints" - "github.com/openziti/zrok/sdk" - "github.com/sirupsen/logrus" -) - -type BackendConfig struct { - CaddyfilePath string - Shr *sdk.Share - Requests chan *endpoints.Request -} - -type Backend struct { - cdycfg []byte -} - -func NewBackend(cfg *BackendConfig) (*Backend, error) { - cdyf, err := preprocessCaddyfile(cfg.CaddyfilePath, cfg.Shr) - if err != nil { - return nil, err - } - var adapter caddyfile.Adapter - adapter.ServerType = httpcaddyfile.ServerType{} - cdycfg, warnings, err := adapter.Adapt([]byte(cdyf), map[string]interface{}{"filename": cfg.CaddyfilePath}) - if err != nil { - return nil, err - } - for _, warning := range warnings { - logrus.Warnf("%v [%d] (%v): %v", cfg.CaddyfilePath, warning.Line, warning.Directive, warning.Message) - } - return &Backend{cdycfg: cdycfg}, nil -} - -func (b *Backend) Run() error { - if err := caddy.Run(&caddy.Config{}); err != nil { - return err - } - if err := caddy.Load(b.cdycfg, true); err != nil { - return err - } - return nil -} - -func (b *Backend) Requests() func() int32 { - return nil -} diff --git a/endpoints/caddyf/preprocessor.go b/endpoints/caddyf/preprocessor.go deleted file mode 100644 index b2aa87fb..00000000 --- a/endpoints/caddyf/preprocessor.go +++ /dev/null @@ -1,24 +0,0 @@ -package caddyf - -import ( - "github.com/openziti/zrok/sdk" - "os" - "strings" - "text/template" -) - -func preprocessCaddyfile(inF string, shr *sdk.Share) (string, error) { - input, err := os.ReadFile(inF) - if err != nil { - return "", err - } - tmpl, err := template.New(inF).Parse(string(input)) - if err != nil { - return "", err - } - output := new(strings.Builder) - if err := tmpl.Execute(output, shr); err != nil { - return "", err - } - return output.String(), nil -} diff --git a/endpoints/proxy/caddyfileBackend.go b/endpoints/proxy/caddyfileBackend.go new file mode 100644 index 00000000..57786c7e --- /dev/null +++ b/endpoints/proxy/caddyfileBackend.go @@ -0,0 +1,72 @@ +package proxy + +import ( + "fmt" + "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" + "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" + "github.com/openziti/zrok/endpoints" + "github.com/openziti/zrok/sdk" + "github.com/sirupsen/logrus" + "os" + "strings" + "text/template" +) + +type CaddyfileBackendConfig struct { + CaddyfilePath string + Shr *sdk.Share + Requests chan *endpoints.Request +} + +type CaddyfileBackend struct { + cfg []byte +} + +func NewCaddyfileBackend(cfg *CaddyfileBackendConfig) (*CaddyfileBackend, error) { + cdyf, err := preprocessCaddyfile(cfg.CaddyfilePath, cfg.Shr) + if err != nil { + return nil, err + } + var adapter caddyfile.Adapter + adapter.ServerType = httpcaddyfile.ServerType{} + caddyCfg, warnings, err := adapter.Adapt([]byte(cdyf), map[string]interface{}{"filename": cfg.CaddyfilePath}) + if err != nil { + return nil, err + } + for _, warning := range warnings { + logrus.Warnf("%v [%d] (%v): %v", cfg.CaddyfilePath, warning.Line, warning.Directive, warning.Message) + } + return &CaddyfileBackend{cfg: caddyCfg}, nil +} + +func (b *CaddyfileBackend) Run() error { + if err := caddy.Load(b.cfg, true); err != nil { + return err + } + return nil +} + +func (b *CaddyfileBackend) Requests() func() int32 { + return nil +} + +func preprocessCaddyfile(inF string, shr *sdk.Share) (string, error) { + input, err := os.ReadFile(inF) + if err != nil { + return "", err + } + tmpl, err := template.New(inF).Parse(string(input)) + if err != nil { + return "", err + } + output := new(strings.Builder) + if err := tmpl.Execute(output, &caddyfileData{ZrokBindAddress: fmt.Sprintf("zrok/%s", shr.Token)}); err != nil { + return "", err + } + return output.String(), nil +} + +type caddyfileData struct { + ZrokBindAddress string +} diff --git a/etc/Caddyfile b/etc/Caddyfile index ba4e9811..d6bbc751 100644 --- a/etc/Caddyfile +++ b/etc/Caddyfile @@ -1,4 +1,4 @@ http:// { - bind zrok/{{ .Token }} + bind {{ .ZrokBindAddress }} reverse_proxy 127.0.0.1:3000 }