Skip to content

Commit

Permalink
fix(internal/host): Extract optimizes the return of the minimum index IP
Browse files Browse the repository at this point in the history
  • Loading branch information
demoManito committed Oct 10, 2024
1 parent 5b5c253 commit 8fb83b0
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions internal/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,16 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
if err != nil {
return "", err
}
minIndex := int(^uint(0) >> 1)
ips := make([]net.IP, 0)
ips := make([]net.IP, 0, 1)
for _, iface := range ifaces {
if (iface.Flags & net.FlagUp) == 0 {
continue
}
if iface.Index >= minIndex && len(ips) != 0 {
if iface.Flags&net.FlagUp == 0 {
continue
}
addrs, err := iface.Addrs()
if err != nil {
continue
}
for i, rawAddr := range addrs {
for _, rawAddr := range addrs {
var ip net.IP
switch addr := rawAddr.(type) {
case *net.IPAddr:
Expand All @@ -74,19 +70,15 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
continue
}
if isValidIP(ip.String()) {
minIndex = iface.Index
if i == 0 {
ips = make([]net.IP, 0, 1)
}
ips = append(ips, ip)
if ip.To4() != nil {
break
}
}
}
}
if len(ips) != 0 {
return net.JoinHostPort(ips[len(ips)-1].String(), port), nil
if len(ips) != 0 {
return net.JoinHostPort(ips[len(ips)-1].String(), port), nil
}
}
return "", nil
}

0 comments on commit 8fb83b0

Please sign in to comment.