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