From 6daa73b95c6f4979c40f7f0cdff0ac6bf2028e01 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 25 Aug 2023 13:02:42 -0400 Subject: [PATCH] use the caddy web backend everywhere (#392) --- cmd/zrok/sharePrivate.go | 8 ++-- cmd/zrok/shareReserved.go | 8 ++-- endpoints/proxy/webBackend.go | 82 ----------------------------------- 3 files changed, 8 insertions(+), 90 deletions(-) delete mode 100644 endpoints/proxy/webBackend.go diff --git a/cmd/zrok/sharePrivate.go b/cmd/zrok/sharePrivate.go index 4b7f908e..601080f8 100644 --- a/cmd/zrok/sharePrivate.go +++ b/cmd/zrok/sharePrivate.go @@ -162,11 +162,11 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { } case "web": - cfg := &proxy.WebBackendConfig{ + cfg := &proxy.CaddyWebBackendConfig{ IdentityPath: zif, WebRoot: target, ShrToken: resp.Payload.ShrToken, - RequestsChan: requestsChan, + Requests: requestsChan, } _, err = cmd.webBackendMode(cfg) if err != nil { @@ -269,8 +269,8 @@ func (cmd *sharePrivateCommand) proxyBackendMode(cfg *proxy.BackendConfig) (endp return be, nil } -func (cmd *sharePrivateCommand) webBackendMode(cfg *proxy.WebBackendConfig) (endpoints.RequestHandler, error) { - be, err := proxy.NewWebBackend(cfg) +func (cmd *sharePrivateCommand) webBackendMode(cfg *proxy.CaddyWebBackendConfig) (endpoints.RequestHandler, error) { + be, err := proxy.NewCaddyWebBackend(cfg) if err != nil { return nil, errors.Wrap(err, "error creating http web backend") } diff --git a/cmd/zrok/shareReserved.go b/cmd/zrok/shareReserved.go index f5dd54ce..33382741 100644 --- a/cmd/zrok/shareReserved.go +++ b/cmd/zrok/shareReserved.go @@ -125,11 +125,11 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) { } case "web": - cfg := &proxy.WebBackendConfig{ + cfg := &proxy.CaddyWebBackendConfig{ IdentityPath: zif, WebRoot: target, ShrToken: shrToken, - RequestsChan: requestsChan, + Requests: requestsChan, } _, err := cmd.webBackendMode(cfg) if err != nil { @@ -203,8 +203,8 @@ func (cmd *shareReservedCommand) proxyBackendMode(cfg *proxy.BackendConfig) (end return be, nil } -func (cmd *shareReservedCommand) webBackendMode(cfg *proxy.WebBackendConfig) (endpoints.RequestHandler, error) { - be, err := proxy.NewWebBackend(cfg) +func (cmd *shareReservedCommand) webBackendMode(cfg *proxy.CaddyWebBackendConfig) (endpoints.RequestHandler, error) { + be, err := proxy.NewCaddyWebBackend(cfg) if err != nil { return nil, errors.Wrap(err, "error creating http web backend") } diff --git a/endpoints/proxy/webBackend.go b/endpoints/proxy/webBackend.go deleted file mode 100644 index 5d330398..00000000 --- a/endpoints/proxy/webBackend.go +++ /dev/null @@ -1,82 +0,0 @@ -package proxy - -import ( - "fmt" - "github.com/openziti/sdk-golang/ziti" - "github.com/openziti/sdk-golang/ziti/edge" - "github.com/openziti/zrok/endpoints" - "github.com/pkg/errors" - "net/http" - "time" -) - -type WebBackendConfig struct { - IdentityPath string - WebRoot string - ShrToken string - RequestsChan chan *endpoints.Request -} - -type WebBackend struct { - cfg *WebBackendConfig - listener edge.Listener - handler http.Handler -} - -func NewWebBackend(cfg *WebBackendConfig) (*WebBackend, error) { - options := ziti.ListenOptions{ - ConnectTimeout: 5 * time.Minute, - MaxConnections: 64, - } - zcfg, err := ziti.NewConfigFromFile(cfg.IdentityPath) - if err != nil { - return nil, errors.Wrap(err, "error loading config") - } - zctx, err := ziti.NewContext(zcfg) - if err != nil { - return nil, errors.Wrap(err, "error loading ziti context") - } - listener, err := zctx.ListenWithOptions(cfg.ShrToken, &options) - if err != nil { - return nil, errors.Wrap(err, "error listening") - } - - be := &WebBackend{ - cfg: cfg, - listener: listener, - } - if cfg.RequestsChan != nil { - be.handler = &requestGrabber{requests: cfg.RequestsChan, handler: http.FileServer(http.Dir(cfg.WebRoot))} - } else { - be.handler = http.FileServer(http.Dir(cfg.WebRoot)) - } - return be, nil -} - -func (self *WebBackend) Run() error { - if err := http.Serve(self.listener, self.handler); err != nil { - return err - } - return nil -} - -func (self *WebBackend) Requests() func() int32 { - return func() int32 { return 0 } -} - -type requestGrabber struct { - requests chan *endpoints.Request - handler http.Handler -} - -func (rl *requestGrabber) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - if rl.requests != nil { - rl.requests <- &endpoints.Request{ - Stamp: time.Now(), - RemoteAddr: fmt.Sprintf("%v", req.Header["X-Real-Ip"]), - Method: req.Method, - Path: req.URL.String(), - } - } - rl.handler.ServeHTTP(resp, req) -}