Web UI over plain HTTP redirects to HTTPS when admin_http forbids HTTP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
15537b90f0
commit
28a0c2952f
3 files changed
+63
-1
No files matched your search
+31
-1
@@ -84,6 +84,36 @@ func (a *App) adminOverHTTP(r *http.Request) error {
|
||||
return fmt.Errorf("the admin API is HTTPS-only here: https://%s (trust the CA from /ca.pem)", net.JoinHostPort(strings.Trim(h, "[]"), port))
|
||||
}
|
||||
|
||||
// httpsURL is the region's HTTPS address for the host the client used.
|
||||
func (a *App) httpsURL(r *http.Request) string {
|
||||
_, port, _ := net.SplitHostPort(a.cfg.TLSListen)
|
||||
h := r.Host
|
||||
if hh, _, err := net.SplitHostPort(h); err == nil {
|
||||
h = hh
|
||||
}
|
||||
h = strings.Trim(h, "[]")
|
||||
if port == "" || port == "443" {
|
||||
if strings.Contains(h, ":") {
|
||||
return "https://[" + h + "]"
|
||||
}
|
||||
return "https://" + h
|
||||
}
|
||||
return "https://" + net.JoinHostPort(h, port)
|
||||
}
|
||||
|
||||
// uiOverHTTPS sends browsers that reach the web UI over plain HTTP to HTTPS when
|
||||
// the admin API is not allowed over HTTP anyway (the page could not work).
|
||||
// Only the UI is redirected: nodes keep using HTTP for images and the agent.
|
||||
func (a *App) uiOverHTTPS(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.TLS == nil && a.cfg.AdminHTTP != "" && a.cfg.AdminHTTP != "allow" && a.cfg.TLSListen != "" && a.adminOverHTTP(r) != nil {
|
||||
http.Redirect(w, r, a.httpsURL(r)+r.URL.RequestURI(), http.StatusPermanentRedirect)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) adminAuth(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := a.adminOverHTTP(r); err != nil {
|
||||
@@ -236,7 +266,7 @@ func (a *App) routes() http.Handler {
|
||||
mux.Handle("GET /os/", http.StripPrefix("/os/", http.FileServer(http.Dir(a.cfg.OSDir))))
|
||||
// embedded web UI
|
||||
sub, _ := fs.Sub(webFS, "web")
|
||||
mux.Handle("GET /", http.FileServer(http.FS(sub)))
|
||||
mux.Handle("GET /", a.uiOverHTTPS(http.FileServer(http.FS(sub))))
|
||||
return logging(mux)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user