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-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
|
|
|
|
config *conf.WgMeshConfiguration
|
|
|
|
routeInstaller route.RouteInstaller
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
type routeNode struct {
|
|
|
|
gateway string
|
|
|
|
route Route
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routeNode) equals(route2 *routeNode) bool {
|
|
|
|
return r.gateway == route2.gateway && RouteEquals(r.route, route2.route)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Device,
|
|
|
|
peerToClients map[string][]net.IPNet,
|
|
|
|
routes map[string][]routeNode) (*wgtypes.PeerConfig, error) {
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
endpoint, err := net.ResolveUDPAddr("udp", node.GetWgEndpoint())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := node.GetPublicKey()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedips := make([]net.IPNet, 1)
|
|
|
|
allowedips[0] = *node.GetWgHost()
|
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
clients, ok := peerToClients[node.GetWgHost().String()]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
allowedips = append(allowedips, clients...)
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
for _, route := range node.GetRoutes() {
|
|
|
|
bestRoutes := routes[route.GetDestination().String()]
|
|
|
|
|
|
|
|
if len(bestRoutes) == 1 {
|
|
|
|
allowedips = append(allowedips, *route.GetDestination())
|
|
|
|
} else if len(bestRoutes) > 1 {
|
|
|
|
keyFunc := func(mn MeshNode) int {
|
|
|
|
pubKey, _ := mn.GetPublicKey()
|
|
|
|
return lib.HashString(pubKey.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketFunc := func(rn routeNode) int {
|
|
|
|
return lib.HashString(rn.gateway)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else there is more than one candidate so consistently hash
|
|
|
|
pickedRoute := lib.ConsistentHash(bestRoutes, node, bucketFunc, keyFunc)
|
|
|
|
|
|
|
|
if pickedRoute.gateway == pubKey.String() {
|
|
|
|
allowedips = append(allowedips, *route.GetDestination())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
keepAlive := time.Duration(m.config.KeepAliveWg) * time.Second
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-22 14:07:33 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-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-11-27 19:55:41 +01:00
|
|
|
func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]routeNode {
|
|
|
|
mesh, _ := meshProvider.GetMesh()
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
routes := make(map[string][]routeNode)
|
|
|
|
|
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()
|
|
|
|
meshRoutes, _ := meshProvider.GetRoutes(pubKey.String())
|
|
|
|
|
|
|
|
for _, route := range meshRoutes {
|
|
|
|
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
|
|
|
if prefix == nil || route == nil || route.GetDestination() == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
|
|
|
|
snap, err := mesh.GetMesh()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
nodes := lib.MapValues(snap.GetNodes())
|
2023-10-26 17:53:12 +02:00
|
|
|
peerConfigs := make([]wgtypes.PeerConfig, len(nodes))
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
peers := lib.Filter(nodes, func(mn MeshNode) bool {
|
|
|
|
return mn.GetType() == conf.PEER_ROLE
|
|
|
|
})
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
var count int = 0
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
self, err := m.meshManager.GetSelf(mesh.GetMeshId())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
peerToClients := make(map[string][]net.IPNet)
|
2023-11-27 19:55:41 +01:00
|
|
|
routes := m.getRoutes(mesh)
|
2023-11-27 16:56:30 +01:00
|
|
|
installedRoutes := make([]lib.Route, 0)
|
2023-11-25 04:15:58 +01:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
for _, n := range nodes {
|
2023-11-21 21:42:43 +01:00
|
|
|
if NodeEquals(n, self) {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
if n.GetType() == conf.CLIENT_ROLE && len(peers) > 0 && self.GetType() == conf.CLIENT_ROLE {
|
2023-11-27 16:56:30 +01:00
|
|
|
hashFunc := func(mn MeshNode) int {
|
2023-11-21 21:42:43 +01:00
|
|
|
return lib.HashString(mn.GetWgHost().String())
|
2023-11-27 16:56:30 +01:00
|
|
|
}
|
|
|
|
peer := lib.ConsistentHash(peers, n, hashFunc, hashFunc)
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
clients, ok := peerToClients[peer.GetWgHost().String()]
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-21 22:26:31 +01:00
|
|
|
if !ok {
|
|
|
|
clients = make([]net.IPNet, 0)
|
|
|
|
peerToClients[peer.GetWgHost().String()] = clients
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
2023-11-21 22:26:31 +01:00
|
|
|
|
|
|
|
peerToClients[peer.GetWgHost().String()] = append(clients, *n.GetWgHost())
|
|
|
|
continue
|
2023-11-21 17:42:49 +01:00
|
|
|
}
|
|
|
|
|
2023-11-22 14:07:33 +01:00
|
|
|
dev, _ := mesh.GetDevice()
|
2023-11-27 16:56:30 +01:00
|
|
|
peer, err := m.convertMeshNode(n, dev, peerToClients, routes)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
for _, route := range peer.AllowedIPs {
|
|
|
|
ula := &ip.ULABuilder{}
|
|
|
|
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
|
|
|
|
|
|
|
if !ipNet.Contains(route.IP) {
|
|
|
|
|
|
|
|
installedRoutes = append(installedRoutes, lib.Route{
|
|
|
|
Gateway: n.GetWgHost().IP,
|
|
|
|
Destination: route,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
peerConfigs[count] = *peer
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := wgtypes.Config{
|
2023-11-27 16:56:30 +01:00
|
|
|
Peers: peerConfigs,
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dev, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
err = m.routeInstaller.InstallRoutes(dev.Name, installedRoutes...)
|
2023-11-25 04:15:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
return m.meshManager.GetClient().ConfigureDevice(dev.Name, cfg)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WgMeshConfigApplyer) ApplyConfig() error {
|
2023-11-05 19:03:58 +01:00
|
|
|
for _, mesh := range m.meshManager.GetMeshes() {
|
2023-10-26 17:53:12 +02:00
|
|
|
err := m.updateWgConf(mesh)
|
|
|
|
|
|
|
|
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-11-01 12:58:10 +01:00
|
|
|
ReplacePeers: true,
|
2023-11-22 14:07:33 +01:00
|
|
|
Peers: make([]wgtypes.PeerConfig, 0),
|
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-11-21 17:42:49 +01:00
|
|
|
func NewWgMeshConfigApplyer(config *conf.WgMeshConfiguration) MeshConfigApplyer {
|
|
|
|
return &WgMeshConfigApplyer{
|
|
|
|
config: config,
|
|
|
|
routeInstaller: route.NewRouteInstaller(),
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|