From c63debf0f021d89397b53710b7f9418e9d12a6c8 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Mon, 30 Dec 2024 18:33:31 +0100 Subject: [PATCH 1/2] Strip any port if present --- dispatcher.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dispatcher.go b/dispatcher.go index 9161fa06..ee522edc 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -136,7 +136,7 @@ func UrlMatches(re *regexp.Regexp) ReqConditionFunc { func DstHostIs(host string) ReqConditionFunc { host = strings.ToLower(host) return func(req *http.Request, ctx *ProxyCtx) bool { - return strings.ToLower(req.URL.Host) == host + return strings.ToLower(req.URL.Hostname()) == host } } From 38874b08f1fb8d19d02f1b8a5d07b3124804db51 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Mon, 30 Dec 2024 18:52:34 +0100 Subject: [PATCH 2/2] Add port matching when it's specified --- dispatcher.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/dispatcher.go b/dispatcher.go index ee522edc..bc15c262 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -134,8 +134,25 @@ 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 { + // Check port matching only if it was specified + if port != "" && port != req.URL.Port() { + return false + } + return strings.ToLower(req.URL.Hostname()) == host } }