diff --git a/internal/host/host.go b/internal/host/host.go index ac0cf2121..078660be5 100644 --- a/internal/host/host.go +++ b/internal/host/host.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 } diff --git a/internal/host/host_test.go b/internal/host/host_test.go index 102be6515..e383ec371 100644 --- a/internal/host/host_test.go +++ b/internal/host/host_test.go @@ -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) + } +}