You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/utils/get_server_ip.go

26 lines
388 B

package utils
import (
3 years ago
"errors"
"net"
)
var ServerIP = ""
3 years ago
func GetLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
3 years ago
for _, address := range addrs {
3 years ago
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
3 years ago
return "", errors.New("no ip")
}