filebrowser/filemanager.go

205 lines
5.2 KiB
Go
Raw Normal View History

//go:generate go get github.com/jteeuwen/go-bindata
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
2016-07-01 13:55:17 +00:00
//go:generate go-bindata -pkg assets -ignore .jsbeautifyrc -prefix "assets/embed" -o assets/binary.go assets/embed/...
// 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"
2016-06-28 09:24:02 +00:00
"os/exec"
"path/filepath"
"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-07-06 11:00:28 +00:00
"github.com/hacdias/caddy-filemanager/errors"
2016-10-18 20:30:10 +00:00
"github.com/hacdias/caddy-filemanager/page"
2016-06-10 13:36:43 +00:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// 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
}
// 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:06:31 +00:00
fi *FileInfo
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
for i := range f.Configs {
2016-06-10 19:54:19 +00:00
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
2016-06-11 09:08:33 +00:00
c = &f.Configs[i]
2016-10-18 16:56:35 +00:00
if r.Method == http.MethodGet && httpserver.Path(r.URL.Path).Matches(c.BaseURL+assets.BaseURL) {
return assets.Serve(w, r, c)
}
2016-08-21 18:44:09 +00:00
username, _, _ := r.BasicAuth()
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 16:56:35 +00:00
if strings.HasPrefix(r.URL.Path, c.WebDavURL) {
if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.WebDavURL)) {
2016-10-18 15:42:48 +00:00
return http.StatusForbidden, nil
}
2016-10-18 16:24:54 +00:00
2016-10-18 15:42:48 +00:00
switch r.Method {
case "PROPPATCH", "MOVE", "PATCH", "PUT", "DELETE":
if !user.AllowEdit {
return http.StatusForbidden, nil
}
case "MKCOL", "COPY":
if !user.AllowNew {
return http.StatusForbidden, nil
}
}
2016-10-18 15:17:01 +00:00
if r.Method == http.MethodPut {
_, err = fi.Update(w, r, c, user)
if err != nil {
return http.StatusInternalServerError, err
}
}
2016-10-18 20:06:31 +00:00
c.Handler.ServeHTTP(w, r)
2016-10-17 20:05:52 +00:00
return 0, nil
}
2016-10-18 16:56:35 +00:00
if !user.Allowed(strings.TrimPrefix(r.URL.Path, c.BaseURL)) {
if r.Method == http.MethodGet {
return errors.PrintHTML(
w,
http.StatusForbidden,
e.New("You don't have permission to access this page."),
)
}
return http.StatusForbidden, nil
2016-08-22 10:37:56 +00:00
}
2016-10-18 16:00:12 +00:00
if r.Method == http.MethodGet {
// Gets the information of the directory/file
2016-10-18 20:06:31 +00:00
fi, code, err = GetInfo(r.URL, c, user)
2016-06-11 20:20:47 +00:00
if err != nil {
2016-07-06 11:00:28 +00:00
if r.Method == http.MethodGet {
return errors.PrintHTML(w, code, err)
}
2016-06-11 20:20:47 +00:00
return code, err
2016-06-10 19:54:19 +00:00
}
2016-06-11 09:08:33 +00:00
// If it's a dir and the path doesn't end with a trailing slash,
// redirect the user.
2016-10-18 20:06:31 +00:00
if fi.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
2016-07-01 14:51:02 +00:00
http.Redirect(w, r, c.AddrPath+r.URL.Path+"/", http.StatusTemporaryRedirect)
2016-06-11 09:08:33 +00:00
return 0, nil
2016-06-10 19:54:19 +00:00
}
2016-06-11 20:20:47 +00:00
// Generate anti security token.
2016-07-05 16:46:45 +00:00
c.GenerateToken()
2016-10-18 20:06:31 +00:00
if !fi.IsDir() {
2016-06-11 22:01:24 +00:00
query := r.URL.Query()
if val, ok := query["raw"]; ok && val[0] == "true" {
2016-10-18 16:00:12 +00:00
r.URL.Path = strings.Replace(r.URL.Path, c.BaseURL, c.WebDavURL, 1)
2016-10-18 20:06:31 +00:00
c.Handler.ServeHTTP(w, r)
2016-10-18 16:00:12 +00:00
return 0, nil
2016-06-11 22:01:24 +00:00
}
if val, ok := query["download"]; ok && val[0] == "true" {
2016-10-18 20:06:31 +00:00
w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name())
2016-10-18 16:00:12 +00:00
r.URL.Path = strings.Replace(r.URL.Path, c.BaseURL, c.WebDavURL, 1)
2016-10-18 20:06:31 +00:00
c.Handler.ServeHTTP(w, r)
2016-10-18 16:00:12 +00:00
return 0, nil
2016-06-11 22:01:24 +00:00
}
2016-06-11 21:07:55 +00:00
}
2016-10-18 20:06:31 +00:00
code, err := fi.ServeHTTP(w, r, c, user)
2016-07-06 11:00:28 +00:00
if err != nil {
return errors.PrintHTML(w, code, err)
}
return code, err
2016-10-18 15:42:48 +00:00
}
if r.Method == http.MethodPost {
2016-10-18 16:56:35 +00:00
// TODO: How to exclude web dav clients? :/
// Security measures against CSRF attacks.
if !c.CheckToken(r) {
return http.StatusForbidden, nil
}
2016-10-18 16:24:54 +00:00
/* TODO: search commands. USE PROPFIND?
// Search and git commands.
2016-06-26 15:52:15 +00:00
if r.Header.Get("Search") == "true" {
2016-10-18 16:24:54 +00:00
} */
// VCS commands.
2016-06-26 17:30:08 +00:00
if r.Header.Get("Command") != "" {
2016-08-21 18:21:09 +00:00
if !user.AllowCommands {
return http.StatusUnauthorized, nil
}
return command(w, r, c, user)
2016-06-26 15:52:15 +00:00
}
2016-06-10 19:54:19 +00:00
}
2016-10-18 15:42:48 +00:00
return http.StatusNotImplemented, nil
2016-06-10 19:54:19 +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-28 09:24:02 +00:00
// command handles the requests for VCS related commands: git, svn and mercurial
2016-08-22 10:37:56 +00:00
func command(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
2016-06-28 09:24:02 +00:00
command := strings.Split(r.Header.Get("command"), " ")
// Check if the command is allowed
mayContinue := false
for _, cmd := range u.Commands {
if cmd == command[0] {
mayContinue = true
}
}
if !mayContinue {
2016-06-28 09:24:02 +00:00
return http.StatusForbidden, nil
}
// Check if the program is talled is installed on the computer
if _, err := exec.LookPath(command[0]); err != nil {
return http.StatusNotImplemented, nil
}
2016-10-18 20:06:31 +00:00
path := strings.Replace(r.URL.Path, c.BaseURL, c.Scope, 1)
2016-06-30 21:24:38 +00:00
path = filepath.Clean(path)
2016-06-28 09:24:02 +00:00
cmd := exec.Command(command[0], command[1:len(command)]...)
2016-06-30 21:24:38 +00:00
cmd.Dir = path
2016-06-28 09:24:02 +00:00
output, err := cmd.CombinedOutput()
if err != nil {
return http.StatusInternalServerError, err
}
2016-10-18 20:30:10 +00:00
p := &page.Page{Info: &page.Info{Data: string(output)}}
2016-10-18 20:06:31 +00:00
return p.PrintAsJSON(w)
2016-06-28 09:24:02 +00:00
}