2019-09-04 09:20:18 +00:00
|
|
|
// Package webdav implements a WebDAV server backed by rclone VFS
|
2017-04-25 13:57:59 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2019-05-07 12:53:16 +00:00
|
|
|
"context"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-25 13:57:59 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-06-12 08:50:56 +00:00
|
|
|
"strings"
|
2020-05-08 15:15:21 +00:00
|
|
|
"time"
|
2017-04-25 13:57:59 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/httplib"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/httplib/httpflags"
|
2019-08-01 16:18:44 +00:00
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-06-10 16:33:55 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2021-04-20 04:50:08 +00:00
|
|
|
"github.com/rclone/rclone/lib/http/serve"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2017-04-25 13:57:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
)
|
|
|
|
|
2018-06-10 10:54:44 +00:00
|
|
|
var (
|
2019-06-12 12:01:48 +00:00
|
|
|
hashName string
|
|
|
|
hashType = hash.None
|
2019-06-12 08:50:56 +00:00
|
|
|
disableGETDir = false
|
2018-06-10 10:54:44 +00:00
|
|
|
)
|
|
|
|
|
2017-04-25 13:57:59 +00:00
|
|
|
func init() {
|
2020-06-10 16:33:55 +00:00
|
|
|
flagSet := Command.Flags()
|
|
|
|
httpflags.AddFlags(flagSet)
|
|
|
|
vfsflags.AddFlags(flagSet)
|
|
|
|
proxyflags.AddFlags(flagSet)
|
|
|
|
flags.StringVarP(flagSet, &hashName, "etag-hash", "", "", "Which hash to use for the ETag, or auto or blank for off")
|
|
|
|
flags.BoolVarP(flagSet, &disableGETDir, "disable-dir-list", "", false, "Disable HTML directory list on GET request for a directory")
|
2017-04-25 13:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "webdav remote:path",
|
2022-06-19 16:59:54 +00:00
|
|
|
Short: `Serve remote:path over WebDAV.`,
|
2022-06-19 17:18:14 +00:00
|
|
|
Long: `Run a basic WebDAV server to serve a remote over HTTP via the
|
|
|
|
WebDAV protocol. This can be viewed with a WebDAV client, through a web
|
|
|
|
browser, or you can make a remote of type WebDAV to read and write it.
|
2017-04-25 13:57:59 +00:00
|
|
|
|
2022-06-19 16:59:54 +00:00
|
|
|
### WebDAV options
|
2018-06-10 10:54:44 +00:00
|
|
|
|
|
|
|
#### --etag-hash
|
|
|
|
|
|
|
|
This controls the ETag header. Without this flag the ETag will be
|
|
|
|
based on the ModTime and Size of the object.
|
|
|
|
|
|
|
|
If this flag is set to "auto" then rclone will choose the first
|
|
|
|
supported hash on the backend or you can use a named hash such as
|
2022-06-19 13:51:37 +00:00
|
|
|
"MD5" or "SHA-1". Use the [hashsum](/commands/rclone_hashsum/) command
|
|
|
|
to see the full list.
|
2018-06-10 10:54:44 +00:00
|
|
|
|
2019-08-01 16:18:44 +00:00
|
|
|
` + httplib.Help + vfs.Help + proxy.Help,
|
2018-06-10 10:54:44 +00:00
|
|
|
RunE: func(command *cobra.Command, args []string) error {
|
2019-08-01 16:18:44 +00:00
|
|
|
var f fs.Fs
|
|
|
|
if proxyflags.Opt.AuthProxy == "" {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f = cmd.NewFsSrc(args)
|
|
|
|
} else {
|
|
|
|
cmd.CheckArgs(0, 0, command, args)
|
|
|
|
}
|
2018-06-10 10:54:44 +00:00
|
|
|
hashType = hash.None
|
|
|
|
if hashName == "auto" {
|
|
|
|
hashType = f.Hashes().GetOne()
|
|
|
|
} else if hashName != "" {
|
|
|
|
err := hashType.Set(hashName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hashType != hash.None {
|
|
|
|
fs.Debugf(f, "Using hash %v for ETag", hashType)
|
|
|
|
}
|
2017-04-25 13:57:59 +00:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2020-11-05 15:18:51 +00:00
|
|
|
s := newWebDAV(context.Background(), f, &httpflags.Opt)
|
2018-11-01 17:16:31 +00:00
|
|
|
err := s.serve()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Wait()
|
2018-02-14 20:39:11 +00:00
|
|
|
return nil
|
2017-04-25 13:57:59 +00:00
|
|
|
})
|
2018-06-10 10:54:44 +00:00
|
|
|
return nil
|
2017-04-25 13:57:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebDAV is a webdav.FileSystem interface
|
|
|
|
//
|
|
|
|
// A FileSystem implements access to a collection of named files. The elements
|
|
|
|
// in a file path are separated by slash ('/', U+002F) characters, regardless
|
|
|
|
// of host operating system convention.
|
|
|
|
//
|
|
|
|
// Each method has the same semantics as the os package's function of the same
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// Note that the os.Rename documentation says that "OS-specific restrictions
|
|
|
|
// might apply". In particular, whether or not renaming a file or directory
|
|
|
|
// overwriting another existing file or directory is an error is OS-dependent.
|
|
|
|
type WebDAV struct {
|
2018-11-01 17:16:31 +00:00
|
|
|
*httplib.Server
|
2019-06-12 08:50:56 +00:00
|
|
|
f fs.Fs
|
2019-08-01 16:18:44 +00:00
|
|
|
_vfs *vfs.VFS // don't use directly, use getVFS
|
2019-06-12 08:50:56 +00:00
|
|
|
webdavhandler *webdav.Handler
|
2019-08-01 16:18:44 +00:00
|
|
|
proxy *proxy.Proxy
|
2020-11-05 15:18:51 +00:00
|
|
|
ctx context.Context // for global config
|
2017-04-25 13:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check interface
|
|
|
|
var _ webdav.FileSystem = (*WebDAV)(nil)
|
|
|
|
|
2018-02-14 20:39:11 +00:00
|
|
|
// Make a new WebDAV to serve the remote
|
2020-11-05 15:18:51 +00:00
|
|
|
func newWebDAV(ctx context.Context, f fs.Fs, opt *httplib.Options) *WebDAV {
|
2018-02-14 20:39:11 +00:00
|
|
|
w := &WebDAV{
|
2020-11-05 15:18:51 +00:00
|
|
|
f: f,
|
|
|
|
ctx: ctx,
|
2019-08-01 16:18:44 +00:00
|
|
|
}
|
|
|
|
if proxyflags.Opt.AuthProxy != "" {
|
2020-11-05 15:18:51 +00:00
|
|
|
w.proxy = proxy.New(ctx, &proxyflags.Opt)
|
2019-08-01 16:18:44 +00:00
|
|
|
// override auth
|
|
|
|
copyOpt := *opt
|
|
|
|
copyOpt.Auth = w.auth
|
|
|
|
opt = ©Opt
|
|
|
|
} else {
|
|
|
|
w._vfs = vfs.New(f, &vfsflags.Opt)
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
2019-08-04 09:56:38 +00:00
|
|
|
w.Server = httplib.NewServer(http.HandlerFunc(w.handler), opt)
|
2019-06-12 08:50:56 +00:00
|
|
|
webdavHandler := &webdav.Handler{
|
2019-08-24 08:10:50 +00:00
|
|
|
Prefix: w.Server.Opt.BaseURL,
|
2018-02-14 20:39:11 +00:00
|
|
|
FileSystem: w,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
Logger: w.logRequest, // FIXME
|
|
|
|
}
|
2019-06-12 08:50:56 +00:00
|
|
|
w.webdavhandler = webdavHandler
|
2018-02-14 20:39:11 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2019-08-01 16:18:44 +00:00
|
|
|
// Gets the VFS in use for this request
|
|
|
|
func (w *WebDAV) getVFS(ctx context.Context) (VFS *vfs.VFS, err error) {
|
|
|
|
if w._vfs != nil {
|
|
|
|
return w._vfs, nil
|
|
|
|
}
|
|
|
|
value := ctx.Value(httplib.ContextAuthKey)
|
|
|
|
if value == nil {
|
|
|
|
return nil, errors.New("no VFS found in context")
|
|
|
|
}
|
|
|
|
VFS, ok := value.(*vfs.VFS)
|
|
|
|
if !ok {
|
2021-11-04 10:12:57 +00:00
|
|
|
return nil, fmt.Errorf("context value is not VFS: %#v", value)
|
2019-08-01 16:18:44 +00:00
|
|
|
}
|
|
|
|
return VFS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// auth does proxy authorization
|
|
|
|
func (w *WebDAV) auth(user, pass string) (value interface{}, err error) {
|
2019-12-24 17:34:35 +00:00
|
|
|
VFS, _, err := w.proxy.Call(user, pass, false)
|
2019-08-01 16:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return VFS, err
|
|
|
|
}
|
|
|
|
|
2019-06-12 08:50:56 +00:00
|
|
|
func (w *WebDAV) handler(rw http.ResponseWriter, r *http.Request) {
|
2019-08-04 09:56:38 +00:00
|
|
|
urlPath, ok := w.Path(rw, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 09:51:16 +00:00
|
|
|
isDir := strings.HasSuffix(urlPath, "/")
|
|
|
|
remote := strings.Trim(urlPath, "/")
|
|
|
|
if !disableGETDir && (r.Method == "GET" || r.Method == "HEAD") && isDir {
|
|
|
|
w.serveDir(rw, r, remote)
|
2019-06-12 08:50:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.webdavhandler.ServeHTTP(rw, r)
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
// serveDir serves a directory index at dirRemote
|
|
|
|
// This is similar to serveDir in serve http.
|
|
|
|
func (w *WebDAV) serveDir(rw http.ResponseWriter, r *http.Request, dirRemote string) {
|
2019-08-01 16:18:44 +00:00
|
|
|
VFS, err := w.getVFS(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "Root directory not found", http.StatusNotFound)
|
|
|
|
fs.Errorf(nil, "Failed to serve directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-06-13 09:51:16 +00:00
|
|
|
// List the directory
|
2019-08-01 16:18:44 +00:00
|
|
|
node, err := VFS.Stat(dirRemote)
|
2019-06-13 09:51:16 +00:00
|
|
|
if err == vfs.ENOENT {
|
|
|
|
http.Error(rw, "Directory not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
serve.Error(dirRemote, rw, "Failed to list directory", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !node.IsDir() {
|
|
|
|
http.Error(rw, "Not a directory", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := node.(*vfs.Dir)
|
|
|
|
dirEntries, err := dir.ReadDirAll()
|
2020-04-30 18:24:11 +00:00
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
if err != nil {
|
|
|
|
serve.Error(dirRemote, rw, "Failed to list directory", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the entries for display
|
|
|
|
directory := serve.NewDirectory(dirRemote, w.HTMLTemplate)
|
|
|
|
for _, node := range dirEntries {
|
2020-05-08 15:15:21 +00:00
|
|
|
if vfsflags.Opt.NoModTime {
|
|
|
|
directory.AddHTMLEntry(node.Path(), node.IsDir(), node.Size(), time.Time{})
|
|
|
|
} else {
|
|
|
|
directory.AddHTMLEntry(node.Path(), node.IsDir(), node.Size(), node.ModTime().UTC())
|
|
|
|
}
|
2019-06-13 09:51:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:24:11 +00:00
|
|
|
sortParm := r.URL.Query().Get("sort")
|
|
|
|
orderParm := r.URL.Query().Get("order")
|
|
|
|
directory.ProcessQueryParams(sortParm, orderParm)
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
directory.Serve(rw, r)
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:16:31 +00:00
|
|
|
// serve runs the http server in the background.
|
|
|
|
//
|
|
|
|
// Use s.Close() and s.Wait() to shutdown server
|
|
|
|
func (w *WebDAV) serve() error {
|
|
|
|
err := w.Serve()
|
2018-04-04 13:56:26 +00:00
|
|
|
if err != nil {
|
2018-11-01 17:16:31 +00:00
|
|
|
return err
|
2018-04-04 13:56:26 +00:00
|
|
|
}
|
2018-11-01 17:16:31 +00:00
|
|
|
fs.Logf(w.f, "WebDav Server started on %s", w.URL())
|
|
|
|
return nil
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 13:57:59 +00:00
|
|
|
// logRequest is called by the webdav module on every request
|
|
|
|
func (w *WebDAV) logRequest(r *http.Request, err error) {
|
|
|
|
fs.Infof(r.URL.Path, "%s from %s", r.Method, r.RemoteAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a directory
|
|
|
|
func (w *WebDAV) Mkdir(ctx context.Context, name string, perm os.FileMode) (err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(name, "perm=%v", perm)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir, leaf, err := VFS.StatParent(name)
|
2017-04-25 13:57:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = dir.Mkdir(leaf)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenFile opens a file or a directory
|
|
|
|
func (w *WebDAV) OpenFile(ctx context.Context, name string, flags int, perm os.FileMode) (file webdav.File, err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(name, "flags=%v, perm=%v", flags, perm)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := VFS.OpenFile(name, flags, perm)
|
2018-06-10 10:54:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return Handle{f}, nil
|
2017-04-25 13:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes a file or a directory and its contents
|
|
|
|
func (w *WebDAV) RemoveAll(ctx context.Context, name string) (err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(name, "")("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
node, err := VFS.Stat(name)
|
2017-04-25 13:57:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = node.RemoveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename a file or a directory
|
|
|
|
func (w *WebDAV) Rename(ctx context.Context, oldName, newName string) (err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(oldName, "newName=%q", newName)("err = %v", &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return VFS.Rename(oldName, newName)
|
2017-04-25 13:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns info about the file or directory
|
|
|
|
func (w *WebDAV) Stat(ctx context.Context, name string) (fi os.FileInfo, err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(name, "")("fi=%+v, err = %v", &fi, &err)
|
|
|
|
VFS, err := w.getVFS(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fi, err = VFS.Stat(name)
|
2018-06-10 10:54:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return FileInfo{fi}, nil
|
2017-04-25 13:57:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:54:44 +00:00
|
|
|
// Handle represents an open file
|
|
|
|
type Handle struct {
|
|
|
|
vfs.Handle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Readdir reads directory entries from the handle
|
|
|
|
func (h Handle) Readdir(count int) (fis []os.FileInfo, err error) {
|
|
|
|
fis, err = h.Handle.Readdir(count)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Wrap each FileInfo
|
|
|
|
for i := range fis {
|
|
|
|
fis[i] = FileInfo{fis[i]}
|
|
|
|
}
|
|
|
|
return fis, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat the handle
|
|
|
|
func (h Handle) Stat() (fi os.FileInfo, err error) {
|
|
|
|
fi, err = h.Handle.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return FileInfo{fi}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileInfo represents info about a file satisfying os.FileInfo and
|
|
|
|
// also some additional interfaces for webdav for ETag and ContentType
|
|
|
|
type FileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// ETag returns an ETag for the FileInfo
|
|
|
|
func (fi FileInfo) ETag(ctx context.Context) (etag string, err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(fi, "")("etag=%q, err=%v", &etag, &err)
|
2018-06-10 10:54:44 +00:00
|
|
|
if hashType == hash.None {
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
node, ok := (fi.FileInfo).(vfs.Node)
|
|
|
|
if !ok {
|
|
|
|
fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo)
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
entry := node.DirEntry()
|
|
|
|
o, ok := entry.(fs.Object)
|
|
|
|
if !ok {
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
2019-06-17 08:34:30 +00:00
|
|
|
hash, err := o.Hash(ctx, hashType)
|
2018-06-10 10:54:44 +00:00
|
|
|
if err != nil || hash == "" {
|
|
|
|
return "", webdav.ErrNotImplemented
|
|
|
|
}
|
|
|
|
return `"` + hash + `"`, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentType returns a content type for the FileInfo
|
|
|
|
func (fi FileInfo) ContentType(ctx context.Context) (contentType string, err error) {
|
2019-08-01 16:18:44 +00:00
|
|
|
// defer log.Trace(fi, "")("etag=%q, err=%v", &contentType, &err)
|
2018-06-10 10:54:44 +00:00
|
|
|
node, ok := (fi.FileInfo).(vfs.Node)
|
|
|
|
if !ok {
|
|
|
|
fs.Errorf(fi, "Expecting vfs.Node, got %T", fi.FileInfo)
|
|
|
|
return "application/octet-stream", nil
|
|
|
|
}
|
|
|
|
entry := node.DirEntry()
|
|
|
|
switch x := entry.(type) {
|
|
|
|
case fs.Object:
|
2019-06-17 08:34:30 +00:00
|
|
|
return fs.MimeType(ctx, x), nil
|
2018-06-10 10:54:44 +00:00
|
|
|
case fs.Directory:
|
|
|
|
return "inode/directory", nil
|
|
|
|
}
|
|
|
|
fs.Errorf(fi, "Expecting fs.Object or fs.Directory, got %T", entry)
|
|
|
|
return "application/octet-stream", nil
|
|
|
|
}
|