2020-04-09 07:26:24 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DetectedHostAddress() string {
|
2020-12-14 08:30:20 +00:00
|
|
|
netInterfaces, err := net.Interfaces()
|
2020-04-09 07:26:24 +00:00
|
|
|
if err != nil {
|
2020-12-14 08:30:20 +00:00
|
|
|
glog.V(0).Infof("failed to detect net interfaces: %v", err)
|
2020-04-09 07:26:24 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-14 08:30:20 +00:00
|
|
|
for _, netInterface := range netInterfaces {
|
|
|
|
if (netInterface.Flags & net.FlagUp) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs, err := netInterface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("get interface addresses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range addrs {
|
|
|
|
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
2021-09-05 13:21:40 +00:00
|
|
|
if ipNet.IP.To4() != nil || ipNet.IP.To16() != nil {
|
2020-12-14 08:30:20 +00:00
|
|
|
return ipNet.IP.String()
|
|
|
|
}
|
2020-04-09 07:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "localhost"
|
|
|
|
}
|