Skip to content
Snippets Groups Projects
Commit 92c80483 authored by Ian Luo's avatar Ian Luo
Browse files

direct return once private ip is detected

parent bf4734a1
No related branches found
No related tags found
No related merge requests found
...@@ -45,7 +45,7 @@ func GetLocalIP() (string, error) { ...@@ -45,7 +45,7 @@ func GetLocalIP() (string, error) {
return "", perrors.WithStack(err) return "", perrors.WithStack(err)
} }
var privateIpv4Addr, ipv4Addr net.IP var addr net.IP
for _, face := range faces { for _, face := range faces {
if face.Flags&net.FlagUp == 0 { if face.Flags&net.FlagUp == 0 {
// interface down // interface down
...@@ -67,22 +67,18 @@ func GetLocalIP() (string, error) { ...@@ -67,22 +67,18 @@ func GetLocalIP() (string, error) {
} }
if ipv4, ok := getValidIPv4(addrs); ok { if ipv4, ok := getValidIPv4(addrs); ok {
ipv4Addr = ipv4 addr = ipv4
if isPrivateIP(ipv4) { if isPrivateIP(ipv4) {
privateIpv4Addr = ipv4 return ipv4.String(), nil
} }
} }
} }
if ipv4Addr == nil { if addr == nil {
return "", perrors.Errorf("can not get local IP") return "", perrors.Errorf("can not get local IP")
} }
if privateIpv4Addr == nil { return addr.String(), nil
return ipv4Addr.String(), nil
}
return privateIpv4Addr.String(), nil
} }
func isPrivateIP(ip net.IP) bool { func isPrivateIP(ip net.IP) bool {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment