Added -https-promote switch

This commit is contained in:
Dj Walker-Morgan 2020-09-08 14:52:18 +01:00
parent 8c42b65687
commit 22acfdcc3c
3 changed files with 14 additions and 2 deletions

View file

@ -15,5 +15,5 @@ FROM scratch
WORKDIR /
COPY --from=builder /go/src/github.com/PierreZ/goStatic/bin/ .
USER appuser
ENTRYPOINT ["/goStatic"]
ENTRYPOINT ["/goStatic","-enable-logging","-https-promote"]

View file

@ -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

View file

@ -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)
}