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

39 lines
631 B

package utils
import (
"Open_IM/pkg/common/config"
"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")
}
func GetRpcIP() (string, error) {
registerIP := config.Config.RpcRegisterIP
if registerIP == "" {
ip, err := GetLocalIP()
if err != nil {
return "", err
}
registerIP = ip
}
return registerIP, nil
}