Filter out link local and dont send the port

This commit is contained in:
Owen
2026-07-16 14:34:25 -04:00
parent e21d608bd8
commit dde44d6666
2 changed files with 15 additions and 17 deletions
+13 -15
View File
@@ -4,7 +4,6 @@ import (
"net"
"regexp"
"sort"
"strconv"
"github.com/fosrl/newt/logger"
)
@@ -66,10 +65,9 @@ var (
)
const (
scorePhysical = 0
scoreUnknown = 10
scoreVirtual = 20
scoreLinkLocal = 1000
scorePhysical = 0
scoreUnknown = 10
scoreVirtual = 20
)
// interfaceScore ranks an interface name by how likely it is to be a
@@ -88,9 +86,9 @@ func interfaceScore(name string) int {
return scoreUnknown
}
// GetLocalEndpoints returns "ip:port" strings (bracketed for IPv6, e.g.
// "[fe80::1]:51820") for every usable, non-loopback IP address bound to a
// network interface on this host. The list is ordered with interfaces most
// GetLocalEndpoints returns IP address strings for every usable,
// non-loopback IP address bound to a network interface on this host. The
// list is ordered with interfaces most
// likely to be a genuine host network (wired/Wi-Fi) first, and interfaces
// that are typically synthetic (Docker, VPN tunnels, hypervisor bridges,
// etc.) last, so callers should try the results roughly in order.
@@ -101,7 +99,7 @@ func interfaceScore(name string) int {
//
// If interfaces cannot be enumerated (e.g. insufficient OS permissions),
// an info message is logged and an empty slice is returned.
func GetLocalEndpoints(port uint16, excludeInterface string) []string {
func GetLocalEndpoints(excludeInterface string) []string {
ifaces, err := net.Interfaces()
if err != nil {
logger.Info("Unable to enumerate local network interfaces, localEndpoints will not be reported: %v", err)
@@ -141,13 +139,14 @@ func GetLocalEndpoints(port uint16, excludeInterface string) []string {
if ip == nil || ip.IsLoopback() || ip.IsUnspecified() {
continue
}
score := baseScore
if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
score += scoreLinkLocal
// Link-local addresses (169.254.0.0/16, fe80::/10) aren't
// routable off the local segment, so they're never a
// reachable endpoint for a peer.
continue
}
candidates = append(candidates, candidate{score: score, ip: ip.String()})
candidates = append(candidates, candidate{score: baseScore, ip: ip.String()})
}
}
@@ -155,10 +154,9 @@ func GetLocalEndpoints(port uint16, excludeInterface string) []string {
return candidates[i].score < candidates[j].score
})
portStr := strconv.Itoa(int(port))
endpoints := make([]string, 0, len(candidates))
for _, c := range candidates {
endpoints = append(endpoints, net.JoinHostPort(c.ip, portStr))
endpoints = append(endpoints, c.ip)
}
return endpoints
}
+2 -2
View File
@@ -103,11 +103,11 @@ func (n *Newt) clientsOnConnect() {
}
}
// localEndpoints returns "ip:port" candidates on this host that could
// localEndpoints returns candidate IP addresses on this host that could
// potentially be used to reach our WireGuard listen port, ranked with the
// most likely genuine host interfaces first.
func (n *Newt) localEndpoints() []string {
return network.GetLocalEndpoints(n.config.Port, n.config.InterfaceName)
return network.GetLocalEndpoints(n.config.InterfaceName)
}
func (n *Newt) clientsStartDirectRelay(tunnelIP string) {