2023-09-18 18:00:43 +02:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
2023-09-19 19:29:35 +02:00
|
|
|
"context"
|
2023-09-18 18:00:43 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2023-09-19 19:29:35 +02:00
|
|
|
ipcRpc "net/rpc"
|
2023-09-18 18:00:43 +02:00
|
|
|
"os"
|
2023-09-19 19:29:35 +02:00
|
|
|
"time"
|
2023-09-18 18:00:43 +02:00
|
|
|
|
2023-09-19 14:45:49 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
2023-09-19 19:29:35 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/rpc"
|
|
|
|
ipctypes "github.com/tim-beatham/wgmesh/pkg/ipc"
|
2023-09-19 14:45:49 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/wg"
|
2023-09-19 19:29:35 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-09-18 18:00:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const SockAddr = "/tmp/wgmesh_ipc.sock"
|
|
|
|
|
|
|
|
type Mesh struct {
|
2023-09-19 14:45:49 +02:00
|
|
|
Server *ctrlserver.MeshCtrlServer
|
2023-09-18 18:00:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new WireGuard mesh network
|
|
|
|
*/
|
|
|
|
func (n Mesh) CreateNewMesh(name *string, reply *string) error {
|
2023-09-19 14:45:49 +02:00
|
|
|
wg.CreateInterface("wgmesh")
|
|
|
|
|
|
|
|
mesh, err := n.Server.CreateMesh()
|
2023-09-18 18:00:43 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:45:49 +02:00
|
|
|
*reply = mesh.SharedKey.String()
|
2023-09-18 18:00:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-19 14:45:49 +02:00
|
|
|
func (n Mesh) ListMeshes(name *string, reply *map[string]ctrlserver.Mesh) error {
|
2023-09-19 19:29:35 +02:00
|
|
|
meshes := n.Server.Meshes
|
|
|
|
*reply = meshes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n Mesh) JoinMesh(args *ipctypes.JoinMeshArgs, reply *string) error {
|
|
|
|
conn, err := grpc.Dial(args.IpAdress+":8080", grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
c := rpc.NewMeshCtrlServerClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
r, err := c.GetMesh(ctx, &rpc.GetMeshRequest{MeshId: args.MeshId})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = r.GetMeshId()
|
|
|
|
return nil
|
2023-09-19 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func RunIpcHandler(server *ctrlserver.MeshCtrlServer) error {
|
2023-09-18 18:00:43 +02:00
|
|
|
if err := os.RemoveAll(SockAddr); err != nil {
|
|
|
|
return errors.New("Could not find to address")
|
|
|
|
}
|
|
|
|
|
|
|
|
newMeshIpc := new(Mesh)
|
2023-09-19 14:45:49 +02:00
|
|
|
newMeshIpc.Server = server
|
2023-09-19 19:29:35 +02:00
|
|
|
ipcRpc.Register(newMeshIpc)
|
|
|
|
ipcRpc.HandleHTTP()
|
2023-09-18 18:00:43 +02:00
|
|
|
|
|
|
|
l, e := net.Listen("unix", SockAddr)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Serve(l, nil)
|
|
|
|
return nil
|
|
|
|
}
|