Skip to content

Commit bbec5f6

Browse files
Fix IPv4 address detection
1 parent d2666a3 commit bbec5f6

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

util/util.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func GetInterfaceAddress(ifaceString string) (string, error) {
116116

117117
// FindIP returns the IP address of the passed interface, and an error
118118
func FindIP(iface net.Interface) (string, error) {
119+
var ip string
119120
addrs, err := iface.Addrs()
120121
if err != nil {
121122
return "", err
@@ -126,12 +127,20 @@ func FindIP(iface net.Interface) (string, error) {
126127
continue
127128
}
128129
if ipnet.IP.To4() != nil {
129-
return ipnet.IP.String(), nil
130+
ip = ipnet.IP.String()
131+
continue
132+
}
133+
// Use IPv6 only if an IPv4 hasn't been found yet.
134+
// This is eventually overwritten with an IPv4, if found (see above)
135+
if ip != "" {
136+
ip = "[" + ipnet.IP.String() + "]"
130137
}
131-
return "[" + ipnet.IP.String() + "]", nil
132138
}
133139
}
134-
return "", errors.New("Unable to find an IP for this interface")
140+
if ip == "" {
141+
return "", errors.New("Unable to find an IP for this interface")
142+
}
143+
return ip, nil
135144
}
136145

137146
func filterInterfaces(ifaces []net.Interface) []net.Interface {

0 commit comments

Comments
 (0)