2023-09-18 16:52:28 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-19 19:29:35 +02:00
|
|
|
"context"
|
|
|
|
"errors"
|
2023-09-19 14:45:49 +02:00
|
|
|
"fmt"
|
2023-09-19 19:29:35 +02:00
|
|
|
"net"
|
2023-09-19 14:45:49 +02:00
|
|
|
|
2023-09-18 16:52:28 +02:00
|
|
|
ctrlserver "github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
2023-09-18 18:00:43 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/ipc"
|
2023-09-19 19:29:35 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/rpc"
|
2023-09-19 14:45:49 +02:00
|
|
|
wg "github.com/tim-beatham/wgmesh/pkg/wg"
|
2023-09-19 19:29:35 +02:00
|
|
|
"google.golang.org/grpc"
|
2023-09-18 16:52:28 +02:00
|
|
|
)
|
|
|
|
|
2023-09-19 19:29:35 +02:00
|
|
|
type meshCtrlServer struct {
|
|
|
|
rpc.UnimplementedMeshCtrlServerServer
|
|
|
|
server *ctrlserver.MeshCtrlServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newServer(ctrl *ctrlserver.MeshCtrlServer) *meshCtrlServer {
|
|
|
|
return &meshCtrlServer{server: ctrl}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *meshCtrlServer) GetMesh(ctx context.Context, request *rpc.GetMeshRequest) (*rpc.GetMeshReply, error) {
|
|
|
|
mesh, contains := m.server.Meshes[request.MeshId]
|
|
|
|
|
|
|
|
if !contains {
|
|
|
|
return nil, errors.New("Element is not in the mesh")
|
|
|
|
}
|
|
|
|
return &rpc.GetMeshReply{MeshId: mesh.SharedKey.String()}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-18 16:52:28 +02:00
|
|
|
func main() {
|
2023-09-19 14:45:49 +02:00
|
|
|
wgClient, err := wg.CreateClient("wgmesh")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrlServer := ctrlserver.NewCtrlServer("0.0.0.0", 21910, wgClient)
|
2023-09-19 19:29:35 +02:00
|
|
|
|
|
|
|
fmt.Println("Running IPC Handler")
|
|
|
|
go ipc.RunIpcHandler(ctrlServer)
|
|
|
|
|
|
|
|
fmt.Println("Running gRPC server")
|
|
|
|
|
|
|
|
grpc := grpc.NewServer()
|
|
|
|
|
|
|
|
rpcServer := newServer(ctrlServer)
|
|
|
|
rpc.RegisterMeshCtrlServerServer(grpc, rpcServer)
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", ":8080")
|
|
|
|
if err := grpc.Serve(lis); err != nil {
|
|
|
|
fmt.Print(err.Error())
|
|
|
|
}
|
2023-09-18 16:52:28 +02:00
|
|
|
}
|