2018-10-27 19:29:20 +02:00
|
|
|
// Package rcserver implements the HTTP endpoint to serve the remote control
|
|
|
|
package rcserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2018-10-28 15:31:24 +01:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
2018-10-27 19:29:20 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib"
|
|
|
|
"github.com/ncw/rclone/cmd/serve/httplib/serve"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-10-28 15:31:24 +01:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
|
|
|
"github.com/ncw/rclone/fs/list"
|
2018-10-27 19:29:20 +02:00
|
|
|
"github.com/ncw/rclone/fs/rc"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/skratchdot/open-golang/open"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Start the remote control server if configured
|
|
|
|
func Start(opt *rc.Options) {
|
|
|
|
if opt.Enabled {
|
2018-10-28 15:31:24 +01:00
|
|
|
// Serve on the DefaultServeMux so can have global registrations appear
|
|
|
|
s := newServer(opt, http.DefaultServeMux)
|
2018-10-27 19:29:20 +02:00
|
|
|
go s.serve()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// server contains everything to run the server
|
|
|
|
type server struct {
|
|
|
|
srv *httplib.Server
|
|
|
|
files http.Handler
|
2018-10-28 15:31:24 +01:00
|
|
|
opt *rc.Options
|
2018-10-27 19:29:20 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 15:31:24 +01:00
|
|
|
func newServer(opt *rc.Options, mux *http.ServeMux) *server {
|
2018-10-27 19:29:20 +02:00
|
|
|
s := &server{
|
|
|
|
srv: httplib.NewServer(mux, &opt.HTTPOptions),
|
2018-10-28 15:31:24 +01:00
|
|
|
opt: opt,
|
2018-10-27 19:29:20 +02:00
|
|
|
}
|
|
|
|
mux.HandleFunc("/", s.handler)
|
|
|
|
|
|
|
|
// Add some more mime types which are often missing
|
|
|
|
_ = mime.AddExtensionType(".wasm", "application/wasm")
|
|
|
|
_ = mime.AddExtensionType(".js", "application/javascript")
|
|
|
|
|
|
|
|
// File handling
|
|
|
|
if opt.Files != "" {
|
|
|
|
fs.Logf(nil, "Serving files from %q", opt.Files)
|
|
|
|
s.files = http.FileServer(http.Dir(opt.Files))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// serve runs the http server - doesn't return
|
|
|
|
func (s *server) serve() {
|
|
|
|
err := s.srv.Serve()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Opening listener: %v", err)
|
|
|
|
}
|
|
|
|
fs.Logf(nil, "Serving remote control on %s", s.srv.URL())
|
|
|
|
// Open the files in the browser if set
|
|
|
|
if s.files != nil {
|
|
|
|
_ = open.Start(s.srv.URL())
|
|
|
|
}
|
|
|
|
s.srv.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeError writes a formatted error to the output
|
|
|
|
func writeError(path string, in rc.Params, w http.ResponseWriter, err error, status int) {
|
|
|
|
fs.Errorf(nil, "rc: %q: error: %v", path, err)
|
|
|
|
// Adjust the error return for some well known errors
|
|
|
|
errOrig := errors.Cause(err)
|
|
|
|
switch {
|
|
|
|
case errOrig == fs.ErrorDirNotFound || errOrig == fs.ErrorObjectNotFound:
|
|
|
|
status = http.StatusNotFound
|
|
|
|
case rc.IsErrParamInvalid(err) || rc.IsErrParamNotFound(err):
|
|
|
|
status = http.StatusBadRequest
|
|
|
|
}
|
|
|
|
w.WriteHeader(status)
|
|
|
|
err = rc.WriteJSON(w, rc.Params{
|
|
|
|
"status": status,
|
|
|
|
"error": err.Error(),
|
|
|
|
"input": in,
|
|
|
|
"path": path,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// can't return the error at this point
|
|
|
|
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handler reads incoming requests and dispatches them
|
|
|
|
func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
2018-10-28 15:31:24 +01:00
|
|
|
path := strings.TrimLeft(r.URL.Path, "/")
|
2018-10-27 19:29:20 +02:00
|
|
|
|
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
// echo back access control headers client needs
|
|
|
|
reqAccessHeaders := r.Header.Get("Access-Control-Request-Headers")
|
|
|
|
w.Header().Add("Access-Control-Allow-Headers", reqAccessHeaders)
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "POST":
|
|
|
|
s.handlePost(w, r, path)
|
|
|
|
case "OPTIONS":
|
|
|
|
s.handleOptions(w, r, path)
|
2018-10-28 15:31:24 +01:00
|
|
|
case "GET", "HEAD":
|
2018-10-27 19:29:20 +02:00
|
|
|
s.handleGet(w, r, path)
|
|
|
|
default:
|
|
|
|
writeError(path, nil, w, errors.Errorf("method %q not allowed", r.Method), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) handlePost(w http.ResponseWriter, r *http.Request, path string) {
|
2018-10-28 15:31:24 +01:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
values := r.URL.Query()
|
|
|
|
if contentType == "application/x-www-form-urlencoded" {
|
|
|
|
// Parse the POST and URL parameters into r.Form, for others r.Form will be empty value
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
writeError(path, nil, w, errors.Wrap(err, "failed to parse form/URL parameters"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
values = r.Form
|
2018-10-27 19:29:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the POST and URL parameters into in
|
|
|
|
in := make(rc.Params)
|
2018-10-28 15:31:24 +01:00
|
|
|
for k, vs := range values {
|
2018-10-27 19:29:20 +02:00
|
|
|
if len(vs) > 0 {
|
|
|
|
in[k] = vs[len(vs)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a JSON blob from the input
|
2018-10-28 15:31:24 +01:00
|
|
|
if contentType == "application/json" {
|
2018-10-27 19:29:20 +02:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&in)
|
|
|
|
if err != nil {
|
|
|
|
writeError(path, in, w, errors.Wrap(err, "failed to read input JSON"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the call
|
|
|
|
call := rc.Calls.Get(path)
|
|
|
|
if call == nil {
|
2018-10-28 15:31:24 +01:00
|
|
|
writeError(path, in, w, errors.Errorf("couldn't find method %q", path), http.StatusNotFound)
|
2018-10-27 19:29:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if it is async or not
|
|
|
|
isAsync, err := in.GetBool("_async")
|
|
|
|
if rc.NotErrParamNotFound(err) {
|
|
|
|
writeError(path, in, w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Debugf(nil, "rc: %q: with parameters %+v", path, in)
|
|
|
|
var out rc.Params
|
|
|
|
if isAsync {
|
|
|
|
out, err = rc.StartJob(call.Fn, in)
|
|
|
|
} else {
|
|
|
|
out, err = call.Fn(in)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
writeError(path, in, w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
out = make(rc.Params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Debugf(nil, "rc: %q: reply %+v: %v", path, out, err)
|
|
|
|
err = rc.WriteJSON(w, out)
|
|
|
|
if err != nil {
|
|
|
|
// can't return the error at this point
|
|
|
|
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) handleOptions(w http.ResponseWriter, r *http.Request, path string) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2018-10-28 15:31:24 +01:00
|
|
|
func (s *server) serveRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
remotes := config.FileSections()
|
|
|
|
sort.Strings(remotes)
|
|
|
|
directory := serve.NewDirectory("")
|
|
|
|
directory.Title = "List of all rclone remotes."
|
|
|
|
q := url.Values{}
|
|
|
|
for _, remote := range remotes {
|
|
|
|
q.Set("fs", remote)
|
|
|
|
directory.AddEntry("["+remote+":]", true)
|
|
|
|
}
|
|
|
|
directory.Serve(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) serveRemote(w http.ResponseWriter, r *http.Request, path string, fsName string) {
|
|
|
|
f, err := rc.GetCachedFs(fsName)
|
|
|
|
if err != nil {
|
|
|
|
writeError(path, nil, w, errors.Wrap(err, "failed to make Fs"), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if path == "" || strings.HasSuffix(path, "/") {
|
|
|
|
path = strings.Trim(path, "/")
|
|
|
|
entries, err := list.DirSorted(f, false, path)
|
2018-10-27 19:29:20 +02:00
|
|
|
if err != nil {
|
2018-10-28 15:31:24 +01:00
|
|
|
writeError(path, nil, w, errors.Wrap(err, "failed to list directory"), http.StatusInternalServerError)
|
2018-10-27 19:29:20 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-28 15:31:24 +01:00
|
|
|
// Make the entries for display
|
|
|
|
directory := serve.NewDirectory(path)
|
|
|
|
for _, entry := range entries {
|
|
|
|
_, isDir := entry.(fs.Directory)
|
|
|
|
directory.AddEntry(entry.Remote(), isDir)
|
|
|
|
}
|
|
|
|
directory.Serve(w, r)
|
|
|
|
} else {
|
2018-10-27 19:29:20 +02:00
|
|
|
o, err := f.NewObject(path)
|
|
|
|
if err != nil {
|
|
|
|
writeError(path, nil, w, errors.Wrap(err, "failed to find object"), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
serve.Object(w, r, o)
|
2018-10-28 15:31:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match URLS of the form [fs]/remote
|
|
|
|
var fsMatch = regexp.MustCompile(`^\[(.*?)\](.*)$`)
|
|
|
|
|
|
|
|
func (s *server) handleGet(w http.ResponseWriter, r *http.Request, path string) {
|
|
|
|
// Look to see if this has an fs in the path
|
|
|
|
match := fsMatch.FindStringSubmatch(path)
|
|
|
|
switch {
|
|
|
|
case match != nil && s.opt.Serve:
|
|
|
|
// Serve /[fs]/remote files
|
|
|
|
s.serveRemote(w, r, match[2], match[1])
|
|
|
|
return
|
|
|
|
case path == "*" && s.opt.Serve:
|
|
|
|
// Serve /* as the remote listing
|
|
|
|
s.serveRoot(w, r)
|
|
|
|
return
|
|
|
|
case s.files != nil:
|
|
|
|
// Serve the files
|
2018-10-27 19:29:20 +02:00
|
|
|
s.files.ServeHTTP(w, r)
|
2018-10-28 15:31:24 +01:00
|
|
|
return
|
|
|
|
case path == "" && s.opt.Serve:
|
|
|
|
// Serve the root as a remote listing
|
|
|
|
s.serveRoot(w, r)
|
|
|
|
return
|
2018-10-27 19:29:20 +02:00
|
|
|
}
|
2018-10-28 15:31:24 +01:00
|
|
|
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
|
2018-10-27 19:29:20 +02:00
|
|
|
}
|