From 6ecc5555b01cb6fc2685189530fedcb290393d14 Mon Sep 17 00:00:00 2001 From: Doron Segal Date: Mon, 16 Apr 2018 16:11:40 -0700 Subject: [PATCH 1/2] hide directory listing --- serve.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/serve.go b/serve.go index 5a7cca3..8df95ee 100644 --- a/serve.go +++ b/serve.go @@ -50,7 +50,9 @@ ENVIRONMENT VARIABLES URL_PREFIX The prefix to use in the URL path. If supplied, then the prefix must start with a forward-slash and NOT end with a forward-slash. If not - supplied then no prefix is used. + supplied then no prefix is used. + SHOW_LISTING + This will allow you to hide the file inside the directory USAGE FILE LAYOUT @@ -71,7 +73,8 @@ USAGE export FOLDER=/var/www/sub export HOST=my.machine export PORT=80 - export URL_PREFIX=/my/stuff + export URL_PREFIX=/my/stuff + export SHOW_LISTING=true static-file-server Retrieve with: wget http://my.machine/my/stuff/my.file @@ -113,6 +116,7 @@ func main() { tlsCert := env("TLS_CERT", "") tlsKey := env("TLS_KEY", "") urlPrefix := env("URL_PREFIX", "") + showListing := env("SHOW_LISTING", "true") // If HTTPS is to be used, verify both TLS_* environment variables are set. if 0 < len(tlsCert) || 0 < len(tlsKey) { @@ -139,7 +143,11 @@ func main() { if 0 == len(urlPrefix) { handler = basicHandler(folder) } else { - handler = prefixHandler(folder, urlPrefix) + if showListing == "false" { + handler = prefixHandlerWithoutListingFilesInDirectory(folder, urlPrefix) + } else { + handler = prefixHandler(folder, urlPrefix) + } } http.HandleFunc("/", handler) @@ -158,6 +166,24 @@ func basicHandler(folder string) http.HandlerFunc { } } +// prefixHandler removes the URL path prefix before serving files from the +// folder passed. +func prefixHandlerWithoutListingFilesInDirectory(folder, urlPrefix string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.HasPrefix(r.URL.Path, urlPrefix) { + http.NotFound(w, r) + return + } + // dont list file in directory + if strings.HasSuffix(r.URL.Path, "/") { + http.NotFound(w, r) + return + } + + http.ServeFile(w, r, folder+strings.TrimPrefix(r.URL.Path, urlPrefix)) + } +} + // prefixHandler removes the URL path prefix before serving files from the // folder passed. func prefixHandler(folder, urlPrefix string) http.HandlerFunc { @@ -166,6 +192,7 @@ func prefixHandler(folder, urlPrefix string) http.HandlerFunc { http.NotFound(w, r) return } + http.ServeFile(w, r, folder+strings.TrimPrefix(r.URL.Path, urlPrefix)) } } From f7255cdf1109b46f9eff6a8105e5784bd4ce3415 Mon Sep 17 00:00:00 2001 From: Doron Segal Date: Mon, 16 Apr 2018 16:12:57 -0700 Subject: [PATCH 2/2] adding the ability to hide files in a directory --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5aa334f..03e8642 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ URL_PREFIX= # served using HTTP. TLS_CERT= TLS_KEY= +# Hide Listing (Only available when URL_PREFIX is set) +SHOW_LISTING= ``` ### Without Docker