util/addr: check ip addrs before return
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
9a7a65f05e
commit
077063c212
@ -79,8 +79,8 @@ func Extract(addr string) (string, error) {
|
|||||||
}
|
}
|
||||||
addrs = append(addrs, loAddrs...)
|
addrs = append(addrs, loAddrs...)
|
||||||
|
|
||||||
var ipAddr []byte
|
var ipAddr string
|
||||||
var publicIP []byte
|
var publicIP string
|
||||||
|
|
||||||
for _, rawAddr := range addrs {
|
for _, rawAddr := range addrs {
|
||||||
var ip net.IP
|
var ip net.IP
|
||||||
@ -94,22 +94,30 @@ func Extract(addr string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !isPrivateIP(ip.String()) {
|
if !isPrivateIP(ip.String()) {
|
||||||
publicIP = ip
|
publicIP = ip.String()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ipAddr = ip
|
ipAddr = ip.String()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// return private ip
|
// return private ip
|
||||||
if ipAddr != nil {
|
if len(ipAddr) > 0 {
|
||||||
return net.IP(ipAddr).String(), nil
|
a := net.ParseIP(ipAddr)
|
||||||
|
if a == nil {
|
||||||
|
return "", fmt.Errorf("ip addr %s is invalid", ipAddr)
|
||||||
|
}
|
||||||
|
return a.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// return public or virtual ip
|
// return public or virtual ip
|
||||||
if publicIP != nil {
|
if len(publicIP) > 0 {
|
||||||
return net.IP(publicIP).String(), nil
|
a := net.ParseIP(publicIP)
|
||||||
|
if a == nil {
|
||||||
|
return "", fmt.Errorf("ip addr %s is invalid", publicIP)
|
||||||
|
}
|
||||||
|
return a.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("No IP address found, and explicit IP not provided")
|
return "", fmt.Errorf("No IP address found, and explicit IP not provided")
|
||||||
|
Loading…
Reference in New Issue
Block a user