From 408830d5e84a133d514baac538984954cd527db6 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Mon, 30 Dec 2024 18:53:48 +0100 Subject: [PATCH] Fix DstHostIs check (#608) * Strip any port if present * Add port matching when it's specified --- dispatcher.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/dispatcher.go b/dispatcher.go index 9161fa06..bc15c262 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -134,9 +134,26 @@ func UrlMatches(re *regexp.Regexp) ReqConditionFunc { // DstHostIs returns a ReqCondition testing wether the host in the request url is the given string. func DstHostIs(host string) ReqConditionFunc { + // Make sure to perform a case-insensitive host check host = strings.ToLower(host) + var port string + + // Check if the user specified a custom port that we need to match + if strings.Contains(host, ":") { + hostOnly, portOnly, err := net.SplitHostPort(host) + if err == nil { + host = hostOnly + port = portOnly + } + } + return func(req *http.Request, ctx *ProxyCtx) bool { - return strings.ToLower(req.URL.Host) == host + // Check port matching only if it was specified + if port != "" && port != req.URL.Port() { + return false + } + + return strings.ToLower(req.URL.Hostname()) == host } }