1
0
mirror of https://github.com/golang/go synced 2024-09-29 19:14:28 -06:00

cmd/go/internal/web: improve IP check testing on ipv6 env

The existing implementation lacks consideration of running test on a
machine which has ipv6 address but no ipv4 address. Use net.IP.IsLoopback
and net.IP.IsUnspecified instead of hardcoded addresses.

Fixes: #48575
This commit is contained in:
Jinwen Wo 2021-09-29 01:41:49 +08:00
parent be571a36c7
commit fc45adbf7b

View File

@ -17,6 +17,7 @@ import (
"errors"
"fmt"
"mime"
"net"
"net/http"
urlpkg "net/url"
"os"
@ -84,8 +85,15 @@ func get(security SecurityMode, url *urlpkg.URL) (*Response, error) {
if url.Host == "localhost.localdev" {
return nil, fmt.Errorf("no such host localhost.localdev")
}
if os.Getenv("TESTGONETWORK") == "panic" && !strings.HasPrefix(url.Host, "127.0.0.1") && !strings.HasPrefix(url.Host, "0.0.0.0") {
panic("use of network: " + url.String())
if os.Getenv("TESTGONETWORK") == "panic" {
host := url.Host
if h, _, err := net.SplitHostPort(url.Host); err == nil && h != "" {
host = h
}
addr := net.ParseIP(host)
if addr == nil || (!addr.IsLoopback() && !addr.IsUnspecified()) {
panic("use of network: " + url.String())
}
}
fetch := func(url *urlpkg.URL) (*urlpkg.URL, *http.Response, error) {