volume: best effort to detect ip address

fix https://github.com/chrislusf/seaweedfs/issues/1264
This commit is contained in:
Chris Lu
2020-04-09 00:26:24 -07:00
parent f6a7e79dc3
commit 59f40e2027
2 changed files with 26 additions and 1 deletions

25
weed/util/network.go Normal file
View File

@@ -0,0 +1,25 @@
package util
import (
"net"
"github.com/chrislusf/seaweedfs/weed/glog"
)
func DetectedHostAddress() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
glog.V(0).Infof("failed to detect ip address: %v", err)
return ""
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return "localhost"
}