fix active ip for all os
This commit is contained in:
@@ -20,15 +20,24 @@ func GetLocalIP() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getActiveIP() string {
|
func getActiveIP() string {
|
||||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
addrs, err := net.InterfaceAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
addr := conn.LocalAddr()
|
for _, address := range addrs {
|
||||||
if udpAddr, ok := addr.(*net.UDPAddr); ok {
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
return udpAddr.IP.String()
|
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
ipStr := ipnet.IP.String()
|
||||||
|
|
||||||
|
if ipnet.IP.IsLinkLocalUnicast() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipStr
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user