smegmesh/pkg/robin/requester.go

249 lines
5.1 KiB
Go
Raw Normal View History

2023-09-29 16:00:20 +02:00
package robin
2023-09-18 18:00:43 +02:00
import (
"context"
2023-09-18 18:00:43 +02:00
"errors"
"fmt"
2023-09-20 00:50:44 +02:00
"strconv"
"time"
2023-09-18 18:00:43 +02:00
"github.com/tim-beatham/wgmesh/pkg/conf"
2023-09-19 14:45:49 +02:00
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
2023-09-20 00:50:44 +02:00
"github.com/tim-beatham/wgmesh/pkg/ipc"
"github.com/tim-beatham/wgmesh/pkg/mesh"
2023-09-29 16:00:20 +02:00
"github.com/tim-beatham/wgmesh/pkg/rpc"
2023-09-18 18:00:43 +02:00
)
type IpcHandler struct {
2023-11-05 19:03:58 +01:00
Server ctrlserver.CtrlServer
2023-09-18 18:00:43 +02:00
}
func getOverrideConfiguration(args *ipc.WireGuardArgs) conf.WgConfiguration {
overrideConf := conf.WgConfiguration{}
if args.Role != "" {
role := conf.NodeType(args.Role)
overrideConf.Role = &role
}
if args.Endpoint != "" {
overrideConf.Endpoint = &args.Endpoint
}
if args.KeepAliveWg != 0 {
keepAliveWg := args.KeepAliveWg
overrideConf.KeepAliveWg = &keepAliveWg
}
overrideConf.AdvertiseRoutes = &args.AdvertiseRoutes
overrideConf.AdvertiseDefaultRoute = &args.AdvertiseDefaultRoute
return overrideConf
}
func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
2023-12-10 23:31:24 +01:00
if overrideConf.Role != nil && *overrideConf.Role == conf.CLIENT_ROLE {
return fmt.Errorf("cannot create a mesh with no public endpoint")
}
meshId, err := n.Server.GetMeshManager().CreateMesh(&mesh.CreateMeshParams{
Port: args.WgArgs.WgPort,
Conf: &overrideConf,
})
if err != nil {
return err
}
2023-10-05 18:48:54 +02:00
2023-11-05 19:03:58 +01:00
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
MeshId: meshId,
WgPort: args.WgArgs.WgPort,
Endpoint: args.WgArgs.Endpoint,
})
if err != nil {
return err
}
2023-10-06 11:12:46 +02:00
*reply = meshId
return err
2023-09-18 18:00:43 +02:00
}
func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
2023-11-05 19:03:58 +01:00
meshNames := make([]string, len(n.Server.GetMeshManager().GetMeshes()))
2023-10-06 19:25:38 +02:00
i := 0
for meshId := range n.Server.GetMeshManager().GetMeshes() {
meshNames[i] = meshId
2023-10-06 19:25:38 +02:00
i++
}
2023-09-20 15:34:34 +02:00
2023-10-06 19:25:38 +02:00
*reply = ipc.ListMeshReply{Meshes: meshNames}
2023-09-20 15:34:34 +02:00
return nil
}
func (n *IpcHandler) JoinMesh(args ipc.JoinMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
2023-11-05 19:03:58 +01:00
peerConnection, err := n.Server.GetConnectionManager().GetConnection(args.IpAdress)
2023-10-01 21:14:09 +02:00
if err != nil {
return err
}
client, err := peerConnection.GetClient()
if err != nil {
return err
}
c := rpc.NewMeshCtrlServerClient(client)
2023-10-01 21:14:09 +02:00
if err != nil {
return err
}
configuration := n.Server.GetConfiguration()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(configuration.Timeout))
defer cancel()
2023-10-06 12:52:51 +02:00
meshReply, err := c.GetMesh(ctx, &rpc.GetMeshRequest{MeshId: args.MeshId})
if err != nil {
return err
}
2023-11-05 19:03:58 +01:00
err = n.Server.GetMeshManager().AddMesh(&mesh.AddMeshParams{
MeshId: args.MeshId,
WgPort: args.WgArgs.WgPort,
MeshBytes: meshReply.Mesh,
Conf: &overrideConf,
})
2023-10-06 12:52:51 +02:00
if err != nil {
return err
}
2023-11-05 19:03:58 +01:00
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
MeshId: args.MeshId,
WgPort: args.WgArgs.WgPort,
Endpoint: args.WgArgs.Endpoint,
})
2023-09-20 00:50:44 +02:00
2023-10-06 12:52:51 +02:00
if err != nil {
return err
}
*reply = strconv.FormatBool(true)
return nil
}
// LeaveMesh leaves a mesh network
func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error {
2023-11-05 19:03:58 +01:00
err := n.Server.GetMeshManager().LeaveMesh(meshId)
2023-10-06 12:52:51 +02:00
if err == nil {
*reply = fmt.Sprintf("Left Mesh %s", meshId)
2023-10-27 18:49:18 +02:00
}
return err
2023-09-20 00:50:44 +02:00
}
func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
theMesh := n.Server.GetMeshManager().GetMesh(meshId)
if theMesh == nil {
return fmt.Errorf("mesh %s does not exist", meshId)
}
meshSnapshot, err := theMesh.GetMesh()
2023-10-06 12:52:51 +02:00
if err != nil {
return err
}
2023-09-20 00:50:44 +02:00
if theMesh == nil {
return errors.New("mesh does not exist")
}
nodes := make([]ctrlserver.MeshNode, len(meshSnapshot.GetNodes()))
2023-09-20 00:50:44 +02:00
i := 0
for _, node := range meshSnapshot.GetNodes() {
node := ctrlserver.NewCtrlNode(theMesh, node)
nodes[i] = *node
i += 1
2023-09-20 00:50:44 +02:00
}
*reply = ipc.GetMeshReply{Nodes: nodes}
return nil
2023-09-19 14:45:49 +02:00
}
func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error {
2023-11-05 19:03:58 +01:00
queryResponse, err := n.Server.GetQuerier().Query(params.MeshId, params.Query)
if err != nil {
return err
}
*reply = string(queryResponse)
return nil
}
func (n *IpcHandler) PutDescription(description string, reply *string) error {
2023-11-05 19:03:58 +01:00
err := n.Server.GetMeshManager().SetDescription(description)
if err != nil {
return err
}
*reply = fmt.Sprintf("Set description to %s", description)
return nil
}
func (n *IpcHandler) PutAlias(alias string, reply *string) error {
err := n.Server.GetMeshManager().SetAlias(alias)
if err != nil {
return err
}
*reply = fmt.Sprintf("Set alias to %s", alias)
return nil
}
func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error {
err := n.Server.GetMeshManager().SetService(service.Service, service.Value)
if err != nil {
return err
}
*reply = "success"
return nil
}
func (n *IpcHandler) DeleteService(service string, reply *string) error {
err := n.Server.GetMeshManager().RemoveService(service)
if err != nil {
return err
}
*reply = "success"
return nil
}
2023-10-05 18:48:54 +02:00
type RobinIpcParams struct {
2023-11-05 19:03:58 +01:00
CtrlServer ctrlserver.CtrlServer
2023-10-05 18:48:54 +02:00
}
func NewRobinIpc(ipcParams RobinIpcParams) IpcHandler {
return IpcHandler{
Server: ipcParams.CtrlServer,
2023-10-05 18:48:54 +02:00
}
2023-09-18 18:00:43 +02:00
}