2023-09-18 16:52:28 +02:00
|
|
|
package ctrlserver
|
|
|
|
|
|
|
|
import (
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/conn"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/crdt"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ip"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/lib"
|
|
|
|
logging "github.com/tim-beatham/smegmesh/pkg/log"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/mesh"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/query"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/rpc"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/wg"
|
2023-09-19 14:45:49 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2023-09-18 16:52:28 +02:00
|
|
|
)
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
// NewCtrlServerParams are the params requried to create a new ctrl server
|
2023-10-02 17:03:41 +02:00
|
|
|
type NewCtrlServerParams struct {
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf *conf.DaemonConfiguration
|
2023-10-24 17:00:46 +02:00
|
|
|
Client *wgctrl.Client
|
2023-10-02 17:03:41 +02:00
|
|
|
CtrlProvider rpc.MeshCtrlServerServer
|
2023-10-20 13:41:06 +02:00
|
|
|
SyncProvider rpc.SyncServiceServer
|
2023-10-30 20:02:28 +01:00
|
|
|
Querier query.Querier
|
2023-11-28 15:42:09 +01:00
|
|
|
OnDelete func(mesh.MeshProvider)
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
// Create a new instance of the MeshCtrlServer or error if the
|
|
|
|
// operation failed
|
2023-10-02 17:03:41 +02:00
|
|
|
func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
2023-09-18 16:52:28 +02:00
|
|
|
ctrlServer := new(MeshCtrlServer)
|
2023-12-10 20:21:54 +01:00
|
|
|
meshFactory := &crdt.TwoPhaseMapFactory{
|
|
|
|
Config: params.Conf,
|
|
|
|
}
|
2023-11-30 03:02:38 +01:00
|
|
|
nodeFactory := &crdt.MeshNodeFactory{
|
2023-10-28 17:38:25 +02:00
|
|
|
Config: *params.Conf,
|
|
|
|
}
|
2023-12-30 23:26:08 +01:00
|
|
|
idGenerator := &lib.ShortIDGenerator{}
|
2023-10-28 17:38:25 +02:00
|
|
|
ipAllocator := &ip.ULABuilder{}
|
|
|
|
interfaceManipulator := wg.NewWgInterfaceManipulator(params.Client)
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
configApplyer := mesh.NewWgMeshConfigApplyer()
|
2023-11-06 10:54:06 +01:00
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
meshManagerParams := &mesh.NewMeshManagerParams{
|
|
|
|
Conf: *params.Conf,
|
|
|
|
Client: params.Client,
|
2023-11-30 03:02:38 +01:00
|
|
|
MeshProvider: meshFactory,
|
|
|
|
NodeFactory: nodeFactory,
|
2023-10-28 17:38:25 +02:00
|
|
|
IdGenerator: idGenerator,
|
|
|
|
IPAllocator: ipAllocator,
|
|
|
|
InterfaceManipulator: interfaceManipulator,
|
2023-11-06 10:54:06 +01:00
|
|
|
ConfigApplyer: configApplyer,
|
2023-11-28 15:42:09 +01:00
|
|
|
OnDelete: params.OnDelete,
|
2023-10-28 17:38:25 +02:00
|
|
|
}
|
2023-11-06 10:54:06 +01:00
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
ctrlServer.MeshManager = mesh.NewMeshManager(meshManagerParams)
|
2023-11-07 20:48:53 +01:00
|
|
|
configApplyer.SetMeshManager(ctrlServer.MeshManager)
|
2023-10-28 17:38:25 +02:00
|
|
|
|
|
|
|
ctrlServer.Conf = params.Conf
|
2023-11-05 19:03:58 +01:00
|
|
|
connManagerParams := conn.NewConnectionManagerParams{
|
2023-10-02 17:03:41 +02:00
|
|
|
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-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
connMgr, err := conn.NewConnectionManager(&connManagerParams)
|
2023-10-02 17:03:41 +02:00
|
|
|
|
|
|
|
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,
|
2023-10-20 13:41:06 +02:00
|
|
|
SyncProvider: params.SyncProvider,
|
2023-10-02 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
connServer, err := conn.NewConnectionServer(&connServerParams)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:02:28 +01:00
|
|
|
ctrlServer.Querier = query.NewJmesQuerier(ctrlServer.MeshManager)
|
2023-10-02 17:03:41 +02:00
|
|
|
ctrlServer.ConnectionServer = connServer
|
2023-10-30 20:02:28 +01:00
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
return ctrlServer, nil
|
2023-09-18 16:52:28 +02:00
|
|
|
}
|
2023-10-24 01:12:38 +02:00
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
func (s *MeshCtrlServer) GetConfiguration() *conf.DaemonConfiguration {
|
2023-11-05 19:03:58 +01:00
|
|
|
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 {
|
2023-11-01 12:58:10 +01:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
2023-10-24 01:12:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:48:53 +01: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 {
|
2023-11-01 12:58:10 +01:00
|
|
|
logging.Log.WriteErrorf(err.Error())
|
2023-10-24 01:12:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|