Files
filebrowser/filemanager.go
T

299 lines
7.5 KiB
Go
Raw Normal View History

2016-06-10 18:27:27 +01:00
//go:generate go get github.com/jteeuwen/go-bindata
//go:generate go install github.com/jteeuwen/go-bindata/go-bindata
2016-07-01 14:55:17 +01:00
//go:generate go-bindata -pkg assets -ignore .jsbeautifyrc -prefix "assets/embed" -o assets/binary.go assets/embed/...
2016-06-10 18:27:27 +01: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 14:36:43 +01:00
package filemanager
import (
2016-08-22 11:37:56 +01:00
e "errors"
2016-06-28 10:24:02 +01:00
"io"
2016-06-28 21:28:39 +01:00
"io/ioutil"
2016-06-28 10:24:02 +01:00
"log"
"mime/multipart"
2016-06-10 14:36:43 +01:00
"net/http"
2016-06-28 10:24:02 +01:00
"os"
"os/exec"
"path/filepath"
2016-06-10 18:27:27 +01:00
"strings"
2016-06-10 14:36:43 +01:00
2016-06-28 10:24:02 +01:00
"github.com/hacdias/caddy-filemanager/assets"
"github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/directory"
2016-07-06 12:00:28 +01:00
"github.com/hacdias/caddy-filemanager/errors"
2016-06-28 10:24:02 +01:00
"github.com/hacdias/caddy-filemanager/page"
2016-06-10 14:36:43 +01:00
"github.com/mholt/caddy/caddyhttp/httpserver"
)
2016-06-10 18:27:27 +01:00
// FileManager is an http.Handler that can show a file listing when
// directories in the given paths are specified.
2016-06-10 14:36:43 +01:00
type FileManager struct {
2016-06-11 21:20:47 +01:00
Next httpserver.Handler
2016-06-25 21:57:10 +01:00
Configs []config.Config
2016-06-10 18:27:27 +01: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 21:20:47 +01:00
var (
2016-06-28 10:24:02 +01:00
c *config.Config
fi *directory.Info
code int
err error
serveAssets bool
2016-08-22 11:37:56 +01:00
user *config.User
2016-06-11 21:20:47 +01:00
)
2016-06-10 22:44:33 +01:00
2016-06-10 18:27:27 +01:00
for i := range f.Configs {
2016-06-10 20:54:19 +01:00
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
2016-06-11 10:08:33 +01:00
c = &f.Configs[i]
2016-06-28 10:24:02 +01:00
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
2016-06-10 20:54:19 +01:00
2016-08-22 12:09:18 +01:00
// Set the current user.
2016-08-21 19:44:09 +01:00
username, _, _ := r.BasicAuth()
if _, ok := c.Users[username]; ok {
user = c.Users[username]
2016-08-22 12:09:18 +01:00
} else {
user = c.User
2016-08-21 19:44:09 +01:00
}
2016-08-22 12:09:18 +01:00
// Checks if the user has permission to access the current directory.
2016-08-22 11:37:56 +01:00
if !user.Allowed(r.URL.Path) {
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 12:09:18 +01:00
// If this request is neither to server assets, nor to upload/create
// a new file or directory.
2016-06-28 10:24:02 +01:00
if r.Method != http.MethodPost && !serveAssets {
2016-08-22 12:09:18 +01:00
// Gets the information of the directory/file
2016-08-21 20:25:40 +01:00
fi, code, err = directory.GetInfo(r.URL, c, user)
2016-06-11 21:20:47 +01:00
if err != nil {
2016-07-06 12:00:28 +01:00
if r.Method == http.MethodGet {
return errors.PrintHTML(w, code, err)
}
2016-06-11 21:20:47 +01:00
return code, err
2016-06-10 20:54:19 +01:00
}
2016-06-11 10:08:33 +01:00
2016-08-22 12:09:18 +01:00
// If it's a dir and the path doesn't end with a trailing slash,
// redirect the user.
2016-06-11 21:20:47 +01:00
if fi.IsDir && !strings.HasSuffix(r.URL.Path, "/") {
2016-07-01 15:51:02 +01:00
http.Redirect(w, r, c.AddrPath+r.URL.Path+"/", http.StatusTemporaryRedirect)
2016-06-11 10:08:33 +01:00
return 0, nil
2016-06-10 20:54:19 +01:00
}
}
2016-06-10 18:27:27 +01:00
2016-08-22 12:09:18 +01:00
// Security measures against CSRF attacks.
2016-07-05 17:46:45 +01:00
if r.Method != http.MethodGet {
if !c.CheckToken(r) {
return http.StatusForbidden, nil
}
}
2016-08-22 12:09:18 +01:00
// Route the request depending on the HTTP Method.
2016-06-10 20:54:19 +01:00
switch r.Method {
2016-06-11 10:08:33 +01:00
case http.MethodGet:
2016-08-22 12:09:18 +01:00
// Read and show directory or file.
2016-06-28 10:24:02 +01:00
if serveAssets {
return assets.Serve(w, r, c)
2016-06-11 10:08:33 +01:00
}
2016-06-11 21:20:47 +01:00
2016-08-22 12:09:18 +01:00
// Generate anti security token.
2016-07-05 17:46:45 +01:00
c.GenerateToken()
2016-06-11 23:01:24 +01:00
if !fi.IsDir {
query := r.URL.Query()
if val, ok := query["raw"]; ok && val[0] == "true" {
2016-06-14 20:33:59 +01:00
return fi.ServeRawFile(w, r, c)
2016-06-11 23:01:24 +01:00
}
if val, ok := query["download"]; ok && val[0] == "true" {
w.Header().Set("Content-Disposition", "attachment; filename="+fi.Name)
2016-06-14 20:33:59 +01:00
return fi.ServeRawFile(w, r, c)
2016-06-11 23:01:24 +01:00
}
2016-06-11 22:07:55 +01:00
}
2016-08-21 19:44:09 +01:00
code, err := fi.ServeAsHTML(w, r, c, user)
2016-07-06 12:00:28 +01:00
if err != nil {
return errors.PrintHTML(w, code, err)
}
return code, err
2016-06-27 13:22:07 +01:00
case http.MethodPut:
if fi.IsDir {
return http.StatusNotAcceptable, nil
}
2016-08-21 19:21:09 +01:00
if !user.AllowEdit {
return http.StatusUnauthorized, nil
}
2016-08-22 12:09:18 +01:00
// Update a file.
return fi.Update(w, r, c, user)
2016-06-11 10:08:33 +01:00
case http.MethodPost:
2016-08-22 12:09:18 +01:00
// Upload a new file.
2016-06-11 21:46:28 +01:00
if r.Header.Get("Upload") == "true" {
2016-08-21 19:21:09 +01:00
if !user.AllowNew {
return http.StatusUnauthorized, nil
}
2016-06-28 10:24:02 +01:00
return upload(w, r, c)
2016-06-11 21:46:28 +01:00
}
2016-08-22 12:09:18 +01:00
// Search and git commands.
2016-06-26 16:52:15 +01:00
if r.Header.Get("Search") == "true" {
2016-08-22 12:09:18 +01:00
// TODO: search commands.
2016-06-26 18:30:08 +01:00
}
2016-08-22 12:09:18 +01:00
// VCS commands.
2016-06-26 18:30:08 +01:00
if r.Header.Get("Command") != "" {
2016-08-21 19:21:09 +01:00
if !user.AllowCommands {
return http.StatusUnauthorized, nil
}
2016-08-21 20:20:13 +01:00
return command(w, r, c, user)
2016-06-26 16:52:15 +01:00
}
2016-08-22 12:09:18 +01:00
// Creates a new folder.
2016-06-28 10:24:02 +01:00
return newDirectory(w, r, c)
2016-06-11 10:08:33 +01:00
case http.MethodDelete:
2016-08-21 19:21:09 +01:00
if !user.AllowEdit {
return http.StatusUnauthorized, nil
}
2016-06-11 10:08:33 +01:00
// Delete a file or a directory
2016-06-11 21:20:47 +01:00
return fi.Delete()
2016-06-11 10:08:33 +01:00
case http.MethodPatch:
2016-08-21 19:21:09 +01:00
if !user.AllowEdit {
return http.StatusUnauthorized, nil
}
2016-06-11 10:08:33 +01:00
// Rename a file or directory
2016-06-11 21:34:38 +01:00
return fi.Rename(w, r)
2016-06-10 20:54:19 +01:00
default:
2016-06-11 10:08:33 +01:00
return http.StatusNotImplemented, nil
2016-06-10 20:54:19 +01:00
}
}
2016-06-10 18:27:27 +01:00
}
2016-06-11 21:20:47 +01:00
2016-06-10 20:54:19 +01:00
return f.Next.ServeHTTP(w, r)
2016-06-10 18:27:27 +01:00
}
2016-06-28 10:24:02 +01:00
// upload is used to handle the upload requests to the server
func upload(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
log.Println(err)
return http.StatusInternalServerError, err
}
// For each file header in the multipart form
for _, headers := range r.MultipartForm.File {
// Handle each file
for _, header := range headers {
// Open the first file
var src multipart.File
if src, err = header.Open(); nil != err {
return http.StatusInternalServerError, err
}
filename := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
filename = filename + header.Filename
filename = filepath.Clean(filename)
// Create the file
var dst *os.File
if dst, err = os.Create(filename); nil != err {
if os.IsExist(err) {
return http.StatusConflict, err
}
return http.StatusInternalServerError, err
}
// Copy the file content
if _, err = io.Copy(dst, src); nil != err {
return http.StatusInternalServerError, err
}
defer dst.Close()
}
}
return http.StatusOK, nil
}
// newDirectory makes a new directory
func newDirectory(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
2016-06-28 21:28:39 +01:00
filename := r.Header.Get("Filename")
if filename == "" {
return http.StatusBadRequest, nil
}
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1) + filename
2016-06-28 10:24:02 +01:00
path = filepath.Clean(path)
2016-06-28 21:28:39 +01:00
extension := filepath.Ext(path)
var err error
if extension == "" {
err = os.MkdirAll(path, 0755)
} else {
err = ioutil.WriteFile(path, []byte(""), 0755)
}
2016-06-28 10:24:02 +01:00
if err != nil {
switch {
case os.IsPermission(err):
return http.StatusForbidden, err
case os.IsExist(err):
return http.StatusConflict, err
default:
return http.StatusInternalServerError, err
}
}
return http.StatusCreated, nil
}
2016-08-21 20:20:13 +01:00
// command handles the requests for VCS related commands: git, svn and mercurial
2016-08-22 11:37:56 +01:00
func command(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User) (int, error) {
2016-06-28 10:24:02 +01:00
command := strings.Split(r.Header.Get("command"), " ")
2016-08-21 20:20:13 +01:00
// Check if the command is allowed
mayContinue := false
for _, cmd := range u.Commands {
if cmd == command[0] {
mayContinue = true
}
}
if !mayContinue {
2016-06-28 10:24:02 +01: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-06-30 22:24:38 +01:00
path := strings.Replace(r.URL.Path, c.BaseURL, c.PathScope, 1)
path = filepath.Clean(path)
2016-06-28 10:24:02 +01:00
cmd := exec.Command(command[0], command[1:len(command)]...)
2016-06-30 22:24:38 +01:00
cmd.Dir = path
2016-06-28 10:24:02 +01:00
output, err := cmd.CombinedOutput()
if err != nil {
return http.StatusInternalServerError, err
}
page := &page.Page{Info: &page.Info{Data: string(output)}}
return page.PrintAsJSON(w)
}