2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MeshManager struct {
|
|
|
|
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-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMesh: Creates a new mesh, stores it and returns the mesh id
|
|
|
|
func (m *MeshManager) CreateMesh(devName string, 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
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeManager, err := m.meshProviderFactory.CreateMesh(&MeshProviderFactoryParams{
|
|
|
|
DevName: devName,
|
|
|
|
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 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
m.Meshes[meshId] = nodeManager
|
|
|
|
return meshId, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddMeshParams struct {
|
|
|
|
MeshId string
|
|
|
|
DevName string
|
|
|
|
WgPort int
|
|
|
|
MeshBytes []byte
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddMesh: Add the mesh to the list of meshes
|
2023-10-28 17:38:25 +02:00
|
|
|
func (m *MeshManager) AddMesh(params *AddMeshParams) error {
|
2023-10-26 17:53:12 +02:00
|
|
|
meshProvider, err := m.meshProviderFactory.CreateMesh(&MeshProviderFactoryParams{
|
2023-10-28 17:38:25 +02:00
|
|
|
DevName: params.DevName,
|
|
|
|
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-10-28 17:38:25 +02:00
|
|
|
m.Meshes[params.MeshId] = meshProvider
|
2023-10-27 18:49:18 +02:00
|
|
|
return err
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasChanges returns true if the mesh has changes
|
|
|
|
func (m *MeshManager) HasChanges(meshId string) bool {
|
|
|
|
return m.Meshes[meshId].HasChanges()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMesh returns the mesh with the given meshid
|
|
|
|
func (m *MeshManager) GetMesh(meshId string) MeshProvider {
|
|
|
|
theMesh, _ := m.Meshes[meshId]
|
|
|
|
return theMesh
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableInterface: Enables the given WireGuard interface.
|
|
|
|
func (s *MeshManager) EnableInterface(meshId string) error {
|
|
|
|
err := s.configApplyer.ApplyConfig()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
meshNode, err := s.GetSelf(meshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh := s.GetMesh(meshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dev, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.interfaceManipulator.EnableInterface(dev.Name, meshNode.GetWgHost().String())
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublicKey: Gets the public key of the WireGuard mesh
|
|
|
|
func (s *MeshManager) GetPublicKey(meshId string) (*wgtypes.Key, error) {
|
|
|
|
mesh, ok := s.Meshes[meshId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("mesh does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
dev, err := mesh.GetDevice()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dev.PublicKey, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// to
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSelf adds this host to the mesh
|
|
|
|
func (s *MeshManager) AddSelf(params *AddSelfParams) error {
|
|
|
|
pubKey, err := s.GetPublicKey(params.MeshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeIP, err := s.ipAllocator.GetIP(*pubKey, params.MeshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
node := s.nodeFactory.Build(&MeshNodeFactoryParams{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
NodeIP: nodeIP,
|
|
|
|
WgPort: params.WgPort,
|
|
|
|
Endpoint: params.Endpoint,
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Meshes[params.MeshId].AddNode(node)
|
|
|
|
return s.RouteManager.UpdateRoutes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveMesh leaves the mesh network
|
|
|
|
func (s *MeshManager) LeaveMesh(meshId string) error {
|
|
|
|
_, exists := s.Meshes[meshId]
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return errors.New(fmt.Sprintf("mesh %s does not exist", meshId))
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now just delete the mesh with the ID.
|
|
|
|
delete(s.Meshes, meshId)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (s *MeshManager) GetSelf(meshId string) (MeshNode, error) {
|
|
|
|
meshInstance, ok := s.Meshes[meshId]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New(fmt.Sprintf("mesh %s does not exist", meshId))
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot, err := meshInstance.GetMesh()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
node, ok := snapshot.GetNodes()[s.HostParameters.HostEndpoint]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("the node doesn't exist in the mesh")
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTimeStamp updates the timestamp of this node in all meshes
|
|
|
|
func (s *MeshManager) UpdateTimeStamp() error {
|
|
|
|
for _, mesh := range s.Meshes {
|
|
|
|
err := mesh.UpdateTimeStamp(s.HostParameters.HostEndpoint)
|
|
|
|
|
|
|
|
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-10-26 17:53:12 +02:00
|
|
|
// Creates a new instance of a mesh manager with the given parameters
|
2023-10-28 17:38:25 +02:00
|
|
|
func NewMeshManager(params *NewMeshManagerParams) *MeshManager {
|
2023-10-26 17:53:12 +02:00
|
|
|
hostParams := HostParameters{}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
switch params.Conf.Endpoint {
|
2023-10-26 17:53:12 +02:00
|
|
|
case "":
|
2023-10-28 17:38:25 +02:00
|
|
|
hostParams.HostEndpoint = fmt.Sprintf("%s:%s", lib.GetOutboundIP().String(), params.Conf.GrpcPort)
|
2023-10-26 17:53:12 +02:00
|
|
|
default:
|
2023-10-28 17:38:25 +02:00
|
|
|
hostParams.HostEndpoint = fmt.Sprintf("%s:%s", params.Conf.Endpoint, params.Conf.GrpcPort)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logging.Log.WriteInfof("Endpoint %s", hostParams.HostEndpoint)
|
|
|
|
|
|
|
|
m := &MeshManager{
|
|
|
|
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
|
|
|
}
|
|
|
|
m.configApplyer = NewWgMeshConfigApplyer(m)
|
|
|
|
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-10-26 17:53:12 +02:00
|
|
|
return m
|
|
|
|
}
|