1
0
forked from extern/smegmesh
smegmesh/pkg/ctrlserver/ctrlserver.go

124 lines
3.4 KiB
Go
Raw Normal View History

package ctrlserver
import (
crdt "github.com/tim-beatham/wgmesh/pkg/automerge"
"github.com/tim-beatham/wgmesh/pkg/conf"
2023-10-01 20:01:35 +02:00
"github.com/tim-beatham/wgmesh/pkg/conn"
"github.com/tim-beatham/wgmesh/pkg/ip"
"github.com/tim-beatham/wgmesh/pkg/lib"
logging "github.com/tim-beatham/wgmesh/pkg/log"
"github.com/tim-beatham/wgmesh/pkg/mesh"
"github.com/tim-beatham/wgmesh/pkg/query"
"github.com/tim-beatham/wgmesh/pkg/rpc"
"github.com/tim-beatham/wgmesh/pkg/wg"
2023-09-19 14:45:49 +02:00
"golang.zx2c4.com/wireguard/wgctrl"
)
2023-10-24 01:12:38 +02:00
// NewCtrlServerParams are the params requried to create a new ctrl server
type NewCtrlServerParams struct {
Conf *conf.WgMeshConfiguration
Client *wgctrl.Client
CtrlProvider rpc.MeshCtrlServerServer
SyncProvider rpc.SyncServiceServer
Querier query.Querier
}
2023-10-24 01:12:38 +02:00
// Create a new instance of the MeshCtrlServer or error if the
// operation failed
func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
ctrlServer := new(MeshCtrlServer)
meshFactory := crdt.CrdtProviderFactory{}
nodeFactory := crdt.MeshNodeFactory{
Config: *params.Conf,
}
idGenerator := &lib.UUIDGenerator{}
ipAllocator := &ip.ULABuilder{}
interfaceManipulator := wg.NewWgInterfaceManipulator(params.Client)
configApplyer := mesh.NewWgMeshConfigApplyer()
meshManagerParams := &mesh.NewMeshManagerParams{
Conf: *params.Conf,
Client: params.Client,
MeshProvider: &meshFactory,
NodeFactory: &nodeFactory,
IdGenerator: idGenerator,
IPAllocator: ipAllocator,
InterfaceManipulator: interfaceManipulator,
ConfigApplyer: configApplyer,
}
ctrlServer.MeshManager = mesh.NewMeshManager(meshManagerParams)
configApplyer.SetMeshManager(ctrlServer.MeshManager)
ctrlServer.Conf = params.Conf
2023-11-05 19:03:58 +01:00
connManagerParams := conn.NewConnectionManagerParams{
CertificatePath: params.Conf.CertificatePath,
PrivateKey: params.Conf.PrivateKeyPath,
SkipCertVerification: params.Conf.SkipCertVerification,
2023-10-27 18:49:18 +02:00
CaCert: params.Conf.CaCertificatePath,
2023-11-05 19:03:58 +01:00
ConnFactory: conn.NewWgCtrlConnection,
}
2023-10-24 01:12:38 +02:00
connMgr, err := conn.NewConnectionManager(&connManagerParams)
if err != nil {
return nil, err
}
ctrlServer.ConnectionManager = connMgr
connServerParams := conn.NewConnectionServerParams{
2023-10-10 21:14:40 +02:00
Conf: params.Conf,
CtrlProvider: params.CtrlProvider,
SyncProvider: params.SyncProvider,
}
connServer, err := conn.NewConnectionServer(&connServerParams)
if err != nil {
return nil, err
}
ctrlServer.Querier = query.NewJmesQuerier(ctrlServer.MeshManager)
ctrlServer.ConnectionServer = connServer
return ctrlServer, nil
}
2023-10-24 01:12:38 +02:00
2023-11-05 19:03:58 +01:00
func (s *MeshCtrlServer) GetConfiguration() *conf.WgMeshConfiguration {
return s.Conf
}
func (s *MeshCtrlServer) GetClient() *wgctrl.Client {
return s.Client
}
func (s *MeshCtrlServer) GetQuerier() query.Querier {
return s.Querier
}
func (s *MeshCtrlServer) GetMeshManager() mesh.MeshManager {
return s.MeshManager
}
func (s *MeshCtrlServer) GetConnectionManager() conn.ConnectionManager {
return s.ConnectionManager
}
2023-10-24 01:12:38 +02:00
// Close closes the ctrl server tearing down any connections that exist
func (s *MeshCtrlServer) Close() error {
if err := s.ConnectionManager.Close(); err != nil {
logging.Log.WriteErrorf(err.Error())
2023-10-24 01:12:38 +02:00
}
if err := s.MeshManager.Close(); err != nil {
logging.Log.WriteErrorf(err.Error())
}
2023-10-24 01:12:38 +02:00
if err := s.ConnectionServer.Close(); err != nil {
logging.Log.WriteErrorf(err.Error())
2023-10-24 01:12:38 +02:00
}
return nil
}