Update network.go since To16 converts the IP address ip up to a 16-byte representation. If ip is not an IP address (it is the wrong length), To16 returns nil. (#5134)

This commit is contained in:
vivekkoya 2023-12-23 15:01:57 -08:00 committed by GitHub
parent 97236389e8
commit 838578b55f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,18 +15,13 @@ func DetectedHostAddress() string {
return "" return ""
} }
if v4Address := selectIpV4(netInterfaces, true); v4Address != "" { if v4Address := selectIpV4(netInterfaces); v4Address != "" {
return v4Address return v4Address
} }
if v6Address := selectIpV4(netInterfaces, false); v6Address != "" {
return v6Address
}
return "localhost" return "localhost"
} }
func selectIpV4(netInterfaces []net.Interface) string {
func selectIpV4(netInterfaces []net.Interface, isIpV4 bool) string {
for _, netInterface := range netInterfaces { for _, netInterface := range netInterfaces {
if (netInterface.Flags & net.FlagUp) == 0 { if (netInterface.Flags & net.FlagUp) == 0 {
continue continue
@ -38,15 +33,9 @@ func selectIpV4(netInterfaces []net.Interface, isIpV4 bool) string {
for _, a := range addrs { for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if isIpV4 {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
} else {
if ipNet.IP.To16() != nil { if ipNet.IP.To16() != nil {
return ipNet.IP.String() return ipNet.IP.String()
} }
}
} }
} }
} }