From f647c1b806814d658afae2ea2d4c9c8ec5b58169 Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Fri, 5 Jan 2024 16:59:02 +0000 Subject: [PATCH] 81-seperate-processes Prep for submission --- pkg/api/apiserver.go | 49 ++++++++++++++++++++++++++++++++++++++++-- pkg/api/types.go | 24 +++++++++++++++++++++ pkg/robin/requester.go | 20 +++++++++-------- 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/pkg/api/apiserver.go b/pkg/api/apiserver.go index d137bc0..d21d2ce 100644 --- a/pkg/api/apiserver.go +++ b/pkg/api/apiserver.go @@ -74,6 +74,25 @@ func (s *SmegServer) meshToAPIMesh(meshId string, nodes []ctrlserver.MeshNode) S 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 func (s *SmegServer) CreateMesh(c *gin.Context) { var createMesh CreateMeshRequest @@ -86,9 +105,15 @@ func (s *SmegServer) CreateMesh(c *gin.Context) { return } + fmt.Printf("%+v\n", createMesh) + ipcRequest := ipc.NewMeshArgs{ 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 } + if createMesh.Alias != "" { + s.putAlias(reply, createMesh.Alias) + } + + if createMesh.Description != "" { + s.putDescription(reply, createMesh.Description) + } + c.JSON(http.StatusOK, &gin.H{ "meshid": reply, }) @@ -123,7 +156,11 @@ func (s *SmegServer) JoinMesh(c *gin.Context) { MeshId: joinMesh.MeshId, IpAddress: joinMesh.Bootstrap, 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 } + if joinMesh.Alias != "" { + s.putAlias(reply, joinMesh.Alias) + } + + if joinMesh.Description != "" { + s.putDescription(reply, joinMesh.Description) + } + c.JSON(http.StatusOK, &gin.H{ "status": "success", }) diff --git a/pkg/api/types.go b/pkg/api/types.go index 6f22b7c..32125ab 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -71,6 +71,18 @@ type SmegMesh struct { type CreateMeshRequest struct { // WgPort is the WireGuard to create the mesh in 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 @@ -81,6 +93,18 @@ type JoinMeshRequest struct { Bootstrap string `json:"bootstrap" binding:"required"` // MeshId is the ID of the mesh to join 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 diff --git a/pkg/robin/requester.go b/pkg/robin/requester.go index c3cb592..775ac2e 100644 --- a/pkg/robin/requester.go +++ b/pkg/robin/requester.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "slices" "time" "github.com/tim-beatham/smegmesh/pkg/conf" @@ -51,7 +52,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error { }) if err != nil { - return err + return errors.New("could not create mesh") } err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{ @@ -61,7 +62,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error { }) if err != nil { - return err + return errors.New("could not create mesh") } *reply = meshId @@ -78,6 +79,7 @@ func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error { i++ } + slices.Sort(meshNames) *reply = ipc.ListMeshReply{Meshes: meshNames} return nil } @@ -93,19 +95,19 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error { peerConnection, err := n.Server.GetConnectionManager().GetConnection(args.IpAddress) if err != nil { - return err + return fmt.Errorf("could not join mesh %s", args.MeshId) } client, err := peerConnection.GetClient() if err != nil { - return err + return fmt.Errorf("could not join mesh %s", args.MeshId) } c := rpc.NewMeshCtrlServerClient(client) if err != nil { - return err + return fmt.Errorf("could not join mesh %s", args.MeshId) } 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}) if err != nil { - return err + return fmt.Errorf("could not join mesh %s", args.MeshId) } err = n.Server.GetMeshManager().AddMesh(&mesh.AddMeshParams{ @@ -127,7 +129,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error { }) if err != nil { - return err + return fmt.Errorf("could not join mesh %s", args.MeshId) } err = n.Server.GetMeshManager().AddSelf(&mesh.AddSelfParams{ @@ -137,7 +139,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error { }) if err != nil { - return err + return fmt.Errorf("could not join mesh %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) if err != nil { - return err + return fmt.Errorf("could not set alias: %s", args.Alias) } *reply = fmt.Sprintf("Set alias to %s", args.Alias)