2023-09-29 16:00:20 +02:00
|
|
|
package robin
|
2023-09-18 18:00:43 +02:00
|
|
|
|
|
|
|
import (
|
2023-09-19 19:29:35 +02:00
|
|
|
"context"
|
2023-09-18 18:00:43 +02:00
|
|
|
"errors"
|
2023-10-24 17:00:46 +02:00
|
|
|
"fmt"
|
2023-09-20 00:50:44 +02:00
|
|
|
"strconv"
|
2023-09-19 19:29:35 +02:00
|
|
|
"time"
|
2023-09-18 18:00:43 +02:00
|
|
|
|
2023-09-19 14:45:49 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
2023-10-05 18:48:54 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
2023-09-20 00:50:44 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
2023-10-22 14:34:49 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
type IpcHandler struct {
|
2023-10-05 18:48:54 +02:00
|
|
|
Server *ctrlserver.MeshCtrlServer
|
|
|
|
ipAllocator ip.IPAllocator
|
2023-09-18 18:00:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
|
2023-10-24 17:00:46 +02:00
|
|
|
meshId, err := n.Server.MeshManager.CreateMesh(args.IfName, args.WgPort)
|
2023-10-21 19:08:45 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-05 18:48:54 +02:00
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
err = n.Server.MeshManager.AddSelf(&mesh.AddSelfParams{
|
|
|
|
MeshId: meshId,
|
|
|
|
WgPort: args.WgPort,
|
|
|
|
Endpoint: args.Endpoint,
|
|
|
|
})
|
2023-10-30 17:49:02 +01:00
|
|
|
|
2023-10-06 11:12:46 +02:00
|
|
|
*reply = meshId
|
2023-10-30 17:49:02 +01:00
|
|
|
return err
|
2023-09-18 18:00:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
|
2023-10-06 19:25:38 +02:00
|
|
|
meshNames := make([]string, len(n.Server.MeshManager.Meshes))
|
|
|
|
|
|
|
|
i := 0
|
2023-10-26 17:53:12 +02:00
|
|
|
for meshId, _ := range n.Server.MeshManager.Meshes {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) JoinMesh(args ipc.JoinMeshArgs, reply *string) error {
|
2023-10-06 19:25:38 +02:00
|
|
|
peerConnection, err := n.Server.ConnectionManager.GetConnection(args.IpAdress)
|
2023-10-01 21:14:09 +02:00
|
|
|
|
2023-10-30 17:49:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
client, err := peerConnection.GetClient()
|
2023-09-19 19:29:35 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-02 17:03:41 +02:00
|
|
|
c := rpc.NewMeshCtrlServerClient(client)
|
2023-09-19 19:29:35 +02:00
|
|
|
|
2023-10-01 21:14:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-24 01:12:38 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
2023-09-19 19:29:35 +02:00
|
|
|
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-10-28 17:38:25 +02:00
|
|
|
err = n.Server.MeshManager.AddMesh(&mesh.AddMeshParams{
|
|
|
|
MeshId: args.MeshId,
|
|
|
|
DevName: args.IfName,
|
|
|
|
WgPort: args.Port,
|
|
|
|
MeshBytes: meshReply.Mesh,
|
|
|
|
})
|
2023-10-06 12:52:51 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
err = n.Server.MeshManager.AddSelf(&mesh.AddSelfParams{
|
|
|
|
MeshId: args.MeshId,
|
|
|
|
WgPort: args.Port,
|
|
|
|
Endpoint: args.Endpoint,
|
|
|
|
})
|
2023-09-20 00:50:44 +02:00
|
|
|
|
2023-10-06 12:52:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
*reply = strconv.FormatBool(true)
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-21 14:50:59 +02:00
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
// LeaveMesh leaves a mesh network
|
|
|
|
func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error {
|
|
|
|
err := n.Server.MeshManager.LeaveMesh(meshId)
|
2023-10-06 12:52:51 +02:00
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
if err == nil {
|
|
|
|
*reply = fmt.Sprintf("Left Mesh %s", meshId)
|
2023-10-27 18:49:18 +02:00
|
|
|
}
|
2023-10-28 17:38:25 +02:00
|
|
|
|
|
|
|
return err
|
2023-09-20 00:50:44 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
|
2023-10-06 12:52:51 +02:00
|
|
|
mesh := n.Server.MeshManager.GetMesh(meshId)
|
2023-10-26 17:53:12 +02:00
|
|
|
meshSnapshot, err := mesh.GetMesh()
|
2023-10-06 12:52:51 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-20 00:50:44 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if mesh == nil {
|
|
|
|
return errors.New("mesh does not exist")
|
|
|
|
}
|
|
|
|
nodes := make([]ctrlserver.MeshNode, len(meshSnapshot.GetNodes()))
|
2023-09-20 00:50:44 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
i := 0
|
|
|
|
for _, node := range meshSnapshot.GetNodes() {
|
|
|
|
pubKey, _ := node.GetPublicKey()
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-09-20 00:50:44 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
node := ctrlserver.MeshNode{
|
|
|
|
HostEndpoint: node.GetHostEndpoint(),
|
|
|
|
WgEndpoint: node.GetWgEndpoint(),
|
|
|
|
PublicKey: pubKey.String(),
|
|
|
|
WgHost: node.GetWgHost().String(),
|
|
|
|
Timestamp: node.GetTimeStamp(),
|
|
|
|
Routes: node.GetRoutes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[i] = node
|
|
|
|
i += 1
|
2023-09-20 00:50:44 +02:00
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
*reply = ipc.GetMeshReply{Nodes: nodes}
|
2023-09-19 19:29:35 +02:00
|
|
|
return nil
|
2023-09-19 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) EnableInterface(meshId string, reply *string) error {
|
2023-10-06 12:52:51 +02:00
|
|
|
err := n.Server.MeshManager.EnableInterface(meshId)
|
2023-09-21 19:43:29 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-06 12:52:51 +02:00
|
|
|
*reply = err.Error()
|
2023-09-21 19:43:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = "up"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (n *IpcHandler) GetDOT(meshId string, reply *string) error {
|
2023-10-22 14:34:49 +02:00
|
|
|
g := mesh.NewMeshDotConverter(n.Server.MeshManager)
|
|
|
|
|
|
|
|
result, err := g.Generate(meshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = result
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:48:54 +02:00
|
|
|
type RobinIpcParams struct {
|
|
|
|
CtrlServer *ctrlserver.MeshCtrlServer
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func NewRobinIpc(ipcParams RobinIpcParams) IpcHandler {
|
|
|
|
return IpcHandler{
|
2023-10-28 17:38:25 +02:00
|
|
|
Server: ipcParams.CtrlServer,
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
2023-09-18 18:00:43 +02:00
|
|
|
}
|