2016-06-10 17:27:27 +00:00
|
|
|
// Package filemanager provides middleware for managing files in a directory
|
|
|
|
// when directory path is requested instead of a specific file. Based on browse
|
|
|
|
// middleware.
|
2016-06-10 13:36:43 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2016-10-18 16:56:35 +00:00
|
|
|
e "errors"
|
2016-06-10 13:36:43 +00:00
|
|
|
"net/http"
|
2017-04-16 13:01:11 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-06-10 17:27:27 +00:00
|
|
|
"strings"
|
2016-06-10 13:36:43 +00:00
|
|
|
|
2016-06-28 09:24:02 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/assets"
|
|
|
|
"github.com/hacdias/caddy-filemanager/config"
|
2016-10-18 20:49:46 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/file"
|
2016-10-22 10:47:49 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/handlers"
|
2016-10-18 20:30:10 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/page"
|
2017-04-16 13:29:35 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/wrapper"
|
2016-06-10 13:36:43 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// FileManager is an http.Handler that can show a file listing when
|
|
|
|
// directories in the given paths are specified.
|
2016-06-10 13:36:43 +00:00
|
|
|
type FileManager struct {
|
2016-06-11 20:20:47 +00:00
|
|
|
Next httpserver.Handler
|
2016-06-25 20:57:10 +00:00
|
|
|
Configs []config.Config
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-11-01 13:46:28 +00:00
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
|
|
|
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2016-06-11 20:20:47 +00:00
|
|
|
var (
|
2016-10-18 16:56:35 +00:00
|
|
|
c *config.Config
|
2016-10-18 20:49:46 +00:00
|
|
|
fi *file.Info
|
2016-10-18 16:56:35 +00:00
|
|
|
code int
|
|
|
|
err error
|
|
|
|
user *config.User
|
2016-06-11 20:20:47 +00:00
|
|
|
)
|
2016-06-10 21:44:33 +00:00
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
for i := range f.Configs {
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks if this Path should be handled by File Manager.
|
|
|
|
if !httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
2017-01-22 09:59:56 +00:00
|
|
|
continue
|
2016-10-18 21:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c = &f.Configs[i]
|
2016-10-18 16:56:35 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks if the URL matches the Assets URL. Returns the asset if the
|
|
|
|
// method is GET and Status Forbidden otherwise.
|
|
|
|
if httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL) {
|
|
|
|
if r.Method == http.MethodGet {
|
2016-10-18 16:56:35 +00:00
|
|
|
return assets.Serve(w, r, c)
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2017-04-16 12:29:57 +00:00
|
|
|
// Obtains the user. See https://github.com/mholt/caddy/blob/master/caddyhttp/basicauth/basicauth.go#L66
|
|
|
|
username, _ := r.Context().Value(httpserver.RemoteUserCtxKey).(string)
|
2016-10-18 21:00:26 +00:00
|
|
|
if _, ok := c.Users[username]; ok {
|
|
|
|
user = c.Users[username]
|
|
|
|
} else {
|
|
|
|
user = c.User
|
|
|
|
}
|
2016-08-21 18:44:09 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks if the request URL is for the WebDav server
|
2017-04-15 13:00:41 +00:00
|
|
|
if httpserver.Path(r.URL.Path).Matches(c.WebDavURL) {
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks for user permissions relatively to this PATH
|
|
|
|
if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.WebDavURL)) {
|
|
|
|
return http.StatusForbidden, nil
|
2016-08-21 18:44:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
switch r.Method {
|
2017-04-16 13:04:30 +00:00
|
|
|
case "GET", "HEAD":
|
2017-04-16 13:01:11 +00:00
|
|
|
// Excerpt from RFC4918, section 9.4:
|
|
|
|
//
|
|
|
|
// GET, when applied to a collection, may return the contents of an
|
|
|
|
// "index.html" resource, a human-readable view of the contents of
|
|
|
|
// the collection, or something else altogether.
|
|
|
|
//
|
|
|
|
// It was decided on https://github.com/hacdias/caddy-filemanager/issues/85
|
2017-04-16 13:02:24 +00:00
|
|
|
// that GET, for collections, will return the same as PROPFIND method.
|
2017-04-16 13:01:11 +00:00
|
|
|
path := strings.Replace(r.URL.Path, c.WebDavURL, "", 1)
|
|
|
|
path = user.Scope + "/" + path
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
|
|
|
var i os.FileInfo
|
|
|
|
i, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
// Is there any error? WebDav will handle it... no worries.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if i.IsDir() {
|
|
|
|
r.Method = "PROPFIND"
|
2017-04-16 13:29:35 +00:00
|
|
|
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
w = wrapper.NewResponseWriterNoBody(w)
|
|
|
|
}
|
2017-04-16 13:01:11 +00:00
|
|
|
}
|
2016-10-18 21:00:26 +00:00
|
|
|
case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE":
|
|
|
|
if !user.AllowEdit {
|
2016-10-18 15:42:48 +00:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
2016-10-18 21:00:26 +00:00
|
|
|
case "MKCOL", "COPY":
|
|
|
|
if !user.AllowNew {
|
|
|
|
return http.StatusForbidden, nil
|
2016-10-18 15:42:48 +00:00
|
|
|
}
|
2016-10-18 21:00:26 +00:00
|
|
|
}
|
2016-10-18 15:42:48 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
// Preprocess the PUT request if it's the case
|
|
|
|
if r.Method == http.MethodPut {
|
2016-12-18 12:34:31 +00:00
|
|
|
if err = c.BeforeSave(r, c, user); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2016-11-15 13:38:55 +00:00
|
|
|
if handlers.PreProccessPUT(w, r, c, user) != nil {
|
2016-10-18 21:00:26 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2016-10-18 15:17:01 +00:00
|
|
|
}
|
2016-10-18 21:00:26 +00:00
|
|
|
}
|
2016-10-18 15:17:01 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
c.Handler.ServeHTTP(w, r)
|
2016-12-18 12:34:31 +00:00
|
|
|
if err = c.AfterSave(r, c, user); err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:29:29 +00:00
|
|
|
w.Header().Set("x-frame-options", "SAMEORIGIN")
|
|
|
|
w.Header().Set("x-content-type", "nosniff")
|
|
|
|
w.Header().Set("x-xss-protection", "1; mode=block")
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
// Checks if the User is allowed to access this file
|
|
|
|
if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.BaseURL)) {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return page.PrintErrorHTML(
|
|
|
|
w, http.StatusForbidden,
|
|
|
|
e.New("You don't have permission to access this page."),
|
|
|
|
)
|
2016-10-17 20:05:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:00:36 +00:00
|
|
|
if r.URL.Query().Get("search") != "" {
|
|
|
|
return handlers.Search(w, r, c, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("command") != "" {
|
|
|
|
return handlers.Command(w, r, c, user)
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
// Gets the information of the directory/file
|
|
|
|
fi, code, err = file.GetInfo(r.URL, c, user)
|
|
|
|
if err != nil {
|
2016-10-18 16:56:35 +00:00
|
|
|
if r.Method == http.MethodGet {
|
2016-10-18 21:00:26 +00:00
|
|
|
return page.PrintErrorHTML(w, code, err)
|
2016-10-18 16:56:35 +00:00
|
|
|
}
|
2016-10-18 21:00:26 +00:00
|
|
|
return code, err
|
|
|
|
}
|
2016-10-18 16:56:35 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
// If it's a dir and the path doesn't end with a trailing slash,
|
|
|
|
// redirect the user.
|
2017-01-03 15:10:33 +00:00
|
|
|
if fi.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
2017-01-25 14:01:30 +00:00
|
|
|
http.Redirect(w, r, c.PrefixURL+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
2016-10-18 21:00:26 +00:00
|
|
|
return 0, nil
|
2016-08-22 10:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 10:47:49 +00:00
|
|
|
switch {
|
|
|
|
case r.URL.Query().Get("download") != "":
|
|
|
|
code, err = handlers.Download(w, r, c, fi)
|
2017-01-03 15:10:33 +00:00
|
|
|
case r.URL.Query().Get("raw") == "true" && !fi.IsDir:
|
2016-10-22 10:47:49 +00:00
|
|
|
http.ServeFile(w, r, fi.Path)
|
|
|
|
code, err = 0, nil
|
2017-01-03 15:10:33 +00:00
|
|
|
case fi.IsDir:
|
2016-10-22 10:47:49 +00:00
|
|
|
code, err = handlers.ServeListing(w, r, c, user, fi)
|
|
|
|
default:
|
|
|
|
code, err = handlers.ServeSingle(w, r, c, user, fi)
|
2016-10-20 20:55:55 +00:00
|
|
|
}
|
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
if err != nil {
|
2016-10-22 10:47:49 +00:00
|
|
|
code, err = page.PrintErrorHTML(w, code, err)
|
2016-10-18 21:00:26 +00:00
|
|
|
}
|
2016-10-22 10:47:49 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
return code, err
|
|
|
|
}
|
2016-08-22 11:09:18 +00:00
|
|
|
|
2016-10-18 21:00:26 +00:00
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|
2016-06-11 20:20:47 +00:00
|
|
|
|
2016-06-10 19:54:19 +00:00
|
|
|
return f.Next.ServeHTTP(w, r)
|
2016-06-10 17:27:27 +00:00
|
|
|
}
|