aboutsummaryrefslogtreecommitdiff
path: root/weed/util/network.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/util/network.go')
-rw-r--r--weed/util/network.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/weed/util/network.go b/weed/util/network.go
index 55a123667..687b6ec22 100644
--- a/weed/util/network.go
+++ b/weed/util/network.go
@@ -2,6 +2,8 @@ package util
import (
"net"
+ "strconv"
+ "strings"
"github.com/chrislusf/seaweedfs/weed/glog"
)
@@ -13,6 +15,18 @@ func DetectedHostAddress() string {
return ""
}
+ if v4Address := selectIpV4(netInterfaces, true); v4Address != "" {
+ return v4Address
+ }
+
+ if v6Address := selectIpV4(netInterfaces, false); v6Address != "" {
+ return v6Address
+ }
+
+ return "localhost"
+}
+
+func selectIpV4(netInterfaces []net.Interface, isIpV4 bool) string {
for _, netInterface := range netInterfaces {
if (netInterface.Flags & net.FlagUp) == 0 {
continue
@@ -24,12 +38,25 @@ func DetectedHostAddress() string {
for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
- if ipNet.IP.To4() != nil {
- return ipNet.IP.String()
+ if isIpV4 {
+ if ipNet.IP.To4() != nil {
+ return ipNet.IP.String()
+ }
+ } else {
+ if ipNet.IP.To16() != nil {
+ return ipNet.IP.String()
+ }
}
}
}
}
+ return ""
+}
- return "localhost"
+func JoinHostPort(host string, port int) string {
+ portStr := strconv.Itoa(port)
+ if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
+ return host + ":" + portStr
+ }
+ return net.JoinHostPort(host, portStr)
}