2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
nerrors "errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
"github.com/filebrowser/filebrowser/v2/auth"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/errors"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(configCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var configCmd = &cobra.Command{
|
2019-02-15 19:54:44 +08:00
|
|
|
Use: "config",
|
|
|
|
Short: "Configuration management utility",
|
|
|
|
Long: `Configuration management utility.`,
|
|
|
|
Args: cobra.NoArgs,
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 18:50:37 +08:00
|
|
|
func addConfigFlags(flags *pflag.FlagSet) {
|
2019-01-08 22:07:55 +08:00
|
|
|
addServerFlags(flags)
|
2019-01-08 18:50:37 +08:00
|
|
|
addUserFlags(flags)
|
|
|
|
flags.BoolP("signup", "s", false, "allow users to signup")
|
|
|
|
flags.String("shell", "", "shell command to which other commands should be appended")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:50:37 +08:00
|
|
|
flags.String("auth.method", string(auth.MethodJSONAuth), "authentication type")
|
|
|
|
flags.String("auth.header", "", "HTTP header for auth.method=proxy")
|
2021-09-13 21:47:06 +08:00
|
|
|
flags.String("auth.command", "", "command for auth.method=hook")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:50:37 +08:00
|
|
|
flags.String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
|
|
|
|
flags.String("recaptcha.key", "", "ReCaptcha site key")
|
|
|
|
flags.String("recaptcha.secret", "", "ReCaptcha secret")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 18:50:37 +08:00
|
|
|
flags.String("branding.name", "", "replace 'File Browser' by this name")
|
2021-09-10 23:08:15 +08:00
|
|
|
flags.String("branding.color", "", "set the theme color")
|
2019-01-08 18:50:37 +08:00
|
|
|
flags.String("branding.files", "", "path to directory with images and custom styles")
|
|
|
|
flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links")
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
//nolint:gocyclo
|
2019-05-20 03:03:26 +08:00
|
|
|
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
|
2019-01-08 22:07:55 +08:00
|
|
|
method := settings.AuthMethod(mustGetString(flags, "auth.method"))
|
2019-05-20 09:55:36 +08:00
|
|
|
|
2019-05-20 03:03:26 +08:00
|
|
|
var defaultAuther map[string]interface{}
|
2019-05-20 09:55:36 +08:00
|
|
|
if len(defaults) > 0 {
|
|
|
|
if hasAuth := defaults[0]; hasAuth != true {
|
|
|
|
for _, arg := range defaults {
|
|
|
|
switch def := arg.(type) {
|
|
|
|
case *settings.Settings:
|
2020-06-01 07:12:36 +08:00
|
|
|
method = def.AuthMethod
|
2019-05-20 09:55:36 +08:00
|
|
|
case auth.Auther:
|
|
|
|
ms, err := json.Marshal(def)
|
|
|
|
checkErr(err)
|
2020-06-01 07:12:36 +08:00
|
|
|
err = json.Unmarshal(ms, &defaultAuther)
|
|
|
|
checkErr(err)
|
2019-05-20 09:55:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-20 03:03:26 +08:00
|
|
|
}
|
2019-05-19 22:13:34 +08:00
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
|
|
|
|
var auther auth.Auther
|
|
|
|
if method == auth.MethodProxyAuth {
|
2019-01-08 22:07:55 +08:00
|
|
|
header := mustGetString(flags, "auth.header")
|
2019-05-20 03:03:26 +08:00
|
|
|
|
|
|
|
if header == "" {
|
|
|
|
header = defaultAuther["header"].(string)
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
if header == "" {
|
2019-05-21 00:20:41 +08:00
|
|
|
checkErr(nerrors.New("you must set the flag 'auth.header' for method 'proxy'"))
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
2019-05-20 03:03:26 +08:00
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
auther = &auth.ProxyAuth{Header: header}
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == auth.MethodNoAuth {
|
|
|
|
auther = &auth.NoAuth{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if method == auth.MethodJSONAuth {
|
|
|
|
jsonAuth := &auth.JSONAuth{}
|
2019-01-08 22:07:55 +08:00
|
|
|
host := mustGetString(flags, "recaptcha.host")
|
|
|
|
key := mustGetString(flags, "recaptcha.key")
|
|
|
|
secret := mustGetString(flags, "recaptcha.secret")
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-05-20 03:03:26 +08:00
|
|
|
if key == "" {
|
2019-05-21 06:57:01 +08:00
|
|
|
if kmap, ok := defaultAuther["recaptcha"].(map[string]interface{}); ok {
|
|
|
|
key = kmap["key"].(string)
|
|
|
|
}
|
2019-05-20 03:03:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if secret == "" {
|
2019-05-21 06:57:01 +08:00
|
|
|
if smap, ok := defaultAuther["recaptcha"].(map[string]interface{}); ok {
|
|
|
|
secret = smap["secret"].(string)
|
|
|
|
}
|
2019-05-19 22:31:25 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 06:57:01 +08:00
|
|
|
if key != "" && secret != "" {
|
|
|
|
jsonAuth.ReCaptcha = &auth.ReCaptcha{
|
|
|
|
Host: host,
|
|
|
|
Key: key,
|
|
|
|
Secret: secret,
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
auther = jsonAuth
|
|
|
|
}
|
|
|
|
|
2021-09-13 21:47:06 +08:00
|
|
|
if method == auth.MethodHookAuth {
|
|
|
|
command := mustGetString(flags, "auth.command")
|
|
|
|
|
|
|
|
if command == "" {
|
|
|
|
command = defaultAuther["command"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if command == "" {
|
|
|
|
checkErr(nerrors.New("you must set the flag 'auth.command' for method 'hook'"))
|
|
|
|
}
|
|
|
|
|
|
|
|
auther = &auth.HookAuth{Command: command}
|
|
|
|
}
|
|
|
|
|
2019-01-06 06:44:33 +08:00
|
|
|
if auther == nil {
|
|
|
|
panic(errors.ErrInvalidAuthMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
return method, auther
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
func printSettings(ser *settings.Server, set *settings.Settings, auther auth.Auther) {
|
2021-07-26 18:00:05 +08:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) //nolint:gomnd
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "Sign up:\t%t\n", set.Signup)
|
2019-02-15 23:12:02 +08:00
|
|
|
fmt.Fprintf(w, "Create User Dir:\t%t\n", set.CreateUserDir)
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "Auth method:\t%s\n", set.AuthMethod)
|
|
|
|
fmt.Fprintf(w, "Shell:\t%s\t\n", strings.Join(set.Shell, " "))
|
2019-01-06 06:44:33 +08:00
|
|
|
fmt.Fprintln(w, "\nBranding:")
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\tName:\t%s\n", set.Branding.Name)
|
|
|
|
fmt.Fprintf(w, "\tFiles override:\t%s\n", set.Branding.Files)
|
|
|
|
fmt.Fprintf(w, "\tDisable external links:\t%t\n", set.Branding.DisableExternal)
|
2021-09-10 23:08:15 +08:00
|
|
|
fmt.Fprintf(w, "\tColor:\t%s\n", set.Branding.Color)
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintln(w, "\nServer:")
|
|
|
|
fmt.Fprintf(w, "\tLog:\t%s\n", ser.Log)
|
|
|
|
fmt.Fprintf(w, "\tPort:\t%s\n", ser.Port)
|
|
|
|
fmt.Fprintf(w, "\tBase URL:\t%s\n", ser.BaseURL)
|
|
|
|
fmt.Fprintf(w, "\tRoot:\t%s\n", ser.Root)
|
2019-05-17 18:48:06 +08:00
|
|
|
fmt.Fprintf(w, "\tSocket:\t%s\n", ser.Socket)
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\tAddress:\t%s\n", ser.Address)
|
|
|
|
fmt.Fprintf(w, "\tTLS Cert:\t%s\n", ser.TLSCert)
|
|
|
|
fmt.Fprintf(w, "\tTLS Key:\t%s\n", ser.TLSKey)
|
2020-10-01 22:45:24 +08:00
|
|
|
fmt.Fprintf(w, "\tExec Enabled:\t%t\n", ser.EnableExec)
|
2019-01-06 06:44:33 +08:00
|
|
|
fmt.Fprintln(w, "\nDefaults:")
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\tScope:\t%s\n", set.Defaults.Scope)
|
|
|
|
fmt.Fprintf(w, "\tLocale:\t%s\n", set.Defaults.Locale)
|
|
|
|
fmt.Fprintf(w, "\tView mode:\t%s\n", set.Defaults.ViewMode)
|
2020-11-24 02:06:37 +08:00
|
|
|
fmt.Fprintf(w, "\tSingle Click:\t%t\n", set.Defaults.SingleClick)
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\tCommands:\t%s\n", strings.Join(set.Defaults.Commands, " "))
|
2019-01-06 06:44:33 +08:00
|
|
|
fmt.Fprintf(w, "\tSorting:\n")
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\t\tBy:\t%s\n", set.Defaults.Sorting.By)
|
|
|
|
fmt.Fprintf(w, "\t\tAsc:\t%t\n", set.Defaults.Sorting.Asc)
|
2019-01-06 06:44:33 +08:00
|
|
|
fmt.Fprintf(w, "\tPermissions:\n")
|
2019-01-08 22:07:55 +08:00
|
|
|
fmt.Fprintf(w, "\t\tAdmin:\t%t\n", set.Defaults.Perm.Admin)
|
|
|
|
fmt.Fprintf(w, "\t\tExecute:\t%t\n", set.Defaults.Perm.Execute)
|
|
|
|
fmt.Fprintf(w, "\t\tCreate:\t%t\n", set.Defaults.Perm.Create)
|
|
|
|
fmt.Fprintf(w, "\t\tRename:\t%t\n", set.Defaults.Perm.Rename)
|
|
|
|
fmt.Fprintf(w, "\t\tModify:\t%t\n", set.Defaults.Perm.Modify)
|
|
|
|
fmt.Fprintf(w, "\t\tDelete:\t%t\n", set.Defaults.Perm.Delete)
|
|
|
|
fmt.Fprintf(w, "\t\tShare:\t%t\n", set.Defaults.Perm.Share)
|
|
|
|
fmt.Fprintf(w, "\t\tDownload:\t%t\n", set.Defaults.Perm.Download)
|
2019-01-06 06:44:33 +08:00
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(auther, "", " ")
|
|
|
|
checkErr(err)
|
|
|
|
fmt.Printf("\nAuther configuration (raw):\n\n%s\n\n", string(b))
|
|
|
|
}
|