mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-16 08:07:57 +02:00
Compare commits
14 Commits
66-improve
...
66-ipv6-ad
Author | SHA1 | Date | |
---|---|---|---|
1a9d9d61ad | |||
6954608c32 | |||
2e6aed6f93 | |||
b0893a0b8e | |||
e7d6055fa3 | |||
e0f3f116b9 | |||
352648b7cb | |||
2d5df25b1d | |||
cabe173831 | |||
d2c8a52ec6 | |||
bf53108384 | |||
77aac5534b | |||
58439fcd56 | |||
311a15363a |
@ -126,26 +126,6 @@ func ValidateDaemonConfiguration(c *DaemonConfiguration) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// ParseMeshConfiguration: parses the mesh network configuration. Parses parameters such as
|
||||
// keepalive time, role and so forth.
|
||||
func ParseMeshConfiguration(filePath string) (*WgConfiguration, error) {
|
||||
var conf WgConfiguration
|
||||
|
||||
yamlBytes, err := os.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(yamlBytes, &conf)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &conf, ValidateMeshConfiguration(&conf)
|
||||
}
|
||||
|
||||
// ParseDaemonConfiguration parses the mesh configuration and validates the configuration
|
||||
func ParseDaemonConfiguration(filePath string) (*DaemonConfiguration, error) {
|
||||
var conf DaemonConfiguration
|
||||
@ -162,6 +142,11 @@ func ParseDaemonConfiguration(filePath string) (*DaemonConfiguration, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if conf.BaseConfiguration.KeepAliveWg == nil {
|
||||
var keepAlive int = 0
|
||||
conf.BaseConfiguration.KeepAliveWg = &keepAlive
|
||||
}
|
||||
|
||||
return &conf, ValidateDaemonConfiguration(&conf)
|
||||
}
|
||||
|
||||
|
@ -19,9 +19,11 @@ type ConnectionManager interface {
|
||||
// If the endpoint does not exist then add the connection. Returns an error
|
||||
// if something went wrong
|
||||
GetConnection(endPoint string) (PeerConnection, error)
|
||||
// HasConnections returns true if a client has already registered at the givne
|
||||
// HasConnections returns true if a peer has already registered at the given
|
||||
// endpoint or false otherwise.
|
||||
HasConnection(endPoint string) bool
|
||||
// Removes a connection if it exists
|
||||
RemoveConnection(endPoint string) error
|
||||
// Goes through all the connections and closes eachone
|
||||
Close() error
|
||||
}
|
||||
@ -32,7 +34,6 @@ type ConnectionManagerImpl struct {
|
||||
// clientConnections maps an endpoint to a connection
|
||||
conLoc sync.RWMutex
|
||||
clientConnections map[string]PeerConnection
|
||||
serverConfig *tls.Config
|
||||
clientConfig *tls.Config
|
||||
connFactory PeerConnectionFactory
|
||||
}
|
||||
@ -61,16 +62,8 @@ func NewConnectionManager(params *NewConnectionManagerParams) (ConnectionManager
|
||||
return nil, err
|
||||
}
|
||||
|
||||
serverAuth := tls.RequireAndVerifyClientCert
|
||||
|
||||
if params.SkipCertVerification {
|
||||
serverAuth = tls.RequireAnyClientCert
|
||||
}
|
||||
|
||||
certPool := x509.NewCertPool()
|
||||
|
||||
if !params.SkipCertVerification {
|
||||
|
||||
if params.CaCert == "" {
|
||||
return nil, errors.New("CA Cert is not specified")
|
||||
}
|
||||
@ -81,17 +74,13 @@ func NewConnectionManager(params *NewConnectionManagerParams) (ConnectionManager
|
||||
return nil, err
|
||||
}
|
||||
|
||||
certPool.AppendCertsFromPEM(caCert)
|
||||
}
|
||||
|
||||
serverConfig := &tls.Config{
|
||||
ClientAuth: serverAuth,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
if ok := certPool.AppendCertsFromPEM(caCert); !ok {
|
||||
return nil, errors.New("could not parse PEM")
|
||||
}
|
||||
|
||||
clientConfig := &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
InsecureSkipVerify: params.SkipCertVerification,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
RootCAs: certPool,
|
||||
}
|
||||
|
||||
@ -99,7 +88,6 @@ func NewConnectionManager(params *NewConnectionManagerParams) (ConnectionManager
|
||||
connMgr := ConnectionManagerImpl{
|
||||
sync.RWMutex{},
|
||||
connections,
|
||||
serverConfig,
|
||||
clientConfig,
|
||||
params.ConnFactory,
|
||||
}
|
||||
@ -150,6 +138,15 @@ func (m *ConnectionManagerImpl) HasConnection(endPoint string) bool {
|
||||
return exists
|
||||
}
|
||||
|
||||
// RemoveConnection removes the given connection if it exists
|
||||
func (m *ConnectionManagerImpl) RemoveConnection(endPoint string) error {
|
||||
m.conLoc.Lock()
|
||||
err := m.clientConnections[endPoint].Close()
|
||||
|
||||
delete(m.clientConnections, endPoint)
|
||||
m.conLoc.Unlock()
|
||||
return err
|
||||
}
|
||||
func (m *ConnectionManagerImpl) Close() error {
|
||||
for _, conn := range m.clientConnections {
|
||||
if err := conn.Close(); err != nil {
|
||||
|
@ -53,13 +53,13 @@ func TestNewConnectionManagerCACertDoesNotExistAndVerify(t *testing.T) {
|
||||
|
||||
func TestNewConnectionManagerCACertDoesNotExistAndNotVerify(t *testing.T) {
|
||||
params := getConnectionManagerParams()
|
||||
params.CaCert = ""
|
||||
params.CaCert = "./cert/sdjsdjsdjk.pem"
|
||||
params.SkipCertVerification = true
|
||||
|
||||
_, err := NewConnectionManager(params)
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(`an error should not be thrown`)
|
||||
if err == nil {
|
||||
t.Fatalf(`an error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,11 @@ package conn
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
@ -14,10 +17,8 @@ import (
|
||||
|
||||
// ConnectionServer manages gRPC server peer connections
|
||||
type ConnectionServer struct {
|
||||
// tlsConfiguration of the server
|
||||
serverConfig *tls.Config
|
||||
// server an instance of the grpc server
|
||||
server *grpc.Server // the authentication service to authenticate nodes
|
||||
server *grpc.Server
|
||||
// the ctrl service to manage node
|
||||
ctrlProvider rpc.MeshCtrlServerServer
|
||||
// the sync service to synchronise nodes
|
||||
@ -48,9 +49,26 @@ func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer,
|
||||
serverAuth = tls.RequireAnyClientCert
|
||||
}
|
||||
|
||||
certPool := x509.NewCertPool()
|
||||
|
||||
if params.Conf.CaCertificatePath == "" {
|
||||
return nil, errors.New("CA Cert is not specified")
|
||||
}
|
||||
|
||||
caCert, err := os.ReadFile(params.Conf.CaCertificatePath)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ok := certPool.AppendCertsFromPEM(caCert); !ok {
|
||||
return nil, errors.New("could not parse PEM")
|
||||
}
|
||||
|
||||
serverConfig := &tls.Config{
|
||||
ClientAuth: serverAuth,
|
||||
Certificates: []tls.Certificate{cert},
|
||||
ClientCAs: certPool,
|
||||
}
|
||||
|
||||
server := grpc.NewServer(
|
||||
@ -61,7 +79,6 @@ func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer,
|
||||
syncProvider := params.SyncProvider
|
||||
|
||||
connServer := ConnectionServer{
|
||||
serverConfig: serverConfig,
|
||||
server: server,
|
||||
ctrlProvider: ctrlProvider,
|
||||
syncProvider: syncProvider,
|
||||
@ -74,7 +91,6 @@ func NewConnectionServer(params *NewConnectionServerParams) (*ConnectionServer,
|
||||
// Listen for incoming requests. Returns an error if something went wrong.
|
||||
func (s *ConnectionServer) Listen() error {
|
||||
rpc.RegisterMeshCtrlServerServer(s.server, s.ctrlProvider)
|
||||
|
||||
rpc.RegisterSyncServiceServer(s.server, s.syncProvider)
|
||||
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Conf.GrpcPort))
|
||||
|
@ -16,6 +16,11 @@ func (s *ConnectionManagerStub) AddConnection(endPoint string) (PeerConnection,
|
||||
return mock, nil
|
||||
}
|
||||
|
||||
func (s *ConnectionManagerStub) RemoveConnection(endPoint string) error {
|
||||
delete(s.Endpoints, endPoint)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ConnectionManagerStub) GetConnection(endPoint string) (PeerConnection, error) {
|
||||
endpoint, ok := s.Endpoints[endPoint]
|
||||
|
||||
|
@ -158,8 +158,8 @@ type TwoPhaseStoreMeshManager struct {
|
||||
IfName string
|
||||
Client *wgctrl.Client
|
||||
LastClock uint64
|
||||
conf *conf.WgConfiguration
|
||||
daemonConf *conf.DaemonConfiguration
|
||||
Conf *conf.WgConfiguration
|
||||
DaemonConf *conf.DaemonConfiguration
|
||||
store *TwoPhaseMap[string, MeshNode]
|
||||
}
|
||||
|
||||
@ -204,7 +204,6 @@ func (m *TwoPhaseStoreMeshManager) Save() []byte {
|
||||
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
|
||||
err := enc.Encode(*snapshot)
|
||||
|
||||
if err != nil {
|
||||
@ -265,7 +264,7 @@ func (m *TwoPhaseStoreMeshManager) UpdateTimeStamp(nodeId string) error {
|
||||
|
||||
peerToUpdate := peers[0]
|
||||
|
||||
if uint64(time.Now().Unix())-m.store.Clock.GetTimestamp(peerToUpdate) > 3*uint64(m.daemonConf.KeepAliveTime) {
|
||||
if uint64(time.Now().Unix())-m.store.Clock.GetTimestamp(peerToUpdate) > 3*uint64(m.DaemonConf.KeepAliveTime) {
|
||||
m.store.Mark(peerToUpdate)
|
||||
|
||||
if len(peers) < 2 {
|
||||
@ -411,6 +410,11 @@ func (m *TwoPhaseStoreMeshManager) RemoveService(nodeId string, key string) erro
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
|
||||
if _, ok := node.Services[key]; !ok {
|
||||
return fmt.Errorf("datastore: node does not contain service %s", key)
|
||||
}
|
||||
|
||||
delete(node.Services, key)
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
@ -510,5 +514,5 @@ func (m *TwoPhaseStoreMeshManager) RemoveNode(nodeId string) error {
|
||||
|
||||
// GetConfiguration implements mesh.MeshProvider.
|
||||
func (m *TwoPhaseStoreMeshManager) GetConfiguration() *conf.WgConfiguration {
|
||||
return m.conf
|
||||
return m.Conf
|
||||
}
|
||||
|
442
pkg/crdt/datastore_test.go
Normal file
442
pkg/crdt/datastore_test.go
Normal file
@ -0,0 +1,442 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"net"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
type TestParams struct {
|
||||
manager mesh.MeshProvider
|
||||
publicKey *wgtypes.Key
|
||||
}
|
||||
|
||||
func setUpTests() *TestParams {
|
||||
advertiseRoutes := false
|
||||
advertiseDefaultRoute := false
|
||||
role := conf.PEER_ROLE
|
||||
discovery := conf.DNS_IP_DISCOVERY
|
||||
|
||||
factory := &TwoPhaseMapFactory{
|
||||
Config: &conf.DaemonConfiguration{
|
||||
CertificatePath: "/somecertificatepath",
|
||||
PrivateKeyPath: "/someprivatekeypath",
|
||||
CaCertificatePath: "/somecacertificatepath",
|
||||
SkipCertVerification: true,
|
||||
GrpcPort: 0,
|
||||
Timeout: 20,
|
||||
Profile: false,
|
||||
SyncRate: 2,
|
||||
KeepAliveTime: 10,
|
||||
ClusterSize: 32,
|
||||
InterClusterChance: 0.15,
|
||||
BranchRate: 3,
|
||||
InfectionCount: 3,
|
||||
BaseConfiguration: conf.WgConfiguration{
|
||||
IPDiscovery: &discovery,
|
||||
AdvertiseRoutes: &advertiseRoutes,
|
||||
AdvertiseDefaultRoute: &advertiseDefaultRoute,
|
||||
Role: &role,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
key, _ := wgtypes.GeneratePrivateKey()
|
||||
|
||||
mesh, _ := factory.CreateMesh(&mesh.MeshProviderFactoryParams{
|
||||
DevName: "bob",
|
||||
MeshId: "meshid123",
|
||||
Client: nil,
|
||||
Conf: &factory.Config.BaseConfiguration,
|
||||
DaemonConf: factory.Config,
|
||||
NodeID: "bob",
|
||||
})
|
||||
|
||||
publicKey := key.PublicKey()
|
||||
|
||||
return &TestParams{
|
||||
manager: mesh,
|
||||
publicKey: &publicKey,
|
||||
}
|
||||
}
|
||||
|
||||
func getOurNode(testParams *TestParams) *MeshNode {
|
||||
return &MeshNode{
|
||||
HostEndpoint: "public-endpoint:8080",
|
||||
WgEndpoint: "public-endpoint:21906",
|
||||
WgHost: "3e9a:1fb3:5e50:8173:9690:f917:b1ab:d218/128",
|
||||
PublicKey: testParams.publicKey.String(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Description: "A node that we are adding",
|
||||
Type: "peer",
|
||||
}
|
||||
}
|
||||
|
||||
func getRandomNode() *MeshNode {
|
||||
key, _ := wgtypes.GeneratePrivateKey()
|
||||
publicKey := key.PublicKey()
|
||||
|
||||
return &MeshNode{
|
||||
HostEndpoint: "public-endpoint:8081",
|
||||
WgEndpoint: "public-endpoint:21907",
|
||||
WgHost: "3e9a:1fb3:5e50:8173:9690:f917:b1ab:d234/128",
|
||||
PublicKey: publicKey.String(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Description: "A node that we are adding",
|
||||
Type: "peer",
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddNodeAddsTheNodesToTheStore(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
if !testParams.manager.NodeExists(testParams.publicKey.String()) {
|
||||
t.Fatalf(`node %s should have been added to the mesh network`, testParams.publicKey.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddNodeNodeAlreadyExistsReplacesTheNode(t *testing.T) {
|
||||
TestAddNodeAddsTheNodesToTheStore(t)
|
||||
TestAddNodeAddsTheNodesToTheStore(t)
|
||||
}
|
||||
|
||||
func TestSaveThenLoad(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
|
||||
bytes := testParams.manager.Save()
|
||||
|
||||
if err := testParams.manager.Load(bytes); err != nil {
|
||||
t.Fatalf(`error caused by loading datastore: %s`, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasChangesReturnsTrueWhenThereAreChangesInTheMesh(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
|
||||
if !testParams.manager.HasChanges() {
|
||||
t.Fatalf(`mesh has change but HasChanges returned false`)
|
||||
}
|
||||
|
||||
testParams.manager.SetDescription(testParams.publicKey.String(), "Bob marley")
|
||||
|
||||
if !testParams.manager.HasChanges() {
|
||||
t.Fatalf(`mesh has change but HasChanges returned false`)
|
||||
}
|
||||
|
||||
testParams.manager.SaveChanges()
|
||||
}
|
||||
|
||||
func TestHasChangesWhenThereAreNoChangesInTheMeshReturnsFalse(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
testParams.manager.AddNode(getRandomNode())
|
||||
|
||||
testParams.manager.SaveChanges()
|
||||
|
||||
if testParams.manager.HasChanges() {
|
||||
t.Fatalf(`mesh has no changes but HasChanges was true`)
|
||||
}
|
||||
|
||||
testParams.manager.SetDescription(testParams.publicKey.String(), "Bob marley")
|
||||
|
||||
testParams.manager.SaveChanges()
|
||||
|
||||
if testParams.manager.HasChanges() {
|
||||
t.Fatalf(`mesh has no changes but HasChanges was true`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTimeStampUpdatesTheTimeStampOfTheGivenNodeIfItIsTheLeader(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
before, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
testParams.manager.UpdateTimeStamp(testParams.publicKey.String())
|
||||
|
||||
after, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
if before.GetTimeStamp() >= after.GetTimeStamp() {
|
||||
t.Fatalf(`before should not be after after`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTimeStampUpdatesTheTimeStampOfTheGivenNodeIfItIsNotLeader(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
newNode := getRandomNode()
|
||||
newNode.PublicKey = "aaaaaaaaaa"
|
||||
|
||||
testParams.manager.AddNode(newNode)
|
||||
|
||||
before, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
after, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
if before.GetTimeStamp() != after.GetTimeStamp() {
|
||||
t.Fatalf(`before and after should be the same`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddRoutesAddsARouteToTheGivenMesh(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
_, destination, _ := net.ParseCIDR("0353:1da7:7f33:acc0:7a3f:6e55:912b:bc1f/64")
|
||||
|
||||
testParams.manager.AddRoutes(testParams.publicKey.String(), &mesh.RouteStub{
|
||||
Destination: destination,
|
||||
HopCount: 0,
|
||||
Path: make([]string, 0),
|
||||
})
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
containsDestination := lib.Contains(node.GetRoutes(), func(r mesh.Route) bool {
|
||||
return r.GetDestination().Contains(destination.IP)
|
||||
})
|
||||
|
||||
if !containsDestination {
|
||||
t.Fatalf(`route has not been added to the node`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveRoutesWithdrawsRoutesFromTheMesh(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
_, destination, _ := net.ParseCIDR("0353:1da7:7f33:acc0:7a3f:6e55:912b:bc1f/64")
|
||||
route := &mesh.RouteStub{
|
||||
Destination: destination,
|
||||
HopCount: 0,
|
||||
Path: make([]string, 0),
|
||||
}
|
||||
|
||||
testParams.manager.AddRoutes(testParams.publicKey.String(), route)
|
||||
testParams.manager.RemoveRoutes(testParams.publicKey.String(), route)
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
containsDestination := lib.Contains(node.GetRoutes(), func(r mesh.Route) bool {
|
||||
return r.GetDestination().Contains(destination.IP)
|
||||
})
|
||||
|
||||
if containsDestination {
|
||||
t.Fatalf(`route has not been removed from the node`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNodeGetsTheNodeWhenItExists(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
if node == nil {
|
||||
t.Fatalf(`node not found returned nil`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetNodeReturnsNilWhenItDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
testParams.manager.RemoveNode(testParams.publicKey.String())
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
if node != nil {
|
||||
t.Fatalf(`node found but should be nil`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeExistsReturnsFalseWhenNotExists(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
testParams.manager.RemoveNode(testParams.publicKey.String())
|
||||
|
||||
if testParams.manager.NodeExists(testParams.publicKey.String()) {
|
||||
t.Fatalf(`nodeexists should be false`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDescriptionReturnsErrorWhenNodeDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.SetDescription("djdjdj", "djdsjkd")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDescriptionSetsTheDescription(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
descriptionToSet := "djdsjkd"
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
err := testParams.manager.SetDescription(testParams.publicKey.String(), descriptionToSet)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`error %s thrown`, err.Error())
|
||||
}
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
description := node.GetDescription()
|
||||
|
||||
if description != descriptionToSet {
|
||||
t.Fatalf(`description was %s should be %s`, description, descriptionToSet)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAliasNodeDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.SetAlias("djdjdj", "djdsjkd")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAliasSetsAlias(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
aliasToSet := "djdsjkd"
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
err := testParams.manager.SetAlias(testParams.publicKey.String(), aliasToSet)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`error %s thrown`, err.Error())
|
||||
}
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
alias := node.GetAlias()
|
||||
|
||||
if alias != aliasToSet {
|
||||
t.Fatalf(`description was %s should be %s`, alias, aliasToSet)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddServiceNodeDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.AddService("djdjdj", "djdsjkd", "sddsds")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddServiceNodeExists(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
service := "djdsjkd"
|
||||
serviceValue := "dsdsds"
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
err := testParams.manager.AddService(testParams.publicKey.String(), service, serviceValue)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`error %s thrown`, err.Error())
|
||||
}
|
||||
|
||||
node, _ := testParams.manager.GetNode(testParams.publicKey.String())
|
||||
|
||||
services := node.GetServices()
|
||||
|
||||
if value, ok := services[service]; !ok || value != serviceValue {
|
||||
t.Fatalf(`service not added to the data store`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveServiceDoesNotExists(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.RemoveService("djdjdj", "dsdssd")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveServiceServiceDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
testParams.manager.AddNode(getOurNode(testParams))
|
||||
|
||||
if err := testParams.manager.RemoveService(testParams.publicKey.String(), "dhsdh"); err == nil {
|
||||
t.Fatalf(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPeersReturnsAllPeersInTheMesh(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
peer1 := getRandomNode()
|
||||
peer2 := getRandomNode()
|
||||
client := getRandomNode()
|
||||
client.Type = "client"
|
||||
|
||||
testParams.manager.AddNode(peer1)
|
||||
testParams.manager.AddNode(peer2)
|
||||
testParams.manager.AddNode(client)
|
||||
|
||||
peers := testParams.manager.GetPeers()
|
||||
slices.Sort(peers)
|
||||
|
||||
if len(peers) != 2 {
|
||||
t.Fatalf(`there should be two peers in the mesh`)
|
||||
}
|
||||
|
||||
peer1Pub, _ := peer1.GetPublicKey()
|
||||
|
||||
if !slices.Contains(peers, peer1Pub.String()) {
|
||||
t.Fatalf(`peer1 not in the list`)
|
||||
}
|
||||
|
||||
peer2Pub, _ := peer2.GetPublicKey()
|
||||
|
||||
if !slices.Contains(peers, peer2Pub.String()) {
|
||||
t.Fatalf(`peer2 not in the list`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveNodeReturnsErrorIfNodeDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.RemoveNode("dsjdssjk")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf(`error should have returned`)
|
||||
}
|
||||
}
|
@ -18,8 +18,8 @@ func (f *TwoPhaseMapFactory) CreateMesh(params *mesh.MeshProviderFactoryParams)
|
||||
MeshId: params.MeshId,
|
||||
IfName: params.DevName,
|
||||
Client: params.Client,
|
||||
conf: params.Conf,
|
||||
daemonConf: params.DaemonConf,
|
||||
Conf: params.Conf,
|
||||
DaemonConf: params.DaemonConf,
|
||||
store: NewTwoPhaseMap[string, MeshNode](params.NodeID, func(s string) uint64 {
|
||||
h := fnv.New64a()
|
||||
h.Write([]byte(s))
|
||||
|
@ -34,7 +34,7 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
||||
nodeFactory := &crdt.MeshNodeFactory{
|
||||
Config: *params.Conf,
|
||||
}
|
||||
idGenerator := &lib.IDNameGenerator{}
|
||||
idGenerator := &lib.ShortIDGenerator{}
|
||||
ipAllocator := &ip.ULABuilder{}
|
||||
interfaceManipulator := wg.NewWgInterfaceManipulator(params.Client)
|
||||
|
||||
@ -89,7 +89,6 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
||||
return ctrlServer, nil
|
||||
}
|
||||
|
||||
|
||||
func (s *MeshCtrlServer) GetConfiguration() *conf.DaemonConfiguration {
|
||||
return s.Conf
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package lib
|
||||
import (
|
||||
"github.com/anandvarma/namegen"
|
||||
"github.com/google/uuid"
|
||||
"github.com/lithammer/shortuuid"
|
||||
)
|
||||
|
||||
// IdGenerator generates unique ids
|
||||
@ -19,6 +20,14 @@ func (g *UUIDGenerator) GetId() (string, error) {
|
||||
return id.String(), nil
|
||||
}
|
||||
|
||||
type ShortIDGenerator struct {
|
||||
}
|
||||
|
||||
func (g *ShortIDGenerator) GetId() (string, error) {
|
||||
id := shortuuid.New()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
type IDNameGenerator struct {
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,11 @@ func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wg
|
||||
return p.PublicKey.String() == pubKey.String()
|
||||
})
|
||||
|
||||
endpoint, err := net.ResolveUDPAddr("udp", params.node.GetWgEndpoint())
|
||||
var endpoint *net.UDPAddr = nil
|
||||
|
||||
if params.node.GetType() == conf.PEER_ROLE {
|
||||
endpoint, err = net.ResolveUDPAddr("udp", params.node.GetWgEndpoint())
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -115,7 +119,7 @@ func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wg
|
||||
|
||||
// getRoutes: finds the routes with the least hop distance. If more than one route exists
|
||||
// consistently hash to evenly spread the distribution of traffic
|
||||
func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]routeNode {
|
||||
func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) (map[string][]routeNode, error) {
|
||||
mesh, _ := meshProvider.GetMesh()
|
||||
routes := make(map[string][]routeNode)
|
||||
|
||||
@ -154,16 +158,18 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
||||
// Client's only acessible by another peer
|
||||
if node.GetType() == conf.CLIENT_ROLE {
|
||||
peer := m.getCorrespondingPeer(peers, node)
|
||||
self, _ := m.meshManager.GetSelf(meshProvider.GetMeshId())
|
||||
self, err := meshProvider.GetNode(m.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If the node isn't the self use that peer as the gateway
|
||||
if !NodeEquals(peer, self) {
|
||||
peerPub, _ := peer.GetPublicKey()
|
||||
rn.gateway = peerPub.String()
|
||||
rn.route = &RouteStub{
|
||||
Destination: rn.route.GetDestination(),
|
||||
HopCount: rn.route.GetHopCount() + 1,
|
||||
// Append the path to this peer
|
||||
Path: append(rn.route.GetPath(), peer.GetWgHost().IP.String()),
|
||||
}
|
||||
}
|
||||
@ -181,7 +187,7 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
||||
}
|
||||
}
|
||||
|
||||
return routes
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
// getCorrespondignPeer: gets the peer corresponding to the client
|
||||
@ -215,7 +221,6 @@ type GetConfigParams struct {
|
||||
}
|
||||
|
||||
func (m *WgMeshConfigApplyer) getClientConfig(params *GetConfigParams) (*wgtypes.Config, error) {
|
||||
self, err := m.meshManager.GetSelf(params.mesh.GetMeshId())
|
||||
ula := &ip.ULABuilder{}
|
||||
meshNet, _ := ula.GetIPNet(params.mesh.GetMeshId())
|
||||
|
||||
@ -231,6 +236,8 @@ func (m *WgMeshConfigApplyer) getClientConfig(params *GetConfigParams) (*wgtypes
|
||||
})
|
||||
routes = append(routes, *meshNet)
|
||||
|
||||
self, err := params.mesh.GetNode(m.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -298,7 +305,7 @@ func (m *WgMeshConfigApplyer) getPeerConfig(params *GetConfigParams) (*wgtypes.C
|
||||
peerToClients := make(map[string][]net.IPNet)
|
||||
installedRoutes := make([]lib.Route, 0)
|
||||
peerConfigs := make([]wgtypes.PeerConfig, 0)
|
||||
self, err := m.meshManager.GetSelf(params.mesh.GetMeshId())
|
||||
self, err := params.mesh.GetNode(m.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -389,7 +396,7 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider, routes map[string]
|
||||
return mn.GetType() == conf.CLIENT_ROLE
|
||||
})
|
||||
|
||||
self, err := m.meshManager.GetSelf(mesh.GetMeshId())
|
||||
self, err := mesh.GetNode(m.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -428,11 +435,15 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider, routes map[string]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *WgMeshConfigApplyer) getAllRoutes() map[string][]routeNode {
|
||||
func (m *WgMeshConfigApplyer) getAllRoutes() (map[string][]routeNode, error) {
|
||||
allRoutes := make(map[string][]routeNode)
|
||||
|
||||
for _, mesh := range m.meshManager.GetMeshes() {
|
||||
routes := m.getRoutes(mesh)
|
||||
routes, err := m.getRoutes(mesh)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for destination, route := range routes {
|
||||
_, ok := allRoutes[destination]
|
||||
@ -450,11 +461,15 @@ func (m *WgMeshConfigApplyer) getAllRoutes() map[string][]routeNode {
|
||||
}
|
||||
}
|
||||
|
||||
return allRoutes
|
||||
return allRoutes, nil
|
||||
}
|
||||
|
||||
func (m *WgMeshConfigApplyer) ApplyConfig() error {
|
||||
allRoutes := m.getAllRoutes()
|
||||
allRoutes, err := m.getAllRoutes()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, mesh := range m.meshManager.GetMeshes() {
|
||||
err := m.updateWgConf(mesh, allRoutes)
|
||||
|
@ -24,7 +24,7 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
||||
continue
|
||||
}
|
||||
|
||||
self, err := r.meshManager.GetSelf(mesh1.GetMeshId())
|
||||
self, err := mesh1.GetNode(r.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -90,11 +90,20 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
||||
|
||||
// Calculate the set different of each, working out routes to remove and to keep.
|
||||
for meshId, meshRoutes := range routes {
|
||||
mesh := r.meshManager.GetMesh(meshId)
|
||||
self, _ := r.meshManager.GetSelf(meshId)
|
||||
toRemove := make([]Route, 0)
|
||||
mesh := meshes[meshId]
|
||||
|
||||
prevRoutes, _ := mesh.GetRoutes(NodeID(self))
|
||||
self, err := mesh.GetNode(r.meshManager.GetPublicKey().String())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
toRemove := make([]Route, 0)
|
||||
prevRoutes, err := mesh.GetRoutes(NodeID(self))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, route := range prevRoutes {
|
||||
if !lib.Contains(meshRoutes, func(r Route) bool {
|
||||
|
@ -81,6 +81,10 @@ func NodeEquals(node1, node2 MeshNode) bool {
|
||||
key1, _ := node1.GetPublicKey()
|
||||
key2, _ := node2.GetPublicKey()
|
||||
|
||||
if node1 == nil || node2 == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return key1.String() == key2.String()
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
@ -16,7 +16,7 @@ import (
|
||||
|
||||
// Syncer: picks random nodes from the meshs
|
||||
type Syncer interface {
|
||||
Sync(meshId string) error
|
||||
Sync(theMesh mesh.MeshProvider) error
|
||||
SyncMeshes() error
|
||||
}
|
||||
|
||||
@ -30,21 +30,33 @@ type SyncerImpl struct {
|
||||
lastSync map[string]uint64
|
||||
}
|
||||
|
||||
// Sync: Sync random nodes
|
||||
func (s *SyncerImpl) Sync(meshId string) error {
|
||||
// Self can be nil if the node is removed
|
||||
self, _ := s.manager.GetSelf(meshId)
|
||||
// Sync: Sync with random nodes
|
||||
func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
if correspondingMesh == nil {
|
||||
return fmt.Errorf("mesh provided was nil cannot sync nil mesh")
|
||||
}
|
||||
|
||||
correspondingMesh := s.manager.GetMesh(meshId)
|
||||
// Self can be nil if the node is removed
|
||||
selfID := s.manager.GetPublicKey()
|
||||
self, _ := correspondingMesh.GetNode(selfID.String())
|
||||
|
||||
// Mesh has been removed
|
||||
if self == nil {
|
||||
return fmt.Errorf("mesh %s does not exist", correspondingMesh.GetMeshId())
|
||||
}
|
||||
|
||||
correspondingMesh.Prune()
|
||||
|
||||
if self != nil && self.GetType() == conf.PEER_ROLE && !s.manager.HasChanges(meshId) && s.infectionCount == 0 {
|
||||
logging.Log.WriteInfof("No changes for %s", meshId)
|
||||
if correspondingMesh.HasChanges() {
|
||||
logging.Log.WriteInfof("meshes %s has changes", correspondingMesh.GetMeshId())
|
||||
}
|
||||
|
||||
if self.GetType() == conf.PEER_ROLE && !correspondingMesh.HasChanges() && s.infectionCount == 0 {
|
||||
logging.Log.WriteInfof("no changes for %s", correspondingMesh.GetMeshId())
|
||||
|
||||
// If not synchronised in certain pull from random neighbour
|
||||
if uint64(time.Now().Unix())-s.lastSync[meshId] > 20 {
|
||||
return s.Pull(meshId)
|
||||
if uint64(time.Now().Unix())-s.lastSync[correspondingMesh.GetMeshId()] > 20 {
|
||||
return s.Pull(self, correspondingMesh)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -54,9 +66,6 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
s.manager.GetRouteManager().UpdateRoutes()
|
||||
|
||||
publicKey := s.manager.GetPublicKey()
|
||||
|
||||
logging.Log.WriteInfof(publicKey.String())
|
||||
|
||||
nodeNames := correspondingMesh.GetPeers()
|
||||
|
||||
if self != nil {
|
||||
@ -68,13 +77,15 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
var gossipNodes []string
|
||||
|
||||
// Clients always pings its peer for configuration
|
||||
if self != nil && self.GetType() == conf.CLIENT_ROLE {
|
||||
keyFunc := lib.HashString
|
||||
bucketFunc := lib.HashString
|
||||
if self != nil && self.GetType() == conf.CLIENT_ROLE && len(nodeNames) > 1 {
|
||||
neighbours := s.cluster.GetNeighbours(nodeNames, publicKey.String())
|
||||
|
||||
neighbour := lib.ConsistentHash(nodeNames, publicKey.String(), keyFunc, bucketFunc)
|
||||
gossipNodes = make([]string, 1)
|
||||
gossipNodes[0] = neighbour
|
||||
if len(neighbours) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
redundancyLength := min(len(neighbours), 3)
|
||||
gossipNodes = neighbours[:redundancyLength]
|
||||
} else {
|
||||
neighbours := s.cluster.GetNeighbours(nodeNames, publicKey.String())
|
||||
gossipNodes = lib.RandomSubsetOfLength(neighbours, s.conf.BranchRate)
|
||||
@ -88,21 +99,21 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
|
||||
// Do this synchronously to conserve bandwidth
|
||||
for _, node := range gossipNodes {
|
||||
correspondingPeer := s.manager.GetNode(meshId, node)
|
||||
correspondingPeer := s.manager.GetNode(correspondingMesh.GetMeshId(), node)
|
||||
|
||||
if correspondingPeer == nil {
|
||||
logging.Log.WriteErrorf("node %s does not exist", node)
|
||||
continue
|
||||
}
|
||||
|
||||
err := s.requester.SyncMesh(meshId, correspondingPeer)
|
||||
err := s.requester.SyncMesh(correspondingMesh.GetMeshId(), correspondingPeer)
|
||||
|
||||
if err == nil || err == io.EOF {
|
||||
succeeded = true
|
||||
} else {
|
||||
// If the synchronisation operation has failed them mark a gravestone
|
||||
// preventing the peer from being re-contacted until it has updated
|
||||
// itself
|
||||
s.manager.GetMesh(meshId).Mark(node)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,36 +128,18 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
s.infectionCount++
|
||||
}
|
||||
|
||||
s.manager.GetMesh(meshId).SaveChanges()
|
||||
s.lastSync[meshId] = uint64(time.Now().Unix())
|
||||
|
||||
logging.Log.WriteInfof("UPDATING WG CONF")
|
||||
err := s.manager.ApplyConfig()
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof("Failed to update config %w", err)
|
||||
}
|
||||
correspondingMesh.SaveChanges()
|
||||
|
||||
s.lastSync[correspondingMesh.GetMeshId()] = uint64(time.Now().Unix())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pull one node in the cluster, if there has not been message dissemination
|
||||
// in a certain period of time pull a random node within the cluster
|
||||
func (s *SyncerImpl) Pull(meshId string) error {
|
||||
mesh := s.manager.GetMesh(meshId)
|
||||
self, err := s.manager.GetSelf(meshId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SyncerImpl) Pull(self mesh.MeshNode, mesh mesh.MeshProvider) error {
|
||||
peers := mesh.GetPeers()
|
||||
pubKey, _ := self.GetPublicKey()
|
||||
|
||||
if mesh == nil {
|
||||
return errors.New("mesh is nil, invalid operation")
|
||||
}
|
||||
|
||||
peers := mesh.GetPeers()
|
||||
neighbours := s.cluster.GetNeighbours(peers, pubKey.String())
|
||||
neighbour := lib.RandomSubsetOfLength(neighbours, 1)
|
||||
|
||||
@ -163,10 +156,10 @@ func (s *SyncerImpl) Pull(meshId string) error {
|
||||
return fmt.Errorf("node %s does not exist in the mesh", neighbour[0])
|
||||
}
|
||||
|
||||
err = s.requester.SyncMesh(meshId, pullNode)
|
||||
err = s.requester.SyncMesh(mesh.GetMeshId(), pullNode)
|
||||
|
||||
if err == nil || err == io.EOF {
|
||||
s.lastSync[meshId] = uint64(time.Now().Unix())
|
||||
s.lastSync[mesh.GetMeshId()] = uint64(time.Now().Unix())
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
@ -177,14 +170,30 @@ func (s *SyncerImpl) Pull(meshId string) error {
|
||||
|
||||
// SyncMeshes: Sync all meshes
|
||||
func (s *SyncerImpl) SyncMeshes() error {
|
||||
for meshId := range s.manager.GetMeshes() {
|
||||
err := s.Sync(meshId)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, mesh := range s.manager.GetMeshes() {
|
||||
wg.Add(1)
|
||||
|
||||
sync := func() {
|
||||
defer wg.Done()
|
||||
|
||||
err := s.Sync(mesh)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteErrorf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
go sync()
|
||||
}
|
||||
|
||||
logging.Log.WriteInfof("updating the WireGuard configuration")
|
||||
err := s.manager.ApplyConfig()
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof("failed to update config %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"github.com/tim-beatham/wgmesh/pkg/conn"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -15,11 +16,34 @@ type SyncErrorHandler interface {
|
||||
// SyncErrorHandlerImpl Is an implementation of the SyncErrorHandler
|
||||
type SyncErrorHandlerImpl struct {
|
||||
meshManager mesh.MeshManager
|
||||
connManager conn.ConnectionManager
|
||||
}
|
||||
|
||||
func (s *SyncErrorHandlerImpl) handleFailed(meshId string, nodeId string) bool {
|
||||
mesh := s.meshManager.GetMesh(meshId)
|
||||
mesh.Mark(nodeId)
|
||||
node, err := mesh.GetNode(nodeId)
|
||||
|
||||
if err != nil {
|
||||
s.connManager.RemoveConnection(node.GetHostEndpoint())
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *SyncErrorHandlerImpl) handleDeadlineExceeded(meshId string, nodeId string) bool {
|
||||
mesh := s.meshManager.GetMesh(meshId)
|
||||
|
||||
if mesh == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
node, err := mesh.GetNode(nodeId)
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
s.connManager.RemoveConnection(node.GetHostEndpoint())
|
||||
return true
|
||||
}
|
||||
|
||||
@ -29,13 +53,15 @@ func (s *SyncErrorHandlerImpl) Handle(meshId string, nodeId string, err error) b
|
||||
logging.Log.WriteInfof("Handled gRPC error: %s", errStatus.Message())
|
||||
|
||||
switch errStatus.Code() {
|
||||
case codes.Unavailable, codes.Unknown, codes.DeadlineExceeded, codes.Internal, codes.NotFound:
|
||||
case codes.Unavailable, codes.Unknown, codes.Internal, codes.NotFound:
|
||||
return s.handleFailed(meshId, nodeId)
|
||||
case codes.DeadlineExceeded:
|
||||
return s.handleDeadlineExceeded(meshId, nodeId)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func NewSyncErrorHandler(m mesh.MeshManager) SyncErrorHandler {
|
||||
return &SyncErrorHandlerImpl{meshManager: m}
|
||||
func NewSyncErrorHandler(m mesh.MeshManager, conn conn.ConnectionManager) SyncErrorHandler {
|
||||
return &SyncErrorHandlerImpl{meshManager: m, connManager: conn}
|
||||
}
|
||||
|
@ -151,6 +151,6 @@ func (s *SyncRequesterImpl) syncMesh(mesh mesh.MeshProvider, ctx context.Context
|
||||
}
|
||||
|
||||
func NewSyncRequester(s *ctrlserver.MeshCtrlServer) SyncRequester {
|
||||
errorHdlr := NewSyncErrorHandler(s.MeshManager)
|
||||
errorHdlr := NewSyncErrorHandler(s.MeshManager, s.ConnectionManager)
|
||||
return &SyncRequesterImpl{server: s, errorHdlr: errorHdlr}
|
||||
}
|
||||
|
Reference in New Issue
Block a user