mirror of
https://github.com/openziti/zrok.git
synced 2024-11-25 09:33:43 +01:00
the most basic drive implementation (#218)
This commit is contained in:
parent
23a6f128d0
commit
62cfa4a2c7
@ -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: ")
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
72
endpoints/drive/backend.go
Normal file
72
endpoints/drive/backend.go
Normal file
@ -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
|
||||
}
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -2,10 +2,6 @@ package endpoints
|
||||
|
||||
import "time"
|
||||
|
||||
type RequestHandler interface {
|
||||
Requests() func() int32
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Stamp time.Time
|
||||
RemoteAddr string
|
||||
|
@ -10,6 +10,7 @@ const (
|
||||
TcpTunnelBackendMode BackendMode = "tcpTunnel"
|
||||
UdpTunnelBackendMode BackendMode = "udpTunnel"
|
||||
CaddyBackendMode BackendMode = "caddy"
|
||||
DriveBackendMode BackendMode = "drive"
|
||||
)
|
||||
|
||||
type ShareMode string
|
||||
|
Loading…
Reference in New Issue
Block a user