2018-09-02 21:45:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-03-03 12:19:23 +00:00
|
|
|
|
|
|
|
"git.teamworkapps.com/shortcut/enhancedfileserver"
|
|
|
|
"github.com/gorilla/mux"
|
2018-09-02 21:45:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2018-11-06 15:53:06 +00:00
|
|
|
listen string
|
|
|
|
port int
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
noindex bool
|
2021-10-21 19:45:49 +00:00
|
|
|
nocache bool
|
2018-11-06 15:53:06 +00:00
|
|
|
log bool
|
2018-09-02 21:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var c config
|
|
|
|
|
|
|
|
func init() {
|
2018-11-06 15:53:06 +00:00
|
|
|
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)")
|
2021-03-03 12:19:23 +00:00
|
|
|
flag.StringVar(&c.username, "username", "", "Require this username tp access. Default is 'admin' if you specify a password.")
|
2018-11-06 15:53:06 +00:00
|
|
|
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")
|
2021-10-21 19:45:49 +00:00
|
|
|
flag.BoolVar(&c.nocache, "nocache", false, "Set http headers to prevent caching of files")
|
2018-09-02 21:45:30 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
2021-03-03 12:19:23 +00:00
|
|
|
if c.username != "" && c.password == "" {
|
|
|
|
c.username = "admin"
|
|
|
|
}
|
2018-11-06 15:53:06 +00:00
|
|
|
if c.username != "" && c.password != "" {
|
2021-03-03 12:19:23 +00:00
|
|
|
r.Use(basicAuth)
|
2018-11-06 15:53:06 +00:00
|
|
|
}
|
|
|
|
if c.log {
|
2021-03-03 12:19:23 +00:00
|
|
|
r.Use(queryLog)
|
2018-11-06 15:53:06 +00:00
|
|
|
}
|
2021-10-21 19:45:49 +00:00
|
|
|
if c.nocache {
|
|
|
|
r.Use(noCache)
|
|
|
|
}
|
2018-11-06 15:53:06 +00:00
|
|
|
if c.noindex {
|
|
|
|
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServerNoIndex(http.Dir("./"))))
|
|
|
|
} else {
|
|
|
|
r.PathPrefix("/").Handler(http.StripPrefix("/", enhancedfileserver.FileServer(http.Dir("./"))))
|
|
|
|
}
|
2018-09-02 21:45:30 +00:00
|
|
|
|
2018-11-06 15:53:06 +00:00
|
|
|
a := fmt.Sprintf("%s:%d", c.listen, c.port)
|
2018-09-02 21:45:30 +00:00
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(a, r))
|
|
|
|
}
|
|
|
|
|
2021-10-21 19:45:49 +00:00
|
|
|
func noCache(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Cache-control", "no-cache, no-store")
|
|
|
|
next.ServeHTTP(w,r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:19:23 +00:00
|
|
|
func basicAuth(next http.Handler) http.Handler {
|
2018-09-02 21:45:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-11-06 15:53:06 +00:00
|
|
|
if c.username != "" && c.password != "" {
|
2018-09-02 21:45:30 +00:00
|
|
|
user, pass, ok := r.BasicAuth()
|
2018-11-06 15:53:06 +00:00
|
|
|
if !ok || user != c.username || pass != c.password {
|
2018-09-02 21:48:12 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="mydomains"`)
|
2018-09-02 21:45:30 +00:00
|
|
|
|
|
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
2020-01-22 12:25:28 +00:00
|
|
|
return
|
2018-09-02 21:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:19:23 +00:00
|
|
|
func queryLog(next http.Handler) http.Handler {
|
2018-09-02 21:45:30 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("%s - %s", r.RemoteAddr, r.URL)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|