From 73a3d635dd33c9ca8c0ac531a20cc942fdeffe63 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Mar 2021 07:19:23 -0500 Subject: [PATCH] default username if no password, don't export private functions --- main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index a957bd5..cc37b4d 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,11 @@ package main import ( "flag" "fmt" - "github.com/gorilla/mux" - "git.teamworkapps.com/shortcut/enhancedfileserver" "log" "net/http" + + "git.teamworkapps.com/shortcut/enhancedfileserver" + "github.com/gorilla/mux" ) type config struct { @@ -23,7 +24,7 @@ var c config func init() { flag.StringVar(&c.listen, "listen", "0.0.0.0", "IP address to listen.") flag.IntVar(&c.port, "port", 8000, "Listen port for interface (ports below 1024 may require super user privileges)") - flag.StringVar(&c.username, "username", "", "Require this username tp access") + flag.StringVar(&c.username, "username", "", "Require this username tp access. Default is 'admin' if you specify a password.") flag.StringVar(&c.password, "password", "", "Require this password to access interface") flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing") flag.BoolVar(&c.log, "log", false, "Enable simple request log") @@ -32,11 +33,14 @@ func init() { func main() { r := mux.NewRouter() + if c.username != "" && c.password == "" { + c.username = "admin" + } if c.username != "" && c.password != "" { - r.Use(BasicAuth) + r.Use(basicAuth) } if c.log { - r.Use(QueryLog) + r.Use(queryLog) } if c.noindex { @@ -50,7 +54,7 @@ func main() { log.Fatal(http.ListenAndServe(a, r)) } -func BasicAuth(next http.Handler) http.Handler { +func basicAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if c.username != "" && c.password != "" { user, pass, ok := r.BasicAuth() @@ -67,7 +71,7 @@ func BasicAuth(next http.Handler) http.Handler { }) } -func QueryLog(next http.Handler) http.Handler { +func queryLog(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf("%s - %s", r.RemoteAddr, r.URL) next.ServeHTTP(w, r)