From 7ee470167433ae5bd44efa5309690b4580e647d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Mon, 7 Nov 2022 14:17:12 +0100 Subject: [PATCH] IsLocalHost: support host:port Without this patch IsLocalHost does not work for URLs with port specified i.e. it works for `http://localhost` but does not work for `http://localhost:80` or `http://localhost:10000`. Fixes #487 --- dispatcher.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dispatcher.go b/dispatcher.go index 25c949c0..a79d35d3 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -102,10 +102,8 @@ var localHostIpv4 = regexp.MustCompile(`127\.0\.0\.\d+`) // IsLocalHost checks whether the destination host is explicitly local host // (buggy, there can be IPv6 addresses it doesn't catch) var IsLocalHost ReqConditionFunc = func(req *http.Request, ctx *ProxyCtx) bool { - return req.URL.Host == "::1" || - req.URL.Host == "0:0:0:0:0:0:0:1" || - localHostIpv4.MatchString(req.URL.Host) || - req.URL.Host == "localhost" + h := req.URL.Hostname() + return h == "::1" || h == "0:0:0:0:0:0:0:1" || localHostIpv4.MatchString(h) || h == "localhost" } // UrlMatches returns a ReqCondition testing whether the destination URL