2016-12-07 02:00:24 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-04-20 13:14:21 +08:00
|
|
|
"html/template"
|
2020-12-27 04:59:45 +08:00
|
|
|
"io"
|
2021-04-20 13:14:21 +08:00
|
|
|
"log"
|
2016-12-07 02:00:24 +08:00
|
|
|
"net/http"
|
2017-12-07 00:38:19 +08:00
|
|
|
"os"
|
2016-12-07 02:00:24 +08:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-08 23:15:21 +08:00
|
|
|
"time"
|
2016-12-07 02:00:24 +08:00
|
|
|
|
2021-04-20 13:14:21 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-05-02 16:08:06 +08:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2021-04-20 13:14:21 +08:00
|
|
|
"github.com/rclone/rclone/cmd/serve/http/data"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
2021-04-20 13:14:21 +08:00
|
|
|
httplib "github.com/rclone/rclone/lib/http"
|
|
|
|
"github.com/rclone/rclone/lib/http/auth"
|
2021-04-20 12:50:08 +08:00
|
|
|
"github.com/rclone/rclone/lib/http/serve"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2016-12-07 02:00:24 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-04-20 13:14:21 +08:00
|
|
|
// Options required for http server
|
|
|
|
type Options struct {
|
|
|
|
data.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values used for Options
|
|
|
|
var DefaultOpt = Options{}
|
|
|
|
|
|
|
|
// Opt is options set by command line flags
|
|
|
|
var Opt = DefaultOpt
|
|
|
|
|
2016-12-07 02:00:24 +08:00
|
|
|
func init() {
|
2021-04-20 13:14:21 +08:00
|
|
|
data.AddFlags(Command.Flags(), "", &Opt.Options)
|
|
|
|
httplib.AddFlags(Command.Flags())
|
|
|
|
auth.AddFlags(Command.Flags())
|
2017-10-28 03:56:31 +08:00
|
|
|
vfsflags.AddFlags(Command.Flags())
|
2016-12-07 02:00:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command definition for cobra
|
|
|
|
var Command = &cobra.Command{
|
|
|
|
Use: "http remote:path",
|
|
|
|
Short: `Serve the remote over HTTP.`,
|
2022-06-20 01:18:14 +08: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-07 02:00:24 +08:00
|
|
|
|
2022-06-20 01:55:37 +08:00
|
|
|
You can use the filter flags (e.g. ` + "`--include`, `--exclude`" + `) to control what
|
2016-12-07 02:00:24 +08:00
|
|
|
is served.
|
|
|
|
|
2022-06-20 01:55:37 +08:00
|
|
|
The server will log errors. Use ` + "`-v`" + ` to see access logs.
|
2016-12-07 02:00:24 +08:00
|
|
|
|
2022-06-20 01:55:37 +08:00
|
|
|
` + "`--bwlimit`" + ` will be respected for file transfers. Use ` + "`--stats`" + ` to
|
2016-12-07 02:00:24 +08:00
|
|
|
control the stats printing.
|
2021-04-20 13:14:21 +08:00
|
|
|
` + httplib.Help + data.Help + auth.Help + vfs.Help,
|
2016-12-07 02:00:24 +08:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
f := cmd.NewFsSrc(args)
|
|
|
|
cmd.Run(false, true, command, func() error {
|
2021-04-20 13:14:21 +08:00
|
|
|
s := newServer(f, Opt.Template)
|
|
|
|
router, err := httplib.Router()
|
2018-11-02 01:16:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-02 16:08:06 +08:00
|
|
|
s.Bind(router)
|
2021-07-24 18:01:58 +08:00
|
|
|
httplib.Wait()
|
2016-12-07 02:00:24 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:51:16 +08:00
|
|
|
// server contains everything to run the server
|
|
|
|
type server struct {
|
2021-04-20 13:14:21 +08:00
|
|
|
f fs.Fs
|
|
|
|
vfs *vfs.VFS
|
|
|
|
HTMLTemplate *template.Template // HTML template for web interface
|
2017-10-28 03:56:31 +08:00
|
|
|
}
|
|
|
|
|
2021-04-20 13:14:21 +08:00
|
|
|
func newServer(f fs.Fs, templatePath string) *server {
|
|
|
|
htmlTemplate, templateErr := data.GetTemplate(templatePath)
|
|
|
|
if templateErr != nil {
|
|
|
|
log.Fatalf(templateErr.Error())
|
|
|
|
}
|
2019-06-13 17:51:16 +08:00
|
|
|
s := &server{
|
2021-04-20 13:14:21 +08:00
|
|
|
f: f,
|
|
|
|
vfs: vfs.New(f, &vfsflags.Opt),
|
|
|
|
HTMLTemplate: htmlTemplate,
|
2017-10-28 03:56:31 +08:00
|
|
|
}
|
|
|
|
return s
|
2016-12-07 02:00:24 +08:00
|
|
|
}
|
|
|
|
|
2021-05-02 16:08:06 +08:00
|
|
|
func (s *server) Bind(router chi.Router) {
|
2021-09-25 02:17:27 +08:00
|
|
|
if m := auth.Auth(auth.Opt); m != nil {
|
|
|
|
router.Use(m)
|
|
|
|
}
|
2021-05-02 16:08:06 +08:00
|
|
|
router.Use(
|
|
|
|
middleware.SetHeader("Accept-Ranges", "bytes"),
|
|
|
|
middleware.SetHeader("Server", "rclone/"+fs.Version),
|
|
|
|
)
|
|
|
|
router.Get("/*", s.handler)
|
|
|
|
router.Head("/*", s.handler)
|
2016-12-07 02:00:24 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 17:51:16 +08:00
|
|
|
// handler reads incoming requests and dispatches them
|
|
|
|
func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
2021-04-20 13:14:21 +08:00
|
|
|
isDir := strings.HasSuffix(r.URL.Path, "/")
|
|
|
|
remote := strings.Trim(r.URL.Path, "/")
|
2016-12-07 02:00:24 +08:00
|
|
|
if isDir {
|
|
|
|
s.serveDir(w, r, remote)
|
|
|
|
} else {
|
|
|
|
s.serveFile(w, r, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// serveDir serves a directory index at dirRemote
|
2019-06-13 17:51:16 +08:00
|
|
|
func (s *server) serveDir(w http.ResponseWriter, r *http.Request, dirRemote string) {
|
2016-12-07 02:00:24 +08:00
|
|
|
// List the directory
|
2017-10-28 03:56:31 +08:00
|
|
|
node, err := s.vfs.Stat(dirRemote)
|
|
|
|
if err == vfs.ENOENT {
|
2016-12-07 02:00:24 +08:00
|
|
|
http.Error(w, "Directory not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2018-10-28 22:08:20 +08:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2016-12-07 02:00:24 +08:00
|
|
|
return
|
|
|
|
}
|
2017-10-28 03:56:31 +08: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 22:08:20 +08:00
|
|
|
serve.Error(dirRemote, w, "Failed to list directory", err)
|
2017-10-28 03:56:31 +08:00
|
|
|
return
|
|
|
|
}
|
2016-12-07 02:00:24 +08:00
|
|
|
|
2018-10-28 22:08:20 +08:00
|
|
|
// Make the entries for display
|
2018-12-23 08:16:50 +08:00
|
|
|
directory := serve.NewDirectory(dirRemote, s.HTMLTemplate)
|
2017-10-28 03:56:31 +08:00
|
|
|
for _, node := range dirEntries {
|
2020-05-08 23:15:21 +08: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-07 02:00:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:24:11 +08:00
|
|
|
sortParm := r.URL.Query().Get("sort")
|
|
|
|
orderParm := r.URL.Query().Get("order")
|
|
|
|
directory.ProcessQueryParams(sortParm, orderParm)
|
|
|
|
|
2020-05-05 16:41:08 +08:00
|
|
|
// Set the Last-Modified header to the timestamp
|
|
|
|
w.Header().Set("Last-Modified", dir.ModTime().UTC().Format(http.TimeFormat))
|
|
|
|
|
2018-10-28 22:08:20 +08:00
|
|
|
directory.Serve(w, r)
|
2016-12-07 02:00:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// serveFile serves a file object at remote
|
2019-06-13 17:51:16 +08:00
|
|
|
func (s *server) serveFile(w http.ResponseWriter, r *http.Request, remote string) {
|
2017-10-28 03:56:31 +08:00
|
|
|
node, err := s.vfs.Stat(remote)
|
|
|
|
if err == vfs.ENOENT {
|
2016-12-07 02:00:24 +08: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 22:08:20 +08:00
|
|
|
serve.Error(remote, w, "Failed to find file", err)
|
2016-12-07 02:00:24 +08:00
|
|
|
return
|
|
|
|
}
|
2017-10-28 03:56:31 +08:00
|
|
|
if !node.IsFile() {
|
|
|
|
http.Error(w, "Not a file", http.StatusNotFound)
|
2016-12-07 02:00:24 +08:00
|
|
|
return
|
|
|
|
}
|
2017-11-18 19:47:21 +08: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-28 03:56:31 +08:00
|
|
|
file := node.(*vfs.File)
|
2016-12-07 02:00:24 +08:00
|
|
|
|
2020-12-27 04:59:45 +08: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-07 02:00:24 +08:00
|
|
|
|
|
|
|
// Set content type
|
2019-06-17 16:34:30 +08:00
|
|
|
mimeType := fs.MimeType(r.Context(), obj)
|
2016-12-07 02:00:24 +08: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 16:41:08 +08:00
|
|
|
// Set the Last-Modified header to the timestamp
|
|
|
|
w.Header().Set("Last-Modified", file.ModTime().UTC().Format(http.TimeFormat))
|
|
|
|
|
2016-12-07 02:00:24 +08: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-07 00:38:19 +08:00
|
|
|
in, err := file.Open(os.O_RDONLY)
|
2016-12-07 02:00:24 +08:00
|
|
|
if err != nil {
|
2018-10-28 22:08:20 +08:00
|
|
|
serve.Error(remote, w, "Failed to open file", err)
|
2016-12-07 02:00:24 +08: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 18:13:54 +08:00
|
|
|
tr := accounting.Stats(r.Context()).NewTransfer(obj)
|
2020-11-06 00:59:59 +08:00
|
|
|
defer tr.Done(r.Context(), nil)
|
2017-10-28 03:56:31 +08:00
|
|
|
// FIXME in = fs.NewAccount(in, obj).WithBuffer() // account the transfer
|
2016-12-07 02:00:24 +08:00
|
|
|
|
2017-10-28 03:56:31 +08:00
|
|
|
// Serve the file
|
2020-12-27 04:59:45 +08: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-07 02:00:24 +08:00
|
|
|
}
|