2019-01-05 22:44:33 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-11-17 03:44:15 +00:00
|
|
|
"github.com/tomasen/realip"
|
|
|
|
|
2020-11-20 10:51:28 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/runner"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
|
|
|
type handleFunc func(w http.ResponseWriter, r *http.Request, d *data) (int, error)
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
*runner.Runner
|
|
|
|
settings *settings.Settings
|
2019-01-08 10:29:09 +00:00
|
|
|
server *settings.Server
|
2019-01-05 22:44:33 +00:00
|
|
|
store *storage.Storage
|
|
|
|
user *users.User
|
|
|
|
raw interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check implements rules.Checker.
|
|
|
|
func (d *data) Check(path string) bool {
|
2020-11-20 10:51:28 +00:00
|
|
|
if d.user.HideDotfiles && rules.MatchHidden(path) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:46:22 +00:00
|
|
|
allow := true
|
|
|
|
for _, rule := range d.settings.Rules {
|
2019-01-05 22:44:33 +00:00
|
|
|
if rule.Matches(path) {
|
2020-06-22 16:46:22 +00:00
|
|
|
allow = rule.Allow
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:46:22 +00:00
|
|
|
for _, rule := range d.user.Rules {
|
2019-01-05 22:44:33 +00:00
|
|
|
if rule.Matches(path) {
|
2020-06-22 16:46:22 +00:00
|
|
|
allow = rule.Allow
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:46:22 +00:00
|
|
|
return allow
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func handle(fn handleFunc, prefix string, store *storage.Storage, server *settings.Server) http.Handler {
|
2019-01-05 22:44:33 +00:00
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-05-31 23:12:36 +00:00
|
|
|
settings, err := store.Settings.Get()
|
2019-01-05 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("ERROR: couldn't get settings")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := fn(w, r, &data{
|
2020-10-01 14:45:24 +00:00
|
|
|
Runner: &runner.Runner{Enabled: server.EnableExec, Settings: settings},
|
2020-05-31 23:12:36 +00:00
|
|
|
store: store,
|
2019-01-05 22:44:33 +00:00
|
|
|
settings: settings,
|
2019-01-08 10:29:09 +00:00
|
|
|
server: server,
|
2019-01-05 22:44:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if status != 0 {
|
|
|
|
txt := http.StatusText(status)
|
|
|
|
http.Error(w, strconv.Itoa(status)+" "+txt, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status >= 400 || err != nil {
|
2019-11-17 03:44:15 +00:00
|
|
|
clientIP := realip.FromRequest(r)
|
|
|
|
log.Printf("%s: %v %s %v", r.URL.Path, status, clientIP, err)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return http.StripPrefix(prefix, handler)
|
|
|
|
}
|