41-bugfix-fluctuating-ips

IPs of clients fluctuating because there isn't a strict order on
clients. Client's need to be processed before the peers.
This commit is contained in:
Tim Beatham 2023-12-04 17:32:50 +00:00
parent 78d748770c
commit c40f7510b8

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"slices" "slices"
"strings"
"time" "time"
"github.com/tim-beatham/wgmesh/pkg/conf" "github.com/tim-beatham/wgmesh/pkg/conf"
@ -52,7 +53,7 @@ func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Dev
allowedips := make([]net.IPNet, 1) allowedips := make([]net.IPNet, 1)
allowedips[0] = *node.GetWgHost() allowedips[0] = *node.GetWgHost()
clients, ok := peerToClients[node.GetWgHost().String()] clients, ok := peerToClients[pubKey.String()]
if ok { if ok {
allowedips = append(allowedips, clients...) allowedips = append(allowedips, clients...)
@ -162,12 +163,21 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
} }
nodes := lib.MapValues(snap.GetNodes()) nodes := lib.MapValues(snap.GetNodes())
slices.SortFunc(nodes, func(a, b MeshNode) int {
return strings.Compare(string(a.GetType()), string(b.GetType()))
})
peerConfigs := make([]wgtypes.PeerConfig, len(nodes)) peerConfigs := make([]wgtypes.PeerConfig, len(nodes))
peers := lib.Filter(nodes, func(mn MeshNode) bool { peers := lib.Filter(nodes, func(mn MeshNode) bool {
return mn.GetType() == conf.PEER_ROLE return mn.GetType() == conf.PEER_ROLE
}) })
clients := lib.Filter(nodes, func(mn MeshNode) bool {
return mn.GetType() == conf.CLIENT_ROLE
})
var count int = 0 var count int = 0
self, err := m.meshManager.GetSelf(mesh.GetMeshId()) self, err := m.meshManager.GetSelf(mesh.GetMeshId())
@ -182,12 +192,12 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
dev, _ := mesh.GetDevice() dev, _ := mesh.GetDevice()
for _, n := range nodes { for _, n := range clients {
if NodeEquals(n, self) { if NodeEquals(n, self) {
continue continue
} }
if n.GetType() == conf.CLIENT_ROLE && len(peers) > 0 && self.GetType() == conf.CLIENT_ROLE { if len(peers) > 0 && self.GetType() == conf.CLIENT_ROLE {
hashFunc := func(mn MeshNode) int { hashFunc := func(mn MeshNode) int {
pubKey, _ := mn.GetPublicKey() pubKey, _ := mn.GetPublicKey()
return lib.HashString(pubKey.String()) return lib.HashString(pubKey.String())
@ -195,17 +205,20 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
peer := lib.ConsistentHash(peers, n, hashFunc, hashFunc) peer := lib.ConsistentHash(peers, n, hashFunc, hashFunc)
clients, ok := peerToClients[peer.GetWgHost().String()] pubKey, _ := peer.GetPublicKey()
clients, ok := peerToClients[pubKey.String()]
if !ok { if !ok {
clients = make([]net.IPNet, 0) clients = make([]net.IPNet, 0)
peerToClients[peer.GetWgHost().String()] = clients peerToClients[pubKey.String()] = clients
} }
peerToClients[peer.GetWgHost().String()] = append(clients, *n.GetWgHost()) peerToClients[pubKey.String()] = append(clients, *n.GetWgHost())
continue }
} }
for _, n := range peers {
peer, err := m.convertMeshNode(n, dev, peerToClients, routes) peer, err := m.convertMeshNode(n, dev, peerToClients, routes)
if err != nil { if err != nil {