transfer.sh/cmd/cmd.go

497 lines
12 KiB
Go
Raw Permalink Normal View History

package cmd
import (
"fmt"
2019-03-19 11:35:30 +01:00
"log"
"os"
"strings"
"github.com/dutchcoders/transfer.sh/server"
"github.com/fatih/color"
2019-08-24 13:19:21 +02:00
"github.com/urfave/cli"
2019-03-18 20:52:38 +01:00
"google.golang.org/api/googleapi"
)
2021-05-21 15:49:48 +02:00
var Version = "1.2.4"
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{
2020-12-31 20:08:42 +01:00
Name: "listener",
Usage: "127.0.0.1:8080",
Value: "127.0.0.1:8080",
EnvVar: "LISTENER",
},
// redirect to https?
// hostnames
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "profile-listener",
Usage: "127.0.0.1:6060",
Value: "",
EnvVar: "PROFILE_LISTENER",
},
cli.BoolFlag{
2020-12-31 20:08:42 +01:00
Name: "force-https",
Usage: "",
EnvVar: "FORCE_HTTPS",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "tls-listener",
Usage: "127.0.0.1:8443",
Value: "",
EnvVar: "TLS_LISTENER",
},
2018-07-07 19:05:16 +02:00
cli.BoolFlag{
2020-12-31 20:08:42 +01:00
Name: "tls-listener-only",
Usage: "",
EnvVar: "TLS_LISTENER_ONLY",
2018-07-07 19:05:16 +02:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "tls-cert-file",
Value: "",
EnvVar: "TLS_CERT_FILE",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "tls-private-key",
Value: "",
EnvVar: "TLS_PRIVATE_KEY",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "temp-path",
Usage: "path to temp files",
Value: os.TempDir(),
EnvVar: "TEMP_PATH",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "web-path",
Usage: "path to static web files",
Value: "",
EnvVar: "WEB_PATH",
},
2019-03-30 12:35:57 +01:00
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "proxy-path",
Usage: "path prefix when service is run behind a proxy",
Value: "",
EnvVar: "PROXY_PATH",
2019-03-30 12:35:57 +01:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "proxy-port",
Usage: "port of the proxy when the service is run behind a proxy",
Value: "",
EnvVar: "PROXY_PORT",
2019-03-30 12:35:57 +01:00
},
2018-06-26 18:39:56 +02:00
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "ga-key",
Usage: "key for google analytics (front end)",
Value: "",
EnvVar: "GA_KEY",
2018-06-26 18:39:56 +02:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "uservoice-key",
Usage: "key for user voice (front end)",
Value: "",
EnvVar: "USERVOICE_KEY",
2018-06-26 18:39:56 +02:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "provider",
Usage: "s3|gdrive|local",
Value: "",
EnvVar: "PROVIDER",
},
cli.StringFlag{
Name: "s3-endpoint",
Usage: "",
Value: "",
EnvVar: "S3_ENDPOINT",
},
cli.StringFlag{
Name: "s3-region",
Usage: "",
Value: "eu-west-1",
EnvVar: "S3_REGION",
},
cli.StringFlag{
Name: "aws-access-key",
Usage: "",
Value: "",
EnvVar: "AWS_ACCESS_KEY",
},
cli.StringFlag{
Name: "aws-secret-key",
Usage: "",
Value: "",
EnvVar: "AWS_SECRET_KEY",
},
cli.StringFlag{
Name: "bucket",
Usage: "",
Value: "",
EnvVar: "BUCKET",
},
2019-03-19 11:35:30 +01:00
cli.BoolFlag{
2020-12-31 20:08:42 +01:00
Name: "s3-no-multipart",
Usage: "Disables S3 Multipart Puts",
EnvVar: "S3_NO_MULTIPART",
2019-03-19 11:35:30 +01:00
},
2019-07-21 16:01:02 +02:00
cli.BoolFlag{
2020-12-31 20:08:42 +01:00
Name: "s3-path-style",
Usage: "Forces path style URLs, required for Minio.",
EnvVar: "S3_PATH_STYLE",
2019-07-21 16:01:02 +02:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "gdrive-client-json-filepath",
Usage: "",
Value: "",
EnvVar: "GDRIVE_CLIENT_JSON_FILEPATH",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "gdrive-local-config-path",
Usage: "",
Value: "",
EnvVar: "GDRIVE_LOCAL_CONFIG_PATH",
},
2019-03-18 20:52:38 +01:00
cli.IntFlag{
2020-12-31 20:08:42 +01:00
Name: "gdrive-chunk-size",
Usage: "",
Value: googleapi.DefaultUploadChunkSize / 1024 / 1024,
EnvVar: "GDRIVE_CHUNK_SIZE",
2019-03-18 20:52:38 +01:00
},
cli.StringFlag{
2020-02-21 23:15:13 +01:00
Name: "storj-access",
Usage: "Access for the project",
2019-08-28 16:57:05 +02:00
Value: "",
2020-02-21 23:15:13 +01:00
EnvVar: "STORJ_ACCESS",
},
cli.StringFlag{
2019-08-28 16:57:05 +02:00
Name: "storj-bucket",
2019-12-01 18:19:29 +01:00
Usage: "Bucket to use within the project",
2019-08-28 16:57:05 +02:00
Value: "",
EnvVar: "STORJ_BUCKET",
},
cli.IntFlag{
Name: "rate-limit",
Usage: "requests per minute",
Value: 0,
EnvVar: "RATE_LIMIT",
},
2021-01-05 17:23:47 +01:00
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",
},
2020-12-31 20:08:42 +01:00
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{
2020-12-31 20:08:42 +01:00
Name: "log",
Usage: "/var/log/transfersh.log",
Value: "",
EnvVar: "LOG",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "basedir",
Usage: "path to storage",
Value: "",
EnvVar: "BASEDIR",
},
2017-03-22 23:13:34 +01:00
cli.StringFlag{
Name: "clamav-host",
Usage: "clamav-host",
Value: "",
EnvVar: "CLAMAV_HOST",
},
2017-03-22 23:14:01 +01:00
cli.StringFlag{
Name: "virustotal-key",
Usage: "virustotal-key",
Value: "",
EnvVar: "VIRUSTOTAL_KEY",
},
cli.BoolFlag{
2020-12-31 20:08:42 +01:00
Name: "profiler",
Usage: "enable profiling",
EnvVar: "PROFILER",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "http-auth-user",
Usage: "user for http basic auth",
Value: "",
EnvVar: "HTTP_AUTH_USER",
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "http-auth-pass",
Usage: "pass for http basic auth",
Value: "",
EnvVar: "HTTP_AUTH_PASS",
},
2019-05-11 14:42:59 +02:00
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "ip-whitelist",
Usage: "comma separated list of ips allowed to connect to the service",
Value: "",
EnvVar: "IP_WHITELIST",
2019-05-11 14:42:59 +02:00
},
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "ip-blacklist",
Usage: "comma separated list of ips not allowed to connect to the service",
Value: "",
EnvVar: "IP_BLACKLIST",
2019-05-11 14:42:59 +02:00
},
2020-04-04 14:29:33 +02:00
cli.StringFlag{
2020-12-31 20:08:42 +01:00
Name: "cors-domains",
Usage: "comma separated list of domains allowed for CORS requests",
Value: "",
EnvVar: "CORS_DOMAINS",
2019-05-11 14:42:59 +02:00
},
cli.Int64Flag{
Name: "random-token-length",
Usage: "",
Value: 6,
EnvVar: "RANDOM_TOKEN_LENGTH",
},
}
type Cmd struct {
*cli.App
}
func VersionAction(c *cli.Context) {
fmt.Println(color.YellowString(fmt.Sprintf("transfer.sh %s: Easy file sharing from the command line", Version)))
}
func New() *Cmd {
logger := 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`
2019-07-06 20:42:33 +02:00
app.Version = Version
app.Flags = globalFlags
app.CustomAppHelpTemplate = helpTemplate
app.Commands = []cli.Command{
{
Name: "version",
Action: VersionAction,
},
}
app.Before = func(c *cli.Context) error {
return nil
}
app.Action = func(c *cli.Context) {
options := []server.OptionFn{}
if v := c.String("listener"); v != "" {
options = append(options, server.Listener(v))
}
2020-04-04 14:29:33 +02:00
if v := c.String("cors-domains"); v != "" {
options = append(options, server.CorsDomains(v))
}
2018-07-07 19:05:16 +02:00
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))
}
2019-03-30 12:35:57 +01:00
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))
}
2018-06-26 18:39:56 +02:00
if v := c.String("ga-key"); v != "" {
options = append(options, server.GoogleAnalytics(v))
}
if v := c.String("uservoice-key"); v != "" {
options = append(options, server.UserVoice(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, ",")))
}
2017-03-22 23:14:01 +01:00
if v := c.String("virustotal-key"); v != "" {
options = append(options, server.VirustotalKey(v))
}
2017-03-22 23:13:34 +01:00
if v := c.String("clamav-host"); v != "" {
options = append(options, server.ClamavHost(v))
}
2020-12-31 20:08:42 +01:00
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))
}
v := c.Int64("random-token-length")
options = append(options, server.RandomTokenLength(v))
2021-01-05 17:23:47 +01:00
purgeDays := c.Int("purge-days")
purgeInterval := c.Int("purge-interval")
2021-01-05 17:24:16 +01:00
if purgeDays > 0 && purgeInterval > 0 {
2021-01-05 17:23:47 +01:00
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))
}
2019-05-11 14:42:59 +02:00
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 "s3":
if accessKey := c.String("aws-access-key"); accessKey == "" {
panic("access-key not set.")
} else if secretKey := c.String("aws-secret-key"); secretKey == "" {
panic("secret-key not set.")
} else if bucket := c.String("bucket"); bucket == "" {
panic("bucket not set.")
2021-01-05 17:23:47 +01:00
} else if storage, err := server.NewS3Storage(accessKey, secretKey, bucket, purgeDays, c.String("s3-region"), c.String("s3-endpoint"), c.Bool("s3-no-multipart"), c.Bool("s3-path-style"), logger); err != nil {
panic(err)
} else {
options = append(options, server.UseStorage(storage))
}
case "gdrive":
2019-03-18 20:52:38 +01:00
chunkSize := c.Int("gdrive-chunk-size")
if clientJsonFilepath := c.String("gdrive-client-json-filepath"); clientJsonFilepath == "" {
panic("client-json-filepath not set.")
} else if localConfigPath := c.String("gdrive-local-config-path"); localConfigPath == "" {
panic("local-config-path not set.")
} else if basedir := c.String("basedir"); basedir == "" {
panic("basedir not set.")
2019-03-18 20:52:38 +01:00
} else if storage, err := server.NewGDriveStorage(clientJsonFilepath, localConfigPath, basedir, chunkSize, logger); err != nil {
panic(err)
} else {
options = append(options, server.UseStorage(storage))
}
case "storj":
if access := c.String("storj-access"); access == "" {
2020-03-06 20:36:46 +01:00
panic("storj-access not set.")
} else if bucket := c.String("storj-bucket"); bucket == "" {
panic("storj-bucket not set.")
2021-01-05 17:23:47 +01:00
} else if storage, err := server.NewStorjStorage(access, bucket, purgeDays, logger); err != nil {
panic(err)
} else {
options = append(options, server.UseStorage(storage))
}
case "local":
if v := c.String("basedir"); v == "" {
panic("basedir not set.")
} else if storage, err := server.NewLocalStorage(v, logger); err != nil {
panic(err)
} else {
options = append(options, server.UseStorage(storage))
}
default:
panic("Provider not set or invalid.")
}
srvr, err := server.New(
options...,
)
if err != nil {
logger.Println(color.RedString("Error starting server: %s", err.Error()))
return
}
srvr.Run()
}
return &Cmd{
App: app,
}
}