From 81b2d4a7b4d298d662779057e06ce8171b9fd66f Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 26 Sep 2023 05:29:55 -0500 Subject: [PATCH] Capitalize logging prefixes, limit values to math.MaxInt32 --- cmd/errors.go | 13 ++++++------- cmd/files.go | 4 ++-- cmd/info.go | 10 +++++----- cmd/root.go | 24 ++++++++---------------- cmd/web.go | 19 ++++++++++++++----- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/cmd/errors.go b/cmd/errors.go index 23b3f84..b555463 100644 --- a/cmd/errors.go +++ b/cmd/errors.go @@ -16,10 +16,10 @@ import ( ) var ( - ErrInvalidFileCountRange = errors.New("maximum file count must be greater than or equal to minimum file count") - ErrInvalidFileCountValue = errors.New("file count value must be an integer between 1 and 2147483647 inclusive") + ErrInvalidFileCountRange = errors.New("maximum file count limit must be greater than or equal to minimum file count limit") + ErrInvalidFileCountValue = errors.New("file count limits must be positive integers no greater than 2147483647") ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive") - ErrInvalidScanCount = errors.New("scan count must be a positive integer") + ErrInvalidScanCount = errors.New("maximum scan count must be a positive integer no greater than 2147483647") ErrNoMediaFound = errors.New("no supported media formats found which match all criteria") ) @@ -39,7 +39,7 @@ func notFound(w http.ResponseWriter, r *http.Request, path string) error { startTime := time.Now() if Verbose { - fmt.Printf("%s | Error: Unavailable file %s requested by %s\n", + fmt.Printf("%s | ERROR: Unavailable file %s requested by %s\n", startTime.Format(logDate), path, r.RemoteAddr, @@ -61,17 +61,16 @@ func serverError(w http.ResponseWriter, r *http.Request, i interface{}) { startTime := time.Now() if Verbose { - fmt.Printf("%s | Error: Invalid request for %s from %s\n", + fmt.Printf("%s | ERROR: Invalid request for %s from %s\n", startTime.Format(logDate), r.URL.Path, r.RemoteAddr, ) } - w.WriteHeader(http.StatusInternalServerError) w.Header().Add("Content-Type", "text/html") - io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "500 Internal Server Error"))) + io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "An error has occurred. Please try again."))) } func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) { diff --git a/cmd/files.go b/cmd/files.go index 0d642d2..9a6e37e 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -216,7 +216,7 @@ func pathIsValid(path string, paths []string) bool { switch { case Verbose && !matchesPrefix: - fmt.Printf("%s | Error: File outside specified path(s): %s\n", + fmt.Printf("%s | ERROR: File outside specified path(s): %s\n", time.Now().Format(logDate), path, ) @@ -432,7 +432,7 @@ Poll: } if Verbose { - fmt.Printf("%s | Index: %d/%d files across %d/%d directories in %s\n", + fmt.Printf("%s | INDEX: %d/%d files across %d/%d directories in %s\n", time.Now().Format(logDate), stats.filesMatched, stats.filesMatched+stats.filesSkipped, diff --git a/cmd/info.go b/cmd/info.go index 3b7f7a0..fd1b3ef 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -178,7 +178,7 @@ func serveIndexJson(args []string, index *fileCache, errorChannel chan<- error) w.Write(response) if Verbose { - fmt.Printf("%s | Serve: JSON index page (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: JSON index page (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), @@ -199,7 +199,7 @@ func serveAvailableExtensions() httprouter.Handle { w.Write(response) if Verbose { - fmt.Printf("%s | Serve: Available extension list (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: Available extension list (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), @@ -220,7 +220,7 @@ func serveEnabledExtensions(formats *types.Types) httprouter.Handle { w.Write(response) if Verbose { - fmt.Printf("%s | Serve: Registered extension list (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: Registered extension list (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), @@ -241,7 +241,7 @@ func serveAvailableMimeTypes() httprouter.Handle { w.Write(response) if Verbose { - fmt.Printf("%s | Serve: Available MIME type list (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: Available MIME type list (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), @@ -262,7 +262,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle { w.Write(response) if Verbose { - fmt.Printf("%s | Serve: Registered MIME type list (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: Registered MIME type list (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), diff --git a/cmd/root.go b/cmd/root.go index 23b993e..44582a0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,12 +6,13 @@ package cmd import ( "log" + "math" "github.com/spf13/cobra" ) const ( - ReleaseVersion string = "0.93.2" + ReleaseVersion string = "0.94.0" ) var ( @@ -51,23 +52,14 @@ var ( Short: "Serves random media from the specified directories.", Args: cobra.MinimumNArgs(1), PreRunE: func(cmd *cobra.Command, args []string) error { - if MaxDirScans < 1 || MaxFileScans < 1 { + switch { + case MaxDirScans < 1 || MaxFileScans < 1 || MaxDirScans > math.MaxInt32 || MaxFileScans > math.MaxInt32: return ErrInvalidScanCount - } - - if MaxFileCount > 1<<31-1 || MaxFileCount < 1 { + case MaxFileCount < 1 || MinFileCount < 1 || MaxFileCount > math.MaxInt32 || MinFileCount > math.MaxInt32: return ErrInvalidFileCountValue - } - - if MinFileCount > 1<<31-1 || MinFileCount < 1 { - return ErrInvalidFileCountValue - } - - if MinFileCount > MaxFileCount { + case MinFileCount > MaxFileCount: return ErrInvalidFileCountRange - } - - if Port < 1 || Port > 65535 { + case Port < 1 || Port > 65535: return ErrInvalidPort } @@ -108,7 +100,7 @@ func init() { rootCmd.Flags().BoolVarP(&Info, "info", "i", false, "expose informational endpoints") rootCmd.Flags().IntVar(&MaxDirScans, "max-directory-scans", 32, "number of directories to scan at once") rootCmd.Flags().IntVar(&MaxFileScans, "max-file-scans", 256, "number of files to scan at once") - rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", 1<<31-1, "skip directories with file counts above this value") + rootCmd.Flags().IntVar(&MaxFileCount, "max-file-count", math.MaxInt32, "skip directories with file counts above this value") rootCmd.Flags().IntVar(&MinFileCount, "min-file-count", 1, "skip directories with file counts below this value") rootCmd.Flags().IntVar(&PageLength, "page-length", 0, "pagination length for info pages") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") diff --git a/cmd/web.go b/cmd/web.go index d915866..71e634d 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -140,7 +140,7 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error } if Verbose { - fmt.Printf("%s | Serve: %s (%s) to %s in %s%s\n", + fmt.Printf("%s | SERVE: %s (%s) to %s in %s%s\n", startTime.Format(logDate), filePath, humanReadableSize(written), @@ -336,7 +336,7 @@ func serveMedia(paths []string, regexes *regexes, cache *fileCache, formats *typ if format.Type() != "embed" { if Verbose { - fmt.Printf("%s | Serve: %s (%s) to %s in %s\n", + fmt.Printf("%s | SERVE: %s (%s) to %s in %s\n", startTime.Format(logDate), path, humanReadableSize(written), @@ -452,8 +452,17 @@ func ServePage(args []string) error { mux := httprouter.New() + listenHost := net.JoinHostPort(Bind, strconv.Itoa(Port)) + + if Verbose { + fmt.Printf("%s | SERVE: Listening on %s...\n", + time.Now().Format(logDate), + listenHost, + ) + } + srv := &http.Server{ - Addr: net.JoinHostPort(Bind, strconv.Itoa(Port)), + Addr: listenHost, Handler: mux, IdleTimeout: 10 * time.Minute, ReadTimeout: 5 * time.Second, @@ -507,10 +516,10 @@ func ServePage(args []string) error { go func() { for err := range errorChannel { - fmt.Printf("%s | Error: %v\n", time.Now().Format(logDate), err) + fmt.Printf("%s | ERROR: %v\n", time.Now().Format(logDate), err) if ExitOnError { - fmt.Printf("%s | Error: Shutting down...\n", time.Now().Format(logDate)) + fmt.Printf("%s | ERROR: Shutting down...\n", time.Now().Format(logDate)) srv.Shutdown(context.Background()) }