From ec87afc235a6ec303438ca241a220d6b9213effd Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Tue, 10 Oct 2023 20:14:40 +0100 Subject: [PATCH] Added sync --- cmd/wgmeshd/configuration.yaml | 5 +- pkg/conf/conf.go | 2 + pkg/conn/conn_server.go | 21 +- pkg/ctrlserver/ctrlserver.go | 9 +- pkg/ctrlserver/ctrltypes.go | 4 +- pkg/grpc/ctrlserver/authentication.proto | 18 ++ pkg/grpc/ctrlserver/ctrlserver.proto | 17 -- pkg/grpc/ctrlserver/syncservice.proto | 26 ++ pkg/robin/robin_requester.go | 7 +- pkg/rpc/ctrlserver.pb.go | 4 +- pkg/rpc/syncservice.pb.go | 355 +++++++++++++++++++++++ pkg/rpc/syncservice_grpc.pb.go | 141 +++++++++ pkg/sync/syncer.go | 20 ++ pkg/sync/syncrequester.go | 111 +++++++ pkg/sync/syncscheduler.go | 46 +++ pkg/sync/syncservice.go | 49 ++++ 16 files changed, 794 insertions(+), 41 deletions(-) create mode 100644 pkg/grpc/ctrlserver/authentication.proto create mode 100644 pkg/grpc/ctrlserver/syncservice.proto create mode 100644 pkg/rpc/syncservice.pb.go create mode 100644 pkg/rpc/syncservice_grpc.pb.go create mode 100644 pkg/sync/syncer.go create mode 100644 pkg/sync/syncrequester.go create mode 100644 pkg/sync/syncscheduler.go create mode 100644 pkg/sync/syncservice.go diff --git a/cmd/wgmeshd/configuration.yaml b/cmd/wgmeshd/configuration.yaml index 7b7faee..2a83469 100644 --- a/cmd/wgmeshd/configuration.yaml +++ b/cmd/wgmeshd/configuration.yaml @@ -1,4 +1,7 @@ certificatePath: "../../cert/cert.pem" privateKeyPath: "../../cert/key.pem" skipCertVerification: true -ifName: "wgmesh" \ No newline at end of file +ifName: "wgmesh" +wgPort: 51820 +gRPCPort: 8080 +secret: "abc123" \ No newline at end of file diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 94c31ce..e549fce 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -13,6 +13,8 @@ type WgMeshConfiguration struct { PrivateKeyPath string `yaml:"privateKeyPath"` SkipCertVerification bool `yaml:"skipCertVerification"` IfName string `yaml:"ifName"` + WgPort string `yaml:"wgPort"` + GrpcPort string `yaml:"grpcPort"` } func ParseConfiguration(filePath string) (*WgMeshConfiguration, error) { diff --git a/pkg/conn/conn_server.go b/pkg/conn/conn_server.go index 8f60691..05be073 100644 --- a/pkg/conn/conn_server.go +++ b/pkg/conn/conn_server.go @@ -6,6 +6,7 @@ import ( "time" "github.com/tim-beatham/wgmesh/pkg/auth" + "github.com/tim-beatham/wgmesh/pkg/conf" logging "github.com/tim-beatham/wgmesh/pkg/log" "github.com/tim-beatham/wgmesh/pkg/rpc" "google.golang.org/grpc" @@ -19,30 +20,29 @@ type ConnectionServer struct { server *grpc.Server authProvider rpc.AuthenticationServer ctrlProvider rpc.MeshCtrlServerServer + Conf *conf.WgMeshConfiguration } type NewConnectionServerParams struct { - CertificatePath string - PrivateKey string - SkipCertVerification bool - AuthProvider rpc.AuthenticationServer - CtrlProvider rpc.MeshCtrlServerServer + Conf *conf.WgMeshConfiguration + AuthProvider rpc.AuthenticationServer + CtrlProvider rpc.MeshCtrlServerServer } // NewConnectionServer: create a new gRPC connection server instance 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 { logging.ErrorLog.Printf("Failed to load key pair: %s\n", err.Error()) - logging.ErrorLog.Printf("Certificate Path: %s\n", params.CertificatePath) - logging.ErrorLog.Printf("Private Key Path: %s\n", params.PrivateKey) + logging.ErrorLog.Printf("Certificate Path: %s\n", params.Conf.CertificatePath) + logging.ErrorLog.Printf("Private Key Path: %s\n", params.Conf.PrivateKeyPath) return nil, err } serverAuth := tls.RequireAndVerifyClientCert - if params.SkipCertVerification { + if params.Conf.SkipCertVerification { serverAuth = tls.RequireAnyClientCert } @@ -67,6 +67,7 @@ func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer, server, authProvider, ctrlProvider, + params.Conf, } return &connServer, nil @@ -76,7 +77,7 @@ func (s *ConnectionServer) Listen() error { rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider) rpc.RegisterAuthenticationServer(s.server, s.authProvider) - lis, err := net.Listen("tcp", ":8080") + lis, err := net.Listen("tcp", s.Conf.GrpcPort) if err != nil { logging.ErrorLog.Println(err.Error()) diff --git a/pkg/ctrlserver/ctrlserver.go b/pkg/ctrlserver/ctrlserver.go index 262e556..5917871 100644 --- a/pkg/ctrlserver/ctrlserver.go +++ b/pkg/ctrlserver/ctrlserver.go @@ -30,6 +30,7 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) { ctrlServer := new(MeshCtrlServer) ctrlServer.Client = params.WgClient ctrlServer.MeshManager = manager.NewMeshManager(*params.WgClient) + ctrlServer.Conf = params.Conf connManagerParams := conn.NewJwtConnectionManagerParams{ CertificatePath: params.Conf.CertificatePath, @@ -46,11 +47,9 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) { ctrlServer.ConnectionManager = connMgr connServerParams := conn.NewConnectionServerParams{ - CertificatePath: params.Conf.CertificatePath, - PrivateKey: params.Conf.PrivateKeyPath, - SkipCertVerification: params.Conf.SkipCertVerification, - AuthProvider: params.AuthProvider, - CtrlProvider: params.CtrlProvider, + Conf: params.Conf, + AuthProvider: params.AuthProvider, + CtrlProvider: params.CtrlProvider, } connServer, err := conn.NewConnectionServer(&connServerParams) diff --git a/pkg/ctrlserver/ctrltypes.go b/pkg/ctrlserver/ctrltypes.go index 5a12892..cf9edeb 100644 --- a/pkg/ctrlserver/ctrltypes.go +++ b/pkg/ctrlserver/ctrltypes.go @@ -1,6 +1,7 @@ package ctrlserver import ( + "github.com/tim-beatham/wgmesh/pkg/conf" "github.com/tim-beatham/wgmesh/pkg/conn" "github.com/tim-beatham/wgmesh/pkg/manager" "golang.zx2c4.com/wireguard/wgctrl" @@ -28,7 +29,8 @@ type Mesh struct { */ type MeshCtrlServer struct { Client *wgctrl.Client - MeshManager *manager.MeshManger + MeshManager *manager.MeshManger ConnectionManager conn.ConnectionManager ConnectionServer *conn.ConnectionServer + Conf *conf.WgMeshConfiguration } diff --git a/pkg/grpc/ctrlserver/authentication.proto b/pkg/grpc/ctrlserver/authentication.proto new file mode 100644 index 0000000..b9301c3 --- /dev/null +++ b/pkg/grpc/ctrlserver/authentication.proto @@ -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; +} \ No newline at end of file diff --git a/pkg/grpc/ctrlserver/ctrlserver.proto b/pkg/grpc/ctrlserver/ctrlserver.proto index ca3822c..e71ff0e 100644 --- a/pkg/grpc/ctrlserver/ctrlserver.proto +++ b/pkg/grpc/ctrlserver/ctrlserver.proto @@ -4,27 +4,10 @@ package rpctypes; option go_package = "pkg/rpc"; service MeshCtrlServer { - rpc GetMesh(GetMeshRequest) returns (GetMeshReply) {} 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 { - bytes changes = 1; string meshId = 2; } diff --git a/pkg/grpc/ctrlserver/syncservice.proto b/pkg/grpc/ctrlserver/syncservice.proto new file mode 100644 index 0000000..9362928 --- /dev/null +++ b/pkg/grpc/ctrlserver/syncservice.proto @@ -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; +} \ No newline at end of file diff --git a/pkg/robin/robin_requester.go b/pkg/robin/robin_requester.go index bcfcda2..4334be5 100644 --- a/pkg/robin/robin_requester.go +++ b/pkg/robin/robin_requester.go @@ -3,7 +3,6 @@ package robin import ( "context" "errors" - "fmt" "strconv" "time" @@ -23,7 +22,7 @@ type RobinIpc struct { } 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") @@ -59,7 +58,6 @@ func (n *RobinIpc) ListMeshes(_ string, reply *ipc.ListMeshReply) error { i := 0 for _, mesh := range n.Server.MeshManager.Meshes { meshNames[i] = mesh.MeshId - fmt.Println(meshNames[i]) i++ } @@ -157,7 +155,7 @@ func (n *RobinIpc) JoinMesh(args ipc.JoinMeshArgs, reply *string) error { 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 { 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 { err := n.Server.MeshManager.EnableInterface(meshId) - fmt.Println("reached") if err != nil { *reply = err.Error() diff --git a/pkg/rpc/ctrlserver.pb.go b/pkg/rpc/ctrlserver.pb.go index a3b0c92..cbcb061 100644 --- a/pkg/rpc/ctrlserver.pb.go +++ b/pkg/rpc/ctrlserver.pb.go @@ -143,7 +143,7 @@ type GetMeshReply struct { sizeCache protoimpl.SizeCache 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() { @@ -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, 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, - 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, 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, diff --git a/pkg/rpc/syncservice.pb.go b/pkg/rpc/syncservice.pb.go new file mode 100644 index 0000000..53a27b0 --- /dev/null +++ b/pkg/rpc/syncservice.pb.go @@ -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 +} diff --git a/pkg/rpc/syncservice_grpc.pb.go b/pkg/rpc/syncservice_grpc.pb.go new file mode 100644 index 0000000..af211d1 --- /dev/null +++ b/pkg/rpc/syncservice_grpc.pb.go @@ -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", +} diff --git a/pkg/sync/syncer.go b/pkg/sync/syncer.go new file mode 100644 index 0000000..61cd631 --- /dev/null +++ b/pkg/sync/syncer.go @@ -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 +} diff --git a/pkg/sync/syncrequester.go b/pkg/sync/syncrequester.go new file mode 100644 index 0000000..885ef12 --- /dev/null +++ b/pkg/sync/syncrequester.go @@ -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 +} diff --git a/pkg/sync/syncscheduler.go b/pkg/sync/syncscheduler.go new file mode 100644 index 0000000..5547a87 --- /dev/null +++ b/pkg/sync/syncscheduler.go @@ -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} +} diff --git a/pkg/sync/syncservice.go b/pkg/sync/syncservice.go new file mode 100644 index 0000000..95f5f1e --- /dev/null +++ b/pkg/sync/syncservice.go @@ -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 +} +