mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-01-25 06:48:35 +01:00
Merge pull request #84 from tim-beatham/81-separate-synchronisation-into-independent-processes
81-seperate-processes
This commit is contained in:
commit
b294f116a2
@ -74,6 +74,25 @@ func (s *SmegServer) meshToAPIMesh(meshId string, nodes []ctrlserver.MeshNode) S
|
|||||||
return smegMesh
|
return smegMesh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// putAlias: place an alias in the mesh
|
||||||
|
func (s *SmegServer) putAlias(meshId, alias string) error {
|
||||||
|
var reply string
|
||||||
|
|
||||||
|
return s.client.PutAlias(ipc.PutAliasArgs{
|
||||||
|
Alias: alias,
|
||||||
|
MeshId: meshId,
|
||||||
|
}, &reply)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SmegServer) putDescription(meshId, description string) error {
|
||||||
|
var reply string
|
||||||
|
|
||||||
|
return s.client.PutDescription(ipc.PutDescriptionArgs{
|
||||||
|
Description: description,
|
||||||
|
MeshId: meshId,
|
||||||
|
}, &reply)
|
||||||
|
}
|
||||||
|
|
||||||
// CreateMesh: creates a mesh network
|
// CreateMesh: creates a mesh network
|
||||||
func (s *SmegServer) CreateMesh(c *gin.Context) {
|
func (s *SmegServer) CreateMesh(c *gin.Context) {
|
||||||
var createMesh CreateMeshRequest
|
var createMesh CreateMeshRequest
|
||||||
@ -86,9 +105,15 @@ func (s *SmegServer) CreateMesh(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%+v\n", createMesh)
|
||||||
|
|
||||||
ipcRequest := ipc.NewMeshArgs{
|
ipcRequest := ipc.NewMeshArgs{
|
||||||
WgArgs: ipc.WireGuardArgs{
|
WgArgs: ipc.WireGuardArgs{
|
||||||
WgPort: createMesh.WgPort,
|
WgPort: createMesh.WgPort,
|
||||||
|
Role: createMesh.Role,
|
||||||
|
Endpoint: createMesh.PublicEndpoint,
|
||||||
|
AdvertiseRoutes: createMesh.AdvertiseRoutes,
|
||||||
|
AdvertiseDefaultRoute: createMesh.AdvertiseDefaults,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +128,14 @@ func (s *SmegServer) CreateMesh(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if createMesh.Alias != "" {
|
||||||
|
s.putAlias(reply, createMesh.Alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
if createMesh.Description != "" {
|
||||||
|
s.putDescription(reply, createMesh.Description)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, &gin.H{
|
c.JSON(http.StatusOK, &gin.H{
|
||||||
"meshid": reply,
|
"meshid": reply,
|
||||||
})
|
})
|
||||||
@ -124,6 +157,10 @@ func (s *SmegServer) JoinMesh(c *gin.Context) {
|
|||||||
IpAddress: joinMesh.Bootstrap,
|
IpAddress: joinMesh.Bootstrap,
|
||||||
WgArgs: ipc.WireGuardArgs{
|
WgArgs: ipc.WireGuardArgs{
|
||||||
WgPort: joinMesh.WgPort,
|
WgPort: joinMesh.WgPort,
|
||||||
|
Endpoint: joinMesh.PublicEndpoint,
|
||||||
|
Role: joinMesh.Role,
|
||||||
|
AdvertiseRoutes: joinMesh.AdvertiseRoutes,
|
||||||
|
AdvertiseDefaultRoute: joinMesh.AdvertiseDefaults,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +175,14 @@ func (s *SmegServer) JoinMesh(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if joinMesh.Alias != "" {
|
||||||
|
s.putAlias(reply, joinMesh.Alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
if joinMesh.Description != "" {
|
||||||
|
s.putDescription(reply, joinMesh.Description)
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, &gin.H{
|
c.JSON(http.StatusOK, &gin.H{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
})
|
})
|
||||||
|
@ -71,6 +71,18 @@ type SmegMesh struct {
|
|||||||
type CreateMeshRequest struct {
|
type CreateMeshRequest struct {
|
||||||
// WgPort is the WireGuard to create the mesh in
|
// WgPort is the WireGuard to create the mesh in
|
||||||
WgPort int `json:"port" binding:"omitempty,gte=1024,lt=65535"`
|
WgPort int `json:"port" binding:"omitempty,gte=1024,lt=65535"`
|
||||||
|
// Role is the role to take on in the mesh
|
||||||
|
Role string `json:"role" binding:"required,eq=client|eq=peer"`
|
||||||
|
// AdvertiseRoutes: advertise thi mesh to other meshes
|
||||||
|
AdvertiseRoutes bool `json:"advertiseRoutes"`
|
||||||
|
// AdvertiseDefaults: advertise an exit point
|
||||||
|
AdvertiseDefaults bool `json:"advertiseDefaults"`
|
||||||
|
// Alias: alias of the node in the mesh
|
||||||
|
Alias string `json:"alias"`
|
||||||
|
// Description: description of the node in the mesh
|
||||||
|
Description string `json:"description"`
|
||||||
|
// PublicEndpoint: an alternative public endpoint to advertise
|
||||||
|
PublicEndpoint string `json:"publicEndpoint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// JoinMeshRequests encapsulates a request to create a mesh network
|
// JoinMeshRequests encapsulates a request to create a mesh network
|
||||||
@ -81,6 +93,18 @@ type JoinMeshRequest struct {
|
|||||||
Bootstrap string `json:"bootstrap" binding:"required"`
|
Bootstrap string `json:"bootstrap" binding:"required"`
|
||||||
// MeshId is the ID of the mesh to join
|
// MeshId is the ID of the mesh to join
|
||||||
MeshId string `json:"meshid" binding:"required"`
|
MeshId string `json:"meshid" binding:"required"`
|
||||||
|
// Role is the role to take on in the mesh
|
||||||
|
Role string `json:"role" binding:"required,eq=client|eq=peer"`
|
||||||
|
// AdvertiseRoutes: advertise thi mesh to other meshes
|
||||||
|
AdvertiseRoutes bool `json:"advertiseRoutes"`
|
||||||
|
// AdvertiseDefaults: advertise an exit point
|
||||||
|
AdvertiseDefaults bool `json:"advertiseDefaults"`
|
||||||
|
// Alias: alias of the node in the mesh
|
||||||
|
Alias string `json:"alias"`
|
||||||
|
// Description: description of the node in the mesh
|
||||||
|
Description string `json:"description"`
|
||||||
|
// PublicEndpoint: an alternative public endpoint to advertise
|
||||||
|
PublicEndpoint string `json:"publicEndpoint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiServerConf configuration to instantiate the API server
|
// ApiServerConf configuration to instantiate the API server
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tim-beatham/smegmesh/pkg/conf"
|
"github.com/tim-beatham/smegmesh/pkg/conf"
|
||||||
@ -51,7 +52,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.New("could not create mesh")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
|
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
|
||||||
@ -61,7 +62,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.New("could not create mesh")
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = meshId
|
*reply = meshId
|
||||||
@ -78,6 +79,7 @@ func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
|
|||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slices.Sort(meshNames)
|
||||||
*reply = ipc.ListMeshReply{Meshes: meshNames}
|
*reply = ipc.ListMeshReply{Meshes: meshNames}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -93,19 +95,19 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
|
|||||||
peerConnection, err := n.Server.GetConnectionManager().GetConnection(args.IpAddress)
|
peerConnection, err := n.Server.GetConnectionManager().GetConnection(args.IpAddress)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := peerConnection.GetClient()
|
client, err := peerConnection.GetClient()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
c := rpc.NewMeshCtrlServerClient(client)
|
c := rpc.NewMeshCtrlServerClient(client)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration := n.Server.GetConfiguration()
|
configuration := n.Server.GetConfiguration()
|
||||||
@ -116,7 +118,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
|
|||||||
meshReply, err := c.GetMesh(ctx, &rpc.GetMeshRequest{MeshId: args.MeshId})
|
meshReply, err := c.GetMesh(ctx, &rpc.GetMeshRequest{MeshId: args.MeshId})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = n.Server.GetMeshManager().AddMesh(&mesh.AddMeshParams{
|
err = n.Server.GetMeshManager().AddMesh(&mesh.AddMeshParams{
|
||||||
@ -127,7 +129,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
|
err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{
|
||||||
@ -137,7 +139,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not join mesh %s", args.MeshId)
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = fmt.Sprintf("Successfully Joined: %s", args.MeshId)
|
*reply = fmt.Sprintf("Successfully Joined: %s", args.MeshId)
|
||||||
@ -219,7 +221,7 @@ func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error {
|
|||||||
err := n.Server.GetMeshManager().SetAlias(args.MeshId, args.Alias)
|
err := n.Server.GetMeshManager().SetAlias(args.MeshId, args.Alias)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not set alias: %s", args.Alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = fmt.Sprintf("Set alias to %s", args.Alias)
|
*reply = fmt.Sprintf("Set alias to %s", args.Alias)
|
||||||
|
Loading…
Reference in New Issue
Block a user