修改函数 GetLocalIP
This commit is contained in:
parent
aa48a0c524
commit
72f4aa8132
64
sysutils.go
64
sysutils.go
@ -117,7 +117,7 @@ func IsNumberType(v interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FormatUnixDateTime(sec int64, loc *time.Location) string {
|
func FormatUnixDateTime(sec int64, loc *time.Location) string {
|
||||||
strTime := time.Unix(sec, 0).In(loc). Format("2006-01-02 15:04:05")
|
strTime := time.Unix(sec, 0).In(loc).Format("2006-01-02 15:04:05")
|
||||||
return strTime
|
return strTime
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,37 +154,45 @@ func UnSetBitFlag(val *int64, bitNum int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetLocalIP() string {
|
func GetLocalIP() string {
|
||||||
interfaces, err := net.Interfaces()
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return ""
|
||||||
}
|
|
||||||
ipList := []string{}
|
|
||||||
for _, iface := range interfaces {
|
|
||||||
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
addrs, err := iface.Addrs()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for _, addr := range addrs {
|
|
||||||
ipnet, ok := addr.(*net.IPNet)
|
|
||||||
if !ok || ipnet.IP.IsLoopback() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if ipnet.IP.To4() != nil {
|
|
||||||
ipList = append(ipList, ipnet.IP.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(ipList) != 1 {
|
|
||||||
panic("GetLocalIP error")
|
|
||||||
} else {
|
|
||||||
return ipList[0]
|
|
||||||
}
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||||
|
return localAddr.IP.String()
|
||||||
|
|
||||||
|
//interfaces, err := net.Interfaces()
|
||||||
|
//if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
//}
|
||||||
|
//ipList := []string{}
|
||||||
|
//for _, iface := range interfaces {
|
||||||
|
// if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
// addrs, err := iface.Addrs()
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
// for _, addr := range addrs {
|
||||||
|
// ipnet, ok := addr.(*net.IPNet)
|
||||||
|
// if !ok || ipnet.IP.IsLoopback() {
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
// if ipnet.IP.To4() != nil {
|
||||||
|
// ipList = append(ipList, ipnet.IP.String())
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//if len(ipList) != 1 {
|
||||||
|
// panic("GetLocalIP error")
|
||||||
|
//} else {
|
||||||
|
// return ipList[0]
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSlice[T any](s *[]T ,len int32, cap int32){
|
func NewSlice[T any](s *[]T, len int32, cap int32) {
|
||||||
*s = make([]T, len, cap)
|
*s = make([]T, len, cap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user