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

70 lines
1.8 KiB
Go
Raw Normal View History

package ctrlserver
import (
"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/mesh"
"github.com/tim-beatham/wgmesh/pkg/rpc"
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 {
WgClient *wgctrl.Client
Conf *conf.WgMeshConfiguration
AuthProvider rpc.AuthenticationServer
CtrlProvider rpc.MeshCtrlServerServer
SyncProvider rpc.SyncServiceServer
}
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)
ctrlServer.Client = params.WgClient
ctrlServer.MeshManager = mesh.NewMeshManager(*params.WgClient, *params.Conf)
2023-10-10 21:14:40 +02:00
ctrlServer.Conf = params.Conf
2023-10-24 01:12:38 +02:00
connManagerParams := conn.NewConnectionManageParams{
CertificatePath: params.Conf.CertificatePath,
PrivateKey: params.Conf.PrivateKeyPath,
SkipCertVerification: params.Conf.SkipCertVerification,
}
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,
AuthProvider: params.AuthProvider,
CtrlProvider: params.CtrlProvider,
SyncProvider: params.SyncProvider,
}
connServer, err := conn.NewConnectionServer(&connServerParams)
if err != nil {
return nil, err
}
ctrlServer.ConnectionServer = connServer
return ctrlServer, nil
}
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 {
return err
}
if err := s.ConnectionServer.Close(); err != nil {
return err
}
return nil
}