mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
26 lines
388 B
Go
26 lines
388 B
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
)
|
|
|
|
var ServerIP = ""
|
|
|
|
func GetLocalIP() (string, error) {
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for _, address := range addrs {
|
|
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
if ipnet.IP.To4() != nil {
|
|
return ipnet.IP.String(), nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return "", errors.New("no ip")
|
|
}
|