379 lines
8.5 KiB
Go
379 lines
8.5 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/zenofile/transfer.sh/server/storage"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/urfave/cli"
|
|
"github.com/zenofile/transfer.sh/server"
|
|
)
|
|
|
|
// Version is inject at build time
|
|
var Version = "0.0.0"
|
|
var helpTemplate = `NAME:
|
|
{{.Name}} - {{.Usage}}
|
|
|
|
DESCRIPTION:
|
|
{{.Description}}
|
|
|
|
USAGE:
|
|
{{.Name}} {{if .Flags}}[flags] {{end}}command{{if .Flags}}{{end}} [arguments...]
|
|
|
|
COMMANDS:
|
|
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
|
|
{{end}}{{if .Flags}}
|
|
FLAGS:
|
|
{{range .Flags}}{{.}}
|
|
{{end}}{{end}}
|
|
VERSION:
|
|
` + Version +
|
|
`{{ "\n"}}`
|
|
|
|
var globalFlags = []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "listener",
|
|
Usage: "127.0.0.1:8080",
|
|
Value: "127.0.0.1:8080",
|
|
EnvVar: "LISTENER",
|
|
},
|
|
// redirect to https?
|
|
// hostnames
|
|
cli.StringFlag{
|
|
Name: "profile-listener",
|
|
Usage: "127.0.0.1:6060",
|
|
Value: "",
|
|
EnvVar: "PROFILE_LISTENER",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "force-https",
|
|
Usage: "",
|
|
EnvVar: "FORCE_HTTPS",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "tls-listener",
|
|
Usage: "127.0.0.1:8443",
|
|
Value: "",
|
|
EnvVar: "TLS_LISTENER",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "tls-listener-only",
|
|
Usage: "",
|
|
EnvVar: "TLS_LISTENER_ONLY",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "tls-cert-file",
|
|
Value: "",
|
|
EnvVar: "TLS_CERT_FILE",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "tls-private-key",
|
|
Value: "",
|
|
EnvVar: "TLS_PRIVATE_KEY",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "temp-path",
|
|
Usage: "path to temp files",
|
|
Value: os.TempDir(),
|
|
EnvVar: "TEMP_PATH",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "web-path",
|
|
Usage: "path to static web files",
|
|
Value: "",
|
|
EnvVar: "WEB_PATH",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "proxy-path",
|
|
Usage: "path prefix when service is run behind a proxy",
|
|
Value: "",
|
|
EnvVar: "PROXY_PATH",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "proxy-port",
|
|
Usage: "port of the proxy when the service is run behind a proxy",
|
|
Value: "",
|
|
EnvVar: "PROXY_PORT",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "email-contact",
|
|
Usage: "email address to link in Contact Us (front end)",
|
|
Value: "",
|
|
EnvVar: "EMAIL_CONTACT",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "provider",
|
|
Usage: "local",
|
|
Value: "",
|
|
EnvVar: "PROVIDER",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "rate-limit",
|
|
Usage: "requests per minute",
|
|
Value: 0,
|
|
EnvVar: "RATE_LIMIT",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "purge-days",
|
|
Usage: "number of days after uploads are purged automatically",
|
|
Value: 0,
|
|
EnvVar: "PURGE_DAYS",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "purge-interval",
|
|
Usage: "interval in hours to run the automatic purge for",
|
|
Value: 0,
|
|
EnvVar: "PURGE_INTERVAL",
|
|
},
|
|
cli.Int64Flag{
|
|
Name: "max-upload-size",
|
|
Usage: "max limit for upload, in kilobytes",
|
|
Value: 0,
|
|
EnvVar: "MAX_UPLOAD_SIZE",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "lets-encrypt-hosts",
|
|
Usage: "host1, host2",
|
|
Value: "",
|
|
EnvVar: "HOSTS",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "log",
|
|
Usage: "/var/log/transfersh.log",
|
|
Value: "",
|
|
EnvVar: "LOG",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "basedir",
|
|
Usage: "path to storage",
|
|
Value: "",
|
|
EnvVar: "BASEDIR",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "profiler",
|
|
Usage: "enable profiling",
|
|
EnvVar: "PROFILER",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "http-auth-user",
|
|
Usage: "user for http basic auth",
|
|
Value: "",
|
|
EnvVar: "HTTP_AUTH_USER",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "http-auth-pass",
|
|
Usage: "pass for http basic auth",
|
|
Value: "",
|
|
EnvVar: "HTTP_AUTH_PASS",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "ip-whitelist",
|
|
Usage: "comma separated list of ips allowed to connect to the service",
|
|
Value: "",
|
|
EnvVar: "IP_WHITELIST",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "ip-blacklist",
|
|
Usage: "comma separated list of ips not allowed to connect to the service",
|
|
Value: "",
|
|
EnvVar: "IP_BLACKLIST",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "cors-domains",
|
|
Usage: "comma separated list of domains allowed for CORS requests",
|
|
Value: "",
|
|
EnvVar: "CORS_DOMAINS",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "random-token-length",
|
|
Usage: "",
|
|
Value: 3,
|
|
EnvVar: "RANDOM_TOKEN_LENGTH",
|
|
},
|
|
cli.IntFlag{
|
|
Name: "secret-token-length",
|
|
Usage: "",
|
|
Value: 8,
|
|
EnvVar: "SECRET_TOKEN_LENGTH",
|
|
},
|
|
}
|
|
|
|
// Cmd wraps cli.app
|
|
type Cmd struct {
|
|
*cli.App
|
|
}
|
|
|
|
func versionCommand(_ *cli.Context) {
|
|
fmt.Printf("transfer.sh %s: Easy file sharing from the command line", Version)
|
|
}
|
|
|
|
// New is the factory for transfer.sh
|
|
func New() *Cmd {
|
|
logger := func() *log.Logger {
|
|
// running under systemd, so use journald timestamps
|
|
if os.Getenv("JOURNAL_STREAM") != "" {
|
|
return log.New(os.Stdout, "", 0)
|
|
}
|
|
return log.New(os.Stdout, "[transfer.sh]", log.LstdFlags)
|
|
}()
|
|
|
|
app := cli.NewApp()
|
|
app.Name = "transfer.sh"
|
|
app.Author = ""
|
|
app.Usage = "transfer.sh"
|
|
app.Description = `Easy file sharing from the command line`
|
|
app.Version = Version
|
|
app.Flags = globalFlags
|
|
app.CustomAppHelpTemplate = helpTemplate
|
|
app.Commands = []cli.Command{
|
|
{
|
|
Name: "version",
|
|
Action: versionCommand,
|
|
},
|
|
}
|
|
|
|
app.Before = func(c *cli.Context) error {
|
|
return nil
|
|
}
|
|
|
|
app.Action = func(c *cli.Context) {
|
|
var options []server.OptionFn
|
|
if v := c.String("listener"); v != "" {
|
|
options = append(options, server.Listener(v))
|
|
}
|
|
|
|
if v := c.String("cors-domains"); v != "" {
|
|
options = append(options, server.CorsDomains(v))
|
|
}
|
|
|
|
if v := c.String("tls-listener"); v == "" {
|
|
} else if c.Bool("tls-listener-only") {
|
|
options = append(options, server.TLSListener(v, true))
|
|
} else {
|
|
options = append(options, server.TLSListener(v, false))
|
|
}
|
|
|
|
if v := c.String("profile-listener"); v != "" {
|
|
options = append(options, server.ProfileListener(v))
|
|
}
|
|
|
|
if v := c.String("web-path"); v != "" {
|
|
options = append(options, server.WebPath(v))
|
|
}
|
|
|
|
if v := c.String("proxy-path"); v != "" {
|
|
options = append(options, server.ProxyPath(v))
|
|
}
|
|
|
|
if v := c.String("proxy-port"); v != "" {
|
|
options = append(options, server.ProxyPort(v))
|
|
}
|
|
|
|
if v := c.String("email-contact"); v != "" {
|
|
options = append(options, server.EmailContact(v))
|
|
}
|
|
|
|
if v := c.String("temp-path"); v != "" {
|
|
options = append(options, server.TempPath(v))
|
|
}
|
|
|
|
if v := c.String("log"); v != "" {
|
|
options = append(options, server.LogFile(logger, v))
|
|
} else {
|
|
options = append(options, server.Logger(logger))
|
|
}
|
|
|
|
if v := c.String("lets-encrypt-hosts"); v != "" {
|
|
options = append(options, server.UseLetsEncrypt(strings.Split(v, ",")))
|
|
}
|
|
|
|
if v := c.Int64("max-upload-size"); v > 0 {
|
|
options = append(options, server.MaxUploadSize(v))
|
|
}
|
|
|
|
if v := c.Int("rate-limit"); v > 0 {
|
|
options = append(options, server.RateLimit(v))
|
|
}
|
|
|
|
if v := c.Int("secret-token-length"); v > 0 {
|
|
options = append(options, server.SecretTokenLength(v))
|
|
}
|
|
|
|
v := c.Uint64("random-token-length")
|
|
options = append(options, server.RandomTokenLength(v))
|
|
|
|
purgeDays := c.Int("purge-days")
|
|
purgeInterval := c.Int("purge-interval")
|
|
if purgeDays > 0 && purgeInterval > 0 {
|
|
options = append(options, server.Purge(purgeDays, purgeInterval))
|
|
}
|
|
|
|
if cert := c.String("tls-cert-file"); cert == "" {
|
|
} else if pk := c.String("tls-private-key"); pk == "" {
|
|
} else {
|
|
options = append(options, server.TLSConfig(cert, pk))
|
|
}
|
|
|
|
if c.Bool("profiler") {
|
|
options = append(options, server.EnableProfiler())
|
|
}
|
|
|
|
if c.Bool("force-https") {
|
|
options = append(options, server.ForceHTTPS())
|
|
}
|
|
|
|
if httpAuthUser := c.String("http-auth-user"); httpAuthUser == "" {
|
|
} else if httpAuthPass := c.String("http-auth-pass"); httpAuthPass == "" {
|
|
} else {
|
|
options = append(options, server.HTTPAuthCredentials(httpAuthUser, httpAuthPass))
|
|
}
|
|
|
|
applyIPFilter := false
|
|
ipFilterOptions := server.IPFilterOptions{}
|
|
if ipWhitelist := c.String("ip-whitelist"); ipWhitelist != "" {
|
|
applyIPFilter = true
|
|
ipFilterOptions.AllowedIPs = strings.Split(ipWhitelist, ",")
|
|
ipFilterOptions.BlockByDefault = true
|
|
}
|
|
|
|
if ipBlacklist := c.String("ip-blacklist"); ipBlacklist != "" {
|
|
applyIPFilter = true
|
|
ipFilterOptions.BlockedIPs = strings.Split(ipBlacklist, ",")
|
|
}
|
|
|
|
if applyIPFilter {
|
|
options = append(options, server.FilterOptions(ipFilterOptions))
|
|
}
|
|
|
|
switch provider := c.String("provider"); provider {
|
|
case "local":
|
|
if v := c.String("basedir"); v == "" {
|
|
panic("basedir not set.")
|
|
} else if store, err := storage.NewLocalStorage(v, logger); err != nil {
|
|
panic(err)
|
|
} else {
|
|
options = append(options, server.UseStorage(store))
|
|
}
|
|
default:
|
|
panic("Provider not set or invalid.")
|
|
}
|
|
|
|
srvr, err := server.New(
|
|
options...,
|
|
)
|
|
|
|
if err != nil {
|
|
logger.Printf("Error starting server: %s\n", err.Error())
|
|
return
|
|
}
|
|
|
|
logger.Printf("Starting %s %s\n", app.Name, Version)
|
|
srvr.Run()
|
|
}
|
|
|
|
return &Cmd{
|
|
App: app,
|
|
}
|
|
}
|