2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
2023-11-01 12:58:10 +01:00
|
|
|
"fmt"
|
2023-10-26 17:53:12 +02:00
|
|
|
"net"
|
2023-11-22 14:07:33 +01:00
|
|
|
"slices"
|
2023-12-04 18:32:50 +01:00
|
|
|
"strings"
|
2023-11-21 17:42:49 +01:00
|
|
|
"time"
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
2023-11-27 16:56:30 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
2023-11-21 17:42:49 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/route"
|
2023-10-26 17:53:12 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MeshConfigApplyer abstracts applying the mesh configuration
|
|
|
|
type MeshConfigApplyer interface {
|
|
|
|
ApplyConfig() error
|
2023-11-01 12:58:10 +01:00
|
|
|
RemovePeers(meshId string) error
|
2023-11-06 10:54:06 +01:00
|
|
|
SetMeshManager(manager MeshManager)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WgMeshConfigApplyer applies WireGuard configuration
|
|
|
|
type WgMeshConfigApplyer struct {
|
2023-11-21 17:42:49 +01:00
|
|
|
meshManager MeshManager
|
|
|
|
routeInstaller route.RouteInstaller
|
2023-12-09 00:56:07 +01:00
|
|
|
hashFunc func(MeshNode) int
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
type routeNode struct {
|
|
|
|
gateway string
|
|
|
|
route Route
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
type convertMeshNodeParams struct {
|
|
|
|
node MeshNode
|
|
|
|
self MeshNode
|
|
|
|
mesh MeshProvider
|
|
|
|
device *wgtypes.Device
|
|
|
|
peerToClients map[string][]net.IPNet
|
|
|
|
routes map[string][]routeNode
|
|
|
|
}
|
2023-11-27 16:56:30 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wgtypes.PeerConfig, error) {
|
|
|
|
pubKey, err := params.node.GetPublicKey()
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedips := make([]net.IPNet, 1)
|
2023-12-10 20:21:54 +01:00
|
|
|
allowedips[0] = *params.node.GetWgHost()
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
clients, ok := params.peerToClients[pubKey.String()]
|
2023-11-21 22:26:31 +01:00
|
|
|
|
|
|
|
if ok {
|
|
|
|
allowedips = append(allowedips, clients...)
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
for _, route := range params.node.GetRoutes() {
|
|
|
|
bestRoutes := params.routes[route.GetDestination().String()]
|
2023-11-28 15:42:09 +01:00
|
|
|
var pickedRoute routeNode
|
2023-11-27 16:56:30 +01:00
|
|
|
|
|
|
|
if len(bestRoutes) == 1 {
|
2023-11-28 15:42:09 +01:00
|
|
|
pickedRoute = bestRoutes[0]
|
2023-11-27 16:56:30 +01:00
|
|
|
} else if len(bestRoutes) > 1 {
|
|
|
|
bucketFunc := func(rn routeNode) int {
|
|
|
|
return lib.HashString(rn.gateway)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else there is more than one candidate so consistently hash
|
2023-12-10 20:21:54 +01:00
|
|
|
pickedRoute = lib.ConsistentHash(bestRoutes, params.self, bucketFunc, m.hashFunc)
|
2023-11-28 15:42:09 +01:00
|
|
|
}
|
2023-11-27 16:56:30 +01:00
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
if pickedRoute.gateway == pubKey.String() {
|
|
|
|
allowedips = append(allowedips, *pickedRoute.route.GetDestination())
|
2023-11-27 16:56:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
config := params.mesh.GetConfiguration()
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
var keepAlive time.Duration = time.Duration(0)
|
|
|
|
|
|
|
|
if config.KeepAliveWg != nil {
|
|
|
|
keepAlive = time.Duration(*config.KeepAliveWg) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
existing := slices.IndexFunc(params.device.Peers, func(p wgtypes.Peer) bool {
|
|
|
|
pubKey, _ := params.node.GetPublicKey()
|
2023-11-22 14:07:33 +01:00
|
|
|
return p.PublicKey.String() == pubKey.String()
|
|
|
|
})
|
|
|
|
|
2023-12-28 18:46:04 +01:00
|
|
|
var endpoint *net.UDPAddr = nil
|
|
|
|
|
|
|
|
if params.node.GetType() == conf.PEER_ROLE {
|
|
|
|
endpoint, err = net.ResolveUDPAddr("udp", params.node.GetWgEndpoint())
|
|
|
|
}
|
2023-12-09 00:56:07 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't override the existing IP in case it already exists
|
2023-11-22 14:07:33 +01:00
|
|
|
if existing != -1 {
|
2023-12-10 20:21:54 +01:00
|
|
|
endpoint = params.device.Peers[existing].Endpoint
|
2023-11-22 14:07:33 +01:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
peerConfig := wgtypes.PeerConfig{
|
2023-11-21 17:42:49 +01:00
|
|
|
PublicKey: pubKey,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
AllowedIPs: allowedips,
|
|
|
|
PersistentKeepaliveInterval: &keepAlive,
|
2023-12-04 18:13:51 +01:00
|
|
|
ReplaceAllowedIPs: true,
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &peerConfig, nil
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
// getRoutes: finds the routes with the least hop distance. If more than one route exists
|
|
|
|
// consistently hash to evenly spread the distribution of traffic
|
2023-12-30 01:44:57 +01:00
|
|
|
func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) (map[string][]routeNode, error) {
|
2023-11-27 19:55:41 +01:00
|
|
|
mesh, _ := meshProvider.GetMesh()
|
2023-11-27 16:56:30 +01:00
|
|
|
routes := make(map[string][]routeNode)
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
peers := lib.Filter(lib.MapValues(mesh.GetNodes()), func(p MeshNode) bool {
|
|
|
|
return p.GetType() == conf.PEER_ROLE
|
|
|
|
})
|
|
|
|
|
2023-11-27 19:55:41 +01:00
|
|
|
meshPrefixes := lib.Map(lib.MapValues(m.meshManager.GetMeshes()), func(mesh MeshProvider) *net.IPNet {
|
|
|
|
ula := &ip.ULABuilder{}
|
|
|
|
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
|
|
|
return ipNet
|
|
|
|
})
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
for _, node := range mesh.GetNodes() {
|
2023-11-27 19:55:41 +01:00
|
|
|
pubKey, _ := node.GetPublicKey()
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
for _, route := range node.GetRoutes() {
|
2023-11-27 19:55:41 +01:00
|
|
|
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
2023-12-11 00:35:00 +01:00
|
|
|
if prefix.IP.Equal(net.IPv6zero) && *meshProvider.GetConfiguration().AdvertiseDefaultRoute {
|
2023-12-08 12:49:24 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:55:41 +01:00
|
|
|
return prefix.Contains(route.GetDestination().IP)
|
|
|
|
}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
destination := route.GetDestination().String()
|
|
|
|
otherRoute, ok := routes[destination]
|
|
|
|
|
|
|
|
rn := routeNode{
|
|
|
|
gateway: pubKey.String(),
|
|
|
|
route: route,
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
// Client's only acessible by another peer
|
|
|
|
if node.GetType() == conf.CLIENT_ROLE {
|
|
|
|
peer := m.getCorrespondingPeer(peers, node)
|
2023-12-30 01:44:57 +01:00
|
|
|
self, err := meshProvider.GetNode(m.meshManager.GetPublicKey().String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-09 00:56:07 +01:00
|
|
|
|
|
|
|
if !NodeEquals(peer, self) {
|
|
|
|
peerPub, _ := peer.GetPublicKey()
|
|
|
|
rn.gateway = peerPub.String()
|
|
|
|
rn.route = &RouteStub{
|
|
|
|
Destination: rn.route.GetDestination(),
|
|
|
|
HopCount: rn.route.GetHopCount() + 1,
|
2023-12-30 01:44:57 +01:00
|
|
|
Path: append(rn.route.GetPath(), peer.GetWgHost().IP.String()),
|
2023-12-09 00:56:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
if !ok {
|
|
|
|
otherRoute = make([]routeNode, 1)
|
|
|
|
otherRoute[0] = rn
|
|
|
|
routes[destination] = otherRoute
|
2023-11-27 19:55:41 +01:00
|
|
|
} else if route.GetHopCount() < otherRoute[0].route.GetHopCount() {
|
2023-11-27 16:56:30 +01:00
|
|
|
otherRoute[0] = rn
|
|
|
|
} else if otherRoute[0].route.GetHopCount() == route.GetHopCount() {
|
|
|
|
routes[destination] = append(otherRoute, rn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
return routes, nil
|
2023-11-27 16:56:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
// getCorrespondignPeer: gets the peer corresponding to the client
|
|
|
|
func (m *WgMeshConfigApplyer) getCorrespondingPeer(peers []MeshNode, client MeshNode) MeshNode {
|
2023-12-09 00:56:07 +01:00
|
|
|
peer := lib.ConsistentHash(peers, client, m.hashFunc, m.hashFunc)
|
2023-12-05 03:00:16 +01:00
|
|
|
return peer
|
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
func (m *WgMeshConfigApplyer) getPeerCfgsToRemove(dev *wgtypes.Device, newPeers []wgtypes.PeerConfig) []wgtypes.PeerConfig {
|
|
|
|
peers := dev.Peers
|
|
|
|
peers = lib.Filter(peers, func(p1 wgtypes.Peer) bool {
|
|
|
|
return !lib.Contains(newPeers, func(p2 wgtypes.PeerConfig) bool {
|
|
|
|
return p1.PublicKey.String() == p2.PublicKey.String()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return lib.Map(peers, func(p wgtypes.Peer) wgtypes.PeerConfig {
|
|
|
|
return wgtypes.PeerConfig{
|
|
|
|
PublicKey: p.PublicKey,
|
|
|
|
Remove: true,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
type GetConfigParams struct {
|
|
|
|
mesh MeshProvider
|
|
|
|
peers []MeshNode
|
|
|
|
clients []MeshNode
|
|
|
|
dev *wgtypes.Device
|
|
|
|
routes map[string][]routeNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WgMeshConfigApplyer) getClientConfig(params *GetConfigParams) (*wgtypes.Config, error) {
|
2023-12-08 21:02:57 +01:00
|
|
|
ula := &ip.ULABuilder{}
|
2023-12-09 00:56:07 +01:00
|
|
|
meshNet, _ := ula.GetIPNet(params.mesh.GetMeshId())
|
2023-12-08 21:02:57 +01:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
routesForMesh := lib.Map(lib.MapValues(params.routes), func(rns []routeNode) []routeNode {
|
|
|
|
return lib.Filter(rns, func(rn routeNode) bool {
|
|
|
|
ip, _, _ := net.ParseCIDR(rn.gateway)
|
|
|
|
return meshNet.Contains(ip)
|
|
|
|
})
|
2023-12-08 12:49:24 +01:00
|
|
|
})
|
2023-12-09 00:56:07 +01:00
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
routes := lib.Map(routesForMesh, func(rs []routeNode) net.IPNet {
|
|
|
|
return *rs[0].route.GetDestination()
|
|
|
|
})
|
2023-12-08 21:02:57 +01:00
|
|
|
routes = append(routes, *meshNet)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
self, err := params.mesh.GetNode(m.meshManager.GetPublicKey().String())
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err != nil {
|
2023-12-05 03:00:16 +01:00
|
|
|
return nil, err
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
peer := m.getCorrespondingPeer(params.peers, self)
|
2023-12-05 03:00:16 +01:00
|
|
|
pubKey, _ := peer.GetPublicKey()
|
2023-12-10 20:21:54 +01:00
|
|
|
|
|
|
|
config := params.mesh.GetConfiguration()
|
|
|
|
|
|
|
|
keepAlive := time.Duration(*config.KeepAliveWg) * time.Second
|
2023-12-05 03:00:16 +01:00
|
|
|
endpoint, err := net.ResolveUDPAddr("udp", peer.GetWgEndpoint())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
peerCfgs := make([]wgtypes.PeerConfig, 1)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
peerCfgs[0] = wgtypes.PeerConfig{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
PersistentKeepaliveInterval: &keepAlive,
|
2023-12-08 12:49:24 +01:00
|
|
|
AllowedIPs: routes,
|
2023-12-09 00:56:07 +01:00
|
|
|
ReplaceAllowedIPs: true,
|
2023-12-05 03:00:16 +01:00
|
|
|
}
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-12-08 21:02:57 +01:00
|
|
|
installedRoutes := make([]lib.Route, 0)
|
|
|
|
|
|
|
|
for _, route := range peerCfgs[0].AllowedIPs {
|
|
|
|
installedRoutes = append(installedRoutes, lib.Route{
|
|
|
|
Gateway: peer.GetWgHost().IP,
|
|
|
|
Destination: route,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
cfg := wgtypes.Config{
|
|
|
|
Peers: peerCfgs,
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
m.routeInstaller.InstallRoutes(params.dev.Name, installedRoutes...)
|
2023-12-05 03:00:16 +01:00
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
func (m *WgMeshConfigApplyer) getRoutesToInstall(wgNode *wgtypes.PeerConfig, mesh MeshProvider, node MeshNode) []lib.Route {
|
|
|
|
routes := make([]lib.Route, 0)
|
|
|
|
|
|
|
|
for _, route := range wgNode.AllowedIPs {
|
|
|
|
ula := &ip.ULABuilder{}
|
|
|
|
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
|
|
|
|
|
|
|
_, defaultRoute, _ := net.ParseCIDR("::/0")
|
|
|
|
|
|
|
|
if !ipNet.Contains(route.IP) && !ipNet.IP.Equal(defaultRoute.IP) {
|
|
|
|
routes = append(routes, lib.Route{
|
|
|
|
Gateway: node.GetWgHost().IP,
|
|
|
|
Destination: route,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WgMeshConfigApplyer) getPeerConfig(params *GetConfigParams) (*wgtypes.Config, error) {
|
2023-11-21 22:26:31 +01:00
|
|
|
peerToClients := make(map[string][]net.IPNet)
|
2023-11-27 16:56:30 +01:00
|
|
|
installedRoutes := make([]lib.Route, 0)
|
2023-12-05 03:00:16 +01:00
|
|
|
peerConfigs := make([]wgtypes.PeerConfig, 0)
|
2023-12-30 01:44:57 +01:00
|
|
|
self, err := params.mesh.GetNode(m.meshManager.GetPublicKey().String())
|
2023-11-25 04:15:58 +01:00
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-04 17:40:15 +01:00
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
for _, n := range params.clients {
|
|
|
|
if len(params.peers) > 0 {
|
|
|
|
peer := m.getCorrespondingPeer(params.peers, n)
|
2023-12-04 18:32:50 +01:00
|
|
|
pubKey, _ := peer.GetPublicKey()
|
|
|
|
clients, ok := peerToClients[pubKey.String()]
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
if !ok {
|
|
|
|
clients = make([]net.IPNet, 0)
|
2023-12-04 18:32:50 +01:00
|
|
|
peerToClients[pubKey.String()] = clients
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
2023-11-21 22:26:31 +01:00
|
|
|
|
2023-12-04 18:32:50 +01:00
|
|
|
peerToClients[pubKey.String()] = append(clients, *n.GetWgHost())
|
2023-12-05 03:00:16 +01:00
|
|
|
|
|
|
|
if NodeEquals(self, peer) {
|
2023-12-10 20:21:54 +01:00
|
|
|
cfg, err := m.convertMeshNode(convertMeshNodeParams{
|
|
|
|
node: n,
|
|
|
|
self: self,
|
|
|
|
mesh: params.mesh,
|
|
|
|
device: params.dev,
|
|
|
|
peerToClients: peerToClients,
|
|
|
|
routes: params.routes,
|
|
|
|
})
|
2023-12-05 03:00:16 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
installedRoutes = append(installedRoutes, m.getRoutesToInstall(cfg, params.mesh, n)...)
|
2023-12-05 03:00:16 +01:00
|
|
|
peerConfigs = append(peerConfigs, *cfg)
|
|
|
|
}
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
2023-12-04 18:32:50 +01:00
|
|
|
}
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
for _, n := range params.peers {
|
2023-12-04 18:40:24 +01:00
|
|
|
if NodeEquals(n, self) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
peer, err := m.convertMeshNode(convertMeshNodeParams{
|
|
|
|
node: n,
|
|
|
|
self: self,
|
|
|
|
mesh: params.mesh,
|
|
|
|
peerToClients: peerToClients,
|
|
|
|
routes: params.routes,
|
|
|
|
device: params.dev,
|
|
|
|
})
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-12-05 03:00:16 +01:00
|
|
|
return nil, err
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
installedRoutes = append(installedRoutes, m.getRoutesToInstall(peer, params.mesh, n)...)
|
2023-12-05 03:00:16 +01:00
|
|
|
peerConfigs = append(peerConfigs, *peer)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg := wgtypes.Config{
|
2023-12-10 16:10:36 +01:00
|
|
|
Peers: peerConfigs,
|
2023-12-05 03:00:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
err = m.routeInstaller.InstallRoutes(params.dev.Name, installedRoutes...)
|
2023-12-05 03:00:16 +01:00
|
|
|
return &cfg, err
|
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider, routes map[string][]routeNode) error {
|
2023-12-05 03:00:16 +01:00
|
|
|
snap, err := mesh.GetMesh()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := lib.MapValues(snap.GetNodes())
|
|
|
|
dev, _ := mesh.GetDevice()
|
|
|
|
|
|
|
|
slices.SortFunc(nodes, func(a, b MeshNode) int {
|
|
|
|
return strings.Compare(string(a.GetType()), string(b.GetType()))
|
|
|
|
})
|
|
|
|
|
|
|
|
peers := lib.Filter(nodes, func(mn MeshNode) bool {
|
|
|
|
return mn.GetType() == conf.PEER_ROLE
|
|
|
|
})
|
|
|
|
|
|
|
|
clients := lib.Filter(nodes, func(mn MeshNode) bool {
|
|
|
|
return mn.GetType() == conf.CLIENT_ROLE
|
|
|
|
})
|
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
self, err := mesh.GetNode(m.meshManager.GetPublicKey().String())
|
2023-12-05 03:00:16 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg *wgtypes.Config = nil
|
|
|
|
|
2023-12-09 00:56:07 +01:00
|
|
|
configParams := &GetConfigParams{
|
|
|
|
mesh: mesh,
|
|
|
|
peers: peers,
|
|
|
|
clients: clients,
|
|
|
|
dev: dev,
|
|
|
|
routes: routes,
|
|
|
|
}
|
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
switch self.GetType() {
|
|
|
|
case conf.PEER_ROLE:
|
2023-12-09 00:56:07 +01:00
|
|
|
cfg, err = m.getPeerConfig(configParams)
|
2023-12-05 03:00:16 +01:00
|
|
|
case conf.CLIENT_ROLE:
|
2023-12-09 00:56:07 +01:00
|
|
|
cfg, err = m.getClientConfig(configParams)
|
2023-12-05 03:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-12-10 16:10:36 +01:00
|
|
|
toRemove := m.getPeerCfgsToRemove(dev, cfg.Peers)
|
|
|
|
cfg.Peers = append(cfg.Peers, toRemove...)
|
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
err = m.meshManager.GetClient().ConfigureDevice(dev.Name, *cfg)
|
2023-11-25 04:15:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-05 03:00:16 +01:00
|
|
|
return nil
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
func (m *WgMeshConfigApplyer) getAllRoutes() (map[string][]routeNode, error) {
|
2023-12-10 16:10:36 +01:00
|
|
|
allRoutes := make(map[string][]routeNode)
|
|
|
|
|
|
|
|
for _, mesh := range m.meshManager.GetMeshes() {
|
2023-12-30 01:44:57 +01:00
|
|
|
routes, err := m.getRoutes(mesh)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-10 16:10:36 +01:00
|
|
|
|
|
|
|
for destination, route := range routes {
|
|
|
|
_, ok := allRoutes[destination]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
allRoutes[destination] = route
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if allRoutes[destination][0].route.GetHopCount() == route[0].route.GetHopCount() {
|
|
|
|
allRoutes[destination] = append(allRoutes[destination], route...)
|
|
|
|
} else if route[0].route.GetHopCount() < allRoutes[destination][0].route.GetHopCount() {
|
|
|
|
allRoutes[destination] = route
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 01:44:57 +01:00
|
|
|
return allRoutes, nil
|
2023-12-10 16:10:36 +01:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *WgMeshConfigApplyer) ApplyConfig() error {
|
2023-12-30 01:44:57 +01:00
|
|
|
allRoutes, err := m.getAllRoutes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-10 16:10:36 +01:00
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
for _, mesh := range m.meshManager.GetMeshes() {
|
2023-12-10 16:10:36 +01:00
|
|
|
err := m.updateWgConf(mesh, allRoutes)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-01 12:58:10 +01:00
|
|
|
func (m *WgMeshConfigApplyer) RemovePeers(meshId string) error {
|
|
|
|
mesh := m.meshManager.GetMesh(meshId)
|
|
|
|
|
|
|
|
if mesh == nil {
|
2023-11-06 10:54:06 +01:00
|
|
|
return fmt.Errorf("mesh %s does not exist", meshId)
|
2023-11-01 12:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dev, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
m.meshManager.GetClient().ConfigureDevice(dev.Name, wgtypes.Config{
|
2023-12-05 03:00:16 +01:00
|
|
|
Peers: make([]wgtypes.PeerConfig, 0),
|
|
|
|
ReplacePeers: true,
|
2023-11-01 12:58:10 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 10:54:06 +01:00
|
|
|
func (m *WgMeshConfigApplyer) SetMeshManager(manager MeshManager) {
|
|
|
|
m.meshManager = manager
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
func NewWgMeshConfigApplyer() MeshConfigApplyer {
|
2023-11-21 17:42:49 +01:00
|
|
|
return &WgMeshConfigApplyer{
|
|
|
|
routeInstaller: route.NewRouteInstaller(),
|
2023-12-09 00:56:07 +01:00
|
|
|
hashFunc: func(mn MeshNode) int {
|
|
|
|
pubKey, _ := mn.GetPublicKey()
|
|
|
|
return lib.HashString(pubKey.String())
|
|
|
|
},
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|