NewProxierWithNoProxyCIDR: fix handling IPv6 URLs

Current logic of splitting hostname from URL does not work if URL
is for IPv6 address and does not explicitly specify port number.
Example: "https://[2001:db8::1]/".

Use standard library function to get hostname out of URL string.
This commit is contained in:
Alexander Kanevskiy 2017-10-13 20:19:22 +03:00
parent 2986b37de0
commit ad3f9a81a3
2 changed files with 25 additions and 11 deletions

View File

@ -276,17 +276,7 @@ func NewProxierWithNoProxyCIDR(delegate func(req *http.Request) (*url.URL, error
}
return func(req *http.Request) (*url.URL, error) {
host := req.URL.Host
// for some urls, the Host is already the host, not the host:port
if net.ParseIP(host) == nil {
var err error
host, _, err = net.SplitHostPort(req.URL.Host)
if err != nil {
return delegate(req)
}
}
ip := net.ParseIP(host)
ip := net.ParseIP(req.URL.Hostname())
if ip == nil {
return delegate(req)
}

View File

@ -172,6 +172,30 @@ func TestProxierWithNoProxyCIDR(t *testing.T) {
url: "https://192.168.143.1:8443/api",
expectedDelegated: false,
},
{
name: "IPv6 cidr",
noProxy: "2001:db8::/48",
url: "https://[2001:db8::1]/api",
expectedDelegated: false,
},
{
name: "IPv6+port cidr",
noProxy: "2001:db8::/48",
url: "https://[2001:db8::1]:8443/api",
expectedDelegated: false,
},
{
name: "IPv6, not matching cidr",
noProxy: "2001:db8::/48",
url: "https://[2001:db8:1::1]/api",
expectedDelegated: true,
},
{
name: "IPv6+port, not matching cidr",
noProxy: "2001:db8::/48",
url: "https://[2001:db8:1::1]:8443/api",
expectedDelegated: true,
},
}
for _, test := range testCases {