2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-11-28 15:42:09 +01:00
|
|
|
"sync"
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
2023-10-28 17:38:25 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
2023-10-26 17:53:12 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-28 17:38:25 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/wg"
|
2023-10-26 17:53:12 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
type MeshManager interface {
|
2023-11-20 14:03:42 +01:00
|
|
|
CreateMesh(port int) (string, error)
|
2023-11-05 19:03:58 +01:00
|
|
|
AddMesh(params *AddMeshParams) error
|
|
|
|
HasChanges(meshid string) bool
|
|
|
|
GetMesh(meshId string) MeshProvider
|
2023-11-28 15:42:09 +01:00
|
|
|
GetPublicKey() *wgtypes.Key
|
2023-11-05 19:03:58 +01:00
|
|
|
AddSelf(params *AddSelfParams) error
|
|
|
|
LeaveMesh(meshId string) error
|
|
|
|
GetSelf(meshId string) (MeshNode, error)
|
|
|
|
ApplyConfig() error
|
|
|
|
SetDescription(description string) error
|
2023-11-17 20:05:21 +01:00
|
|
|
SetAlias(alias string) error
|
2023-11-17 23:13:51 +01:00
|
|
|
SetService(service string, value string) error
|
|
|
|
RemoveService(service string) error
|
2023-11-05 19:03:58 +01:00
|
|
|
UpdateTimeStamp() error
|
|
|
|
GetClient() *wgctrl.Client
|
|
|
|
GetMeshes() map[string]MeshProvider
|
2023-11-06 14:37:28 +01:00
|
|
|
Prune() error
|
2023-11-07 20:48:53 +01:00
|
|
|
Close() error
|
2023-11-17 20:05:21 +01:00
|
|
|
GetMonitor() MeshMonitor
|
2023-11-20 12:28:12 +01:00
|
|
|
GetNode(string, string) MeshNode
|
2023-11-25 04:15:58 +01:00
|
|
|
GetRouteManager() RouteManager
|
2023-11-05 19:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type MeshManagerImpl struct {
|
2023-11-28 15:42:09 +01:00
|
|
|
lock sync.RWMutex
|
2023-10-26 17:53:12 +02:00
|
|
|
Meshes map[string]MeshProvider
|
|
|
|
RouteManager RouteManager
|
|
|
|
Client *wgctrl.Client
|
|
|
|
// HostParameters contains information that uniquely locates
|
|
|
|
// the node in the mesh network.
|
2023-10-28 17:38:25 +02:00
|
|
|
HostParameters *HostParameters
|
|
|
|
conf *conf.WgMeshConfiguration
|
|
|
|
meshProviderFactory MeshProviderFactory
|
|
|
|
nodeFactory MeshNodeFactory
|
|
|
|
configApplyer MeshConfigApplyer
|
|
|
|
idGenerator lib.IdGenerator
|
|
|
|
ipAllocator ip.IPAllocator
|
|
|
|
interfaceManipulator wg.WgInterfaceManipulator
|
2023-11-17 20:05:21 +01:00
|
|
|
Monitor MeshMonitor
|
2023-11-28 15:42:09 +01:00
|
|
|
OnDelete func(MeshProvider)
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
// GetRouteManager implements MeshManager.
|
|
|
|
func (m *MeshManagerImpl) GetRouteManager() RouteManager {
|
|
|
|
return m.RouteManager
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
// RemoveService implements MeshManager.
|
|
|
|
func (m *MeshManagerImpl) RemoveService(service string) error {
|
|
|
|
for _, mesh := range m.Meshes {
|
2023-11-24 13:07:03 +01:00
|
|
|
err := mesh.RemoveService(m.HostParameters.GetPublicKey(), service)
|
2023-11-17 23:13:51 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetService implements MeshManager.
|
|
|
|
func (m *MeshManagerImpl) SetService(service string, value string) error {
|
|
|
|
for _, mesh := range m.Meshes {
|
2023-11-24 13:07:03 +01:00
|
|
|
err := mesh.AddService(m.HostParameters.GetPublicKey(), service, value)
|
2023-11-17 23:13:51 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
func (m *MeshManagerImpl) GetNode(meshid, nodeId string) MeshNode {
|
|
|
|
mesh, ok := m.Meshes[meshid]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := mesh.GetNode(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
// GetMonitor implements MeshManager.
|
|
|
|
func (m *MeshManagerImpl) GetMonitor() MeshMonitor {
|
|
|
|
return m.Monitor
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 14:37:28 +01:00
|
|
|
// Prune implements MeshManager.
|
|
|
|
func (m *MeshManagerImpl) Prune() error {
|
|
|
|
for _, mesh := range m.Meshes {
|
|
|
|
err := mesh.Prune(m.conf.PruneTime)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// CreateMesh: Creates a new mesh, stores it and returns the mesh id
|
2023-11-20 14:03:42 +01:00
|
|
|
func (m *MeshManagerImpl) CreateMesh(port int) (string, error) {
|
2023-10-28 17:38:25 +02:00
|
|
|
meshId, err := m.idGenerator.GetId()
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-11-20 14:03:42 +01:00
|
|
|
var ifName string = ""
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-11-20 14:03:42 +01:00
|
|
|
if !m.conf.StubWg {
|
2023-11-24 13:07:03 +01:00
|
|
|
ifName, err = m.interfaceManipulator.CreateInterface(port, m.HostParameters.PrivateKey)
|
2023-11-20 14:03:42 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error creating mesh: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
nodeManager, err := m.meshProviderFactory.CreateMesh(&MeshProviderFactoryParams{
|
2023-11-20 14:03:42 +01:00
|
|
|
DevName: ifName,
|
2023-10-26 17:53:12 +02:00
|
|
|
Port: port,
|
|
|
|
Conf: m.conf,
|
|
|
|
Client: m.Client,
|
2023-10-28 17:38:25 +02:00
|
|
|
MeshId: meshId,
|
2023-10-26 17:53:12 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2023-11-07 20:48:53 +01:00
|
|
|
return "", fmt.Errorf("error creating mesh: %w", err)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
m.lock.Lock()
|
2023-11-01 12:58:10 +01:00
|
|
|
m.Meshes[meshId] = nodeManager
|
2023-11-28 15:42:09 +01:00
|
|
|
m.lock.Unlock()
|
2023-11-01 12:58:10 +01:00
|
|
|
return meshId, nil
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddMeshParams struct {
|
|
|
|
MeshId string
|
|
|
|
WgPort int
|
|
|
|
MeshBytes []byte
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddMesh: Add the mesh to the list of meshes
|
2023-11-05 19:03:58 +01:00
|
|
|
func (m *MeshManagerImpl) AddMesh(params *AddMeshParams) error {
|
2023-11-20 14:03:42 +01:00
|
|
|
var ifName string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if !m.conf.StubWg {
|
2023-11-24 13:07:03 +01:00
|
|
|
ifName, err = m.interfaceManipulator.CreateInterface(params.WgPort, m.HostParameters.PrivateKey)
|
2023-11-20 14:03:42 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
meshProvider, err := m.meshProviderFactory.CreateMesh(&MeshProviderFactoryParams{
|
2023-11-20 14:03:42 +01:00
|
|
|
DevName: ifName,
|
2023-10-28 17:38:25 +02:00
|
|
|
Port: params.WgPort,
|
2023-10-26 17:53:12 +02:00
|
|
|
Conf: m.conf,
|
|
|
|
Client: m.Client,
|
2023-10-28 17:38:25 +02:00
|
|
|
MeshId: params.MeshId,
|
2023-10-26 17:53:12 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
err = meshProvider.Load(params.MeshBytes)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
m.lock.Lock()
|
2023-10-28 17:38:25 +02:00
|
|
|
m.Meshes[params.MeshId] = meshProvider
|
2023-11-28 15:42:09 +01:00
|
|
|
m.lock.Unlock()
|
2023-11-20 12:28:12 +01:00
|
|
|
return nil
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasChanges returns true if the mesh has changes
|
2023-11-05 19:03:58 +01:00
|
|
|
func (m *MeshManagerImpl) HasChanges(meshId string) bool {
|
2023-10-26 17:53:12 +02:00
|
|
|
return m.Meshes[meshId].HasChanges()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMesh returns the mesh with the given meshid
|
2023-11-05 19:03:58 +01:00
|
|
|
func (m *MeshManagerImpl) GetMesh(meshId string) MeshProvider {
|
|
|
|
theMesh := m.Meshes[meshId]
|
2023-10-26 17:53:12 +02:00
|
|
|
return theMesh
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublicKey: Gets the public key of the WireGuard mesh
|
2023-11-28 15:42:09 +01:00
|
|
|
func (s *MeshManagerImpl) GetPublicKey() *wgtypes.Key {
|
2023-11-20 12:28:12 +01:00
|
|
|
if s.conf.StubWg {
|
|
|
|
zeroedKey := make([]byte, wgtypes.KeyLen)
|
2023-11-28 15:42:09 +01:00
|
|
|
return (*wgtypes.Key)(zeroedKey)
|
2023-11-20 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
key := s.HostParameters.PrivateKey.PublicKey()
|
|
|
|
return &key
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
type AddSelfParams struct {
|
|
|
|
// MeshId is the ID of the mesh to add this instance to
|
|
|
|
MeshId string
|
|
|
|
// WgPort is the WireGuard port to advertise
|
|
|
|
WgPort int
|
|
|
|
// Endpoint is the alias of the machine to send routable packets
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSelf adds this host to the mesh
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) AddSelf(params *AddSelfParams) error {
|
2023-11-07 20:48:53 +01:00
|
|
|
mesh := s.GetMesh(params.MeshId)
|
|
|
|
|
|
|
|
if mesh == nil {
|
|
|
|
return fmt.Errorf("addself: mesh %s does not exist", params.MeshId)
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
if params.WgPort == 0 && !s.conf.StubWg {
|
|
|
|
device, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
params.WgPort = device.ListenPort
|
|
|
|
}
|
|
|
|
|
2023-11-24 18:49:06 +01:00
|
|
|
pubKey := s.HostParameters.PrivateKey.PublicKey()
|
2023-10-28 17:38:25 +02:00
|
|
|
|
2023-11-24 18:49:06 +01:00
|
|
|
nodeIP, err := s.ipAllocator.GetIP(pubKey, params.MeshId)
|
2023-10-28 17:38:25 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
node := s.nodeFactory.Build(&MeshNodeFactoryParams{
|
2023-11-24 18:49:06 +01:00
|
|
|
PublicKey: &pubKey,
|
2023-10-28 17:38:25 +02:00
|
|
|
NodeIP: nodeIP,
|
|
|
|
WgPort: params.WgPort,
|
|
|
|
Endpoint: params.Endpoint,
|
|
|
|
})
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if !s.conf.StubWg {
|
|
|
|
device, err := mesh.GetDevice()
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get device %w", err)
|
|
|
|
}
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
err = s.interfaceManipulator.AddAddress(device.Name, fmt.Sprintf("%s/64", nodeIP))
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("addSelf: failed to add address to dev %w", err)
|
|
|
|
}
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
s.Meshes[params.MeshId].AddNode(node)
|
|
|
|
return s.RouteManager.UpdateRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveMesh leaves the mesh network
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) LeaveMesh(meshId string) error {
|
2023-11-28 15:42:09 +01:00
|
|
|
mesh := s.GetMesh(meshId)
|
2023-10-28 17:38:25 +02:00
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
if mesh == nil {
|
2023-11-06 14:37:28 +01:00
|
|
|
return fmt.Errorf("mesh %s does not exist", meshId)
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
var err error
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
s.RouteManager.RemoveRoutes(meshId)
|
|
|
|
err = mesh.RemoveNode(s.HostParameters.GetPublicKey())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.OnDelete != nil {
|
|
|
|
s.OnDelete(mesh)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.lock.Lock()
|
|
|
|
delete(s.Meshes, meshId)
|
|
|
|
s.lock.Unlock()
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if !s.conf.StubWg {
|
2023-11-25 04:15:58 +01:00
|
|
|
device, err := mesh.GetDevice()
|
2023-11-07 20:48:53 +01:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
if err != nil {
|
2023-11-20 12:28:12 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.interfaceManipulator.RemoveInterface(device.Name)
|
2023-11-25 04:15:58 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) GetSelf(meshId string) (MeshNode, error) {
|
2023-10-26 17:53:12 +02:00
|
|
|
meshInstance, ok := s.Meshes[meshId]
|
|
|
|
|
|
|
|
if !ok {
|
2023-11-06 14:37:28 +01:00
|
|
|
return nil, fmt.Errorf("mesh %s does not exist", meshId)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-24 13:07:03 +01:00
|
|
|
logging.Log.WriteInfof(s.HostParameters.GetPublicKey())
|
|
|
|
node, err := meshInstance.GetNode(s.HostParameters.GetPublicKey())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("the node doesn't exist in the mesh")
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) ApplyConfig() error {
|
2023-11-21 14:31:34 +01:00
|
|
|
if s.conf.StubWg {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:48:53 +01:00
|
|
|
err := s.configApplyer.ApplyConfig()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
return nil
|
2023-11-01 11:39:46 +01:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) SetDescription(description string) error {
|
2023-11-28 15:42:09 +01:00
|
|
|
meshes := s.GetMeshes()
|
|
|
|
for _, mesh := range meshes {
|
2023-11-24 13:07:03 +01:00
|
|
|
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
|
|
|
err := mesh.SetDescription(s.HostParameters.GetPublicKey(), description)
|
2023-11-01 12:58:10 +01:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-01 12:58:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
// SetAlias implements MeshManager.
|
|
|
|
func (s *MeshManagerImpl) SetAlias(alias string) error {
|
2023-11-28 15:42:09 +01:00
|
|
|
meshes := s.GetMeshes()
|
|
|
|
for _, mesh := range meshes {
|
2023-11-24 13:07:03 +01:00
|
|
|
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
|
|
|
err := mesh.SetAlias(s.HostParameters.GetPublicKey(), alias)
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// UpdateTimeStamp updates the timestamp of this node in all meshes
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) UpdateTimeStamp() error {
|
2023-11-28 15:42:09 +01:00
|
|
|
meshes := s.GetMeshes()
|
|
|
|
for _, mesh := range meshes {
|
2023-11-24 13:07:03 +01:00
|
|
|
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
|
|
|
err := mesh.UpdateTimeStamp(s.HostParameters.GetPublicKey())
|
2023-10-30 17:49:02 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func (s *MeshManagerImpl) GetClient() *wgctrl.Client {
|
|
|
|
return s.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MeshManagerImpl) GetMeshes() map[string]MeshProvider {
|
2023-11-28 15:42:09 +01:00
|
|
|
meshes := make(map[string]MeshProvider)
|
|
|
|
|
|
|
|
s.lock.RLock()
|
|
|
|
|
|
|
|
for id, mesh := range s.Meshes {
|
|
|
|
meshes[id] = mesh
|
|
|
|
}
|
|
|
|
|
|
|
|
s.lock.RUnlock()
|
|
|
|
return meshes
|
2023-11-05 19:03:58 +01:00
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
// Close the mesh manager
|
2023-11-07 20:48:53 +01:00
|
|
|
func (s *MeshManagerImpl) Close() error {
|
2023-11-20 12:28:12 +01:00
|
|
|
if s.conf.StubWg {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:48:53 +01:00
|
|
|
for _, mesh := range s.Meshes {
|
|
|
|
dev, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.interfaceManipulator.RemoveInterface(dev.Name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
// NewMeshManagerParams params required to create an instance of a mesh manager
|
|
|
|
type NewMeshManagerParams struct {
|
|
|
|
Conf conf.WgMeshConfiguration
|
|
|
|
Client *wgctrl.Client
|
|
|
|
MeshProvider MeshProviderFactory
|
|
|
|
NodeFactory MeshNodeFactory
|
|
|
|
IdGenerator lib.IdGenerator
|
|
|
|
IPAllocator ip.IPAllocator
|
|
|
|
InterfaceManipulator wg.WgInterfaceManipulator
|
2023-11-05 19:03:58 +01:00
|
|
|
ConfigApplyer MeshConfigApplyer
|
2023-11-07 20:48:53 +01:00
|
|
|
RouteManager RouteManager
|
2023-11-28 15:42:09 +01:00
|
|
|
OnDelete func(MeshProvider)
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// Creates a new instance of a mesh manager with the given parameters
|
2023-11-17 20:05:21 +01:00
|
|
|
func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
2023-11-24 13:07:03 +01:00
|
|
|
privateKey, _ := wgtypes.GeneratePrivateKey()
|
|
|
|
hostParams := HostParameters{
|
|
|
|
PrivateKey: &privateKey,
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
m := &MeshManagerImpl{
|
2023-10-26 17:53:12 +02:00
|
|
|
Meshes: make(map[string]MeshProvider),
|
|
|
|
HostParameters: &hostParams,
|
2023-10-28 17:38:25 +02:00
|
|
|
meshProviderFactory: params.MeshProvider,
|
|
|
|
nodeFactory: params.NodeFactory,
|
|
|
|
Client: params.Client,
|
|
|
|
conf: ¶ms.Conf,
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
2023-11-06 10:54:06 +01:00
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
m.configApplyer = params.ConfigApplyer
|
2023-11-07 20:48:53 +01:00
|
|
|
m.RouteManager = params.RouteManager
|
|
|
|
|
|
|
|
if m.RouteManager == nil {
|
|
|
|
m.RouteManager = NewRouteManager(m)
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
m.idGenerator = params.IdGenerator
|
|
|
|
m.ipAllocator = params.IPAllocator
|
|
|
|
m.interfaceManipulator = params.InterfaceManipulator
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
m.Monitor = NewMeshMonitor(m)
|
|
|
|
|
|
|
|
aliasManager := NewAliasManager()
|
|
|
|
m.Monitor.AddUpdateCallback(aliasManager.AddAliases)
|
|
|
|
m.Monitor.AddRemoveCallback(aliasManager.RemoveAliases)
|
2023-11-28 15:42:09 +01:00
|
|
|
m.OnDelete = params.OnDelete
|
2023-10-26 17:53:12 +02:00
|
|
|
return m
|
|
|
|
}
|