Add HTTP to HTTPS redirect

This commit is contained in:
Zoltán Papp
2026-02-13 13:18:07 +01:00
parent 97a561c56b
commit 9dc47d462c
2 changed files with 113 additions and 2 deletions

View File

@@ -301,7 +301,20 @@ func (s *Server) ListenAndServe(ctx context.Context, addr string) (err error) {
}
}()
// Start the reverse proxy HTTPS server.
// Create listener with connection sniffing for HTTP redirect
// listener is closed by http.Server.ServeTLS when it exits
listener, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("failed to listen on %s: %w", addr, err)
}
// Wrap listener to detect and redirect plain HTTP requests to HTTPS
redirectListener := &httpRedirectListener{
Listener: listener,
logger: s.Logger,
}
// Start the reverse proxy HTTPS server
s.https = &http.Server{
Addr: addr,
Handler: s.meter.Middleware(accessLog.Middleware(web.AssetHandler(s.auth.Protect(s.proxy)))),
@@ -312,7 +325,7 @@ func (s *Server) ListenAndServe(ctx context.Context, addr string) (err error) {
httpsErr := make(chan error, 1)
go func() {
s.Logger.Debugf("starting reverse proxy server on %s", addr)
httpsErr <- s.https.ListenAndServeTLS("", "")
httpsErr <- s.https.ServeTLS(redirectListener, "", "")
}()
select {