2023-09-29 16:00:20 +02:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
2024-01-04 14:15:29 +01:00
|
|
|
ipcRPC "net/rpc"
|
2023-09-29 16:00:20 +02:00
|
|
|
"os"
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ctrlserver"
|
2023-09-29 16:00:20 +02:00
|
|
|
)
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
const SockAddr = "/tmp/smeg.sock"
|
2023-12-31 15:25:06 +01:00
|
|
|
|
|
|
|
type MeshIpc interface {
|
|
|
|
CreateMesh(args *NewMeshArgs, reply *string) error
|
|
|
|
ListMeshes(name string, reply *ListMeshReply) error
|
|
|
|
JoinMesh(args *JoinMeshArgs, reply *string) error
|
|
|
|
LeaveMesh(meshId string, reply *string) error
|
|
|
|
GetMesh(meshId string, reply *GetMeshReply) error
|
|
|
|
Query(query QueryMesh, reply *string) error
|
|
|
|
PutDescription(args PutDescriptionArgs, reply *string) error
|
|
|
|
PutAlias(args PutAliasArgs, reply *string) error
|
|
|
|
PutService(args PutServiceArgs, reply *string) error
|
|
|
|
DeleteService(args DeleteServiceArgs, reply *string) error
|
|
|
|
}
|
|
|
|
|
2023-12-12 12:58:47 +01:00
|
|
|
// WireGuardArgs are provided args specific to WireGuard
|
|
|
|
type WireGuardArgs struct {
|
2023-10-28 17:38:25 +02:00
|
|
|
// WgPort is the WireGuard port to expose
|
2023-10-24 17:00:46 +02:00
|
|
|
WgPort int
|
2023-12-12 12:58:47 +01:00
|
|
|
// KeepAliveWg is the number of seconds to keep alive
|
|
|
|
// for WireGuard NAT/firewall traversal
|
|
|
|
KeepAliveWg int
|
|
|
|
// AdvertiseRoutes whether or not to advertise routes to and from the
|
|
|
|
// mesh network
|
|
|
|
AdvertiseRoutes bool
|
|
|
|
// AdvertiseDefaultRoute whether or not to advertise the default route
|
|
|
|
// into the mesh network
|
|
|
|
AdvertiseDefaultRoute bool
|
2023-10-28 17:38:25 +02:00
|
|
|
// Endpoint is the routable alias of the machine. Can be an IP
|
|
|
|
// or DNS entry
|
|
|
|
Endpoint string
|
2023-12-12 12:58:47 +01:00
|
|
|
// Role is the role of the individual in the mesh
|
|
|
|
Role string
|
|
|
|
}
|
|
|
|
|
|
|
|
type NewMeshArgs struct {
|
|
|
|
// WgArgs are specific WireGuard args to use
|
|
|
|
WgArgs WireGuardArgs
|
2023-10-24 17:00:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-29 16:00:20 +02:00
|
|
|
type JoinMeshArgs struct {
|
2023-10-28 17:38:25 +02:00
|
|
|
// MeshId is the ID of the mesh to join
|
|
|
|
MeshId string
|
|
|
|
// IpAddress is a routable IP in another mesh
|
2023-12-31 15:25:06 +01:00
|
|
|
IpAddress string
|
2023-12-12 12:58:47 +01:00
|
|
|
// WgArgs is the WireGuard parameters to use.
|
|
|
|
WgArgs WireGuardArgs
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutServiceArgs: args to place a service into the data store
|
2023-11-17 23:13:51 +01:00
|
|
|
type PutServiceArgs struct {
|
|
|
|
Service string
|
|
|
|
Value string
|
2023-12-31 15:25:06 +01:00
|
|
|
MeshId string
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// DeleteServiceArgs: args to remove a service from the data store
|
2023-12-31 15:25:06 +01:00
|
|
|
type DeleteServiceArgs struct {
|
|
|
|
Service string
|
|
|
|
MeshId string
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutAliasArgs: args to assign an alias to a node
|
2023-12-31 15:25:06 +01:00
|
|
|
type PutAliasArgs struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// Alias: represents the alias of the node
|
2024-01-04 14:15:29 +01:00
|
|
|
Alias string
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshId: represents the meshID of the node
|
2023-12-31 15:25:06 +01:00
|
|
|
MeshId string
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutDescriptionArgs: args to assign a description to a node
|
2023-12-31 15:25:06 +01:00
|
|
|
type PutDescriptionArgs struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// Description: descriptio to add to the network
|
2023-12-31 15:25:06 +01:00
|
|
|
Description string
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshID to add to the mesh network
|
2024-01-04 14:15:29 +01:00
|
|
|
MeshId string
|
2023-11-17 23:13:51 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// GetMeshReply: ipc reply to get the mesh network
|
2023-09-29 16:00:20 +02:00
|
|
|
type GetMeshReply struct {
|
2023-10-20 18:35:02 +02:00
|
|
|
Nodes []ctrlserver.MeshNode
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// ListMeshReply: ipc reply of the networks the node is part of
|
2023-10-06 19:25:38 +02:00
|
|
|
type ListMeshReply struct {
|
|
|
|
Meshes []string
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// Querymesh: ipc args to query a mesh network
|
2023-10-30 20:02:28 +01:00
|
|
|
type QueryMesh struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshId: id of the mesh to query
|
2023-10-30 20:02:28 +01:00
|
|
|
MeshId string
|
2024-01-04 14:10:08 +01:00
|
|
|
// JMESPath: query string to query
|
2024-01-04 14:15:29 +01:00
|
|
|
Query string
|
2023-10-30 20:02:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// ClientIpc: Framework to invoke ipc calls to the daemon
|
2024-01-02 00:55:50 +01:00
|
|
|
type ClientIpc interface {
|
2024-01-04 14:10:08 +01:00
|
|
|
// CreateMesh: create a mesh network, return an error if the operation failed
|
2024-01-02 00:55:50 +01:00
|
|
|
CreateMesh(args *NewMeshArgs, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// ListMesh: list mesh network the node is a part of, return an error if the operation failed
|
2024-01-02 00:55:50 +01:00
|
|
|
ListMeshes(args *ListMeshReply, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// JoinMesh: join a mesh network return an error if the operation failed
|
2024-01-02 00:55:50 +01:00
|
|
|
JoinMesh(args JoinMeshArgs, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// LeaveMesh: leave a mesh network, return an error if the operation failed
|
2024-01-02 00:55:50 +01:00
|
|
|
LeaveMesh(meshId string, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// GetMesh: get the given mesh network, return an error if the operation failed
|
2024-01-02 00:55:50 +01:00
|
|
|
GetMesh(meshId string, reply *GetMeshReply) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// Query: query the given mesh network
|
2024-01-02 00:55:50 +01:00
|
|
|
Query(query QueryMesh, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutDescription: assign a description to yourself
|
2024-01-02 00:55:50 +01:00
|
|
|
PutDescription(args PutDescriptionArgs, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutAlias: assign an alias to yourself
|
2024-01-02 00:55:50 +01:00
|
|
|
PutAlias(args PutAliasArgs, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// PutService: assign a service to yourself
|
2024-01-02 00:55:50 +01:00
|
|
|
PutService(args PutServiceArgs, reply *string) error
|
2024-01-04 14:10:08 +01:00
|
|
|
// DeleteService: retract a service
|
2024-01-02 00:55:50 +01:00
|
|
|
DeleteService(args DeleteServiceArgs, reply *string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SmegmeshIpc struct {
|
2024-01-04 14:15:29 +01:00
|
|
|
client *ipcRPC.Client
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func NewClientIpc() (*SmegmeshIpc, error) {
|
2024-01-04 14:15:29 +01:00
|
|
|
client, err := ipcRPC.DialHTTP("unix", SockAddr)
|
2023-12-31 15:25:06 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
return &SmegmeshIpc{
|
2023-12-31 15:25:06 +01:00
|
|
|
client: client,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) CreateMesh(args *NewMeshArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.CreateMesh", args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) ListMeshes(reply *ListMeshReply) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.ListMeshes", "", reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) JoinMesh(args JoinMeshArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.JoinMesh", &args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) LeaveMesh(meshId string, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.LeaveMesh", &meshId, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) GetMesh(meshId string, reply *GetMeshReply) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.GetMesh", &meshId, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) Query(query QueryMesh, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.Query", &query, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) PutDescription(args PutDescriptionArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.PutDescription", &args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) PutAlias(args PutAliasArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.PutAlias", &args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) PutService(args PutServiceArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.PutService", &args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) DeleteService(args DeleteServiceArgs, reply *string) error {
|
2023-12-31 15:25:06 +01:00
|
|
|
return c.client.Call("IpcHandler.DeleteService", &args, reply)
|
|
|
|
}
|
|
|
|
|
2024-01-02 00:55:50 +01:00
|
|
|
func (c *SmegmeshIpc) Close() error {
|
2024-01-04 14:10:08 +01:00
|
|
|
return c.client.Close()
|
2023-12-31 15:25:06 +01:00
|
|
|
}
|
2023-09-29 16:00:20 +02:00
|
|
|
|
|
|
|
func RunIpcHandler(server MeshIpc) error {
|
|
|
|
if err := os.RemoveAll(SockAddr); err != nil {
|
2023-11-13 11:44:14 +01:00
|
|
|
return errors.New("could not find to address")
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rpc.Register(server)
|
|
|
|
rpc.HandleHTTP()
|
|
|
|
|
|
|
|
l, e := net.Listen("unix", SockAddr)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Serve(l, nil)
|
|
|
|
return nil
|
|
|
|
}
|