2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-01-06 13:21:31 +00:00
|
|
|
"errors"
|
2019-01-05 22:44:33 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-01-06 13:01:42 +00:00
|
|
|
"path/filepath"
|
2019-01-05 22:44:33 +00:00
|
|
|
"strconv"
|
2019-01-06 05:11:15 +00:00
|
|
|
"strings"
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/auth"
|
2019-01-06 13:20:17 +00:00
|
|
|
fbhttp "github.com/filebrowser/filebrowser/v2/http"
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
2019-01-06 13:20:17 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/spf13/cobra"
|
2019-01-06 05:11:15 +00:00
|
|
|
v "github.com/spf13/viper"
|
2019-01-05 22:44:33 +00:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
2019-01-06 05:11:15 +00:00
|
|
|
var (
|
|
|
|
cfgFile string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-01-06 13:21:31 +00:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
2019-01-06 05:11:15 +00:00
|
|
|
f := rootCmd.Flags()
|
|
|
|
pf := rootCmd.PersistentFlags()
|
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
pf.StringVarP(&cfgFile, "config", "c", "", "config file path")
|
2019-01-06 05:11:15 +00:00
|
|
|
vaddP(pf, "database", "d", "./filebrowser.db", "path to the database")
|
2019-01-06 13:20:17 +00:00
|
|
|
vaddP(f, "address", "a", "127.0.0.1", "address to listen on")
|
|
|
|
vaddP(f, "log", "l", "stdout", "log output")
|
|
|
|
vaddP(f, "port", "p", 8080, "port to listen on")
|
|
|
|
vaddP(f, "cert", "t", "", "tls certificate")
|
|
|
|
vaddP(f, "key", "k", "", "tls key")
|
|
|
|
vaddP(f, "scope", "s", ".", "scope to prepend to a user's scope when it is relative")
|
2019-01-06 13:21:31 +00:00
|
|
|
vaddP(f, "baseurl", "b", "", "base url")
|
|
|
|
vadd(f, "username", "admin", "username for the first user when using quick config")
|
2019-01-06 17:55:47 +00:00
|
|
|
vadd(f, "password", "", "hashed password for the first user when using quick config (default \"admin\")")
|
2019-01-06 05:11:15 +00:00
|
|
|
|
|
|
|
if err := v.BindPFlags(f); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-06 13:20:17 +00:00
|
|
|
|
2019-01-06 05:11:15 +00:00
|
|
|
if err := v.BindPFlags(pf); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "filebrowser",
|
|
|
|
Short: "A stylish web-based file browser",
|
|
|
|
Long: `File Browser CLI lets you create the database to use with File Browser,
|
2019-01-06 13:21:31 +00:00
|
|
|
manage your users and all the configurations without acessing the
|
2019-01-05 22:44:33 +00:00
|
|
|
web interface.
|
2019-01-06 13:21:31 +00:00
|
|
|
|
|
|
|
If you've never run File Browser, you'll need to have a database for
|
|
|
|
it. Don't worry: you don't need to setup a separate database server.
|
|
|
|
We're using Bolt DB which is a single file database and all managed
|
|
|
|
by ourselves.
|
|
|
|
|
|
|
|
For this specific command, all the flags you have available (except
|
|
|
|
"config" for the configuration file), can be given either through
|
|
|
|
environment variables or configuration files.
|
|
|
|
|
|
|
|
If you don't set "config", it will look for a configuration file called
|
|
|
|
.filebrowser.{json, toml, yaml, yml} in the following directories:
|
|
|
|
|
|
|
|
- ./
|
|
|
|
- $HOME/
|
|
|
|
- /etc/filebrowser/
|
|
|
|
|
|
|
|
The precedence of the configuration values are as follows:
|
|
|
|
|
|
|
|
- flag
|
|
|
|
- environment variable
|
|
|
|
- configuration file
|
|
|
|
- defaults
|
2019-01-06 05:11:15 +00:00
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
The environment variables are prefixed by "FB_" followed by the option
|
|
|
|
name in caps. So to set "database" via an env variable, you should
|
|
|
|
set FB_DATABASE equals to the path.
|
|
|
|
|
|
|
|
Also, if the database path doesn't exist, File Browser will enter into
|
|
|
|
the quick setup mode and a new database will be bootstraped and a new
|
|
|
|
user created with the credentials from options "username" and "password".`,
|
2019-01-07 20:24:23 +00:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
|
|
|
switch logMethod := v.GetString("log"); logMethod {
|
|
|
|
case "stdout":
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
case "stderr":
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
case "":
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
default:
|
|
|
|
log.SetOutput(&lumberjack.Logger{
|
|
|
|
Filename: logMethod,
|
|
|
|
MaxSize: 100,
|
|
|
|
MaxAge: 14,
|
|
|
|
MaxBackups: 10,
|
|
|
|
})
|
|
|
|
}
|
2019-01-06 05:11:15 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
if !d.hadDB {
|
|
|
|
quickSetup(d)
|
|
|
|
}
|
2019-01-06 05:11:15 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
port := v.GetInt("port")
|
|
|
|
address := v.GetString("address")
|
|
|
|
cert := v.GetString("cert")
|
|
|
|
key := v.GetString("key")
|
|
|
|
scope := v.GetString("scope")
|
2019-01-06 05:11:15 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
scope, err := filepath.Abs(scope)
|
2019-01-06 12:26:48 +00:00
|
|
|
checkErr(err)
|
2019-01-07 20:24:23 +00:00
|
|
|
settings, err := d.store.Settings.Get()
|
2019-01-06 12:26:48 +00:00
|
|
|
checkErr(err)
|
2019-01-07 20:24:23 +00:00
|
|
|
|
|
|
|
// Despite Base URL and Scope being "server" type of
|
|
|
|
// variables, we persist them to the database because
|
|
|
|
// they are needed during the execution and not only
|
|
|
|
// to start up the server.
|
|
|
|
settings.BaseURL = v.GetString("baseurl")
|
|
|
|
settings.Scope = scope
|
|
|
|
err = d.store.Settings.Save(settings)
|
2019-01-06 12:26:48 +00:00
|
|
|
checkErr(err)
|
2019-01-06 05:11:15 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
handler, err := fbhttp.NewHandler(d.store)
|
|
|
|
checkErr(err)
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
var listener net.Listener
|
|
|
|
|
|
|
|
if key != "" && cert != "" {
|
|
|
|
cer, err := tls.LoadX509KeyPair(cert, key)
|
|
|
|
checkErr(err)
|
|
|
|
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
|
|
|
listener, err = tls.Listen("tcp", address+":"+strconv.Itoa(port), config)
|
|
|
|
checkErr(err)
|
|
|
|
} else {
|
|
|
|
listener, err = net.Listen("tcp", address+":"+strconv.Itoa(port))
|
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Listening on", listener.Addr().String())
|
|
|
|
if err := http.Serve(listener, handler); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}, pythonConfig{noDB: true}),
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
func quickSetup(d pythonData) {
|
2019-01-05 22:44:33 +00:00
|
|
|
set := &settings.Settings{
|
|
|
|
Key: generateRandomBytes(64), // 256 bit
|
2019-01-06 13:21:31 +00:00
|
|
|
BaseURL: v.GetString("baseurl"),
|
2019-01-05 22:44:33 +00:00
|
|
|
Signup: false,
|
|
|
|
AuthMethod: auth.MethodJSONAuth,
|
|
|
|
Defaults: settings.UserDefaults{
|
2019-01-06 13:01:42 +00:00
|
|
|
Scope: ".",
|
2019-01-05 22:44:33 +00:00
|
|
|
Locale: "en",
|
|
|
|
Perm: users.Permissions{
|
|
|
|
Admin: false,
|
|
|
|
Execute: true,
|
|
|
|
Create: true,
|
|
|
|
Rename: true,
|
|
|
|
Modify: true,
|
|
|
|
Delete: true,
|
|
|
|
Share: true,
|
|
|
|
Download: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
err := d.store.Settings.Save(set)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
err = d.store.Auth.Save(&auth.JSONAuth{})
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
username := v.GetString("username")
|
|
|
|
password := v.GetString("password")
|
2019-01-06 17:55:47 +00:00
|
|
|
|
|
|
|
if password == "" {
|
|
|
|
password, err = users.HashPwd("admin")
|
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
if username == "" || password == "" {
|
|
|
|
checkErr(errors.New("username and password cannot be empty during quick setup"))
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
user := &users.User{
|
2019-01-06 13:21:31 +00:00
|
|
|
Username: username,
|
2019-01-05 22:44:33 +00:00
|
|
|
Password: password,
|
|
|
|
LockPassword: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Defaults.Apply(user)
|
|
|
|
user.Perm.Admin = true
|
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
err = d.store.Users.Save(user)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:20:17 +00:00
|
|
|
func initConfig() {
|
|
|
|
if cfgFile == "" {
|
|
|
|
home, err := homedir.Dir()
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
2019-01-06 13:20:17 +00:00
|
|
|
v.AddConfigPath(".")
|
|
|
|
v.AddConfigPath(home)
|
|
|
|
v.AddConfigPath("/etc/filebrowser/")
|
|
|
|
v.SetConfigName(".filebrowser")
|
2019-01-05 22:44:33 +00:00
|
|
|
} else {
|
2019-01-06 13:20:17 +00:00
|
|
|
v.SetConfigFile(cfgFile)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 13:20:17 +00:00
|
|
|
v.SetEnvPrefix("FB")
|
|
|
|
v.AutomaticEnv()
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2019-01-06 13:20:17 +00:00
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(v.ConfigParseError); ok {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-06 13:21:31 +00:00
|
|
|
// TODO: log.Println("No config file provided")
|
2019-01-06 13:20:17 +00:00
|
|
|
}
|
2019-01-06 13:21:31 +00:00
|
|
|
// else TODO: log.Println("Using config file:", v.ConfigFileUsed())
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|