2023-10-17 19:49:44 +02:00
|
|
|
package drive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/openziti/sdk-golang/ziti"
|
|
|
|
"github.com/openziti/sdk-golang/ziti/edge"
|
2024-01-10 17:57:21 +01:00
|
|
|
"github.com/openziti/zrok/drives/davServer"
|
2023-10-17 19:49:44 +02:00
|
|
|
"github.com/openziti/zrok/endpoints"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"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{
|
2023-12-13 17:22:20 +01:00
|
|
|
ConnectTimeout: 5 * time.Minute,
|
|
|
|
WaitForNEstablishedListeners: 1,
|
2023-10-17 19:49:44 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-10 17:57:21 +01:00
|
|
|
handler := &davServer.Handler{
|
|
|
|
FileSystem: davServer.Dir(cfg.DriveRoot),
|
|
|
|
LockSystem: davServer.NewMemLS(),
|
2023-10-17 19:49:44 +02:00
|
|
|
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
|
|
|
|
}
|