2023-10-05 18:48:54 +02:00
|
|
|
package crdt
|
|
|
|
|
2023-10-06 12:52:51 +02:00
|
|
|
import (
|
2023-10-21 19:08:45 +02:00
|
|
|
"errors"
|
2023-10-06 12:52:51 +02:00
|
|
|
"net"
|
2023-10-20 13:41:06 +02:00
|
|
|
"strings"
|
2023-10-06 12:52:51 +02:00
|
|
|
|
|
|
|
"github.com/automerge/automerge-go"
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
2023-10-05 18:48:54 +02:00
|
|
|
|
|
|
|
// CrdtNodeManager manages nodes in the crdt mesh
|
|
|
|
type CrdtNodeManager struct {
|
2023-10-06 19:25:38 +02:00
|
|
|
MeshId string
|
2023-10-05 18:48:54 +02:00
|
|
|
IfName string
|
2023-10-21 19:08:45 +02:00
|
|
|
NodeId string
|
2023-10-06 12:52:51 +02:00
|
|
|
Client *wgctrl.Client
|
2023-10-05 18:48:54 +02:00
|
|
|
doc *automerge.Doc
|
|
|
|
}
|
|
|
|
|
2023-10-20 18:35:02 +02:00
|
|
|
const maxFails = 5
|
|
|
|
|
2023-10-05 18:48:54 +02:00
|
|
|
func (c *CrdtNodeManager) AddNode(crdt MeshNodeCrdt) {
|
2023-10-21 19:08:45 +02:00
|
|
|
crdt.FailedMap = automerge.NewMap()
|
2023-10-20 18:35:02 +02:00
|
|
|
c.doc.Path("nodes").Map().Set(crdt.HostEndpoint, crdt)
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
func (c *CrdtNodeManager) ApplyWg() error {
|
2023-10-06 12:52:51 +02:00
|
|
|
snapshot, err := c.GetCrdt()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
c.updateWgConf(c.IfName, snapshot.Nodes, *c.Client)
|
2023-10-06 12:52:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:48:54 +02:00
|
|
|
// GetCrdt(): Converts the document into a struct
|
|
|
|
func (c *CrdtNodeManager) GetCrdt() (*MeshCrdt, error) {
|
|
|
|
return automerge.As[*MeshCrdt](c.doc.Root())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load: Load an entire mesh network
|
|
|
|
func (c *CrdtNodeManager) Load(bytes []byte) error {
|
|
|
|
doc, err := automerge.Load(bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.doc = doc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save: Save an entire mesh network
|
2023-10-06 12:52:51 +02:00
|
|
|
func (c *CrdtNodeManager) Save() []byte {
|
2023-10-05 18:48:54 +02:00
|
|
|
return c.doc.Save()
|
|
|
|
}
|
|
|
|
|
2023-10-06 12:52:51 +02:00
|
|
|
func (c *CrdtNodeManager) LoadChanges(changes []byte) error {
|
|
|
|
err := c.doc.LoadIncremental(changes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
return nil
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CrdtNodeManager) SaveChanges() []byte {
|
2023-10-06 12:52:51 +02:00
|
|
|
return c.doc.SaveIncremental()
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCrdtNodeManager: Create a new crdt node manager
|
2023-10-21 19:08:45 +02:00
|
|
|
func NewCrdtNodeManager(meshId, hostId, devName string, client *wgctrl.Client) *CrdtNodeManager {
|
2023-10-05 18:48:54 +02:00
|
|
|
var manager CrdtNodeManager
|
2023-10-06 19:25:38 +02:00
|
|
|
manager.MeshId = meshId
|
2023-10-05 18:48:54 +02:00
|
|
|
manager.doc = automerge.New()
|
|
|
|
manager.IfName = devName
|
2023-10-06 12:52:51 +02:00
|
|
|
manager.Client = client
|
2023-10-21 19:08:45 +02:00
|
|
|
manager.NodeId = hostId
|
2023-10-05 18:48:54 +02:00
|
|
|
return &manager
|
|
|
|
}
|
2023-10-06 12:52:51 +02:00
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
func (m *CrdtNodeManager) convertMeshNode(node MeshNodeCrdt) (*wgtypes.PeerConfig, error) {
|
2023-10-06 12:52:51 +02:00
|
|
|
peerEndpoint, err := net.ResolveUDPAddr("udp", node.WgEndpoint)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerPublic, err := wgtypes.ParseKey(node.PublicKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedIps := make([]net.IPNet, 1)
|
|
|
|
_, ipnet, err := net.ParseCIDR(node.WgHost)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedIps[0] = *ipnet
|
|
|
|
|
|
|
|
peerConfig := wgtypes.PeerConfig{
|
|
|
|
PublicKey: peerPublic,
|
2023-10-21 19:08:45 +02:00
|
|
|
Remove: m.HasFailed(node.HostEndpoint),
|
2023-10-06 12:52:51 +02:00
|
|
|
Endpoint: peerEndpoint,
|
|
|
|
AllowedIPs: allowedIps,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &peerConfig, nil
|
|
|
|
}
|
|
|
|
|
2023-10-20 13:41:06 +02:00
|
|
|
func (m1 *MeshNodeCrdt) Compare(m2 *MeshNodeCrdt) int {
|
|
|
|
return strings.Compare(m1.PublicKey, m2.PublicKey)
|
|
|
|
}
|
|
|
|
|
2023-10-20 18:35:02 +02:00
|
|
|
func (c *CrdtNodeManager) changeFailedCount(meshId, endpoint string, incAmount int64) error {
|
|
|
|
node, err := c.doc.Path("nodes").Map().Get(endpoint)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
counterMap, err := node.Map().Get("failedMap")
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
if counterMap.Kind() == automerge.KindVoid {
|
|
|
|
return errors.New("Something went wrong map does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
counter, _ := counterMap.Map().Get(c.NodeId)
|
|
|
|
|
|
|
|
if counter.Kind() == automerge.KindVoid {
|
|
|
|
err = counterMap.Map().Set(c.NodeId, incAmount)
|
|
|
|
} else {
|
|
|
|
if counter.Int64()+incAmount < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = counterMap.Map().Set(c.NodeId, counter.Int64()+1)
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment failed count increments the number of times we have attempted
|
|
|
|
// to contact the node and it's failed
|
|
|
|
func (c *CrdtNodeManager) IncrementFailedCount(endpoint string) error {
|
2023-10-21 19:08:45 +02:00
|
|
|
return c.changeFailedCount(c.MeshId, endpoint, +1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CrdtNodeManager) removeNode(endpoint string) error {
|
|
|
|
err := c.doc.Path("nodes").Map().Delete(endpoint)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrement failed count decrements the number of times we have attempted to
|
|
|
|
// contact the node and it's failed
|
|
|
|
func (c *CrdtNodeManager) DecrementFailedCount(endpoint string) error {
|
|
|
|
return c.changeFailedCount(c.MeshId, endpoint, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode: returns a mesh node crdt.
|
|
|
|
func (m *CrdtNodeManager) GetNode(endpoint string) (*MeshNodeCrdt, error) {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(endpoint)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-21 19:08:45 +02:00
|
|
|
return nil, err
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
meshNode, err := automerge.As[*MeshNodeCrdt](node)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-21 19:08:45 +02:00
|
|
|
return nil, err
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
return meshNode, nil
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
const threshold = 2
|
|
|
|
const thresholdVotes = 0.1
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
func (m *CrdtNodeManager) Length() int {
|
|
|
|
return m.doc.Path("nodes").Map().Len()
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
func (m *CrdtNodeManager) HasFailed(endpoint string) bool {
|
|
|
|
node, err := m.GetNode(endpoint)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-21 19:08:45 +02:00
|
|
|
logging.InfoLog.Printf("Cannot get node node: %s\n", endpoint)
|
|
|
|
return true
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
values, err := node.FailedMap.Values()
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-21 19:08:45 +02:00
|
|
|
return true
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
countFailed := 0
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
count := value.Int64()
|
|
|
|
|
|
|
|
if count >= threshold {
|
|
|
|
countFailed++
|
|
|
|
}
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
logging.InfoLog.Printf("Count Failed Value: %d\n", countFailed)
|
|
|
|
logging.InfoLog.Printf("Threshold Value: %d\n", int(thresholdVotes*float64(m.Length())+1))
|
|
|
|
return countFailed >= int(thresholdVotes*float64(m.Length())+1)
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 19:08:45 +02:00
|
|
|
func (m *CrdtNodeManager) updateWgConf(devName string, nodes map[string]MeshNodeCrdt, client wgctrl.Client) error {
|
2023-10-06 12:52:51 +02:00
|
|
|
peerConfigs := make([]wgtypes.PeerConfig, len(nodes))
|
|
|
|
|
|
|
|
var count int = 0
|
|
|
|
|
|
|
|
for _, n := range nodes {
|
2023-10-21 19:08:45 +02:00
|
|
|
peer, err := m.convertMeshNode(n)
|
2023-10-06 12:52:51 +02:00
|
|
|
logging.InfoLog.Println(n.HostEndpoint)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerConfigs[count] = *peer
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := wgtypes.Config{
|
|
|
|
Peers: peerConfigs,
|
|
|
|
ReplacePeers: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
client.ConfigureDevice(devName, cfg)
|
|
|
|
return nil
|
|
|
|
}
|