2023-09-29 16:00:20 +02:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/rpc"
|
|
|
|
"os"
|
|
|
|
|
2023-10-20 18:35:02 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
2023-09-29 16:00:20 +02:00
|
|
|
)
|
|
|
|
|
2023-10-24 17:00:46 +02:00
|
|
|
type NewMeshArgs 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-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-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-09-29 16:00:20 +02:00
|
|
|
IpAdress string
|
2023-10-28 17:38:25 +02:00
|
|
|
// Port is the WireGuard port to expose
|
|
|
|
Port int
|
|
|
|
// Endpoint is the routable address of this machine. If not provided
|
|
|
|
// defaults to the default address
|
|
|
|
Endpoint string
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
type PutServiceArgs struct {
|
|
|
|
Service string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-06 19:25:38 +02:00
|
|
|
type ListMeshReply struct {
|
|
|
|
Meshes []string
|
|
|
|
}
|
|
|
|
|
2023-10-30 20:02:28 +01:00
|
|
|
type QueryMesh struct {
|
|
|
|
MeshId string
|
|
|
|
Query string
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
type GetNodeArgs struct {
|
|
|
|
NodeId string
|
|
|
|
MeshId string
|
|
|
|
}
|
|
|
|
|
2023-09-29 16:00:20 +02:00
|
|
|
type MeshIpc interface {
|
2023-10-24 17:00:46 +02:00
|
|
|
CreateMesh(args *NewMeshArgs, reply *string) error
|
2023-10-06 19:25:38 +02:00
|
|
|
ListMeshes(name string, reply *ListMeshReply) error
|
2023-09-29 16:00:20 +02:00
|
|
|
JoinMesh(args JoinMeshArgs, reply *string) error
|
2023-10-28 17:38:25 +02:00
|
|
|
LeaveMesh(meshId string, reply *string) error
|
2023-09-29 16:00:20 +02:00
|
|
|
GetMesh(meshId string, reply *GetMeshReply) error
|
|
|
|
EnableInterface(meshId string, reply *string) error
|
2023-10-22 14:34:49 +02:00
|
|
|
GetDOT(meshId string, reply *string) error
|
2023-10-30 20:02:28 +01:00
|
|
|
Query(query QueryMesh, reply *string) error
|
2023-11-01 12:58:10 +01:00
|
|
|
PutDescription(description string, reply *string) error
|
2023-11-17 20:05:21 +01:00
|
|
|
PutAlias(alias string, reply *string) error
|
2023-11-17 23:13:51 +01:00
|
|
|
PutService(args PutServiceArgs, reply *string) error
|
2023-11-20 12:28:12 +01:00
|
|
|
GetNode(args GetNodeArgs, reply *string) error
|
2023-11-17 23:13:51 +01:00
|
|
|
DeleteService(service string, reply *string) error
|
2023-09-29 16:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const SockAddr = "/tmp/wgmesh_ipc.sock"
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|