smegmesh/pkg/robin/responder.go

58 lines
1.1 KiB
Go
Raw Normal View History

2023-09-29 16:00:20 +02:00
package robin
2023-09-20 00:50:44 +02:00
import (
2023-09-29 16:00:20 +02:00
"context"
2023-09-20 00:50:44 +02:00
"errors"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
2023-09-29 16:00:20 +02:00
"github.com/tim-beatham/wgmesh/pkg/rpc"
2023-09-20 00:50:44 +02:00
)
type WgRpc struct {
2023-09-29 16:00:20 +02:00
rpc.UnimplementedMeshCtrlServerServer
Server *ctrlserver.MeshCtrlServer
2023-09-20 00:50:44 +02:00
}
2023-09-29 16:00:20 +02:00
func nodeToRpcNode(node ctrlserver.MeshNode) *rpc.MeshNode {
return &rpc.MeshNode{
2023-09-20 00:50:44 +02:00
PublicKey: node.PublicKey,
WgEndpoint: node.WgEndpoint,
2023-09-20 15:34:34 +02:00
WgHost: node.WgHost,
2023-09-20 00:50:44 +02:00
Endpoint: node.HostEndpoint,
}
}
2023-09-29 16:00:20 +02:00
func nodesToRpcNodes(nodes map[string]ctrlserver.MeshNode) []*rpc.MeshNode {
2023-09-20 00:50:44 +02:00
n := len(nodes)
2023-09-29 16:00:20 +02:00
meshNodes := make([]*rpc.MeshNode, n)
2023-09-20 00:50:44 +02:00
var i int = 0
for _, v := range nodes {
meshNodes[i] = nodeToRpcNode(v)
i++
}
return meshNodes
}
func (m *WgRpc) GetMesh(ctx context.Context, request *rpc.GetMeshRequest) (*rpc.GetMeshReply, error) {
2023-10-06 12:52:51 +02:00
mesh := m.Server.MeshManager.GetMesh(request.MeshId)
2023-09-20 00:50:44 +02:00
2023-10-06 12:52:51 +02:00
if mesh == nil {
return nil, errors.New("mesh does not exist")
2023-09-20 00:50:44 +02:00
}
2023-10-06 12:52:51 +02:00
meshBytes := mesh.Save()
2023-09-29 16:00:20 +02:00
reply := rpc.GetMeshReply{
2023-10-06 12:52:51 +02:00
Mesh: meshBytes,
2023-09-20 00:50:44 +02:00
}
return &reply, nil
}
func (m *WgRpc) JoinMesh(ctx context.Context, request *rpc.JoinMeshRequest) (*rpc.JoinMeshReply, error) {
2023-10-06 12:52:51 +02:00
return &rpc.JoinMeshReply{Success: true}, nil
2023-09-20 00:50:44 +02:00
}