2023-11-30 03:02:38 +01:00
|
|
|
package automerge
|
2023-10-05 18:48:54 +02:00
|
|
|
|
2023-10-06 12:52:51 +02:00
|
|
|
import (
|
2023-10-21 19:08:45 +02:00
|
|
|
"errors"
|
2023-11-01 12:58:10 +01:00
|
|
|
"fmt"
|
2023-10-06 12:52:51 +02:00
|
|
|
"net"
|
2023-11-28 15:42:09 +01:00
|
|
|
"slices"
|
2023-10-20 13:41:06 +02:00
|
|
|
"strings"
|
2023-10-24 17:00:46 +02:00
|
|
|
"time"
|
2023-10-06 12:52:51 +02:00
|
|
|
|
|
|
|
"github.com/automerge/automerge-go"
|
2023-10-25 19:34:38 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
2023-10-26 17:53:12 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
2023-10-06 12:52:51 +02:00
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-26 17:53:12 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
2023-10-06 12:52:51 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
2023-10-05 18:48:54 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// CrdtMeshManager manages nodes in the crdt mesh
|
|
|
|
type CrdtMeshManager struct {
|
2023-11-20 12:28:12 +01:00
|
|
|
MeshId string
|
|
|
|
IfName string
|
|
|
|
Client *wgctrl.Client
|
|
|
|
doc *automerge.Doc
|
|
|
|
LastHash automerge.ChangeHash
|
2023-12-10 20:21:54 +01:00
|
|
|
conf *conf.WgConfiguration
|
2023-11-20 12:28:12 +01:00
|
|
|
cache *MeshCrdt
|
|
|
|
lastCacheHash automerge.ChangeHash
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (c *CrdtMeshManager) AddNode(node mesh.MeshNode) {
|
|
|
|
crdt, ok := node.(*MeshNodeCrdt)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
panic("node must be of type *MeshNodeCrdt")
|
|
|
|
}
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
crdt.Routes = make(map[string]Route)
|
2023-11-17 23:13:51 +01:00
|
|
|
crdt.Services = make(map[string]string)
|
2023-10-24 17:00:46 +02:00
|
|
|
crdt.Timestamp = time.Now().Unix()
|
2023-11-17 23:13:51 +01:00
|
|
|
|
2023-11-24 13:07:03 +01:00
|
|
|
c.doc.Path("nodes").Map().Set(crdt.PublicKey, crdt)
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
func (c *CrdtMeshManager) isPeer(nodeId string) bool {
|
|
|
|
node, err := c.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil || node.Kind() != automerge.KindMap {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeType, err := node.Map().Get("type")
|
|
|
|
|
|
|
|
if err != nil || nodeType.Kind() != automerge.KindStr {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeType.Str() == string(conf.PEER_ROLE)
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:37:54 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
return true
|
|
|
|
// return (time.Now().Unix() - keepAliveTime) < int64(c.conf.DeadTime)
|
2023-11-24 13:37:54 +01:00
|
|
|
}
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
func (c *CrdtMeshManager) GetPeers() []string {
|
2023-11-20 12:28:12 +01:00
|
|
|
keys, _ := c.doc.Path("nodes").Map().Keys()
|
2023-11-21 17:42:49 +01:00
|
|
|
|
2023-11-24 13:37:54 +01:00
|
|
|
keys = lib.Filter(keys, func(publicKey string) bool {
|
|
|
|
return c.isPeer(publicKey) && c.isAlive(publicKey)
|
2023-11-21 17:42:49 +01:00
|
|
|
})
|
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// GetMesh(): Converts the document into a struct
|
|
|
|
func (c *CrdtMeshManager) GetMesh() (mesh.MeshSnapshot, error) {
|
2023-11-20 12:28:12 +01:00
|
|
|
changes, err := c.doc.Changes(c.lastCacheHash)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-21 14:31:34 +01:00
|
|
|
if c.cache == nil || len(changes) > 0 {
|
2023-11-20 12:28:12 +01:00
|
|
|
c.lastCacheHash = c.LastHash
|
|
|
|
cache, err := automerge.As[*MeshCrdt](c.doc.Root())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cache = cache
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.cache, nil
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// GetMeshId returns the meshid of the mesh
|
|
|
|
func (c *CrdtMeshManager) GetMeshId() string {
|
|
|
|
return c.MeshId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save: Save an entire mesh network
|
|
|
|
func (c *CrdtMeshManager) Save() []byte {
|
|
|
|
return c.doc.Save()
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:48:54 +02:00
|
|
|
// Load: Load an entire mesh network
|
2023-10-26 17:53:12 +02:00
|
|
|
func (c *CrdtMeshManager) Load(bytes []byte) error {
|
2023-10-05 18:48:54 +02:00
|
|
|
doc, err := automerge.Load(bytes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.doc = doc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
type NewCrdtNodeMangerParams struct {
|
|
|
|
MeshId string
|
|
|
|
DevName string
|
|
|
|
Port int
|
2023-12-10 20:21:54 +01:00
|
|
|
Conf *conf.WgConfiguration
|
2023-10-28 17:38:25 +02:00
|
|
|
Client *wgctrl.Client
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:48:54 +02:00
|
|
|
// NewCrdtNodeManager: Create a new crdt node manager
|
2023-10-28 17:38:25 +02:00
|
|
|
func NewCrdtNodeManager(params *NewCrdtNodeMangerParams) (*CrdtMeshManager, error) {
|
2023-10-26 17:53:12 +02:00
|
|
|
var manager CrdtMeshManager
|
2023-10-28 17:38:25 +02:00
|
|
|
manager.MeshId = params.MeshId
|
2023-10-05 18:48:54 +02:00
|
|
|
manager.doc = automerge.New()
|
2023-10-28 17:38:25 +02:00
|
|
|
manager.IfName = params.DevName
|
|
|
|
manager.Client = params.Client
|
2023-12-10 20:21:54 +01:00
|
|
|
manager.conf = params.Conf
|
2023-11-20 12:28:12 +01:00
|
|
|
manager.cache = nil
|
2023-10-24 17:00:46 +02:00
|
|
|
return &manager, nil
|
2023-10-05 18:48:54 +02:00
|
|
|
}
|
2023-10-06 12:52:51 +02:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
// NodeExists: returns true if the node exists. Returns false
|
|
|
|
func (m *CrdtMeshManager) NodeExists(key string) bool {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(key)
|
2023-11-21 14:31:34 +01:00
|
|
|
return node.Kind() == automerge.KindMap && err == nil
|
2023-11-20 12:28:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CrdtMeshManager) GetNode(endpoint string) (mesh.MeshNode, error) {
|
2023-10-21 19:08:45 +02:00
|
|
|
node, err := m.doc.Path("nodes").Map().Get(endpoint)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-11-20 12:28:12 +01:00
|
|
|
if node.Kind() != automerge.KindMap {
|
|
|
|
return nil, fmt.Errorf("GetNode: something went wrong %s is not a map type")
|
|
|
|
}
|
|
|
|
|
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-26 17:53:12 +02:00
|
|
|
func (m *CrdtMeshManager) Length() int {
|
2023-10-21 19:08:45 +02:00
|
|
|
return m.doc.Path("nodes").Map().Len()
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *CrdtMeshManager) GetDevice() (*wgtypes.Device, error) {
|
2023-10-24 17:00:46 +02:00
|
|
|
dev, err := m.Client.Device(m.IfName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasChanges returns true if we have changes since the last time we synced
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *CrdtMeshManager) HasChanges() bool {
|
2023-10-24 17:00:46 +02:00
|
|
|
changes, err := m.doc.Changes(m.LastHash)
|
|
|
|
|
|
|
|
logging.Log.WriteInfof("Changes %s", m.LastHash.String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.Log.WriteInfof("Changes length %d", len(changes))
|
|
|
|
return len(changes) > 0
|
|
|
|
}
|
2023-10-22 14:34:49 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *CrdtMeshManager) SaveChanges() {
|
|
|
|
hashes := m.doc.Heads()
|
|
|
|
hash := hashes[len(hashes)-1]
|
2023-10-20 18:35:02 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
logging.Log.WriteInfof("Saved Hash %s", hash.String())
|
|
|
|
m.LastHash = hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CrdtMeshManager) UpdateTimeStamp(nodeId string) error {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(nodeId)
|
2023-10-20 18:35:02 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-10-26 17:53:12 +02:00
|
|
|
return err
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if node.Kind() != automerge.KindMap {
|
|
|
|
return errors.New("node is not a map")
|
|
|
|
}
|
2023-10-21 19:08:45 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
err = node.Map().Set("timestamp", time.Now().Unix())
|
2023-10-21 19:08:45 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err == nil {
|
2023-11-01 12:58:10 +01:00
|
|
|
logging.Log.WriteInfof("Timestamp Updated for %s", nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CrdtMeshManager) SetDescription(nodeId string, description string) error {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Kind() != automerge.KindMap {
|
2023-11-05 13:08:20 +01:00
|
|
|
return fmt.Errorf("%s does not exist", nodeId)
|
2023-11-01 12:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = node.Map().Set("description", description)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
logging.Log.WriteInfof("Description Updated for %s", nodeId)
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
return err
|
2023-10-20 18:35:02 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
func (m *CrdtMeshManager) SetAlias(nodeId string, alias string) error {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("%s does not exist", nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = node.Map().Set("alias", alias)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
logging.Log.WriteInfof("Updated Alias for %s to %s", nodeId, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
func (m *CrdtMeshManager) AddService(nodeId, key, value string) error {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil || node.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("AddService: node %s does not exist", nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
service, err := node.Map().Get("services")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("AddService: services property does not exist in node")
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
err = service.Map().Set(key, value)
|
|
|
|
return err
|
2023-11-17 23:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CrdtMeshManager) RemoveService(nodeId, key string) error {
|
|
|
|
node, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil || node.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("RemoveService: node %s does not exist", nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
service, err := node.Map().Get("services")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if service.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("services property does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = service.Map().Delete(key)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("service %s does not exist", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// AddRoutes: adds routes to the specific nodeId
|
2023-11-25 04:15:58 +01:00
|
|
|
func (m *CrdtMeshManager) AddRoutes(nodeId string, routes ...mesh.Route) error {
|
2023-10-26 17:53:12 +02:00
|
|
|
nodeVal, err := m.doc.Path("nodes").Map().Get(nodeId)
|
2023-10-27 18:49:18 +02:00
|
|
|
logging.Log.WriteInfof("Adding route to %s", nodeId)
|
2023-10-24 17:00:46 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-24 17:00:46 +02:00
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
if nodeVal.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("node does not exist")
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
routeMap, err := nodeVal.Map().Get("routes")
|
2023-10-24 17:00:46 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
for _, route := range routes {
|
2023-11-28 15:42:09 +01:00
|
|
|
prevRoute, err := routeMap.Map().Get(route.GetDestination().String())
|
|
|
|
|
|
|
|
if prevRoute.Kind() == automerge.KindVoid && err != nil {
|
|
|
|
path, err := prevRoute.Map().Get("path")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Kind() != automerge.KindList {
|
|
|
|
return fmt.Errorf("path is not a list")
|
|
|
|
}
|
|
|
|
|
|
|
|
pathStr, err := automerge.As[[]string](path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Equal(route.GetPath(), pathStr)
|
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
err = routeMap.Map().Set(route.GetDestination().String(), Route{
|
|
|
|
Destination: route.GetDestination().String(),
|
2023-11-27 19:55:41 +01:00
|
|
|
Path: route.GetPath(),
|
2023-11-25 04:15:58 +01:00
|
|
|
})
|
2023-10-24 17:00:46 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-24 17:00:46 +02:00
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
return nil
|
2023-10-24 17:00:46 +02:00
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
func (m *CrdtMeshManager) getRoutes(nodeId string) ([]Route, error) {
|
|
|
|
nodeVal, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeVal.Kind() != automerge.KindMap {
|
|
|
|
return nil, fmt.Errorf("node does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
routeMap, err := nodeVal.Map().Get("routes")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if routeMap.Kind() != automerge.KindMap {
|
|
|
|
return nil, fmt.Errorf("node %s is not a map", nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := automerge.As[map[string]Route](routeMap)
|
2023-11-27 19:55:41 +01:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
return lib.MapValues(routes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *CrdtMeshManager) GetRoutes(targetNode string) (map[string]mesh.Route, error) {
|
|
|
|
node, err := m.GetNode(targetNode)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
routes := make(map[string]mesh.Route)
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
// Add routes that the node directly has
|
2023-11-25 04:15:58 +01:00
|
|
|
for _, route := range node.GetRoutes() {
|
|
|
|
routes[route.GetDestination().String()] = route
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
// Work out the other routes in the mesh
|
2023-11-25 04:15:58 +01:00
|
|
|
for _, node := range m.GetPeers() {
|
|
|
|
nodeRoutes, err := m.getRoutes(node)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range nodeRoutes {
|
|
|
|
otherRoute, ok := routes[route.GetDestination().String()]
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
hopCount := route.GetHopCount()
|
|
|
|
|
|
|
|
if node != targetNode {
|
|
|
|
hopCount += 1
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:55:41 +01:00
|
|
|
if !ok || route.GetHopCount()+1 < otherRoute.GetHopCount() {
|
2023-11-25 04:15:58 +01:00
|
|
|
routes[route.GetDestination().String()] = &Route{
|
|
|
|
Destination: route.GetDestination().String(),
|
2023-11-27 19:55:41 +01:00
|
|
|
Path: append(route.Path, m.GetMeshId()),
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 15:42:09 +01:00
|
|
|
func (m *CrdtMeshManager) RemoveNode(nodeId string) error {
|
|
|
|
err := m.doc.Path("nodes").Map().Delete(nodeId)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:48:53 +01:00
|
|
|
// DeleteRoutes deletes the specified routes
|
2023-12-10 16:10:36 +01:00
|
|
|
func (m *CrdtMeshManager) RemoveRoutes(nodeId string, routes ...mesh.Route) error {
|
2023-11-07 20:48:53 +01:00
|
|
|
nodeVal, err := m.doc.Path("nodes").Map().Get(nodeId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nodeVal.Kind() != automerge.KindMap {
|
|
|
|
return fmt.Errorf("node is not a map")
|
|
|
|
}
|
|
|
|
|
|
|
|
routeMap, err := nodeVal.Map().Get("routes")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
2023-12-10 16:10:36 +01:00
|
|
|
err = routeMap.Map().Delete(route.GetDestination().String())
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-10 20:21:54 +01:00
|
|
|
// GetConfiguration: gets the configuration for this mesh network
|
|
|
|
func (m *CrdtMeshManager) GetConfiguration() *conf.WgConfiguration {
|
|
|
|
return m.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark: mark the node as locally dead
|
|
|
|
func (m *CrdtMeshManager) Mark(nodeId string) {
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *CrdtMeshManager) GetSyncer() mesh.MeshSyncer {
|
|
|
|
return NewAutomergeSync(m)
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-12-07 02:44:54 +01:00
|
|
|
func (m *CrdtMeshManager) Prune() error {
|
2023-11-06 10:54:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m1 *MeshNodeCrdt) Compare(m2 *MeshNodeCrdt) int {
|
|
|
|
return strings.Compare(m1.PublicKey, m2.PublicKey)
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshNodeCrdt) GetHostEndpoint() string {
|
|
|
|
return m.HostEndpoint
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshNodeCrdt) GetPublicKey() (wgtypes.Key, error) {
|
|
|
|
return wgtypes.ParseKey(m.PublicKey)
|
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshNodeCrdt) GetWgEndpoint() string {
|
2023-10-27 18:49:18 +02:00
|
|
|
return m.WgEndpoint
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
2023-10-25 19:34:38 +02:00
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshNodeCrdt) GetWgHost() *net.IPNet {
|
|
|
|
_, ipnet, err := net.ParseCIDR(m.WgHost)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2023-10-25 19:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
return ipnet
|
2023-10-25 19:34:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshNodeCrdt) GetTimeStamp() int64 {
|
|
|
|
return m.Timestamp
|
|
|
|
}
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
func (m *MeshNodeCrdt) GetRoutes() []mesh.Route {
|
|
|
|
return lib.Map(lib.MapValues(m.Routes), func(r Route) mesh.Route {
|
2023-11-27 19:55:41 +01:00
|
|
|
return &Route{
|
|
|
|
Destination: r.Destination,
|
|
|
|
Path: r.Path,
|
|
|
|
}
|
2023-11-25 04:15:58 +01:00
|
|
|
})
|
2023-10-23 19:13:08 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 12:58:10 +01:00
|
|
|
func (m *MeshNodeCrdt) GetDescription() string {
|
|
|
|
return m.Description
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
func (m *MeshNodeCrdt) GetIdentifier() string {
|
|
|
|
ipv6 := m.WgHost[:len(m.WgHost)-4]
|
|
|
|
|
|
|
|
constituents := strings.Split(ipv6, ":")
|
|
|
|
constituents = constituents[4:]
|
|
|
|
return strings.Join(constituents, ":")
|
|
|
|
}
|
|
|
|
|
2023-11-17 20:05:21 +01:00
|
|
|
func (m *MeshNodeCrdt) GetAlias() string {
|
|
|
|
return m.Alias
|
|
|
|
}
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
func (m *MeshNodeCrdt) GetServices() map[string]string {
|
|
|
|
services := make(map[string]string)
|
|
|
|
|
|
|
|
for key, service := range m.Services {
|
|
|
|
services[key] = service
|
|
|
|
}
|
|
|
|
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2023-11-21 17:42:49 +01:00
|
|
|
// GetType refers to the type of the node. Peer means that the node is globally accessible
|
|
|
|
// Client means the node is only accessible through another peer
|
|
|
|
func (n *MeshNodeCrdt) GetType() conf.NodeType {
|
|
|
|
return conf.NodeType(n.Type)
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
func (m *MeshCrdt) GetNodes() map[string]mesh.MeshNode {
|
|
|
|
nodes := make(map[string]mesh.MeshNode)
|
|
|
|
|
|
|
|
for _, node := range m.Nodes {
|
|
|
|
nodes[node.HostEndpoint] = &MeshNodeCrdt{
|
|
|
|
HostEndpoint: node.HostEndpoint,
|
|
|
|
WgEndpoint: node.WgEndpoint,
|
|
|
|
PublicKey: node.PublicKey,
|
|
|
|
WgHost: node.WgHost,
|
|
|
|
Timestamp: node.Timestamp,
|
|
|
|
Routes: node.Routes,
|
2023-11-01 12:58:10 +01:00
|
|
|
Description: node.Description,
|
2023-11-17 20:05:21 +01:00
|
|
|
Alias: node.Alias,
|
2023-11-17 23:13:51 +01:00
|
|
|
Services: node.GetServices(),
|
2023-11-21 17:42:49 +01:00
|
|
|
Type: node.Type,
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
2023-10-22 14:34:49 +02:00
|
|
|
}
|
2023-11-25 04:15:58 +01:00
|
|
|
|
|
|
|
func (r *Route) GetDestination() *net.IPNet {
|
|
|
|
_, ipnet, _ := net.ParseCIDR(r.Destination)
|
|
|
|
return ipnet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) GetHopCount() int {
|
2023-11-27 19:55:41 +01:00
|
|
|
return len(r.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) GetPath() []string {
|
|
|
|
return r.Path
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|