mirror of
https://github.com/fosrl/newt.git
synced 2026-09-27 16:29:06 +02:00
Move the local endpoints to get config
This commit is contained in:
+4
-3
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+8
-11
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user