fix in force-https redirect

This commit is contained in:
Andrea Spacca
2021-12-07 14:05:23 +01:00
parent 014b95ff07
commit 168b71eefd

View File

@ -630,7 +630,6 @@ func getURL(r *http.Request, proxyPort string) *url.URL {
u.Scheme = "http" u.Scheme = "http"
} }
if u.Host == "" {
host, port, err := net.SplitHostPort(r.Host) host, port, err := net.SplitHostPort(r.Host)
if err != nil { if err != nil {
host = r.Host host = r.Host
@ -639,6 +638,7 @@ func getURL(r *http.Request, proxyPort string) *url.URL {
if len(proxyPort) != 0 { if len(proxyPort) != 0 {
port = proxyPort port = proxyPort
} }
if len(port) == 0 { if len(port) == 0 {
u.Host = host u.Host = host
} else { } else {
@ -650,7 +650,6 @@ func getURL(r *http.Request, proxyPort string) *url.URL {
u.Host = net.JoinHostPort(host, port) u.Host = net.JoinHostPort(host, port)
} }
} }
}
return u return u
} }
@ -1099,10 +1098,22 @@ func (s *Server) RedirectHandler(h http.Handler) http.HandlerFunc {
} else if strings.HasSuffix(ipAddrFromRemoteAddr(r.Host), ".onion") { } else if strings.HasSuffix(ipAddrFromRemoteAddr(r.Host), ".onion") {
// .onion addresses cannot get a valid certificate, so don't redirect // .onion addresses cannot get a valid certificate, so don't redirect
} else if r.Header.Get("X-Forwarded-Proto") == "https" { } else if r.Header.Get("X-Forwarded-Proto") == "https" {
} else if r.URL.Scheme == "https" { } else if r.TLS != nil {
} else { } else {
u := getURL(r, s.proxyPort) u := getURL(r, s.proxyPort)
u.Scheme = "https" u.Scheme = "https"
if len(s.proxyPort) == 0 && len(s.TLSListenerString) > 0 {
_, port, err := net.SplitHostPort(s.TLSListenerString)
if err != nil || port == "443" {
port = ""
}
if len(port) > 0 {
u.Host = net.JoinHostPort(u.Hostname(), port)
} else {
u.Host = u.Hostname()
}
}
http.Redirect(w, r, u.String(), http.StatusPermanentRedirect) http.Redirect(w, r, u.String(), http.StatusPermanentRedirect)
return return