diff --git a/Dockerfile b/Dockerfile index c488369..59b0b1e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,5 +15,5 @@ FROM scratch WORKDIR / COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ . USER appuser -ENTRYPOINT ["/goStatic"] - \ No newline at end of file +ENTRYPOINT ["/goStatic","-enable-logging","-https-promote"] + diff --git a/README.md b/README.md index 205c165..105b51f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ Usage of /goStatic: The listening port (default 8043) -set-basic-auth string Define the basic auth. Form must be user:password + -https-promote + Connections to http: are redirected to https: + -enable-logging + Writes a simple log entry for requests to the server ``` #### Fallback diff --git a/main.go b/main.go index 48c7f2c..c383c81 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ var ( defaultUsernameBasicAuth = flag.String("default-user-basic-auth", "gopher", "Define the user") sizeRandom = flag.Int("password-length", 16, "Size of the randomized password") logRequest = flag.Bool("enable-logging", false, "Enable log request") + httpsPromote = flag.Bool("https-promote", false, "All HTTP requests should be redirected to HTTPS") username string password string @@ -68,6 +69,13 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) { func handleReq(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Forwarded-Proto") == "http" { + http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently) + if *logRequest { + log.Println(301, r.Method, r.URL.Path) + } + return + } if *logRequest { log.Println(r.Method, r.URL.Path) }