From 10adb416f8e406a938c951e5aa1f933cfca58243 Mon Sep 17 00:00:00 2001 From: Owen Date: Thu, 16 Jul 2026 14:42:26 -0400 Subject: [PATCH] Move the local endpoints to get config --- clients/clients.go | 7 ++++--- network/localendpoints.go | 12 +++++++----- newt/clients.go | 8 -------- newt/handlers.go | 19 ++++++++----------- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/clients/clients.go b/clients/clients.go index 13d629f..6661151 100644 --- a/clients/clients.go +++ b/clients/clients.go @@ -504,9 +504,10 @@ func (s *WireGuardService) LoadRemoteConfig() error { chainId := generateChainId() s.pendingConfigChainId = chainId s.stopGetConfig = s.client.SendMessageInterval("newt/wg/get-config", map[string]interface{}{ - "publicKey": s.key.PublicKey().String(), - "port": s.Port, - "chainId": chainId, + "publicKey": s.key.PublicKey().String(), + "port": s.Port, + "chainId": chainId, + "localEndpoints": network.GetLocalEndpoints(s.Port, s.interfaceName), }, 2*time.Second) logger.Debug("Requesting WireGuard configuration from remote server") diff --git a/network/localendpoints.go b/network/localendpoints.go index bf226c2..4f03b1a 100644 --- a/network/localendpoints.go +++ b/network/localendpoints.go @@ -4,6 +4,7 @@ import ( "net" "regexp" "sort" + "strconv" "github.com/fosrl/newt/logger" ) @@ -86,9 +87,9 @@ func interfaceScore(name string) int { return scoreUnknown } -// 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 +// 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 // 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. @@ -99,7 +100,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(excludeInterface string) []string { +func GetLocalEndpoints(port uint16, 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) @@ -154,9 +155,10 @@ func GetLocalEndpoints(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, c.ip) + endpoints = append(endpoints, net.JoinHostPort(c.ip, portStr)) } return endpoints } diff --git a/newt/clients.go b/newt/clients.go index af0f2bb..73b9a03 100644 --- a/newt/clients.go +++ b/newt/clients.go @@ -6,7 +6,6 @@ import ( wgnetstack "github.com/fosrl/newt/clients" "github.com/fosrl/newt/clients/permissions" "github.com/fosrl/newt/logger" - "github.com/fosrl/newt/network" "golang.zx2c4.com/wireguard/tun/netstack" ) @@ -103,13 +102,6 @@ func (n *Newt) clientsOnConnect() { } } -// 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.InterfaceName) -} - func (n *Newt) clientsStartDirectRelay(tunnelIP string) { if !n.ready { return diff --git a/newt/handlers.go b/newt/handlers.go index 1aa8a27..0a448f1 100644 --- a/newt/handlers.go +++ b/newt/handlers.go @@ -167,11 +167,10 @@ func (n *Newt) registerHandlers(ctx context.Context) { chainId := generateChainId() n.pendingRegisterChainId = chainId n.stopFunc = n.client.SendMessageInterval(topicWGRegister, map[string]interface{}{ - "publicKey": n.publicKey.String(), - "pingResults": pingResults, - "newtVersion": n.config.Version, - "chainId": chainId, - "localEndpoints": n.localEndpoints(), + "publicKey": n.publicKey.String(), + "pingResults": pingResults, + "newtVersion": n.config.Version, + "chainId": chainId, }, 2*time.Second) return @@ -268,11 +267,10 @@ func (n *Newt) registerHandlers(ctx context.Context) { chainId := generateChainId() n.pendingRegisterChainId = chainId n.stopFunc = n.client.SendMessageInterval(topicWGRegister, map[string]interface{}{ - "publicKey": n.publicKey.String(), - "pingResults": pingResults, - "newtVersion": n.config.Version, - "chainId": chainId, - "localEndpoints": n.localEndpoints(), + "publicKey": n.publicKey.String(), + "pingResults": pingResults, + "newtVersion": n.config.Version, + "chainId": chainId, }, 2*time.Second) logger.Debug("Sent exit node ping results to cloud for selection: pingResults=%+v", pingResults) @@ -1018,7 +1016,6 @@ func (n *Newt) registerHandlers(ctx context.Context) { "newtVersion": n.config.Version, "backwardsCompatible": true, "chainId": bcChainId, - "localEndpoints": n.localEndpoints(), }); err != nil { logger.Error("Failed to send registration message: %v", err) return err