1
0
forked from extern/smegmesh
smegmesh/pkg/conn/connectionserver.go

109 lines
2.7 KiB
Go
Raw Normal View History

package conn
import (
"crypto/tls"
"net"
2023-10-10 21:14:40 +02:00
"github.com/tim-beatham/wgmesh/pkg/conf"
logging "github.com/tim-beatham/wgmesh/pkg/log"
"github.com/tim-beatham/wgmesh/pkg/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
2023-10-24 01:12:38 +02:00
// ConnectionServer manages gRPC server peer connections
type ConnectionServer struct {
2023-10-24 01:12:38 +02:00
// tlsConfiguration of the server
serverConfig *tls.Config
// server an instance of the grpc server
2023-11-05 19:03:58 +01:00
server *grpc.Server // the authentication service to authenticate nodes
2023-10-24 01:12:38 +02:00
// the ctrl service to manage node
ctrlProvider rpc.MeshCtrlServerServer
2023-10-24 01:12:38 +02:00
// the sync service to synchronise nodes
syncProvider rpc.SyncServiceServer
2023-10-10 21:14:40 +02:00
Conf *conf.WgMeshConfiguration
2023-10-24 01:12:38 +02:00
listener net.Listener
}
2023-10-24 01:12:38 +02:00
// NewConnectionServerParams contains params for creating a new connection server
type NewConnectionServerParams struct {
2023-10-10 21:14:40 +02:00
Conf *conf.WgMeshConfiguration
CtrlProvider rpc.MeshCtrlServerServer
SyncProvider rpc.SyncServiceServer
}
// NewConnectionServer: create a new gRPC connection server instance
func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer, error) {
2023-10-10 21:14:40 +02:00
cert, err := tls.LoadX509KeyPair(params.Conf.CertificatePath, params.Conf.PrivateKeyPath)
if err != nil {
2023-10-24 01:12:38 +02:00
logging.Log.WriteErrorf("Failed to load key pair: %s\n", err.Error())
return nil, err
}
serverAuth := tls.RequireAndVerifyClientCert
2023-10-10 21:14:40 +02:00
if params.Conf.SkipCertVerification {
serverAuth = tls.RequireAnyClientCert
}
serverConfig := &tls.Config{
ClientAuth: serverAuth,
Certificates: []tls.Certificate{cert},
}
server := grpc.NewServer(
grpc.Creds(credentials.NewTLS(serverConfig)),
)
ctrlProvider := params.CtrlProvider
syncProvider := params.SyncProvider
connServer := ConnectionServer{
2023-10-24 01:12:38 +02:00
serverConfig: serverConfig,
server: server,
ctrlProvider: ctrlProvider,
syncProvider: syncProvider,
Conf: params.Conf,
}
return &connServer, nil
}
2023-10-24 01:12:38 +02:00
// Listen for incoming requests. Returns an error if something went wrong.
func (s *ConnectionServer) Listen() error {
rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider)
rpc.RegisterSyncServiceServer(s.server, s.syncProvider)
lis, err := net.Listen("tcp", ":"+s.Conf.GrpcPort)
2023-10-24 01:12:38 +02:00
s.listener = lis
2023-10-24 01:12:38 +02:00
logging.Log.WriteInfof("GRPC listening on %s\n", s.Conf.GrpcPort)
if err != nil {
2023-10-24 01:12:38 +02:00
logging.Log.WriteErrorf(err.Error())
return err
}
if err := s.server.Serve(lis); err != nil {
2023-10-24 01:12:38 +02:00
logging.Log.WriteErrorf(err.Error())
return err
}
return nil
}
2023-10-24 01:12:38 +02:00
// Close closes the connection server. Returns an error
// if something went wrong whilst attempting to close the connection
func (c *ConnectionServer) Close() error {
var err error = nil
c.server.Stop()
if c.listener != nil {
err = c.listener.Close()
}
return err
}