1
0
forked from extern/smegmesh

Only updating WireGuard config if node exists

This commit is contained in:
Tim Beatham 2023-11-22 13:07:33 +00:00
parent 09c64c4628
commit e7ac8c5542

View File

@ -3,6 +3,7 @@ package mesh
import ( import (
"fmt" "fmt"
"net" "net"
"slices"
"time" "time"
"github.com/tim-beatham/wgmesh/pkg/conf" "github.com/tim-beatham/wgmesh/pkg/conf"
@ -25,7 +26,7 @@ type WgMeshConfigApplyer struct {
routeInstaller route.RouteInstaller routeInstaller route.RouteInstaller
} }
func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, peerToClients map[string][]net.IPNet) (*wgtypes.PeerConfig, error) { func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Device, peerToClients map[string][]net.IPNet) (*wgtypes.PeerConfig, error) {
endpoint, err := net.ResolveUDPAddr("udp", node.GetWgEndpoint()) endpoint, err := net.ResolveUDPAddr("udp", node.GetWgEndpoint())
if err != nil { if err != nil {
@ -54,6 +55,15 @@ func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, peerToClients map[s
keepAlive := time.Duration(m.config.KeepAliveWg) * time.Second keepAlive := time.Duration(m.config.KeepAliveWg) * time.Second
existing := slices.IndexFunc(device.Peers, func(p wgtypes.Peer) bool {
pubKey, _ := node.GetPublicKey()
return p.PublicKey.String() == pubKey.String()
})
if existing != -1 {
endpoint = device.Peers[existing].Endpoint
}
peerConfig := wgtypes.PeerConfig{ peerConfig := wgtypes.PeerConfig{
PublicKey: pubKey, PublicKey: pubKey,
Endpoint: endpoint, Endpoint: endpoint,
@ -130,7 +140,9 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
continue continue
} }
peer, err := m.convertMeshNode(n, peerToClients) dev, _ := mesh.GetDevice()
peer, err := m.convertMeshNode(n, dev, peerToClients)
if err != nil { if err != nil {
return err return err
@ -180,7 +192,7 @@ func (m *WgMeshConfigApplyer) RemovePeers(meshId string) error {
m.meshManager.GetClient().ConfigureDevice(dev.Name, wgtypes.Config{ m.meshManager.GetClient().ConfigureDevice(dev.Name, wgtypes.Config{
ReplacePeers: true, ReplacePeers: true,
Peers: make([]wgtypes.PeerConfig, 1), Peers: make([]wgtypes.PeerConfig, 0),
}) })
return nil return nil