From 2c406718df23e96260b2802b49df8fcdc707c27f Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Fri, 24 Nov 2023 12:37:54 +0000 Subject: [PATCH] 29-only-ping-clients-who-have-updated-their-config Only consider clients who have updated their config when synchronising with peers. Consider a dead time where we don't have a handshake and a prune time when we remove them from the WireGuard configuration. --- pkg/automerge/automerge.go | 23 +++++++++++++++++++++-- pkg/conf/conf.go | 15 ++++++++++++--- pkg/mesh/types.go | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/pkg/automerge/automerge.go b/pkg/automerge/automerge.go index 8188d37..9e2fc3c 100644 --- a/pkg/automerge/automerge.go +++ b/pkg/automerge/automerge.go @@ -58,11 +58,30 @@ func (c *CrdtMeshManager) isPeer(nodeId string) bool { return nodeType.Str() == string(conf.PEER_ROLE) } +// isAlive: checks that the node's configuration has been updated +// since the rquired keep alive time +func (c *CrdtMeshManager) isAlive(nodeId string) bool { + node, err := c.doc.Path("nodes").Map().Get(nodeId) + + if err != nil || node.Kind() != automerge.KindMap { + return false + } + + timestamp, err := node.Map().Get("timestamp") + + if err != nil || timestamp.Kind() != automerge.KindInt64 { + return false + } + + keepAliveTime := timestamp.Int64() + return (time.Now().Unix() - keepAliveTime) < int64(c.conf.DeadTime) +} + func (c *CrdtMeshManager) GetPeers() []string { keys, _ := c.doc.Path("nodes").Map().Keys() - keys = lib.Filter(keys, func(s string) bool { - return c.isPeer(s) + keys = lib.Filter(keys, func(publicKey string) bool { + return c.isPeer(publicKey) && c.isAlive(publicKey) }) return keys diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 4afad08..8ae48c3 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -64,8 +64,11 @@ type WgMeshConfiguration struct { KeepAliveTime int `yaml:"keepAliveTime"` // Timeout number of seconds before we consider the node as dead Timeout int `yaml:"timeout"` - // PruneTime number of seconds before we consider the 'node' as dead + // PruneTime number of seconds before we remove nodes that are likely to be dead PruneTime int `yaml:"pruneTime"` + // DeadTime: number of seconds before we consider the node as dead and stop considering it + // when picking a random peer + DeadTime int `yaml:"deadTime"` // Profile whether or not to include a http server that profiles the code Profile bool `yaml:"profile"` // StubWg whether or not to stub the WireGuard types @@ -145,9 +148,15 @@ func ValidateConfiguration(c *WgMeshConfiguration) error { } } - if c.PruneTime <= 1 { + if c.PruneTime < 1 { return &WgMeshConfigurationError{ - msg: "Prune time cannot be <= 1", + msg: "Prune time cannot be < 1", + } + } + + if c.DeadTime < 1 { + return &WgMeshConfigurationError{ + msg: "Dead time cannot be < 1", } } diff --git a/pkg/mesh/types.go b/pkg/mesh/types.go index daf99b4..cd5d0ed 100644 --- a/pkg/mesh/types.go +++ b/pkg/mesh/types.go @@ -101,6 +101,7 @@ type MeshProvider interface { // Prune: prunes all nodes that have not updated their timestamp in // pruneAmount seconds Prune(pruneAmount int) error + // GetPeers: get a list of contactable peers GetPeers() []string }