File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
118118func 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
137146func filterInterfaces (ifaces []net.Interface ) []net.Interface {
You can’t perform that action at this time.
0 commit comments