smegmesh/pkg/ipc/ipc.go

96 lines
2.1 KiB
Go
Raw Normal View History

2023-09-29 16:00:20 +02:00
package ipc
import (
"errors"
"net"
"net/http"
"net/rpc"
"os"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
2023-09-29 16:00:20 +02:00
)
// WireGuardArgs are provided args specific to WireGuard
type WireGuardArgs struct {
// WgPort is the WireGuard port to expose
WgPort int
// 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
// Endpoint is the routable alias of the machine. Can be an IP
// or DNS entry
Endpoint string
// 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-09-29 16:00:20 +02:00
type JoinMeshArgs struct {
// 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
// WgArgs is the WireGuard parameters to use.
WgArgs WireGuardArgs
2023-09-29 16:00:20 +02:00
}
type PutServiceArgs struct {
Service string
Value string
}
2023-09-29 16:00:20 +02:00
type GetMeshReply struct {
Nodes []ctrlserver.MeshNode
2023-09-29 16:00:20 +02:00
}
2023-10-06 19:25:38 +02:00
type ListMeshReply struct {
Meshes []string
}
type QueryMesh struct {
MeshId string
Query string
}
2023-09-29 16:00:20 +02:00
type MeshIpc interface {
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
LeaveMesh(meshId string, reply *string) error
2023-09-29 16:00:20 +02:00
GetMesh(meshId string, reply *GetMeshReply) error
Query(query QueryMesh, reply *string) error
PutDescription(description string, reply *string) error
PutAlias(alias string, reply *string) error
PutService(args PutServiceArgs, reply *string) error
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
}