2017-06-20 10:40:52 +00:00
|
|
|
package filemanager
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-25 10:40:23 +00:00
|
|
|
"errors"
|
2017-06-24 11:12:15 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
rice "github.com/GeertJohan/go.rice"
|
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
)
|
|
|
|
|
2017-06-25 11:53:49 +00:00
|
|
|
var (
|
|
|
|
// ErrDuplicated occurs when you try to create a user that already exists.
|
|
|
|
ErrDuplicated = errors.New("Duplicated user")
|
|
|
|
)
|
|
|
|
|
2017-06-25 14:24:16 +00:00
|
|
|
// FileManager is a file manager instance. It should be creating using the
|
|
|
|
// 'New' function and not directly.
|
2017-06-24 11:12:15 +00:00
|
|
|
type FileManager struct {
|
2017-06-25 14:24:16 +00:00
|
|
|
*User
|
2017-06-27 08:28:29 +00:00
|
|
|
assets *assets
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// BaseURL is the path where the GUI will be accessible. It musn't end with
|
2017-06-25 14:49:24 +00:00
|
|
|
// a trailing slash and mustn't contain PrefixURL, if set. Despite being
|
|
|
|
// exported, it should only be manipulated using SetBaseURL function.
|
|
|
|
BaseURL string
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// WebDavURL is the path where the WebDAV will be accessible. It can be set to ""
|
|
|
|
// in order to override the GUI and only use the WebDAV. It musn't end with
|
2017-06-25 14:49:24 +00:00
|
|
|
// a trailing slash. Despite being exported, it should only be manipulated
|
|
|
|
// using SetWebDavURL function.
|
|
|
|
WebDavURL string
|
2017-06-25 12:09:49 +00:00
|
|
|
|
|
|
|
// PrefixURL is a part of the URL that is trimmed from the http.Request.URL before
|
|
|
|
// it arrives to our handlers. It may be useful when using FileManager as a middleware
|
|
|
|
// such as in caddy-filemanager plugin. It musn't end with a trailing slash.
|
|
|
|
PrefixURL string
|
2017-06-24 11:12:15 +00:00
|
|
|
|
|
|
|
// Users is a map with the different configurations for each user.
|
2017-06-25 14:24:16 +00:00
|
|
|
Users map[string]*User
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-06-25 14:19:23 +00:00
|
|
|
// BeforeSave is a function that is called before saving a file.
|
|
|
|
BeforeSave Command
|
|
|
|
|
|
|
|
// AfterSave is a function that is called before saving a file.
|
|
|
|
AfterSave Command
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 14:19:23 +00:00
|
|
|
// Command is a command function.
|
2017-06-25 14:24:16 +00:00
|
|
|
type Command func(r *http.Request, m *FileManager, u *User) error
|
2017-06-25 14:19:23 +00:00
|
|
|
|
2017-06-25 14:24:16 +00:00
|
|
|
// User contains the configuration for each user. It should be created
|
|
|
|
// using NewUser on a File Manager instance.
|
|
|
|
type User struct {
|
2017-06-25 09:52:34 +00:00
|
|
|
// scope is the physical path the user has access to.
|
2017-06-25 10:40:23 +00:00
|
|
|
scope string
|
2017-06-25 09:52:34 +00:00
|
|
|
|
|
|
|
// fileSystem is the virtual file system the user has access.
|
|
|
|
fileSystem webdav.FileSystem
|
|
|
|
|
|
|
|
// handler handles incoming requests to the WebDAV backend.
|
|
|
|
handler *webdav.Handler
|
|
|
|
|
|
|
|
// Rules is an array of access and deny rules.
|
|
|
|
Rules []*Rule `json:"-"`
|
|
|
|
|
|
|
|
// TODO: this MUST be done in another way
|
|
|
|
StyleSheet string `json:"-"`
|
|
|
|
|
|
|
|
// These indicate if the user can perform certain actions.
|
|
|
|
AllowNew bool // Create files and folders
|
|
|
|
AllowEdit bool // Edit/rename files
|
|
|
|
AllowCommands bool // Execute commands
|
|
|
|
|
|
|
|
// Commands is the list of commands the user can execute.
|
|
|
|
Commands []string
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// assets are the static and front-end assets, such as JS, CSS and HTML templates.
|
|
|
|
type assets struct {
|
2017-06-27 08:28:29 +00:00
|
|
|
templates *rice.Box
|
|
|
|
css *rice.Box
|
|
|
|
js *rice.Box
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rule is a dissalow/allow rule.
|
|
|
|
type Rule struct {
|
2017-06-25 09:52:34 +00:00
|
|
|
// Regex indicates if this rule uses Regular Expressions or not.
|
|
|
|
Regex bool
|
|
|
|
|
|
|
|
// Allow indicates if this is an allow rule. Set 'false' to be a disallow rule.
|
|
|
|
Allow bool
|
|
|
|
|
|
|
|
// Path is the corresponding URL path for this rule.
|
|
|
|
Path string
|
|
|
|
|
|
|
|
// Regexp is the regular expression. Only use this when 'Regex' was set to true.
|
2017-06-24 11:12:15 +00:00
|
|
|
Regexp *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2017-06-25 14:19:23 +00:00
|
|
|
// New creates a new File Manager instance with the needed
|
|
|
|
// configuration to work.
|
2017-06-25 11:53:49 +00:00
|
|
|
func New(scope string) *FileManager {
|
2017-06-25 08:11:25 +00:00
|
|
|
m := &FileManager{
|
2017-06-25 14:24:16 +00:00
|
|
|
User: &User{
|
2017-06-25 08:11:25 +00:00
|
|
|
AllowCommands: true,
|
|
|
|
AllowEdit: true,
|
|
|
|
AllowNew: true,
|
2017-06-25 14:19:23 +00:00
|
|
|
Commands: []string{},
|
|
|
|
Rules: []*Rule{},
|
2017-06-25 08:11:25 +00:00
|
|
|
},
|
2017-06-25 14:24:16 +00:00
|
|
|
Users: map[string]*User{},
|
|
|
|
BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
|
|
|
|
AfterSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
|
2017-06-27 08:28:29 +00:00
|
|
|
assets: &assets{
|
|
|
|
templates: rice.MustFindBox("./_assets/templates"),
|
|
|
|
css: rice.MustFindBox("./_assets/css"),
|
|
|
|
js: rice.MustFindBox("./_assets/js"),
|
2017-06-25 08:11:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-25 11:53:49 +00:00
|
|
|
m.SetScope(scope, "")
|
2017-06-25 08:11:25 +00:00
|
|
|
m.SetBaseURL("/")
|
|
|
|
m.SetWebDavURL("/webdav")
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// AbsoluteURL returns the actual URL where
|
|
|
|
// File Manager interface can be accessed.
|
2017-06-24 11:12:15 +00:00
|
|
|
func (m FileManager) AbsoluteURL() string {
|
2017-06-25 14:49:24 +00:00
|
|
|
return m.PrefixURL + m.BaseURL
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// AbsoluteWebDavURL returns the actual URL
|
|
|
|
// where WebDAV can be accessed.
|
|
|
|
func (m FileManager) AbsoluteWebDavURL() string {
|
2017-06-25 14:49:24 +00:00
|
|
|
return m.PrefixURL + m.WebDavURL
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// SetBaseURL updates the BaseURL of a File Manager
|
|
|
|
// object.
|
2017-06-25 08:11:25 +00:00
|
|
|
func (m *FileManager) SetBaseURL(url string) {
|
|
|
|
url = strings.TrimPrefix(url, "/")
|
|
|
|
url = strings.TrimSuffix(url, "/")
|
|
|
|
url = "/" + url
|
2017-06-25 14:49:24 +00:00
|
|
|
m.BaseURL = strings.TrimSuffix(url, "/")
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 09:52:34 +00:00
|
|
|
// SetWebDavURL updates the WebDavURL of a File Manager
|
|
|
|
// object and updates it's main handler.
|
2017-06-25 08:11:25 +00:00
|
|
|
func (m *FileManager) SetWebDavURL(url string) {
|
2017-06-25 09:52:34 +00:00
|
|
|
url = strings.TrimPrefix(url, "/")
|
|
|
|
url = strings.TrimSuffix(url, "/")
|
|
|
|
|
2017-06-25 14:49:24 +00:00
|
|
|
m.WebDavURL = m.BaseURL + "/" + url
|
2017-06-25 10:40:23 +00:00
|
|
|
|
|
|
|
// update base user webdav handler
|
|
|
|
m.handler = &webdav.Handler{
|
2017-06-25 14:49:24 +00:00
|
|
|
Prefix: m.WebDavURL,
|
2017-06-25 09:52:34 +00:00
|
|
|
FileSystem: m.fileSystem,
|
2017-06-25 08:11:25 +00:00
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
}
|
2017-06-25 10:40:23 +00:00
|
|
|
|
|
|
|
// update other users' handlers to match
|
|
|
|
// the new URL
|
|
|
|
for _, u := range m.Users {
|
|
|
|
u.handler = &webdav.Handler{
|
2017-06-25 14:49:24 +00:00
|
|
|
Prefix: m.WebDavURL,
|
2017-06-25 10:40:23 +00:00
|
|
|
FileSystem: u.fileSystem,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
}
|
|
|
|
}
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 10:21:42 +00:00
|
|
|
// SetScope updates a user scope and its virtual file system.
|
2017-06-25 10:40:23 +00:00
|
|
|
// If the user string is blank, it will change the base scope.
|
|
|
|
func (m *FileManager) SetScope(scope string, username string) error {
|
2017-06-25 14:24:16 +00:00
|
|
|
var u *User
|
2017-06-25 10:40:23 +00:00
|
|
|
|
|
|
|
if username == "" {
|
2017-06-25 14:24:16 +00:00
|
|
|
u = m.User
|
2017-06-25 10:40:23 +00:00
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
u, ok = m.Users[username]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Inexistent user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u.scope = strings.TrimSuffix(scope, "/")
|
|
|
|
u.fileSystem = webdav.Dir(u.scope)
|
|
|
|
|
|
|
|
u.handler = &webdav.Handler{
|
2017-06-25 14:49:24 +00:00
|
|
|
Prefix: m.WebDavURL,
|
2017-06-25 10:40:23 +00:00
|
|
|
FileSystem: u.fileSystem,
|
|
|
|
LockSystem: webdav.NewMemLS(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-25 11:53:49 +00:00
|
|
|
// NewUser creates a new user on a File Manager struct
|
|
|
|
// which inherits its configuration from the main user.
|
|
|
|
func (m *FileManager) NewUser(username string) error {
|
|
|
|
if _, ok := m.Users[username]; ok {
|
|
|
|
return ErrDuplicated
|
|
|
|
}
|
|
|
|
|
2017-06-25 14:24:16 +00:00
|
|
|
m.Users[username] = &User{
|
|
|
|
scope: m.User.scope,
|
|
|
|
fileSystem: m.User.fileSystem,
|
|
|
|
handler: m.User.handler,
|
|
|
|
Rules: m.User.Rules,
|
|
|
|
AllowNew: m.User.AllowNew,
|
|
|
|
AllowEdit: m.User.AllowEdit,
|
|
|
|
AllowCommands: m.User.AllowCommands,
|
|
|
|
Commands: m.User.Commands,
|
2017-06-25 11:53:49 +00:00
|
|
|
}
|
2017-06-25 10:40:23 +00:00
|
|
|
|
2017-06-25 11:53:49 +00:00
|
|
|
return nil
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 08:28:29 +00:00
|
|
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
|
|
|
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
var (
|
2017-06-27 08:57:11 +00:00
|
|
|
ctx = &requestContext{
|
|
|
|
FileManager: m,
|
|
|
|
User: nil,
|
|
|
|
Info: nil,
|
|
|
|
}
|
2017-06-27 08:28:29 +00:00
|
|
|
code int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Checks if the URL matches the Assets URL. Returns the asset if the
|
|
|
|
// method is GET and Status Forbidden otherwise.
|
|
|
|
if matchURL(r.URL.Path, m.BaseURL+assetsURL) {
|
|
|
|
if r.Method == http.MethodGet {
|
2017-06-27 08:57:11 +00:00
|
|
|
return serveAssets(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
username, _, _ := r.BasicAuth()
|
|
|
|
if _, ok := m.Users[username]; ok {
|
2017-06-27 08:57:11 +00:00
|
|
|
ctx.User = m.Users[username]
|
2017-06-27 08:28:29 +00:00
|
|
|
} else {
|
2017-06-27 08:57:11 +00:00
|
|
|
ctx.User = m.User
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if the request URL is for the WebDav server
|
|
|
|
if matchURL(r.URL.Path, m.WebDavURL) {
|
2017-06-27 08:57:11 +00:00
|
|
|
return serveWebDAV(ctx, w, r)
|
2017-06-27 08:28: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")
|
|
|
|
|
|
|
|
// Checks if the User is allowed to access this file
|
2017-06-27 08:57:11 +00:00
|
|
|
if !ctx.User.Allowed(strings.TrimPrefix(r.URL.Path, m.BaseURL)) {
|
2017-06-27 08:28:29 +00:00
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return htmlError(
|
|
|
|
w, http.StatusForbidden,
|
|
|
|
errors.New("You don't have permission to access this page"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("search") != "" {
|
2017-06-27 08:57:11 +00:00
|
|
|
return search(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("command") != "" {
|
2017-06-27 08:57:11 +00:00
|
|
|
return command(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
var f *fileInfo
|
|
|
|
|
|
|
|
// Obtains the information of the directory/file.
|
2017-06-27 08:57:11 +00:00
|
|
|
f, err = getInfo(r.URL, m, ctx.User)
|
2017-06-27 08:28:29 +00:00
|
|
|
if err != nil {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return htmlError(w, code, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code = errorToHTTP(err, false)
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a dir and the path doesn't end with a trailing slash,
|
|
|
|
// redirect the user.
|
|
|
|
if f.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
|
|
|
|
http.Redirect(w, r, m.PrefixURL+r.URL.Path+"/", http.StatusTemporaryRedirect)
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case r.URL.Query().Get("download") != "":
|
2017-06-27 08:57:11 +00:00
|
|
|
code, err = download(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
case !f.IsDir && r.URL.Query().Get("checksum") != "":
|
2017-06-27 08:57:11 +00:00
|
|
|
code, err = checksum(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
case r.URL.Query().Get("raw") == "true" && !f.IsDir:
|
|
|
|
http.ServeFile(w, r, f.Path)
|
|
|
|
code, err = 0, nil
|
|
|
|
case f.IsDir:
|
2017-06-27 08:57:11 +00:00
|
|
|
code, err = serveListing(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
default:
|
2017-06-27 08:57:11 +00:00
|
|
|
code, err = serveSingle(ctx, w, r)
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
code, err = htmlError(w, code, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusNotImplemented, nil
|
|
|
|
}
|
|
|
|
|
2017-06-24 11:12:15 +00:00
|
|
|
// Allowed checks if the user has permission to access a directory/file.
|
2017-06-25 14:24:16 +00:00
|
|
|
func (u User) Allowed(url string) bool {
|
2017-06-24 11:12:15 +00:00
|
|
|
var rule *Rule
|
|
|
|
i := len(u.Rules) - 1
|
|
|
|
|
|
|
|
for i >= 0 {
|
|
|
|
rule = u.Rules[i]
|
|
|
|
|
|
|
|
if rule.Regex {
|
|
|
|
if rule.Regexp.MatchString(url) {
|
|
|
|
return rule.Allow
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(url, rule.Path) {
|
|
|
|
return rule.Allow
|
|
|
|
}
|
|
|
|
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2017-06-25 19:52:06 +00:00
|
|
|
|
|
|
|
// Scope returns the user scope.
|
|
|
|
func (u User) Scope() string {
|
|
|
|
return u.scope
|
|
|
|
}
|