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"
|
2017-07-03 07:59:49 +00:00
|
|
|
"github.com/asdine/storm"
|
2017-06-24 11:12:15 +00:00
|
|
|
"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-07-06 13:40:06 +00:00
|
|
|
// The BoltDB database for this instance.
|
|
|
|
db *storm.DB
|
|
|
|
|
|
|
|
// The key used to sign the JWT tokens.
|
2017-07-03 07:59:49 +00:00
|
|
|
key []byte
|
2017-06-24 11:12:15 +00:00
|
|
|
|
2017-07-06 13:40:06 +00:00
|
|
|
// The static assets.
|
|
|
|
assets *rice.Box
|
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// PrefixURL is a part of the URL that is already trimmed from the request URL before it
|
2017-06-27 09:45:06 +00:00
|
|
|
// arrives to our handlers. It may be useful when using File Manager as a middleware
|
|
|
|
// such as in caddy-filemanager plugin. It is only useful in certain situations.
|
2017-07-03 07:59:49 +00:00
|
|
|
PrefixURL string
|
2017-06-27 09:31:50 +00:00
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// BaseURL is the path where the GUI will be accessible. It musn't end with
|
|
|
|
// a trailing slash and mustn't contain PrefixURL, if set. It shouldn't be
|
|
|
|
// edited directly. Use SetBaseURL.
|
|
|
|
BaseURL 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-07-06 13:40:06 +00:00
|
|
|
// The plugins that have been plugged in.
|
|
|
|
Plugins []*Plugin
|
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-07-03 07:59:49 +00:00
|
|
|
// ID is the required primary key with auto increment0
|
|
|
|
ID int `storm:"id,increment"`
|
|
|
|
|
|
|
|
// Username is the user username used to login.
|
|
|
|
Username string `json:"username" storm:"index,unique"`
|
2017-06-25 09:52:34 +00:00
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// The hashed password. This never reaches the front-end because it's temporarily
|
|
|
|
// emptied during JSON marshall.
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
2017-07-06 08:33:36 +00:00
|
|
|
// Tells if this user is an admin.
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// FileSystem is the virtual file system the user has access.
|
|
|
|
FileSystem webdav.Dir `json:"filesystem"`
|
2017-06-25 09:52:34 +00:00
|
|
|
|
|
|
|
// Rules is an array of access and deny rules.
|
2017-07-03 07:59:49 +00:00
|
|
|
Rules []*Rule `json:"rules"`
|
2017-06-25 09:52:34 +00:00
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// Costum styles for this user.
|
|
|
|
CSS string `json:"css"`
|
2017-06-25 09:52:34 +00:00
|
|
|
|
|
|
|
// These indicate if the user can perform certain actions.
|
2017-06-28 15:05:30 +00:00
|
|
|
AllowNew bool `json:"allowNew"` // Create files and folders
|
|
|
|
AllowEdit bool `json:"allowEdit"` // Edit/rename files
|
|
|
|
AllowCommands bool `json:"allowCommands"` // Execute commands
|
2017-06-25 09:52:34 +00:00
|
|
|
|
|
|
|
// Commands is the list of commands the user can execute.
|
2017-06-28 15:05:30 +00:00
|
|
|
Commands []string `json:"commands"`
|
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-07-03 07:59:49 +00:00
|
|
|
Regexp *Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regexp is a regular expression wrapper around native regexp.
|
|
|
|
type Regexp struct {
|
|
|
|
Raw string
|
|
|
|
regexp *regexp.Regexp
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 13:40:06 +00:00
|
|
|
// Plugin is a File Manager plugin.
|
|
|
|
type Plugin struct {
|
|
|
|
// The JavaScript that will be injected into the main page.
|
|
|
|
JavaScript string
|
|
|
|
}
|
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// DefaultUser is used on New, when no 'base' user is provided.
|
|
|
|
var DefaultUser = User{
|
|
|
|
Username: "admin",
|
|
|
|
Password: "admin",
|
|
|
|
AllowCommands: true,
|
|
|
|
AllowEdit: true,
|
|
|
|
AllowNew: true,
|
|
|
|
Commands: []string{},
|
|
|
|
Rules: []*Rule{},
|
|
|
|
CSS: "",
|
2017-07-06 08:33:36 +00:00
|
|
|
Admin: true,
|
2017-07-03 07:59:49 +00:00
|
|
|
FileSystem: webdav.Dir("."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new File Manager instance. If 'database' file already
|
|
|
|
// exists, it will load the users from there. Otherwise, a new user
|
|
|
|
// will be created using the 'base' variable. The 'base' User should
|
|
|
|
// not have the Password field hashed.
|
|
|
|
// TODO: should it ask for a baseURL on New????
|
|
|
|
func New(database string, base User) (*FileManager, error) {
|
|
|
|
// Creates a new File Manager instance with the Users
|
|
|
|
// map and Assets box.
|
2017-06-25 08:11:25 +00:00
|
|
|
m := &FileManager{
|
2017-07-03 07:59:49 +00:00
|
|
|
Users: map[string]*User{},
|
2017-07-06 07:52:39 +00:00
|
|
|
assets: rice.MustFindBox("./assets/dist"),
|
2017-07-03 07:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to open a database on the location provided. This
|
|
|
|
// function will automatically create a new one if it doesn't
|
|
|
|
// exist.
|
|
|
|
db, err := storm.Open(database)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to get the encryption key from the database.
|
|
|
|
// If it doesn't exist, create a new one of 256 bits.
|
|
|
|
err = db.Get("config", "key", &m.key)
|
|
|
|
if err != nil && err == storm.ErrNotFound {
|
|
|
|
m.key = []byte(randomString(64))
|
|
|
|
err = db.Set("config", "key", m.key)
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to fetch the users from the database and if there are
|
|
|
|
// any, add them to the current File Manager instance.
|
|
|
|
var users []User
|
|
|
|
err = db.All(&users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range users {
|
|
|
|
m.Users[users[i].Username] = &users[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no users in the database, it creates a new one
|
|
|
|
// based on 'base' User that must be provided by the function caller.
|
|
|
|
if len(users) == 0 {
|
|
|
|
// Hashes the password.
|
|
|
|
pw, err := hashPassword(base.Password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-06 08:33:36 +00:00
|
|
|
// The first user must be an administrator.
|
|
|
|
base.Admin = true
|
2017-07-03 07:59:49 +00:00
|
|
|
base.Password = pw
|
|
|
|
|
|
|
|
// Saves the user to the database.
|
|
|
|
if err := db.Save(&base); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Users[base.Username] = &base
|
|
|
|
}
|
2017-06-25 08:11:25 +00:00
|
|
|
|
2017-07-03 07:59:49 +00:00
|
|
|
// Attaches db to this File Manager instance.
|
|
|
|
m.db = db
|
|
|
|
return m, nil
|
2017-06-25 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 09:45:06 +00:00
|
|
|
// RootURL returns the actual URL where
|
2017-06-25 09:52:34 +00:00
|
|
|
// File Manager interface can be accessed.
|
2017-06-27 09:45:06 +00:00
|
|
|
func (m FileManager) RootURL() string {
|
2017-07-03 07:59:49 +00:00
|
|
|
return m.PrefixURL + m.BaseURL
|
2017-06-24 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 09:45:06 +00:00
|
|
|
// SetPrefixURL updates the prefixURL of a File
|
|
|
|
// Manager object.
|
2017-06-27 13:49:46 +00:00
|
|
|
func (m *FileManager) SetPrefixURL(url string) {
|
2017-06-27 09:45:06 +00:00
|
|
|
url = strings.TrimPrefix(url, "/")
|
|
|
|
url = strings.TrimSuffix(url, "/")
|
|
|
|
url = "/" + url
|
2017-07-03 07:59:49 +00:00
|
|
|
m.PrefixURL = strings.TrimSuffix(url, "/")
|
2017-06-27 09:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBaseURL updates the baseURL of a File Manager
|
2017-06-25 09:52:34 +00:00
|
|
|
// 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-07-03 07:59:49 +00:00
|
|
|
m.BaseURL = strings.TrimSuffix(url, "/")
|
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) {
|
2017-06-27 14:35:39 +00:00
|
|
|
// TODO: Handle errors here and make it compatible with http.Handler
|
2017-07-06 13:40:06 +00:00
|
|
|
code, err := serveHTTP(&requestContext{
|
2017-06-27 14:35:39 +00:00
|
|
|
fm: m,
|
|
|
|
us: nil,
|
|
|
|
fi: nil,
|
|
|
|
}, w, r)
|
2017-07-06 13:40:06 +00:00
|
|
|
|
|
|
|
if code != 0 && err != nil {
|
|
|
|
w.WriteHeader(code)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
2017-06-27 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
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-07-03 07:59:49 +00:00
|
|
|
|
|
|
|
// SetScope updates a user scope and its virtual file system.
|
|
|
|
// If the user string is blank, it will change the base scope.
|
|
|
|
func (u *User) SetScope(scope string) {
|
|
|
|
scope = strings.TrimSuffix(scope, "/")
|
|
|
|
u.FileSystem = webdav.Dir(scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchString checks if this string matches the regular expression.
|
|
|
|
func (r *Regexp) MatchString(s string) bool {
|
|
|
|
if r.regexp == nil {
|
|
|
|
r.regexp = regexp.MustCompile(r.Raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.regexp.MatchString(s)
|
|
|
|
}
|