1
0
forked from extern/smegmesh

Added sync

This commit is contained in:
Tim Beatham 2023-10-10 20:14:40 +01:00
parent e729c5b181
commit ec87afc235
16 changed files with 794 additions and 41 deletions

View File

@ -1,4 +1,7 @@
certificatePath: "../../cert/cert.pem" certificatePath: "../../cert/cert.pem"
privateKeyPath: "../../cert/key.pem" privateKeyPath: "../../cert/key.pem"
skipCertVerification: true skipCertVerification: true
ifName: "wgmesh" ifName: "wgmesh"
wgPort: 51820
gRPCPort: 8080
secret: "abc123"

View File

@ -13,6 +13,8 @@ type WgMeshConfiguration struct {
PrivateKeyPath string `yaml:"privateKeyPath"` PrivateKeyPath string `yaml:"privateKeyPath"`
SkipCertVerification bool `yaml:"skipCertVerification"` SkipCertVerification bool `yaml:"skipCertVerification"`
IfName string `yaml:"ifName"` IfName string `yaml:"ifName"`
WgPort string `yaml:"wgPort"`
GrpcPort string `yaml:"grpcPort"`
} }
func ParseConfiguration(filePath string) (*WgMeshConfiguration, error) { func ParseConfiguration(filePath string) (*WgMeshConfiguration, error) {

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/tim-beatham/wgmesh/pkg/auth" "github.com/tim-beatham/wgmesh/pkg/auth"
"github.com/tim-beatham/wgmesh/pkg/conf"
logging "github.com/tim-beatham/wgmesh/pkg/log" logging "github.com/tim-beatham/wgmesh/pkg/log"
"github.com/tim-beatham/wgmesh/pkg/rpc" "github.com/tim-beatham/wgmesh/pkg/rpc"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -19,30 +20,29 @@ type ConnectionServer struct {
server *grpc.Server server *grpc.Server
authProvider rpc.AuthenticationServer authProvider rpc.AuthenticationServer
ctrlProvider rpc.MeshCtrlServerServer ctrlProvider rpc.MeshCtrlServerServer
Conf *conf.WgMeshConfiguration
} }
type NewConnectionServerParams struct { type NewConnectionServerParams struct {
CertificatePath string Conf *conf.WgMeshConfiguration
PrivateKey string AuthProvider rpc.AuthenticationServer
SkipCertVerification bool CtrlProvider rpc.MeshCtrlServerServer
AuthProvider rpc.AuthenticationServer
CtrlProvider rpc.MeshCtrlServerServer
} }
// NewConnectionServer: create a new gRPC connection server instance // NewConnectionServer: create a new gRPC connection server instance
func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer, error) { func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer, error) {
cert, err := tls.LoadX509KeyPair(params.CertificatePath, params.PrivateKey) cert, err := tls.LoadX509KeyPair(params.Conf.CertificatePath, params.Conf.PrivateKeyPath)
if err != nil { if err != nil {
logging.ErrorLog.Printf("Failed to load key pair: %s\n", err.Error()) logging.ErrorLog.Printf("Failed to load key pair: %s\n", err.Error())
logging.ErrorLog.Printf("Certificate Path: %s\n", params.CertificatePath) logging.ErrorLog.Printf("Certificate Path: %s\n", params.Conf.CertificatePath)
logging.ErrorLog.Printf("Private Key Path: %s\n", params.PrivateKey) logging.ErrorLog.Printf("Private Key Path: %s\n", params.Conf.PrivateKeyPath)
return nil, err return nil, err
} }
serverAuth := tls.RequireAndVerifyClientCert serverAuth := tls.RequireAndVerifyClientCert
if params.SkipCertVerification { if params.Conf.SkipCertVerification {
serverAuth = tls.RequireAnyClientCert serverAuth = tls.RequireAnyClientCert
} }
@ -67,6 +67,7 @@ func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer,
server, server,
authProvider, authProvider,
ctrlProvider, ctrlProvider,
params.Conf,
} }
return &connServer, nil return &connServer, nil
@ -76,7 +77,7 @@ func (s *ConnectionServer) Listen() error {
rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider) rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider)
rpc.RegisterAuthenticationServer(s.server, s.authProvider) rpc.RegisterAuthenticationServer(s.server, s.authProvider)
lis, err := net.Listen("tcp", ":8080") lis, err := net.Listen("tcp", s.Conf.GrpcPort)
if err != nil { if err != nil {
logging.ErrorLog.Println(err.Error()) logging.ErrorLog.Println(err.Error())

View File

@ -30,6 +30,7 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
ctrlServer := new(MeshCtrlServer) ctrlServer := new(MeshCtrlServer)
ctrlServer.Client = params.WgClient ctrlServer.Client = params.WgClient
ctrlServer.MeshManager = manager.NewMeshManager(*params.WgClient) ctrlServer.MeshManager = manager.NewMeshManager(*params.WgClient)
ctrlServer.Conf = params.Conf
connManagerParams := conn.NewJwtConnectionManagerParams{ connManagerParams := conn.NewJwtConnectionManagerParams{
CertificatePath: params.Conf.CertificatePath, CertificatePath: params.Conf.CertificatePath,
@ -46,11 +47,9 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
ctrlServer.ConnectionManager = connMgr ctrlServer.ConnectionManager = connMgr
connServerParams := conn.NewConnectionServerParams{ connServerParams := conn.NewConnectionServerParams{
CertificatePath: params.Conf.CertificatePath, Conf: params.Conf,
PrivateKey: params.Conf.PrivateKeyPath, AuthProvider: params.AuthProvider,
SkipCertVerification: params.Conf.SkipCertVerification, CtrlProvider: params.CtrlProvider,
AuthProvider: params.AuthProvider,
CtrlProvider: params.CtrlProvider,
} }
connServer, err := conn.NewConnectionServer(&connServerParams) connServer, err := conn.NewConnectionServer(&connServerParams)

View File

@ -1,6 +1,7 @@
package ctrlserver package ctrlserver
import ( import (
"github.com/tim-beatham/wgmesh/pkg/conf"
"github.com/tim-beatham/wgmesh/pkg/conn" "github.com/tim-beatham/wgmesh/pkg/conn"
"github.com/tim-beatham/wgmesh/pkg/manager" "github.com/tim-beatham/wgmesh/pkg/manager"
"golang.zx2c4.com/wireguard/wgctrl" "golang.zx2c4.com/wireguard/wgctrl"
@ -28,7 +29,8 @@ type Mesh struct {
*/ */
type MeshCtrlServer struct { type MeshCtrlServer struct {
Client *wgctrl.Client Client *wgctrl.Client
MeshManager *manager.MeshManger MeshManager *manager.MeshManger
ConnectionManager conn.ConnectionManager ConnectionManager conn.ConnectionManager
ConnectionServer *conn.ConnectionServer ConnectionServer *conn.ConnectionServer
Conf *conf.WgMeshConfiguration
} }

View File

@ -0,0 +1,18 @@
syntax = "proto3";
package rpctypes;
option go_package = "pkg/rpc";
service Authentication {
rpc JoinMesh(JoinAuthMeshRequest) returns (JoinAuthMeshReply) {}
}
message JoinAuthMeshRequest {
string meshId = 1;
string alias = 2;
}
message JoinAuthMeshReply {
bool success = 1;
optional string token = 2;
}

View File

@ -4,27 +4,10 @@ package rpctypes;
option go_package = "pkg/rpc"; option go_package = "pkg/rpc";
service MeshCtrlServer { service MeshCtrlServer {
rpc GetMesh(GetMeshRequest) returns (GetMeshReply) {}
rpc JoinMesh(JoinMeshRequest) returns (JoinMeshReply) {} rpc JoinMesh(JoinMeshRequest) returns (JoinMeshReply) {}
} }
message MeshNode {
string publicKey = 1;
string wgEndpoint = 2;
string endpoint = 3;
string wgHost = 4;
}
message GetMeshRequest {
string meshId = 1;
}
message GetMeshReply {
bytes mesh = 2;
}
message JoinMeshRequest { message JoinMeshRequest {
bytes changes = 1;
string meshId = 2; string meshId = 2;
} }

View File

@ -0,0 +1,26 @@
syntax = "proto3";
package syncservice;
option go_package = "pkg/rpc";
service SyncService {
rpc GetConf(GetConfRequest) returns (GetConfReply) {}
rpc SyncMesh(SyncMeshRequest) returns (SyncMeshReply) {}
}
message GetConfRequest {
string meshId = 1;
}
message GetConfReply {
bytes mesh = 1;
}
message SyncMeshRequest {
string meshId = 1;
bytes changes = 2;
}
message SyncMeshReply {
bool success = 1;
}

View File

@ -3,7 +3,6 @@ package robin
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"strconv" "strconv"
"time" "time"
@ -23,7 +22,7 @@ type RobinIpc struct {
} }
func (n *RobinIpc) CreateMesh(name string, reply *string) error { func (n *RobinIpc) CreateMesh(name string, reply *string) error {
wg.CreateInterface("wgmesh") wg.CreateInterface(n.Server.Conf.IfName)
meshId, err := n.Server.MeshManager.CreateMesh("wgmesh") meshId, err := n.Server.MeshManager.CreateMesh("wgmesh")
@ -59,7 +58,6 @@ func (n *RobinIpc) ListMeshes(_ string, reply *ipc.ListMeshReply) error {
i := 0 i := 0
for _, mesh := range n.Server.MeshManager.Meshes { for _, mesh := range n.Server.MeshManager.Meshes {
meshNames[i] = mesh.MeshId meshNames[i] = mesh.MeshId
fmt.Println(meshNames[i])
i++ i++
} }
@ -157,7 +155,7 @@ func (n *RobinIpc) JoinMesh(args ipc.JoinMeshArgs, reply *string) error {
return err return err
} }
err = n.Server.MeshManager.AddMesh(args.MeshId, "wgmesh", meshReply.Mesh) err = n.Server.MeshManager.AddMesh(args.MeshId, n.Server.Conf.IfName, meshReply.Mesh)
if err != nil { if err != nil {
return err return err
@ -234,7 +232,6 @@ func (n *RobinIpc) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
func (n *RobinIpc) EnableInterface(meshId string, reply *string) error { func (n *RobinIpc) EnableInterface(meshId string, reply *string) error {
err := n.Server.MeshManager.EnableInterface(meshId) err := n.Server.MeshManager.EnableInterface(meshId)
fmt.Println("reached")
if err != nil { if err != nil {
*reply = err.Error() *reply = err.Error()

View File

@ -143,7 +143,7 @@ type GetMeshReply struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Mesh []byte `protobuf:"bytes,2,opt,name=mesh,proto3" json:"mesh,omitempty"` Mesh []byte `protobuf:"bytes,1,opt,name=mesh,proto3" json:"mesh,omitempty"`
} }
func (x *GetMeshReply) Reset() { func (x *GetMeshReply) Reset() {
@ -305,7 +305,7 @@ var file_pkg_grpc_ctrlserver_ctrlserver_proto_rawDesc = []byte{
0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x22, 0x22, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x4d, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x22, 0x22, 0x0a, 0x0c, 0x47, 0x65, 0x74, 0x4d,
0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x6d, 0x65, 0x73, 0x68, 0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12, 0x0a, 0x04, 0x6d, 0x65, 0x73, 0x68,
0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x6d, 0x65, 0x73, 0x68, 0x22, 0x43, 0x0a, 0x0f, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x6d, 0x65, 0x73, 0x68, 0x22, 0x43, 0x0a, 0x0f,
0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x4a, 0x6f, 0x69, 0x6e, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
0x18, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x18, 0x0a, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c,
0x52, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x52, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73,

355
pkg/rpc/syncservice.pb.go Normal file
View File

@ -0,0 +1,355 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.21.12
// source: pkg/grpc/ctrlserver/syncservice.proto
package rpc
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type GetConfRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
MeshId string `protobuf:"bytes,1,opt,name=meshId,proto3" json:"meshId,omitempty"`
}
func (x *GetConfRequest) Reset() {
*x = GetConfRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetConfRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetConfRequest) ProtoMessage() {}
func (x *GetConfRequest) ProtoReflect() protoreflect.Message {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetConfRequest.ProtoReflect.Descriptor instead.
func (*GetConfRequest) Descriptor() ([]byte, []int) {
return file_pkg_grpc_ctrlserver_syncservice_proto_rawDescGZIP(), []int{0}
}
func (x *GetConfRequest) GetMeshId() string {
if x != nil {
return x.MeshId
}
return ""
}
type GetConfReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Mesh []byte `protobuf:"bytes,1,opt,name=mesh,proto3" json:"mesh,omitempty"`
}
func (x *GetConfReply) Reset() {
*x = GetConfReply{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetConfReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetConfReply) ProtoMessage() {}
func (x *GetConfReply) ProtoReflect() protoreflect.Message {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetConfReply.ProtoReflect.Descriptor instead.
func (*GetConfReply) Descriptor() ([]byte, []int) {
return file_pkg_grpc_ctrlserver_syncservice_proto_rawDescGZIP(), []int{1}
}
func (x *GetConfReply) GetMesh() []byte {
if x != nil {
return x.Mesh
}
return nil
}
type SyncMeshRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
MeshId string `protobuf:"bytes,1,opt,name=meshId,proto3" json:"meshId,omitempty"`
Changes []byte `protobuf:"bytes,2,opt,name=changes,proto3" json:"changes,omitempty"`
}
func (x *SyncMeshRequest) Reset() {
*x = SyncMeshRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SyncMeshRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SyncMeshRequest) ProtoMessage() {}
func (x *SyncMeshRequest) ProtoReflect() protoreflect.Message {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[2]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SyncMeshRequest.ProtoReflect.Descriptor instead.
func (*SyncMeshRequest) Descriptor() ([]byte, []int) {
return file_pkg_grpc_ctrlserver_syncservice_proto_rawDescGZIP(), []int{2}
}
func (x *SyncMeshRequest) GetMeshId() string {
if x != nil {
return x.MeshId
}
return ""
}
func (x *SyncMeshRequest) GetChanges() []byte {
if x != nil {
return x.Changes
}
return nil
}
type SyncMeshReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
}
func (x *SyncMeshReply) Reset() {
*x = SyncMeshReply{}
if protoimpl.UnsafeEnabled {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SyncMeshReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SyncMeshReply) ProtoMessage() {}
func (x *SyncMeshReply) ProtoReflect() protoreflect.Message {
mi := &file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[3]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SyncMeshReply.ProtoReflect.Descriptor instead.
func (*SyncMeshReply) Descriptor() ([]byte, []int) {
return file_pkg_grpc_ctrlserver_syncservice_proto_rawDescGZIP(), []int{3}
}
func (x *SyncMeshReply) GetSuccess() bool {
if x != nil {
return x.Success
}
return false
}
var File_pkg_grpc_ctrlserver_syncservice_proto protoreflect.FileDescriptor
var file_pkg_grpc_ctrlserver_syncservice_proto_rawDesc = []byte{
0x0a, 0x25, 0x70, 0x6b, 0x67, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x63, 0x74, 0x72, 0x6c, 0x73,
0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, 0x73, 0x79, 0x6e, 0x63, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63,
0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0b, 0x73, 0x79, 0x6e, 0x63, 0x73, 0x65, 0x72,
0x76, 0x69, 0x63, 0x65, 0x22, 0x28, 0x0a, 0x0e, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x22, 0x22,
0x0a, 0x0c, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x12,
0x0a, 0x04, 0x6d, 0x65, 0x73, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x6d, 0x65,
0x73, 0x68, 0x22, 0x43, 0x0a, 0x0f, 0x53, 0x79, 0x6e, 0x63, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6d, 0x65, 0x73, 0x68, 0x49, 0x64, 0x12, 0x18, 0x0a,
0x07, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07,
0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x22, 0x29, 0x0a, 0x0d, 0x53, 0x79, 0x6e, 0x63, 0x4d,
0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63,
0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65,
0x73, 0x73, 0x32, 0x9a, 0x01, 0x0a, 0x0b, 0x53, 0x79, 0x6e, 0x63, 0x53, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x12, 0x43, 0x0a, 0x07, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x12, 0x1b, 0x2e,
0x73, 0x79, 0x6e, 0x63, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x43,
0x6f, 0x6e, 0x66, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x73, 0x79, 0x6e,
0x63, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x43, 0x6f, 0x6e, 0x66,
0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x46, 0x0a, 0x08, 0x53, 0x79, 0x6e, 0x63, 0x4d,
0x65, 0x73, 0x68, 0x12, 0x1c, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63,
0x65, 0x2e, 0x53, 0x79, 0x6e, 0x63, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x1a, 0x1a, 0x2e, 0x73, 0x79, 0x6e, 0x63, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x2e,
0x53, 0x79, 0x6e, 0x63, 0x4d, 0x65, 0x73, 0x68, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42,
0x09, 0x5a, 0x07, 0x70, 0x6b, 0x67, 0x2f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x33,
}
var (
file_pkg_grpc_ctrlserver_syncservice_proto_rawDescOnce sync.Once
file_pkg_grpc_ctrlserver_syncservice_proto_rawDescData = file_pkg_grpc_ctrlserver_syncservice_proto_rawDesc
)
func file_pkg_grpc_ctrlserver_syncservice_proto_rawDescGZIP() []byte {
file_pkg_grpc_ctrlserver_syncservice_proto_rawDescOnce.Do(func() {
file_pkg_grpc_ctrlserver_syncservice_proto_rawDescData = protoimpl.X.CompressGZIP(file_pkg_grpc_ctrlserver_syncservice_proto_rawDescData)
})
return file_pkg_grpc_ctrlserver_syncservice_proto_rawDescData
}
var file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_pkg_grpc_ctrlserver_syncservice_proto_goTypes = []interface{}{
(*GetConfRequest)(nil), // 0: syncservice.GetConfRequest
(*GetConfReply)(nil), // 1: syncservice.GetConfReply
(*SyncMeshRequest)(nil), // 2: syncservice.SyncMeshRequest
(*SyncMeshReply)(nil), // 3: syncservice.SyncMeshReply
}
var file_pkg_grpc_ctrlserver_syncservice_proto_depIdxs = []int32{
0, // 0: syncservice.SyncService.GetConf:input_type -> syncservice.GetConfRequest
2, // 1: syncservice.SyncService.SyncMesh:input_type -> syncservice.SyncMeshRequest
1, // 2: syncservice.SyncService.GetConf:output_type -> syncservice.GetConfReply
3, // 3: syncservice.SyncService.SyncMesh:output_type -> syncservice.SyncMeshReply
2, // [2:4] is the sub-list for method output_type
0, // [0:2] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_pkg_grpc_ctrlserver_syncservice_proto_init() }
func file_pkg_grpc_ctrlserver_syncservice_proto_init() {
if File_pkg_grpc_ctrlserver_syncservice_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetConfRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetConfReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SyncMeshRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SyncMeshReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_pkg_grpc_ctrlserver_syncservice_proto_rawDesc,
NumEnums: 0,
NumMessages: 4,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_pkg_grpc_ctrlserver_syncservice_proto_goTypes,
DependencyIndexes: file_pkg_grpc_ctrlserver_syncservice_proto_depIdxs,
MessageInfos: file_pkg_grpc_ctrlserver_syncservice_proto_msgTypes,
}.Build()
File_pkg_grpc_ctrlserver_syncservice_proto = out.File
file_pkg_grpc_ctrlserver_syncservice_proto_rawDesc = nil
file_pkg_grpc_ctrlserver_syncservice_proto_goTypes = nil
file_pkg_grpc_ctrlserver_syncservice_proto_depIdxs = nil
}

View File

@ -0,0 +1,141 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc v3.21.12
// source: pkg/grpc/ctrlserver/syncservice.proto
package rpc
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// SyncServiceClient is the client API for SyncService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type SyncServiceClient interface {
GetConf(ctx context.Context, in *GetConfRequest, opts ...grpc.CallOption) (*GetConfReply, error)
SyncMesh(ctx context.Context, in *SyncMeshRequest, opts ...grpc.CallOption) (*SyncMeshReply, error)
}
type syncServiceClient struct {
cc grpc.ClientConnInterface
}
func NewSyncServiceClient(cc grpc.ClientConnInterface) SyncServiceClient {
return &syncServiceClient{cc}
}
func (c *syncServiceClient) GetConf(ctx context.Context, in *GetConfRequest, opts ...grpc.CallOption) (*GetConfReply, error) {
out := new(GetConfReply)
err := c.cc.Invoke(ctx, "/syncservice.SyncService/GetConf", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *syncServiceClient) SyncMesh(ctx context.Context, in *SyncMeshRequest, opts ...grpc.CallOption) (*SyncMeshReply, error) {
out := new(SyncMeshReply)
err := c.cc.Invoke(ctx, "/syncservice.SyncService/SyncMesh", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// SyncServiceServer is the server API for SyncService service.
// All implementations must embed UnimplementedSyncServiceServer
// for forward compatibility
type SyncServiceServer interface {
GetConf(context.Context, *GetConfRequest) (*GetConfReply, error)
SyncMesh(context.Context, *SyncMeshRequest) (*SyncMeshReply, error)
mustEmbedUnimplementedSyncServiceServer()
}
// UnimplementedSyncServiceServer must be embedded to have forward compatible implementations.
type UnimplementedSyncServiceServer struct {
}
func (UnimplementedSyncServiceServer) GetConf(context.Context, *GetConfRequest) (*GetConfReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetConf not implemented")
}
func (UnimplementedSyncServiceServer) SyncMesh(context.Context, *SyncMeshRequest) (*SyncMeshReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method SyncMesh not implemented")
}
func (UnimplementedSyncServiceServer) mustEmbedUnimplementedSyncServiceServer() {}
// UnsafeSyncServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to SyncServiceServer will
// result in compilation errors.
type UnsafeSyncServiceServer interface {
mustEmbedUnimplementedSyncServiceServer()
}
func RegisterSyncServiceServer(s grpc.ServiceRegistrar, srv SyncServiceServer) {
s.RegisterService(&SyncService_ServiceDesc, srv)
}
func _SyncService_GetConf_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetConfRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SyncServiceServer).GetConf(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/syncservice.SyncService/GetConf",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SyncServiceServer).GetConf(ctx, req.(*GetConfRequest))
}
return interceptor(ctx, in, info, handler)
}
func _SyncService_SyncMesh_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(SyncMeshRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SyncServiceServer).SyncMesh(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/syncservice.SyncService/SyncMesh",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SyncServiceServer).SyncMesh(ctx, req.(*SyncMeshRequest))
}
return interceptor(ctx, in, info, handler)
}
// SyncService_ServiceDesc is the grpc.ServiceDesc for SyncService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var SyncService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "syncservice.SyncService",
HandlerType: (*SyncServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetConf",
Handler: _SyncService_GetConf_Handler,
},
{
MethodName: "SyncMesh",
Handler: _SyncService_SyncMesh_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "pkg/grpc/ctrlserver/syncservice.proto",
}

20
pkg/sync/syncer.go Normal file
View File

@ -0,0 +1,20 @@
package sync
// Syncer: picks random nodes from the mesh
type Syncer interface {
Sync(meshId string) error
SyncMeshes() error
}
type SyncerImpl struct {
}
// Sync: Sync random nodes
func (s *SyncerImpl) Sync(meshId string) error {
return nil
}
// SyncMeshes:
func (s *SyncerImpl) SyncMeshes() error {
return nil
}

111
pkg/sync/syncrequester.go Normal file
View File

@ -0,0 +1,111 @@
package sync
import (
"context"
"errors"
"time"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
"github.com/tim-beatham/wgmesh/pkg/rpc"
)
// SyncRequester: coordinates the syncing of meshes
type SyncRequester interface {
GetMesh(meshId string) error
SyncMesh(meshid string) error
}
type SyncRequesterImpl struct {
server *ctrlserver.MeshCtrlServer
}
// GetMesh: Retrieves the local state of the mesh at the endpoint
func (s *SyncRequesterImpl) GetMesh(meshId string, endPoint string) error {
peerConnection, err := s.server.ConnectionManager.GetConnection(endPoint)
if err != nil {
return err
}
err = peerConnection.Connect()
if err != nil {
return err
}
client, err := peerConnection.GetClient()
if err != nil {
return err
}
c := rpc.NewSyncServiceClient(client)
authContext, err := peerConnection.CreateAuthContext(meshId)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(authContext, time.Second)
defer cancel()
reply, err := c.GetConf(ctx, &rpc.GetConfRequest{MeshId: meshId})
if err != nil {
return err
}
err = s.server.MeshManager.AddMesh(meshId, s.server.Conf.IfName, reply.Mesh)
return err
}
// SyncMesh: Proactively send a sync request to the other mesh
func (s *SyncRequesterImpl) SyncMesh(meshId string, endpoint string) error {
peerConnection, err := s.server.ConnectionManager.GetConnection(endpoint)
if err != nil {
return err
}
err = peerConnection.Connect()
if err != nil {
return err
}
client, err := peerConnection.GetClient()
if err != nil {
return err
}
authContext, err := peerConnection.CreateAuthContext(meshId)
if err != nil {
return err
}
mesh := s.server.MeshManager.GetMesh(meshId)
if mesh == nil {
return errors.New("mesh does not exist")
}
syncMeshRequest := rpc.SyncMeshRequest{
MeshId: meshId,
Changes: mesh.SaveChanges(),
}
c := rpc.NewSyncServiceClient(client)
ctx, cancel := context.WithTimeout(authContext, time.Second)
defer cancel()
_, err = c.SyncMesh(ctx, &syncMeshRequest)
if err != nil {
return err
}
return nil
}

46
pkg/sync/syncscheduler.go Normal file
View File

@ -0,0 +1,46 @@
package sync
import (
"time"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
)
// SyncScheduler: Loops through all nodes in the mesh and runs a schedule to
// sync each event
type SyncScheduler interface {
Run() error
Stop() error
}
type SyncSchedulerImpl struct {
quit chan struct{}
server *ctrlserver.MeshCtrlServer
}
// Run implements SyncScheduler.
func (s *SyncSchedulerImpl) Run() error {
ticker := time.NewTicker(time.Second)
quit := make(chan struct{})
s.quit = quit
for {
select {
case <-ticker.C:
break
case <-quit:
break
}
}
}
// Stop implements SyncScheduler.
func (s *SyncSchedulerImpl) Stop() error {
close(s.quit)
return nil
}
func NewSyncScheduler(s *ctrlserver.MeshCtrlServer) SyncScheduler {
return &SyncSchedulerImpl{server: s}
}

49
pkg/sync/syncservice.go Normal file
View File

@ -0,0 +1,49 @@
// sync merges shared state between two nodes
package sync
import (
"context"
"errors"
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
"github.com/tim-beatham/wgmesh/pkg/rpc"
)
type SyncServiceImpl struct {
server *ctrlserver.MeshCtrlServer
}
// GetMesh: Gets a nodes local mesh configuration as a CRDT
func (s *SyncServiceImpl) GetConf(context context.Context, request *rpc.GetConfRequest) (*rpc.GetConfReply, error) {
mesh := s.server.MeshManager.GetMesh(request.MeshId)
if mesh == nil {
return nil, errors.New("mesh does not exist")
}
meshBytes := mesh.Save()
reply := rpc.GetConfReply{
Mesh: meshBytes,
}
return &reply, nil
}
// Sync: Pings a node and syncs the mesh configuration with the other node
func (s *SyncServiceImpl) SyncMesh(conext context.Context, request *rpc.SyncMeshRequest) (*rpc.SyncMeshReply, error) {
mesh := s.server.MeshManager.GetMesh(request.MeshId)
if mesh == nil {
return nil, errors.New("mesh does not exist")
}
err := s.server.MeshManager.UpdateMesh(request.MeshId, request.Changes)
if err != nil {
return nil, err
}
return &rpc.SyncMeshReply{Success: true}, nil
}