default username if no password, don't export private functions

This commit is contained in:
Your Name 2021-03-03 07:19:23 -05:00
parent 38a41d4b25
commit 73a3d635dd
1 changed files with 11 additions and 7 deletions

18
main.go
View File

@ -3,10 +3,11 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/gorilla/mux"
"git.teamworkapps.com/shortcut/enhancedfileserver"
"log" "log"
"net/http" "net/http"
"git.teamworkapps.com/shortcut/enhancedfileserver"
"github.com/gorilla/mux"
) )
type config struct { type config struct {
@ -23,7 +24,7 @@ var c config
func init() { func init() {
flag.StringVar(&c.listen, "listen", "0.0.0.0", "IP address to listen.") 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.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.StringVar(&c.password, "password", "", "Require this password to access interface")
flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing") flag.BoolVar(&c.noindex, "noindex", false, "Disable directory indexing")
flag.BoolVar(&c.log, "log", false, "Enable simple request log") flag.BoolVar(&c.log, "log", false, "Enable simple request log")
@ -32,11 +33,14 @@ func init() {
func main() { func main() {
r := mux.NewRouter() r := mux.NewRouter()
if c.username != "" && c.password == "" {
c.username = "admin"
}
if c.username != "" && c.password != "" { if c.username != "" && c.password != "" {
r.Use(BasicAuth) r.Use(basicAuth)
} }
if c.log { if c.log {
r.Use(QueryLog) r.Use(queryLog)
} }
if c.noindex { if c.noindex {
@ -50,7 +54,7 @@ func main() {
log.Fatal(http.ListenAndServe(a, r)) 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.username != "" && c.password != "" { if c.username != "" && c.password != "" {
user, pass, ok := r.BasicAuth() 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) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - %s", r.RemoteAddr, r.URL) log.Printf("%s - %s", r.RemoteAddr, r.URL)
next.ServeHTTP(w, r) next.ServeHTTP(w, r)