fix:internal/host:选择ip未判断ip是否可用&&返回序号最小的ip(#1686) (#1687)

pull/1696/head
hshe 3 years ago committed by GitHub
parent 03f5ee015c
commit 7aa9f352f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      internal/host/host.go
  2. 10
      internal/host/host_test.go

@ -53,7 +53,17 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
if err != nil {
return "", err
}
lowest := int(^uint(0) >> 1)
var result net.IP
for _, iface := range ifaces {
if (iface.Flags & net.FlagUp) == 0 {
continue
}
if iface.Index < lowest || result == nil {
lowest = iface.Index
} else if result != nil {
continue
}
addrs, err := iface.Addrs()
if err != nil {
continue
@ -69,9 +79,12 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
continue
}
if isValidIP(ip.String()) {
return net.JoinHostPort(ip.String(), port), nil
result = ip
}
}
}
if result != nil {
return net.JoinHostPort(result.String(), port), nil
}
return "", nil
}

@ -128,3 +128,13 @@ func TestExtractHostPort(t *testing.T) {
}
t.Logf("host port: %s, %d", host, port)
}
func TestIpIsUp(t *testing.T) {
interfaces, err := net.Interfaces()
if err != nil {
t.Fail()
}
for i := range interfaces {
println(interfaces[i].Name, interfaces[i].Flags&net.FlagUp)
}
}

Loading…
Cancel
Save