2016-06-25 20:57:10 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-08-20 20:48:04 +00:00
|
|
|
"regexp"
|
2016-08-20 19:34:08 +00:00
|
|
|
"strconv"
|
2016-06-25 20:57:10 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-07-01 14:46:06 +00:00
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2016-06-25 20:57:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is a configuration for browsing in a particualr path.
|
|
|
|
type Config struct {
|
2016-08-22 10:37:56 +00:00
|
|
|
*User
|
2016-06-25 20:57:10 +00:00
|
|
|
BaseURL string
|
2016-07-01 14:46:06 +00:00
|
|
|
AbsoluteURL string
|
2016-07-01 14:51:02 +00:00
|
|
|
AddrPath string
|
2016-07-05 16:46:45 +00:00
|
|
|
Token string // Anti CSRF token
|
2016-06-26 17:30:08 +00:00
|
|
|
HugoEnabled bool // Enables the Hugo plugin for File Manager
|
2016-08-22 10:37:56 +00:00
|
|
|
Users map[string]*User
|
|
|
|
CurrentUser *User
|
2016-08-20 20:48:04 +00:00
|
|
|
}
|
2016-08-20 19:34:08 +00:00
|
|
|
|
2016-08-21 17:44:22 +00:00
|
|
|
// Rule is a dissalow/allow rule
|
|
|
|
type Rule struct {
|
|
|
|
Regex bool
|
|
|
|
Allow bool
|
|
|
|
Path string
|
2016-08-22 10:37:56 +00:00
|
|
|
Regexp *regexp.Regexp
|
2016-06-25 20:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses the configuration set by the user so it can
|
|
|
|
// be used by the middleware
|
|
|
|
func Parse(c *caddy.Controller) ([]Config, error) {
|
|
|
|
var configs []Config
|
|
|
|
|
|
|
|
appendConfig := func(cfg Config) error {
|
|
|
|
for _, c := range configs {
|
|
|
|
if c.PathScope == cfg.PathScope {
|
|
|
|
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configs = append(configs, cfg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-20 19:34:08 +00:00
|
|
|
var err error
|
2016-08-22 10:37:56 +00:00
|
|
|
var cCfg *User
|
2016-08-20 21:15:29 +00:00
|
|
|
var baseURL string
|
2016-08-20 19:34:08 +00:00
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
for c.Next() {
|
2016-08-22 10:37:56 +00:00
|
|
|
var cfg = Config{User: &User{}}
|
2016-08-21 19:25:40 +00:00
|
|
|
cfg.PathScope = "."
|
|
|
|
cfg.Root = http.Dir(cfg.PathScope)
|
2016-08-21 18:44:09 +00:00
|
|
|
cfg.BaseURL = ""
|
2016-08-20 20:48:04 +00:00
|
|
|
cfg.FrontMatter = "yaml"
|
|
|
|
cfg.HugoEnabled = false
|
2016-08-22 10:37:56 +00:00
|
|
|
cfg.Users = map[string]*User{}
|
2016-08-21 18:07:33 +00:00
|
|
|
cfg.AllowCommands = true
|
|
|
|
cfg.AllowEdit = true
|
|
|
|
cfg.AllowNew = true
|
|
|
|
cfg.Commands = []string{"git", "svn", "hg"}
|
2016-08-22 10:37:56 +00:00
|
|
|
cfg.Rules = []*Rule{&Rule{
|
|
|
|
Regex: true,
|
|
|
|
Allow: false,
|
|
|
|
Regexp: regexp.MustCompile("\\/\\..+"),
|
|
|
|
}}
|
2016-08-20 21:15:29 +00:00
|
|
|
|
|
|
|
baseURL = ""
|
2016-08-22 10:37:56 +00:00
|
|
|
cCfg = cfg.User
|
2016-08-20 20:48:04 +00:00
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
2016-08-20 21:15:29 +00:00
|
|
|
case "on":
|
2016-06-25 20:57:10 +00:00
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
baseURL = c.Val()
|
2016-06-29 09:43:13 +00:00
|
|
|
case "frontmatter":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.FrontMatter = c.Val()
|
|
|
|
if cCfg.FrontMatter != "yaml" && cCfg.FrontMatter != "json" && cCfg.FrontMatter != "toml" {
|
2016-06-29 09:43:13 +00:00
|
|
|
return configs, c.Err("frontmatter type not supported")
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
case "show":
|
2016-06-25 20:57:10 +00:00
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.PathScope = c.Val()
|
|
|
|
cCfg.PathScope = strings.TrimSuffix(cCfg.PathScope, "/")
|
2016-08-21 19:20:13 +00:00
|
|
|
cCfg.Root = http.Dir(cCfg.PathScope)
|
2016-06-25 20:57:10 +00:00
|
|
|
case "styles":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 19:34:08 +00:00
|
|
|
var tplBytes []byte
|
|
|
|
tplBytes, err = ioutil.ReadFile(c.Val())
|
2016-06-25 20:57:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.StyleSheet = string(tplBytes)
|
2016-08-20 19:34:08 +00:00
|
|
|
case "allow_new":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.AllowNew, err = strconv.ParseBool(c.Val())
|
2016-08-20 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
|
|
|
case "allow_edit":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.AllowEdit, err = strconv.ParseBool(c.Val())
|
2016-08-20 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
2016-08-21 18:07:33 +00:00
|
|
|
case "allow_commands":
|
2016-08-20 19:34:08 +00:00
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg.AllowCommands, err = strconv.ParseBool(c.Val())
|
2016-08-20 19:34:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
2016-08-21 18:07:33 +00:00
|
|
|
case "allow_command":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
cCfg.Commands = append(cCfg.Commands, c.Val())
|
|
|
|
case "block_command":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
|
|
|
|
for i, val := range cCfg.Commands {
|
|
|
|
if val == c.Val() {
|
|
|
|
index = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cCfg.Commands = append(cCfg.Commands[:index], cCfg.Commands[index+1:]...)
|
|
|
|
case "allow":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
2016-08-22 10:40:21 +00:00
|
|
|
if c.Val() == "dotfiles" {
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: true,
|
|
|
|
Allow: true,
|
|
|
|
Regexp: regexp.MustCompile("\\/\\..+"),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: false,
|
|
|
|
Allow: true,
|
|
|
|
Path: c.Val(),
|
|
|
|
Regexp: nil,
|
|
|
|
})
|
|
|
|
}
|
2016-08-21 18:07:33 +00:00
|
|
|
case "allow_r":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: true,
|
|
|
|
Allow: true,
|
|
|
|
Path: "",
|
2016-08-22 10:37:56 +00:00
|
|
|
Regexp: regexp.MustCompile(c.Val()),
|
2016-08-21 18:07:33 +00:00
|
|
|
})
|
|
|
|
case "block":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
2016-08-22 10:40:21 +00:00
|
|
|
if c.Val() == "dotfiles" {
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: true,
|
|
|
|
Allow: false,
|
|
|
|
Regexp: regexp.MustCompile("\\/\\..+"),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: false,
|
|
|
|
Allow: false,
|
|
|
|
Path: c.Val(),
|
|
|
|
Regexp: nil,
|
|
|
|
})
|
|
|
|
}
|
2016-08-21 18:07:33 +00:00
|
|
|
case "block_r":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return configs, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
cCfg.Rules = append(cCfg.Rules, &Rule{
|
|
|
|
Regex: true,
|
|
|
|
Allow: false,
|
|
|
|
Path: "",
|
2016-08-22 10:37:56 +00:00
|
|
|
Regexp: regexp.MustCompile(c.Val()),
|
2016-08-21 18:07:33 +00:00
|
|
|
})
|
2016-08-20 21:15:29 +00:00
|
|
|
// NEW USER BLOCK?
|
|
|
|
default:
|
|
|
|
val := c.Val()
|
|
|
|
// Checks if it's a new user
|
|
|
|
if !strings.HasSuffix(val, ":") {
|
|
|
|
fmt.Println("Unknown option " + val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the username, sets the current user, and initializes it
|
|
|
|
val = strings.TrimSuffix(val, ":")
|
2016-08-22 10:37:56 +00:00
|
|
|
cfg.Users[val] = &User{}
|
2016-08-21 18:07:33 +00:00
|
|
|
|
|
|
|
// Initialize the new user
|
2016-08-20 21:15:29 +00:00
|
|
|
cCfg = cfg.Users[val]
|
2016-08-21 18:07:33 +00:00
|
|
|
cCfg.AllowCommands = cfg.AllowCommands
|
|
|
|
cCfg.AllowEdit = cfg.AllowEdit
|
|
|
|
cCfg.AllowNew = cfg.AllowEdit
|
|
|
|
cCfg.Commands = cfg.Commands
|
|
|
|
cCfg.FrontMatter = cfg.FrontMatter
|
|
|
|
cCfg.PathScope = cfg.PathScope
|
|
|
|
cCfg.Root = cfg.Root
|
|
|
|
cCfg.Rules = cfg.Rules
|
|
|
|
cCfg.StyleSheet = cfg.StyleSheet
|
2016-06-25 20:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-21 18:07:33 +00:00
|
|
|
// Set global base url
|
2016-08-20 21:15:29 +00:00
|
|
|
cfg.BaseURL = baseURL
|
|
|
|
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
|
|
|
|
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
|
|
|
|
cfg.BaseURL = "/" + cfg.BaseURL
|
|
|
|
|
2016-07-01 14:46:06 +00:00
|
|
|
caddyConf := httpserver.GetConfig(c)
|
2016-07-03 18:45:46 +00:00
|
|
|
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
|
2016-07-01 14:46:06 +00:00
|
|
|
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)
|
2016-07-03 18:45:46 +00:00
|
|
|
cfg.AbsoluteURL = strings.TrimSuffix(cfg.AbsoluteURL, "/")
|
2016-07-01 14:51:02 +00:00
|
|
|
cfg.AddrPath = strings.TrimSuffix(caddyConf.Addr.Path, "/")
|
2016-06-25 20:57:10 +00:00
|
|
|
if err := appendConfig(cfg); err != nil {
|
|
|
|
return configs, err
|
|
|
|
}
|
2016-08-21 18:44:09 +00:00
|
|
|
|
2016-06-25 20:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return configs, nil
|
|
|
|
}
|