From f31a87fcc8c4c2fb0ce686e582753faf58280d13 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Tue, 17 Dec 2024 11:48:19 +0100 Subject: [PATCH] Case insensitive host comparison (#565) * Add case-insensitive URL block in the example * Make a case-insensitive comparation inside DstHostIs --- README.md | 5 +++-- dispatcher.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 495afc2d..c1116703 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,9 @@ proxy.OnResponse(Some RespConditions).Do(YourRespHandlerFunc()) For example: ```go -// This rejects the HTTPS request to *.reddit.com during HTTP CONNECT phase -proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("reddit.*:443$"))).HandleConnect(goproxy.AlwaysReject) +// This rejects the HTTPS request to *.reddit.com during HTTP CONNECT phase. +// Reddit URL check is case-insensitive, so the block will work also if the user types something like rEdDit.com. +proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("(?i)reddit.*:443$"))).HandleConnect(goproxy.AlwaysReject) // This will NOT reject the HTTPS request with URL ending with gif, due to the fact that proxy // only got the URL.Hostname and URL.Port during the HTTP CONNECT phase if the scheme is HTTPS, which is diff --git a/dispatcher.go b/dispatcher.go index 9b79bcc3..b050af49 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -126,8 +126,9 @@ 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 { + host = strings.ToLower(host) return func(req *http.Request, ctx *ProxyCtx) bool { - return req.URL.Host == host + return strings.ToLower(req.URL.Host) == host } }