Don't do HTTPS redirects for requests associated with Let's Encrypt certificate renewals.

This commit is contained in:
Solderpunk 2019-04-22 09:22:54 -04:00
parent 309dd05f4d
commit b8276dd23f
2 changed files with 12 additions and 4 deletions

View File

@ -116,6 +116,14 @@ func GetHandler(config Config) http.HandlerFunc {
}
}
func redirectTLS(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
}
func GetRedirectTLSHandler(config Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Don't redirect Let's Encrypt requests
if strings.HasPrefix(r.URL.Path, "/.well-known/acme-challenge/") {
path := filepath.Join(config.DocBase, r.URL.Path)
http.ServeFile(w, r, path)
// But redirect everything else
} else {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusMovedPermanently)
}
} }

View File

@ -34,7 +34,7 @@ func main() {
errs := make(chan error, 2)
// Start the HTTP server, which redirect all incoming connections to HTTPS
http.HandleFunc("/", LoggingWrapper(logfile, GetHandler(config)))
http_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: http.HandlerFunc(redirectTLS)}
http_server := &http.Server{Addr: ":"+strconv.Itoa(config.HttpPort), Handler: http.HandlerFunc(GetRedirectTLSHandler(config))}
go func() {
errs <- http_server.ListenAndServe()
}()