@marcel91200 / caddy Mirror · read-only / commits / 7a69ae75
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 01:45 UTC

reverseproxy: Honor `tls_except_port` for active health checks (#5591)

Francis Lavoie committed Jun 22, 2023 at 18:20 UTC 7a69ae757197660d26095045fba385c613926d77
1 file changed +25 -16
modules/caddyhttp/reverseproxy/healthchecks.go
+25 -16
@@ -306,16 +306,35 @@ func (h *Handler) doActiveHealthCheckForAllHosts() {
306 // the host's health status fails.
307 func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstream *Upstream) error {
308 // create the URL for the request that acts as a health check
309 - scheme := "http"
310 - if ht, ok := h.Transport.(TLSTransport); ok && ht.TLSEnabled() {
311 - // this is kind of a hacky way to know if we should use HTTPS, but whatever
312 - scheme = "https"
313 - }
309 u := &url.URL{
315 - Scheme: scheme,
310 + Scheme: "http",
311 Host: hostAddr,
312 }
313
314 + // split the host and port if possible, override the port if configured
315 + host, port, err := net.SplitHostPort(hostAddr)
316 + if err != nil {
317 + host = hostAddr
318 + }
319 + if h.HealthChecks.Active.Port != 0 {
320 + port := strconv.Itoa(h.HealthChecks.Active.Port)
321 + u.Host = net.JoinHostPort(host, port)
322 + }
323 +
324 + // this is kind of a hacky way to know if we should use HTTPS, but whatever
325 + if tt, ok := h.Transport.(TLSTransport); ok && tt.TLSEnabled() {
326 + u.Scheme = "https"
327 +
328 + // if the port is in the except list, flip back to HTTP
329 + if ht, ok := h.Transport.(*HTTPTransport); ok {
330 + for _, exceptPort := range ht.TLS.ExceptPorts {
331 + if exceptPort == port {
332 + u.Scheme = "http"
333 + }
334 + }
335 + }
336 + }
337 +
338 // if we have a provisioned uri, use that, otherwise use
339 // the deprecated Path option
340 if h.HealthChecks.Active.uri != nil {
@@ -325,16 +344,6 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
344 u.Path = h.HealthChecks.Active.Path
345 }
346
328 - // adjust the port, if configured to be different
329 - if h.HealthChecks.Active.Port != 0 {
330 - portStr := strconv.Itoa(h.HealthChecks.Active.Port)
331 - host, _, err := net.SplitHostPort(hostAddr)
332 - if err != nil {
333 - host = hostAddr
334 - }
335 - u.Host = net.JoinHostPort(host, portStr)
336 - }
337 -
347 // attach dialing information to this request, as well as context values that
348 // may be expected by handlers of this request
349 ctx := h.ctx.Context