2022-08-28 11:21:57 +00:00
|
|
|
// Package http provides common functionality for http servers
|
2016-12-06 18:00:24 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2022-11-08 11:49:19 +00:00
|
|
|
"context"
|
2023-02-22 14:55:24 +00:00
|
|
|
"errors"
|
2022-11-08 11:49:19 +00:00
|
|
|
"fmt"
|
2020-12-26 20:59:45 +00:00
|
|
|
"io"
|
2021-04-20 05:14:21 +00:00
|
|
|
"log"
|
2016-12-06 18:00:24 +00:00
|
|
|
"net/http"
|
2017-12-06 16:38:19 +00:00
|
|
|
"os"
|
2016-12-06 18:00:24 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-08 15:15:21 +00:00
|
|
|
"time"
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2021-05-02 08:08:06 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2023-02-22 14:55:24 +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"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2022-11-08 11:49:19 +00:00
|
|
|
libhttp "github.com/rclone/rclone/lib/http"
|
2021-04-20 04:50:08 +00:00
|
|
|
"github.com/rclone/rclone/lib/http/serve"
|
2023-09-04 15:32:04 +00:00
|
|
|
"github.com/rclone/rclone/lib/systemd"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2016-12-06 18:00:24 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-04-20 05:14:21 +00:00
|
|
|
// Options required for http server
|
|
|
|
type Options struct {
|
2022-11-08 11:49:19 +00:00
|
|
|
Auth libhttp.AuthConfig
|
2022-11-08 12:03:26 +00:00
|
|
|
HTTP libhttp.Config
|
2022-11-08 11:49:19 +00:00
|
|
|
Template libhttp.TemplateConfig
|
2021-04-20 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values used for Options
|
2022-11-08 11:49:19 +00:00
|
|
|
var DefaultOpt = Options{
|
|
|
|
Auth: libhttp.DefaultAuthCfg(),
|
2022-11-08 12:03:26 +00:00
|
|
|
HTTP: libhttp.DefaultCfg(),
|
2022-11-08 11:49:19 +00:00
|
|
|
Template: libhttp.DefaultTemplateCfg(),
|
|
|
|
}
|
2021-04-20 05:14:21 +00:00
|
|
|
|
|
|
|
// Opt is options set by command line flags
|
|
|
|
var Opt = DefaultOpt
|
|
|
|
|
2023-01-11 05:05:44 +00:00
|
|
|
// flagPrefix is the prefix used to uniquely identify command line flags.
|
|
|
|
// It is intentionally empty for this package.
|
|
|
|
const flagPrefix = ""
|
|
|
|
|
2016-12-06 18:00:24 +00:00
|
|
|
func init() {
|
2022-11-08 11:49:19 +00:00
|
|
|
flagSet := Command.Flags()
|
2023-01-11 05:05:44 +00:00
|
|
|
libhttp.AddAuthFlagsPrefix(flagSet, flagPrefix, &Opt.Auth)
|
|
|
|
libhttp.AddHTTPFlagsPrefix(flagSet, flagPrefix, &Opt.HTTP)
|
|
|
|
libhttp.AddTemplateFlagsPrefix(flagSet, flagPrefix, &Opt.Template)
|
2022-11-08 11:49:19 +00:00
|
|
|
vfsflags.AddFlags(flagSet)
|
2023-02-22 14:55:24 +00:00
|
|
|
proxyflags.AddFlags(flagSet)
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "http remote:path",
|
|
|
|
Short: `Serve the remote over HTTP.`,
|
2022-06-19 17:18:14 +00:00
|
|
|
Long: `Run a basic web server to serve a remote over HTTP.
|
|
|
|
This can be viewed in a web browser or you can make a remote of type
|
|
|
|
http read from it.
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
You can use the filter flags (e.g. ` + "`--include`, `--exclude`" + `) to control what
|
2016-12-06 18:00:24 +00:00
|
|
|
is served.
|
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
The server will log errors. Use ` + "`-v`" + ` to see access logs.
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2022-06-19 17:55:37 +00:00
|
|
|
` + "`--bwlimit`" + ` will be respected for file transfers. Use ` + "`--stats`" + ` to
|
2016-12-06 18:00:24 +00:00
|
|
|
control the stats printing.
|
2023-01-11 05:05:44 +00:00
|
|
|
` + libhttp.Help(flagPrefix) + libhttp.TemplateHelp(flagPrefix) + libhttp.AuthHelp(flagPrefix) + vfs.Help + proxy.Help,
|
2022-11-26 22:40:49 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.39",
|
2023-07-10 17:34:10 +00:00
|
|
|
"groups": "Filter",
|
2022-11-26 22:40:49 +00:00
|
|
|
},
|
2016-12-06 18:00:24 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
2023-02-22 14:55:24 +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)
|
|
|
|
}
|
2022-11-08 11:49:19 +00:00
|
|
|
|
2016-12-06 18:00:24 +00:00
|
|
|
cmd.Run(false, true, command, func() error {
|
2023-02-22 14:55:24 +00:00
|
|
|
s, err := run(context.Background(), f, Opt)
|
2018-11-01 17:16:31 +00:00
|
|
|
if err != nil {
|
2022-11-08 11:49:19 +00:00
|
|
|
log.Fatal(err)
|
2018-11-01 17:16:31 +00:00
|
|
|
}
|
2022-11-08 11:49:19 +00:00
|
|
|
|
2023-09-04 15:32:04 +00:00
|
|
|
defer systemd.Notify()()
|
2022-11-08 11:49:19 +00:00
|
|
|
s.server.Wait()
|
2016-12-06 18:00:24 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:55:24 +00:00
|
|
|
// HTTP contains everything to run the server
|
|
|
|
type HTTP struct {
|
2022-11-08 11:49:19 +00:00
|
|
|
f fs.Fs
|
2023-02-22 14:55:24 +00:00
|
|
|
_vfs *vfs.VFS // don't use directly, use getVFS
|
2022-11-23 09:44:53 +00:00
|
|
|
server *libhttp.Server
|
2023-02-22 14:55:24 +00:00
|
|
|
opt Options
|
|
|
|
proxy *proxy.Proxy
|
|
|
|
ctx context.Context // for global config
|
2017-10-27 19:56:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:55:24 +00:00
|
|
|
// Gets the VFS in use for this request
|
|
|
|
func (s *HTTP) getVFS(ctx context.Context) (VFS *vfs.VFS, err error) {
|
|
|
|
if s._vfs != nil {
|
|
|
|
return s._vfs, nil
|
|
|
|
}
|
|
|
|
value := libhttp.CtxGetAuth(ctx)
|
|
|
|
if value == nil {
|
|
|
|
return nil, errors.New("no VFS found in context")
|
|
|
|
}
|
|
|
|
VFS, ok := value.(*vfs.VFS)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("context value is not VFS: %#v", value)
|
|
|
|
}
|
|
|
|
return VFS, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// auth does proxy authorization
|
|
|
|
func (s *HTTP) auth(user, pass string) (value interface{}, err error) {
|
|
|
|
VFS, _, err := s.proxy.Call(user, pass, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return VFS, err
|
|
|
|
}
|
2022-11-08 11:49:19 +00:00
|
|
|
|
2023-02-22 14:55:24 +00:00
|
|
|
func run(ctx context.Context, f fs.Fs, opt Options) (s *HTTP, err error) {
|
|
|
|
s = &HTTP{
|
2022-11-08 11:49:19 +00:00
|
|
|
f: f,
|
2023-02-22 14:55:24 +00:00
|
|
|
ctx: ctx,
|
|
|
|
opt: opt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if proxyflags.Opt.AuthProxy != "" {
|
|
|
|
s.proxy = proxy.New(ctx, &proxyflags.Opt)
|
|
|
|
// override auth
|
|
|
|
s.opt.Auth.CustomAuthFn = s.auth
|
|
|
|
} else {
|
|
|
|
s._vfs = vfs.New(f, &vfsflags.Opt)
|
2017-10-27 19:56:31 +00:00
|
|
|
}
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2022-11-08 11:49:19 +00:00
|
|
|
s.server, err = libhttp.NewServer(ctx,
|
2023-02-22 14:55:24 +00:00
|
|
|
libhttp.WithConfig(s.opt.HTTP),
|
|
|
|
libhttp.WithAuth(s.opt.Auth),
|
|
|
|
libhttp.WithTemplate(s.opt.Template),
|
2022-11-08 11:49:19 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init server: %w", err)
|
2021-09-24 18:17:27 +00:00
|
|
|
}
|
2022-11-08 11:49:19 +00:00
|
|
|
|
|
|
|
router := s.server.Router()
|
2021-05-02 08:08:06 +00:00
|
|
|
router.Use(
|
|
|
|
middleware.SetHeader("Accept-Ranges", "bytes"),
|
|
|
|
middleware.SetHeader("Server", "rclone/"+fs.Version),
|
|
|
|
)
|
|
|
|
router.Get("/*", s.handler)
|
|
|
|
router.Head("/*", s.handler)
|
2022-11-08 11:49:19 +00:00
|
|
|
|
|
|
|
s.server.Serve()
|
|
|
|
|
|
|
|
return s, nil
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:51:16 +00:00
|
|
|
// handler reads incoming requests and dispatches them
|
2023-02-22 14:55:24 +00:00
|
|
|
func (s *HTTP) handler(w http.ResponseWriter, r *http.Request) {
|
2021-04-20 05:14:21 +00:00
|
|
|
isDir := strings.HasSuffix(r.URL.Path, "/")
|
|
|
|
remote := strings.Trim(r.URL.Path, "/")
|
2016-12-06 18:00:24 +00:00
|
|
|
if isDir {
|
|
|
|
s.serveDir(w, r, remote)
|
|
|
|
} else {
|
|
|
|
s.serveFile(w, r, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveDir serves a directory index at dirRemote
|
2023-02-22 14:55:24 +00:00
|
|
|
func (s *HTTP) serveDir(w http.ResponseWriter, r *http.Request, dirRemote string) {
|
|
|
|
VFS, err := s.getVFS(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Root directory not found", http.StatusNotFound)
|
|
|
|
fs.Errorf(nil, "Failed to serve directory: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2016-12-06 18:00:24 +00:00
|
|
|
// List the directory
|
2023-02-22 14:55:24 +00:00
|
|
|
node, err := VFS.Stat(dirRemote)
|
2017-10-27 19:56:31 +00:00
|
|
|
if err == vfs.ENOENT {
|
2016-12-06 18:00:24 +00:00
|
|
|
http.Error(w, "Directory not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:56:31 +00:00
|
|
|
if !node.IsDir() {
|
|
|
|
http.Error(w, "Not a directory", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dir := node.(*vfs.Dir)
|
|
|
|
dirEntries, err := dir.ReadDirAll()
|
|
|
|
if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2017-10-27 19:56:31 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2018-10-28 14:08:20 +00:00
|
|
|
// Make the entries for display
|
2022-11-08 11:49:19 +00:00
|
|
|
directory := serve.NewDirectory(dirRemote, s.server.HTMLTemplate())
|
2017-10-27 19:56:31 +00:00
|
|
|
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())
|
|
|
|
}
|
2016-12-06 18:00:24 +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)
|
|
|
|
|
2020-05-05 08:41:08 +00:00
|
|
|
// Set the Last-Modified header to the timestamp
|
|
|
|
w.Header().Set("Last-Modified", dir.ModTime().UTC().Format(http.TimeFormat))
|
|
|
|
|
2018-10-28 14:08:20 +00:00
|
|
|
directory.Serve(w, r)
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serveFile serves a file object at remote
|
2023-02-22 14:55:24 +00:00
|
|
|
func (s *HTTP) serveFile(w http.ResponseWriter, r *http.Request, remote string) {
|
|
|
|
VFS, err := s.getVFS(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
|
|
fs.Errorf(nil, "Failed to serve file: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := VFS.Stat(remote)
|
2017-10-27 19:56:31 +00:00
|
|
|
if err == vfs.ENOENT {
|
2016-12-06 18:00:24 +00:00
|
|
|
fs.Infof(remote, "%s: File not found", r.RemoteAddr)
|
|
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(remote, w, "Failed to find file", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:56:31 +00:00
|
|
|
if !node.IsFile() {
|
|
|
|
http.Error(w, "Not a file", http.StatusNotFound)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-18 11:47:21 +00:00
|
|
|
entry := node.DirEntry()
|
|
|
|
if entry == nil {
|
|
|
|
http.Error(w, "Can't open file being written", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
obj := entry.(fs.Object)
|
2017-10-27 19:56:31 +00:00
|
|
|
file := node.(*vfs.File)
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2020-12-26 20:59:45 +00:00
|
|
|
// Set content length if we know how long the object is
|
|
|
|
knownSize := obj.Size() >= 0
|
|
|
|
if knownSize {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(node.Size(), 10))
|
|
|
|
}
|
2016-12-06 18:00:24 +00:00
|
|
|
|
|
|
|
// Set content type
|
2019-06-17 08:34:30 +00:00
|
|
|
mimeType := fs.MimeType(r.Context(), obj)
|
2016-12-06 18:00:24 +00:00
|
|
|
if mimeType == "application/octet-stream" && path.Ext(remote) == "" {
|
|
|
|
// Leave header blank so http server guesses
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
|
|
|
}
|
|
|
|
|
2020-05-05 08:41:08 +00:00
|
|
|
// Set the Last-Modified header to the timestamp
|
|
|
|
w.Header().Set("Last-Modified", file.ModTime().UTC().Format(http.TimeFormat))
|
|
|
|
|
2016-12-06 18:00:24 +00:00
|
|
|
// If HEAD no need to read the object since we have set the headers
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the object
|
2017-12-06 16:38:19 +00:00
|
|
|
in, err := file.Open(os.O_RDONLY)
|
2016-12-06 18:00:24 +00:00
|
|
|
if err != nil {
|
2018-10-28 14:08:20 +00:00
|
|
|
serve.Error(remote, w, "Failed to open file", err)
|
2016-12-06 18:00:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := in.Close()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(remote, "Failed to close file: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Account the transfer
|
2019-07-18 10:13:54 +00:00
|
|
|
tr := accounting.Stats(r.Context()).NewTransfer(obj)
|
2020-11-05 16:59:59 +00:00
|
|
|
defer tr.Done(r.Context(), nil)
|
2017-10-27 19:56:31 +00:00
|
|
|
// FIXME in = fs.NewAccount(in, obj).WithBuffer() // account the transfer
|
2016-12-06 18:00:24 +00:00
|
|
|
|
2017-10-27 19:56:31 +00:00
|
|
|
// Serve the file
|
2020-12-26 20:59:45 +00:00
|
|
|
if knownSize {
|
|
|
|
http.ServeContent(w, r, remote, node.ModTime(), in)
|
|
|
|
} else {
|
|
|
|
// http.ServeContent can't serve unknown length files
|
|
|
|
if rangeRequest := r.Header.Get("Range"); rangeRequest != "" {
|
|
|
|
http.Error(w, "Can't use Range: on files of unknown length", http.StatusRequestedRangeNotSatisfiable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err := io.Copy(w, in)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(obj, "Didn't finish writing GET request (wrote %d/unknown bytes): %v", n, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 18:00:24 +00:00
|
|
|
}
|