mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-19 21:18:07 +02:00
feat(internal/host): prefer ipv4 than ipv6 (#2342)
* feat:prefer ipv4 than ipv6 * feat:prefer ipv4 than ipv6 * feat:prefer ipv4 than ipv6 * feat:prefer ipv4 than ipv6
This commit is contained in:
parent
0f0c75e20b
commit
e176ddfcdd
@ -51,19 +51,19 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
minIndex := int(^uint(0) >> 1)
|
minIndex := int(^uint(0) >> 1)
|
||||||
var result net.IP
|
ips := make([]net.IP, 0)
|
||||||
for _, iface := range ifaces {
|
for _, iface := range ifaces {
|
||||||
if (iface.Flags & net.FlagUp) == 0 {
|
if (iface.Flags & net.FlagUp) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if iface.Index >= minIndex && result != nil {
|
if iface.Index >= minIndex && len(ips) != 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
addrs, err := iface.Addrs()
|
addrs, err := iface.Addrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, rawAddr := range addrs {
|
for i, rawAddr := range addrs {
|
||||||
var ip net.IP
|
var ip net.IP
|
||||||
switch addr := rawAddr.(type) {
|
switch addr := rawAddr.(type) {
|
||||||
case *net.IPAddr:
|
case *net.IPAddr:
|
||||||
@ -75,13 +75,18 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
|
|||||||
}
|
}
|
||||||
if isValidIP(ip.String()) {
|
if isValidIP(ip.String()) {
|
||||||
minIndex = iface.Index
|
minIndex = iface.Index
|
||||||
result = ip
|
if i == 0 {
|
||||||
|
ips = make([]net.IP, 0, 1)
|
||||||
|
}
|
||||||
|
ips = append(ips, ip)
|
||||||
|
if ip.To4() != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if result != nil {
|
}
|
||||||
return net.JoinHostPort(result.String(), port), nil
|
if len(ips) != 0 {
|
||||||
|
return net.JoinHostPort(ips[len(ips)-1].String(), port), nil
|
||||||
}
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user