2022-09-08 15:12:06 +00:00
|
|
|
/*
|
2023-01-18 17:19:29 +00:00
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
2022-09-08 15:12:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-01-06 15:52:38 +00:00
|
|
|
"fmt"
|
2023-09-26 10:29:55 +00:00
|
|
|
"math"
|
2024-01-06 15:52:38 +00:00
|
|
|
"os"
|
2023-12-31 21:50:13 +00:00
|
|
|
"regexp"
|
2024-01-06 02:39:05 +00:00
|
|
|
"slices"
|
2024-01-06 15:52:38 +00:00
|
|
|
"time"
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-06-03 20:51:08 +00:00
|
|
|
const (
|
2023-12-31 22:04:54 +00:00
|
|
|
AllowedCharacters string = `^[A-z0-9.\-_]+$`
|
2024-01-08 04:43:51 +00:00
|
|
|
ReleaseVersion string = "4.0.4"
|
2023-06-03 20:51:08 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
var (
|
2024-01-06 03:42:22 +00:00
|
|
|
AdminPrefix string
|
|
|
|
All bool
|
|
|
|
AllowEmpty bool
|
|
|
|
Audio bool
|
|
|
|
BinaryPrefix bool
|
|
|
|
Bind string
|
|
|
|
CaseSensitive bool
|
|
|
|
Code bool
|
|
|
|
CodeTheme string
|
|
|
|
Compression string
|
|
|
|
CompressionFast bool
|
|
|
|
Concurrency int
|
2024-01-06 15:41:30 +00:00
|
|
|
Debug bool
|
2024-01-06 03:42:22 +00:00
|
|
|
DisableButtons bool
|
|
|
|
ExitOnError bool
|
|
|
|
Fallback bool
|
|
|
|
Filtering bool
|
|
|
|
Flash bool
|
|
|
|
Fun bool
|
|
|
|
Handlers bool
|
|
|
|
Ignore bool
|
|
|
|
IgnoreFile string
|
|
|
|
Images bool
|
|
|
|
Index bool
|
|
|
|
IndexFile string
|
|
|
|
Info bool
|
|
|
|
MaxFileCount int
|
|
|
|
MinFileCount int
|
|
|
|
PageLength int
|
|
|
|
Port int
|
|
|
|
Prefix string
|
|
|
|
Profile bool
|
|
|
|
Recursive bool
|
|
|
|
Redact bool
|
|
|
|
Refresh bool
|
|
|
|
Russian bool
|
|
|
|
Sorting bool
|
|
|
|
Text bool
|
|
|
|
Verbose bool
|
|
|
|
Version bool
|
|
|
|
Videos bool
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2024-01-06 02:39:05 +00:00
|
|
|
CompressionFormats = []string{
|
|
|
|
"flate",
|
|
|
|
"gzip",
|
2024-01-06 03:30:08 +00:00
|
|
|
"lz4",
|
2024-01-06 02:39:05 +00:00
|
|
|
"lzw",
|
|
|
|
"none",
|
2024-01-06 03:30:08 +00:00
|
|
|
"snappy",
|
2024-01-06 02:39:05 +00:00
|
|
|
"zlib",
|
|
|
|
"zstd",
|
|
|
|
}
|
|
|
|
|
2023-12-28 02:31:18 +00:00
|
|
|
RequiredArgs = []string{
|
|
|
|
"all",
|
|
|
|
"audio",
|
|
|
|
"code",
|
|
|
|
"fallback",
|
|
|
|
"flash",
|
|
|
|
"images",
|
|
|
|
"text",
|
|
|
|
"video",
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:17:13 +00:00
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "roulette <path> [path]...",
|
2023-09-11 02:05:35 +00:00
|
|
|
Short: "Serves random media from the specified directories.",
|
2023-01-27 19:17:13 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2023-09-26 08:34:50 +00:00
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-26 10:29:55 +00:00
|
|
|
switch {
|
2023-10-03 20:00:32 +00:00
|
|
|
case MaxFileCount < 0 || MinFileCount < 0 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32:
|
2023-09-26 09:03:52 +00:00
|
|
|
return ErrInvalidFileCountValue
|
2023-09-26 10:29:55 +00:00
|
|
|
case MinFileCount > MaxFileCount:
|
2023-09-26 09:03:52 +00:00
|
|
|
return ErrInvalidFileCountRange
|
2023-09-26 10:29:55 +00:00
|
|
|
case Port < 1 || Port > 65535:
|
2023-09-26 08:49:20 +00:00
|
|
|
return ErrInvalidPort
|
2024-01-01 17:51:28 +00:00
|
|
|
case Concurrency < 1:
|
2023-12-17 12:35:03 +00:00
|
|
|
return ErrInvalidConcurrency
|
2023-12-31 22:04:54 +00:00
|
|
|
case Ignore && !regexp.MustCompile(AllowedCharacters).MatchString(IgnoreFile):
|
2023-12-31 21:50:13 +00:00
|
|
|
return ErrInvalidIgnoreFile
|
2024-01-06 02:39:05 +00:00
|
|
|
case !slices.Contains(CompressionFormats, Compression):
|
|
|
|
return ErrInvalidCompression
|
2023-12-31 22:04:54 +00:00
|
|
|
case AdminPrefix != "" && !regexp.MustCompile(AllowedCharacters).MatchString(AdminPrefix):
|
2023-12-17 12:35:03 +00:00
|
|
|
return ErrInvalidAdminPrefix
|
|
|
|
case AdminPrefix != "":
|
|
|
|
AdminPrefix = "/" + AdminPrefix
|
2023-09-26 08:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2023-01-27 19:20:07 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-01-27 19:17:13 +00:00
|
|
|
err := ServePage(args)
|
|
|
|
if err != nil {
|
2023-01-27 19:20:07 +00:00
|
|
|
return err
|
2023-01-27 19:17:13 +00:00
|
|
|
}
|
2023-01-27 19:20:07 +00:00
|
|
|
|
|
|
|
return nil
|
2023-01-27 19:17:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2022-09-08 15:12:06 +00:00
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
2024-01-06 15:52:38 +00:00
|
|
|
fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err)
|
|
|
|
|
|
|
|
os.Exit(1)
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-12-17 12:35:03 +00:00
|
|
|
rootCmd.Flags().StringVar(&AdminPrefix, "admin-prefix", "", "string to prepend to administrative paths")
|
2023-09-13 04:35:17 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&All, "all", "a", false, "enable all supported file types")
|
2024-01-06 00:50:45 +00:00
|
|
|
rootCmd.Flags().BoolVar(&AllowEmpty, "allow-empty", false, "allow specifying paths containing no supported files")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Audio, "audio", false, "enable support for audio files")
|
2024-01-03 18:37:32 +00:00
|
|
|
rootCmd.Flags().BoolVar(&BinaryPrefix, "binary-prefix", false, "use IEC binary prefixes instead of SI decimal prefixes")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().StringVarP(&Bind, "bind", "b", "0.0.0.0", "address to bind to")
|
2023-09-25 16:15:49 +00:00
|
|
|
rootCmd.Flags().BoolVar(&CaseSensitive, "case-sensitive", false, "use case-sensitive matching for filters")
|
2023-09-14 22:37:22 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Code, "code", false, "enable support for source code files")
|
2023-09-15 06:31:23 +00:00
|
|
|
rootCmd.Flags().StringVar(&CodeTheme, "code-theme", "solarized-dark256", "theme for source code syntax highlighting")
|
2024-01-06 15:52:38 +00:00
|
|
|
rootCmd.Flags().StringVar(&Compression, "compression", "zstd", "compression format to use for index (flate, gzip, lz4, lzw, none, snappy, zlib, zstd)")
|
2024-01-06 03:42:22 +00:00
|
|
|
rootCmd.Flags().BoolVar(&CompressionFast, "compression-fast", false, "use fastest compression level (default is best)")
|
2024-01-06 15:47:07 +00:00
|
|
|
rootCmd.Flags().IntVar(&Concurrency, "concurrency", math.MaxInt32, "maximum concurrency for scan threads")
|
2024-01-06 15:41:30 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Debug, "debug", "d", false, "display even more verbose logs")
|
2023-10-20 22:18:35 +00:00
|
|
|
rootCmd.Flags().BoolVar(&DisableButtons, "disable-buttons", false, "disable first/prev/next/last buttons")
|
2024-01-06 02:39:05 +00:00
|
|
|
rootCmd.Flags().BoolVar(&ExitOnError, "exit-on-error", false, "shut down webserver on error, instead of just printing error")
|
2023-10-03 14:44:50 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Fallback, "fallback", false, "serve files as application/octet-stream if no matching format is registered")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Filtering, "filter", "f", false, "enable filtering")
|
2023-09-12 19:00:15 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Flash, "flash", false, "enable support for shockwave flash files (via ruffle.rs)")
|
2023-10-18 01:38:45 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Fun, "fun", false, "add a bit of excitement to your day")
|
2023-09-14 04:24:29 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Handlers, "handlers", false, "display registered handlers (for debugging)")
|
2023-12-31 21:50:13 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Ignore, "ignore", false, "skip all directories containing a specified filename")
|
|
|
|
rootCmd.Flags().StringVar(&IgnoreFile, "ignore-file", ".roulette-ignore", "filename used to indicate directory to be skipped")
|
2023-09-12 18:51:00 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Images, "images", false, "enable support for image files")
|
2023-09-29 13:31:35 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Index, "index", false, "generate index of supported file paths at startup")
|
2023-09-28 15:09:45 +00:00
|
|
|
rootCmd.Flags().StringVar(&IndexFile, "index-file", "", "path to optional persistent index file")
|
2023-09-13 04:35:17 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints")
|
2023-09-26 10:29:55 +00:00
|
|
|
rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", math.MaxInt32, "skip directories with file counts above this value")
|
2023-10-03 20:00:32 +00:00
|
|
|
rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 0, "skip directories with file counts below this value")
|
2023-09-26 08:49:20 +00:00
|
|
|
rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages")
|
|
|
|
rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on")
|
2023-09-14 05:01:50 +00:00
|
|
|
rootCmd.Flags().StringVar(&Prefix, "prefix", "/", "root path for http handlers (for reverse proxying)")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Profile, "profile", false, "register net/http/pprof handlers")
|
|
|
|
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
|
2024-01-06 01:00:15 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Redact, "redact", false, "redact admin prefix in log output")
|
2023-09-14 05:07:54 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Refresh, "refresh", false, "enable automatic page refresh via query parameter")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().BoolVar(&Russian, "russian", false, "remove selected images after serving")
|
|
|
|
rootCmd.Flags().BoolVarP(&Sorting, "sort", "s", false, "enable sorting")
|
|
|
|
rootCmd.Flags().BoolVar(&Text, "text", false, "enable support for text files")
|
2023-09-13 04:35:17 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Verbose, "verbose", "v", false, "log accessed files and other information to stdout")
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Flags().BoolVarP(&Version, "version", "V", false, "display version and exit")
|
|
|
|
rootCmd.Flags().BoolVar(&Videos, "video", false, "enable support for video files")
|
2023-09-11 02:02:03 +00:00
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
|
|
|
|
2023-12-17 15:07:41 +00:00
|
|
|
rootCmd.Flags().SetInterspersed(true)
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
rootCmd.MarkFlagsMutuallyExclusive("debug", "exit-on-error")
|
|
|
|
|
2023-12-28 02:31:18 +00:00
|
|
|
rootCmd.MarkFlagsOneRequired(RequiredArgs...)
|
2023-11-05 14:23:49 +00:00
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
rootCmd.SetHelpCommand(&cobra.Command{
|
|
|
|
Hidden: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
rootCmd.SetVersionTemplate("roulette v{{.Version}}\n")
|
2023-12-17 15:07:41 +00:00
|
|
|
|
|
|
|
rootCmd.SilenceErrors = true
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
rootCmd.Version = ReleaseVersion
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|