Skip to content

Commit

Permalink
Fix DstHostIs check (#608)
Browse files Browse the repository at this point in the history
* Strip any port if present

* Add port matching when it's specified
  • Loading branch information
ErikPelli authored Dec 30, 2024
1 parent 020a007 commit 408830d
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 408830d

Please sign in to comment.