1
0
forked from extern/smegmesh
smegmesh/pkg/robin/requester.go

264 lines
5.8 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/smegmesh/pkg/conf"
"github.com/tim-beatham/smegmesh/pkg/ctrlserver"
"github.com/tim-beatham/smegmesh/pkg/ipc"
"github.com/tim-beatham/smegmesh/pkg/mesh"
"github.com/tim-beatham/smegmesh/pkg/rpc"
2023-09-18 18:00:43 +02:00
)
// IpcHandler: represents a handler for ipc calls
type IpcHandler struct {
2023-11-05 19:03:58 +01:00
Server ctrlserver.CtrlServer
2023-09-18 18:00:43 +02:00
}
// getOverrideConfiguration: override any specific WireGuard configuration
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
}
// CreateMesh: create a new mesh network
func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
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
}
// ListMeshes: list mesh networks
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
}
// JoinMesh: join a mesh network
func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
overrideConf := getOverrideConfiguration(&args.WgArgs)
if n.Server.GetMeshManager().GetMesh(args.MeshId) != nil {
return fmt.Errorf("user is already apart of the mesh")
}
peerConnection, err := n.Server.GetConnectionManager().GetConnection(args.IpAddress)
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
}
// GetMesh: get a mesh network at the given meshid
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
}
// Query: perform a jmespath query
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
}
// PutDescription: change your description in the mesh
func (n *IpcHandler) PutDescription(args ipc.PutDescriptionArgs, reply *string) error {
err := n.Server.GetMeshManager().SetDescription(args.MeshId, args.Description)
if err != nil {
return err
}
*reply = fmt.Sprintf("set description to %s for %s", args.Description, args.MeshId)
return nil
}
// PutAlias: put your aliasin the mesh
func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error {
if args.Alias == "" {
return fmt.Errorf("alias not provided")
}
err := n.Server.GetMeshManager().SetAlias(args.MeshId, args.Alias)
if err != nil {
return err
}
*reply = fmt.Sprintf("Set alias to %s", args.Alias)
return nil
}
// PutService: place a service in the mesh
func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error {
err := n.Server.GetMeshManager().SetService(service.MeshId, service.Service, service.Value)
if err != nil {
return err
}
*reply = "success"
return nil
}
// DeleteService: withtract a service in the mesh
func (n *IpcHandler) DeleteService(service ipc.DeleteServiceArgs, reply *string) error {
err := n.Server.GetMeshManager().RemoveService(service.MeshId, service.Service)
if err != nil {
return err
}
*reply = "success"
return nil
}
// RobinIpcParams: parameters required to construct a new mesh network
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
}