From 59f40e202783dae6c83421e278608eeda7a97dbf Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 9 Apr 2020 00:26:24 -0700 Subject: [PATCH] volume: best effort to detect ip address fix https://github.com/chrislusf/seaweedfs/issues/1264 --- weed/command/volume.go | 2 +- weed/util/network.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 weed/util/network.go diff --git a/weed/command/volume.go b/weed/command/volume.go index 68a0ce223..341111ecd 100644 --- a/weed/command/volume.go +++ b/weed/command/volume.go @@ -127,7 +127,7 @@ func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, v } if *v.ip == "" { - *v.ip = "127.0.0.1" + *v.ip = util.DetectedHostAddress() } if *v.publicPort == 0 { diff --git a/weed/util/network.go b/weed/util/network.go new file mode 100644 index 000000000..7108cfea6 --- /dev/null +++ b/weed/util/network.go @@ -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" +}