diff --git a/cmd/drive/main.go b/cmd/drive/main.go deleted file mode 100644 index b29faa3a..00000000 --- a/cmd/drive/main.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "golang.org/x/net/webdav" - "log" - "net/http" -) - -func main() { - dav := &webdav.Handler{ - FileSystem: webdav.Dir("."), - LockSystem: webdav.NewMemLS(), - Logger: func(r *http.Request, err error) { - if err != nil { - log.Printf("WEBDAV [%s]: %s, ERROR: %s\n", r.Method, r.URL, err) - } else { - log.Printf("WEBDAV [%s]: %s \n", r.Method, r.URL) - } - }, - } - http.Handle("/", dav) - if err := http.ListenAndServe("0.0.0.0:8800", nil); err != nil { - log.Fatalf("error serving: ") - } -} diff --git a/cmd/zrok/sharePrivate.go b/cmd/zrok/sharePrivate.go index 3c6de78b..8a25a7e9 100644 --- a/cmd/zrok/sharePrivate.go +++ b/cmd/zrok/sharePrivate.go @@ -4,6 +4,7 @@ import ( "fmt" tea "github.com/charmbracelet/bubbletea" "github.com/openziti/zrok/endpoints" + "github.com/openziti/zrok/endpoints/drive" "github.com/openziti/zrok/endpoints/proxy" "github.com/openziti/zrok/endpoints/tcpTunnel" "github.com/openziti/zrok/endpoints/udpTunnel" @@ -72,8 +73,11 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { target = args[0] cmd.headless = true + case "drive": + target = args[0] + default: - tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web, tcpTunnel, udpTunnel, caddy}", cmd.backendMode), nil) + tui.Error(fmt.Sprintf("invalid backend mode '%v'; expected {proxy, web, tcpTunnel, udpTunnel, caddy, drive}", cmd.backendMode), nil) } root, err := environment.LoadRoot() @@ -238,6 +242,28 @@ func (cmd *sharePrivateCommand) run(_ *cobra.Command, args []string) { } }() + case "drive": + cfg := &drive.BackendConfig{ + IdentityPath: zif, + DriveRoot: target, + ShrToken: shr.Token, + Requests: requests, + } + + be, err := drive.NewBackend(cfg) + if err != nil { + if !panicInstead { + tui.Error("error creating drive backend", err) + } + panic(err) + } + + go func() { + if err := be.Run(); err != nil { + logrus.Errorf("error running drive backend: %v", err) + } + }() + default: tui.Error("invalid backend mode", nil) } diff --git a/endpoints/drive/backend.go b/endpoints/drive/backend.go new file mode 100644 index 00000000..f9147e8c --- /dev/null +++ b/endpoints/drive/backend.go @@ -0,0 +1,72 @@ +package drive + +import ( + "fmt" + "github.com/openziti/sdk-golang/ziti" + "github.com/openziti/sdk-golang/ziti/edge" + "github.com/openziti/zrok/endpoints" + "github.com/pkg/errors" + "golang.org/x/net/webdav" + "net/http" + "time" +) + +type BackendConfig struct { + IdentityPath string + DriveRoot string + ShrToken string + Requests chan *endpoints.Request +} + +type Backend struct { + cfg *BackendConfig + listener edge.Listener + handler http.Handler +} + +func NewBackend(cfg *BackendConfig) (*Backend, 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 ziti identity") + } + 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, err + } + + handler := &webdav.Handler{ + FileSystem: webdav.Dir(cfg.DriveRoot), + LockSystem: webdav.NewMemLS(), + Logger: func(r *http.Request, err error) { + if cfg.Requests != nil { + cfg.Requests <- &endpoints.Request{ + Stamp: time.Now(), + RemoteAddr: fmt.Sprintf("%v", r.Header["X-Real-Ip"]), + Method: r.Method, + Path: r.URL.String(), + } + } + }, + } + + return &Backend{ + cfg: cfg, + listener: listener, + handler: handler, + }, nil +} + +func (b *Backend) Run() error { + if err := http.Serve(b.listener, b.handler); err != nil { + return err + } + return nil +} diff --git a/endpoints/proxy/backend.go b/endpoints/proxy/backend.go index 8b8b62e1..06858ada 100644 --- a/endpoints/proxy/backend.go +++ b/endpoints/proxy/backend.go @@ -25,7 +25,6 @@ type BackendConfig struct { type Backend struct { cfg *BackendConfig - requests func() int32 listener edge.Listener handler http.Handler } @@ -56,7 +55,6 @@ func NewBackend(cfg *BackendConfig) (*Backend, error) { handler := util.NewProxyHandler(proxy) return &Backend{ cfg: cfg, - requests: handler.Requests, listener: listener, handler: handler, }, nil @@ -69,10 +67,6 @@ func (b *Backend) Run() error { return nil } -func (b *Backend) Requests() func() int32 { - return b.requests -} - func newReverseProxy(cfg *BackendConfig) (*httputil.ReverseProxy, error) { targetURL, err := url.Parse(cfg.EndpointAddress) if err != nil { diff --git a/endpoints/proxy/caddyfileBackend.go b/endpoints/proxy/caddyfileBackend.go index 30b98abb..c69d93aa 100644 --- a/endpoints/proxy/caddyfileBackend.go +++ b/endpoints/proxy/caddyfileBackend.go @@ -56,10 +56,6 @@ func (b *CaddyfileBackend) Run() error { 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 { diff --git a/endpoints/requests.go b/endpoints/requests.go index 883a8d58..6eed6013 100644 --- a/endpoints/requests.go +++ b/endpoints/requests.go @@ -2,10 +2,6 @@ package endpoints import "time" -type RequestHandler interface { - Requests() func() int32 -} - type Request struct { Stamp time.Time RemoteAddr string diff --git a/sdk/model.go b/sdk/model.go index fa0310eb..4592a483 100644 --- a/sdk/model.go +++ b/sdk/model.go @@ -10,6 +10,7 @@ const ( TcpTunnelBackendMode BackendMode = "tcpTunnel" UdpTunnelBackendMode BackendMode = "udpTunnel" CaddyBackendMode BackendMode = "caddy" + DriveBackendMode BackendMode = "drive" ) type ShareMode string