mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-21 21:27:16 +02:00
parent
03f5ee015c
commit
7aa9f352f8
@ -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…
x
Reference in New Issue
Block a user