mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-07-03 05:50:22 +02:00
Compare commits
4 Commits
36-add-rou
...
39-impleme
Author | SHA1 | Date | |
---|---|---|---|
650901aba1 | |||
a82eab0686 | |||
32e7e4c7df | |||
1fae0a6c2c |
@ -45,21 +45,26 @@ func main() {
|
||||
var robinRpc robin.WgRpc
|
||||
var robinIpc robin.IpcHandler
|
||||
var syncProvider sync.SyncServiceImpl
|
||||
var syncRequester sync.SyncRequester
|
||||
var syncer sync.Syncer
|
||||
|
||||
ctrlServerParams := ctrlserver.NewCtrlServerParams{
|
||||
Conf: conf,
|
||||
CtrlProvider: &robinRpc,
|
||||
SyncProvider: &syncProvider,
|
||||
Client: client,
|
||||
OnDelete: func(mp mesh.MeshProvider) {
|
||||
syncer.SyncMeshes()
|
||||
},
|
||||
}
|
||||
|
||||
ctrlServer, err := ctrlserver.NewCtrlServer(&ctrlServerParams)
|
||||
syncProvider.Server = ctrlServer
|
||||
syncRequester := sync.NewSyncRequester(ctrlServer)
|
||||
syncScheduler := sync.NewSyncScheduler(ctrlServer, syncRequester)
|
||||
syncRequester = sync.NewSyncRequester(ctrlServer)
|
||||
syncer = sync.NewSyncer(ctrlServer.MeshManager, conf, syncRequester)
|
||||
syncScheduler := sync.NewSyncScheduler(ctrlServer, syncRequester, syncer)
|
||||
timestampScheduler := timer.NewTimestampScheduler(ctrlServer)
|
||||
pruneScheduler := mesh.NewPruner(ctrlServer.MeshManager, *conf)
|
||||
routeScheduler := timer.NewRouteScheduler(ctrlServer)
|
||||
|
||||
robinIpcParams := robin.RobinIpcParams{
|
||||
CtrlServer: ctrlServer,
|
||||
@ -79,7 +84,6 @@ func main() {
|
||||
go syncScheduler.Run()
|
||||
go timestampScheduler.Run()
|
||||
go pruneScheduler.Run()
|
||||
go routeScheduler.Run()
|
||||
|
||||
closeResources := func() {
|
||||
logging.Log.WriteInfof("Closing resources")
|
||||
|
@ -1,9 +1,10 @@
|
||||
package crdt
|
||||
package automerge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -289,7 +290,8 @@ func (m *CrdtMeshManager) AddService(nodeId, key, value string) error {
|
||||
return fmt.Errorf("AddService: services property does not exist in node")
|
||||
}
|
||||
|
||||
return service.Map().Set(key, value)
|
||||
err = service.Map().Set(key, value)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *CrdtMeshManager) RemoveService(nodeId, key string) error {
|
||||
@ -338,6 +340,28 @@ func (m *CrdtMeshManager) AddRoutes(nodeId string, routes ...mesh.Route) error {
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
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)
|
||||
}
|
||||
|
||||
err = routeMap.Map().Set(route.GetDestination().String(), Route{
|
||||
Destination: route.GetDestination().String(),
|
||||
Path: route.GetPath(),
|
||||
@ -385,10 +409,12 @@ func (m *CrdtMeshManager) GetRoutes(targetNode string) (map[string]mesh.Route, e
|
||||
|
||||
routes := make(map[string]mesh.Route)
|
||||
|
||||
// Add routes that the node directly has
|
||||
for _, route := range node.GetRoutes() {
|
||||
routes[route.GetDestination().String()] = route
|
||||
}
|
||||
|
||||
// Work out the other routes in the mesh
|
||||
for _, node := range m.GetPeers() {
|
||||
nodeRoutes, err := m.getRoutes(node)
|
||||
|
||||
@ -399,6 +425,12 @@ func (m *CrdtMeshManager) GetRoutes(targetNode string) (map[string]mesh.Route, e
|
||||
for _, route := range nodeRoutes {
|
||||
otherRoute, ok := routes[route.GetDestination().String()]
|
||||
|
||||
hopCount := route.GetHopCount()
|
||||
|
||||
if node != targetNode {
|
||||
hopCount += 1
|
||||
}
|
||||
|
||||
if !ok || route.GetHopCount()+1 < otherRoute.GetHopCount() {
|
||||
routes[route.GetDestination().String()] = &Route{
|
||||
Destination: route.GetDestination().String(),
|
||||
@ -411,6 +443,11 @@ func (m *CrdtMeshManager) GetRoutes(targetNode string) (map[string]mesh.Route, e
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
func (m *CrdtMeshManager) RemoveNode(nodeId string) error {
|
||||
err := m.doc.Path("nodes").Map().Delete(nodeId)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteRoutes deletes the specified routes
|
||||
func (m *CrdtMeshManager) RemoveRoutes(nodeId string, routes ...string) error {
|
||||
nodeVal, err := m.doc.Path("nodes").Map().Get(nodeId)
|
||||
@ -512,7 +549,6 @@ func (m *MeshNodeCrdt) GetWgHost() *net.IPNet {
|
||||
_, ipnet, err := net.ParseCIDR(m.WgHost)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteErrorf("Cannot parse WgHost %s", err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -540,7 +576,6 @@ func (m *MeshNodeCrdt) GetIdentifier() string {
|
||||
ipv6 := m.WgHost[:len(m.WgHost)-4]
|
||||
|
||||
constituents := strings.Split(ipv6, ":")
|
||||
logging.Log.WriteInfof(ipv6)
|
||||
constituents = constituents[4:]
|
||||
return strings.Join(constituents, ":")
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package crdt
|
||||
package automerge
|
||||
|
||||
import (
|
||||
"github.com/automerge/automerge-go"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package crdt
|
||||
package automerge
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package crdt
|
||||
package automerge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package crdt
|
||||
package automerge
|
||||
|
||||
// Route: Represents a CRDT of the given route
|
||||
type Route struct {
|
||||
|
442
pkg/crdt/datastore.go
Normal file
442
pkg/crdt/datastore.go
Normal file
@ -0,0 +1,442 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||
"golang.zx2c4.com/wireguard/wgctrl"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
Destination string
|
||||
Path []string
|
||||
}
|
||||
|
||||
// GetDestination implements mesh.Route.
|
||||
func (r *Route) GetDestination() *net.IPNet {
|
||||
_, ipnet, _ := net.ParseCIDR(r.Destination)
|
||||
return ipnet
|
||||
}
|
||||
|
||||
// GetHopCount implements mesh.Route.
|
||||
func (r *Route) GetHopCount() int {
|
||||
return len(r.Path)
|
||||
}
|
||||
|
||||
// GetPath implements mesh.Route.
|
||||
func (r *Route) GetPath() []string {
|
||||
return r.Path
|
||||
}
|
||||
|
||||
type MeshNode struct {
|
||||
HostEndpoint string
|
||||
WgEndpoint string
|
||||
PublicKey string
|
||||
WgHost string
|
||||
Timestamp int64
|
||||
Routes map[string]Route
|
||||
Alias string
|
||||
Description string
|
||||
Services map[string]string
|
||||
Type string
|
||||
}
|
||||
|
||||
// GetHostEndpoint: gets the gRPC endpoint of the node
|
||||
func (n *MeshNode) GetHostEndpoint() string {
|
||||
return n.HostEndpoint
|
||||
}
|
||||
|
||||
// GetPublicKey: gets the public key of the node
|
||||
func (n *MeshNode) GetPublicKey() (wgtypes.Key, error) {
|
||||
return wgtypes.ParseKey(n.PublicKey)
|
||||
}
|
||||
|
||||
// GetWgEndpoint(): get IP and port of the wireguard endpoint
|
||||
func (n *MeshNode) GetWgEndpoint() string {
|
||||
return n.WgEndpoint
|
||||
}
|
||||
|
||||
// GetWgHost: get the IP address of the WireGuard node
|
||||
func (n *MeshNode) GetWgHost() *net.IPNet {
|
||||
_, ipnet, _ := net.ParseCIDR(n.WgHost)
|
||||
return ipnet
|
||||
}
|
||||
|
||||
// GetTimestamp: get the UNIX time stamp of the ndoe
|
||||
func (n *MeshNode) GetTimeStamp() int64 {
|
||||
return n.Timestamp
|
||||
}
|
||||
|
||||
// GetRoutes: returns the routes that the nodes provides
|
||||
func (n *MeshNode) GetRoutes() []mesh.Route {
|
||||
routes := make([]mesh.Route, len(n.Routes))
|
||||
|
||||
for index, route := range lib.MapValues(n.Routes) {
|
||||
routes[index] = &Route{
|
||||
Destination: route.Destination,
|
||||
Path: route.Path,
|
||||
}
|
||||
}
|
||||
|
||||
return routes
|
||||
}
|
||||
|
||||
// GetIdentifier: returns the identifier of the node
|
||||
func (m *MeshNode) GetIdentifier() string {
|
||||
ipv6 := m.WgHost[:len(m.WgHost)-4]
|
||||
|
||||
constituents := strings.Split(ipv6, ":")
|
||||
constituents = constituents[4:]
|
||||
return strings.Join(constituents, ":")
|
||||
}
|
||||
|
||||
// GetDescription: returns the description for this node
|
||||
func (n *MeshNode) GetDescription() string {
|
||||
return n.Description
|
||||
}
|
||||
|
||||
// GetAlias: associates the node with an alias. Potentially used
|
||||
// for DNS and so forth.
|
||||
func (n *MeshNode) GetAlias() string {
|
||||
return n.Alias
|
||||
}
|
||||
|
||||
// GetServices: returns a list of services offered by the node
|
||||
func (n *MeshNode) GetServices() map[string]string {
|
||||
return n.Services
|
||||
}
|
||||
|
||||
func (n *MeshNode) GetType() conf.NodeType {
|
||||
return conf.NodeType(n.Type)
|
||||
}
|
||||
|
||||
type MeshSnapshot struct {
|
||||
Nodes map[string]MeshNode
|
||||
}
|
||||
|
||||
// GetNodes() returns the nodes in the mesh
|
||||
func (m *MeshSnapshot) GetNodes() map[string]mesh.MeshNode {
|
||||
newMap := make(map[string]mesh.MeshNode)
|
||||
|
||||
for key, value := range m.Nodes {
|
||||
newMap[key] = &MeshNode{
|
||||
HostEndpoint: value.HostEndpoint,
|
||||
PublicKey: value.PublicKey,
|
||||
WgHost: value.WgHost,
|
||||
WgEndpoint: value.WgEndpoint,
|
||||
Timestamp: value.Timestamp,
|
||||
Routes: value.Routes,
|
||||
Alias: value.Alias,
|
||||
Description: value.Description,
|
||||
Services: value.Services,
|
||||
Type: value.Type,
|
||||
}
|
||||
}
|
||||
|
||||
return newMap
|
||||
}
|
||||
|
||||
type TwoPhaseStoreMeshManager struct {
|
||||
MeshId string
|
||||
IfName string
|
||||
Client *wgctrl.Client
|
||||
LastClock uint64
|
||||
conf *conf.WgMeshConfiguration
|
||||
store *TwoPhaseMap[string, MeshNode]
|
||||
}
|
||||
|
||||
// AddNode() adds a node to the mesh
|
||||
func (m *TwoPhaseStoreMeshManager) AddNode(node mesh.MeshNode) {
|
||||
crdt, ok := node.(*MeshNode)
|
||||
|
||||
if !ok {
|
||||
panic("node must be of type mesh node")
|
||||
}
|
||||
|
||||
crdt.Routes = make(map[string]Route)
|
||||
crdt.Services = make(map[string]string)
|
||||
crdt.Timestamp = time.Now().Unix()
|
||||
|
||||
m.store.Put(crdt.PublicKey, *crdt)
|
||||
}
|
||||
|
||||
// GetMesh() returns a snapshot of the mesh provided by the mesh provider.
|
||||
func (m *TwoPhaseStoreMeshManager) GetMesh() (mesh.MeshSnapshot, error) {
|
||||
return &MeshSnapshot{
|
||||
Nodes: m.store.AsMap(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetMeshId() returns the ID of the mesh network
|
||||
func (m *TwoPhaseStoreMeshManager) GetMeshId() string {
|
||||
return m.MeshId
|
||||
}
|
||||
|
||||
// Save() saves the mesh network
|
||||
func (m *TwoPhaseStoreMeshManager) Save() []byte {
|
||||
snapshot := m.store.Snapshot()
|
||||
|
||||
var buf bytes.Buffer
|
||||
enc := gob.NewEncoder(&buf)
|
||||
|
||||
err := enc.Encode(*snapshot)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof(err.Error())
|
||||
}
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// Load() loads a mesh network
|
||||
func (m *TwoPhaseStoreMeshManager) Load(bs []byte) error {
|
||||
buf := bytes.NewBuffer(bs)
|
||||
|
||||
dec := gob.NewDecoder(buf)
|
||||
|
||||
var snapshot TwoPhaseMapSnapshot[string, MeshNode]
|
||||
err := dec.Decode(&snapshot)
|
||||
m.store.Merge(snapshot)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetDevice() get the device corresponding with the mesh
|
||||
func (m *TwoPhaseStoreMeshManager) GetDevice() (*wgtypes.Device, error) {
|
||||
dev, err := m.Client.Device(m.IfName)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dev, nil
|
||||
}
|
||||
|
||||
// HasChanges returns true if we have changes since last time we synced
|
||||
func (m *TwoPhaseStoreMeshManager) HasChanges() bool {
|
||||
clockValue := m.store.GetClock()
|
||||
return clockValue != m.LastClock
|
||||
}
|
||||
|
||||
// Record that we have changes and save the corresponding changes
|
||||
func (m *TwoPhaseStoreMeshManager) SaveChanges() {
|
||||
clockValue := m.store.GetClock()
|
||||
m.LastClock = clockValue
|
||||
}
|
||||
|
||||
// UpdateTimeStamp: update the timestamp of the given node
|
||||
func (m *TwoPhaseStoreMeshManager) UpdateTimeStamp(nodeId string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
node.Timestamp = time.Now().Unix()
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRoutes: adds routes to the given node
|
||||
func (m *TwoPhaseStoreMeshManager) AddRoutes(nodeId string, routes ...mesh.Route) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
if len(routes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
|
||||
for _, route := range routes {
|
||||
node.Routes[route.GetDestination().String()] = Route{
|
||||
Destination: route.GetDestination().String(),
|
||||
Path: route.GetPath(),
|
||||
}
|
||||
}
|
||||
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteRoutes: deletes the routes from the node
|
||||
func (m *TwoPhaseStoreMeshManager) RemoveRoutes(nodeId string, routes ...string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
if len(routes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
|
||||
for _, route := range routes {
|
||||
delete(node.Routes, route)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSyncer: returns the automerge syncer for sync
|
||||
func (m *TwoPhaseStoreMeshManager) GetSyncer() mesh.MeshSyncer {
|
||||
return NewTwoPhaseSyncer(m)
|
||||
}
|
||||
|
||||
// GetNode get a particular not within the mesh
|
||||
func (m *TwoPhaseStoreMeshManager) GetNode(nodeId string) (mesh.MeshNode, error) {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return nil, fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
return &node, nil
|
||||
}
|
||||
|
||||
// NodeExists: returns true if a particular node exists false otherwise
|
||||
func (m *TwoPhaseStoreMeshManager) NodeExists(nodeId string) bool {
|
||||
return m.store.Contains(nodeId)
|
||||
}
|
||||
|
||||
// SetDescription: sets the description of this automerge data type
|
||||
func (m *TwoPhaseStoreMeshManager) SetDescription(nodeId string, description string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
node.Description = description
|
||||
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetAlias: set the alias of the nodeId
|
||||
func (m *TwoPhaseStoreMeshManager) SetAlias(nodeId string, alias string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
node.Description = alias
|
||||
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddService: adds the service to the given node
|
||||
func (m *TwoPhaseStoreMeshManager) AddService(nodeId string, key string, value string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
node.Services[key] = value
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveService: removes the service form the node. throws an error if the service does not exist
|
||||
func (m *TwoPhaseStoreMeshManager) RemoveService(nodeId string, key string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
delete(node.Services, key)
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Prune: prunes all nodes that have not updated their timestamp in
|
||||
// pruneAmount seconds
|
||||
func (m *TwoPhaseStoreMeshManager) Prune(pruneAmount int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPeers: get a list of contactable peers
|
||||
func (m *TwoPhaseStoreMeshManager) GetPeers() []string {
|
||||
nodes := lib.MapValues(m.store.AsMap())
|
||||
nodes = lib.Filter(nodes, func(mn MeshNode) bool {
|
||||
if mn.Type != string(conf.PEER_ROLE) {
|
||||
return false
|
||||
}
|
||||
|
||||
return time.Now().Unix()-mn.Timestamp < int64(m.conf.DeadTime)
|
||||
})
|
||||
|
||||
return lib.Map(nodes, func(mn MeshNode) string {
|
||||
return mn.PublicKey
|
||||
})
|
||||
}
|
||||
|
||||
func (m *TwoPhaseStoreMeshManager) getRoutes(targetNode string) (map[string]Route, error) {
|
||||
if !m.store.Contains(targetNode) {
|
||||
return nil, fmt.Errorf("getRoute: cannot get route %s does not exist", targetNode)
|
||||
}
|
||||
|
||||
node := m.store.Get(targetNode)
|
||||
return node.Routes, nil
|
||||
}
|
||||
|
||||
// GetRoutes(): Get all unique routes. Where the route with the least hop count is chosen
|
||||
func (m *TwoPhaseStoreMeshManager) 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)
|
||||
|
||||
// Add routes that the node directly has
|
||||
for _, route := range node.GetRoutes() {
|
||||
routes[route.GetDestination().String()] = route
|
||||
}
|
||||
|
||||
// Work out the other routes in the mesh
|
||||
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()]
|
||||
|
||||
hopCount := route.GetHopCount()
|
||||
|
||||
if node != targetNode {
|
||||
hopCount += 1
|
||||
}
|
||||
|
||||
if !ok || route.GetHopCount()+1 < otherRoute.GetHopCount() {
|
||||
routes[route.GetDestination().String()] = &Route{
|
||||
Destination: route.GetDestination().String(),
|
||||
Path: append(route.GetPath(), m.GetMeshId()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
// RemoveNode(): remove the node from the mesh
|
||||
func (m *TwoPhaseStoreMeshManager) RemoveNode(nodeId string) error {
|
||||
if !m.store.Contains(nodeId) {
|
||||
return fmt.Errorf("datastore: %s does not exist in the mesh", nodeId)
|
||||
}
|
||||
|
||||
m.store.Remove(nodeId)
|
||||
return nil
|
||||
}
|
73
pkg/crdt/factory.go
Normal file
73
pkg/crdt/factory.go
Normal file
@ -0,0 +1,73 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||
)
|
||||
|
||||
type TwoPhaseMapFactory struct{}
|
||||
|
||||
func (f *TwoPhaseMapFactory) CreateMesh(params *mesh.MeshProviderFactoryParams) (mesh.MeshProvider, error) {
|
||||
return &TwoPhaseStoreMeshManager{
|
||||
MeshId: params.MeshId,
|
||||
IfName: params.DevName,
|
||||
Client: params.Client,
|
||||
conf: params.Conf,
|
||||
store: NewTwoPhaseMap[string, MeshNode](params.NodeID),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type MeshNodeFactory struct {
|
||||
Config conf.WgMeshConfiguration
|
||||
}
|
||||
|
||||
func (f *MeshNodeFactory) Build(params *mesh.MeshNodeFactoryParams) mesh.MeshNode {
|
||||
hostName := f.getAddress(params)
|
||||
|
||||
grpcEndpoint := fmt.Sprintf("%s:%s", hostName, f.Config.GrpcPort)
|
||||
|
||||
if f.Config.Role == conf.CLIENT_ROLE {
|
||||
grpcEndpoint = "-"
|
||||
}
|
||||
|
||||
return &MeshNode{
|
||||
HostEndpoint: grpcEndpoint,
|
||||
PublicKey: params.PublicKey.String(),
|
||||
WgEndpoint: fmt.Sprintf("%s:%d", hostName, params.WgPort),
|
||||
WgHost: fmt.Sprintf("%s/128", params.NodeIP.String()),
|
||||
Routes: make(map[string]Route),
|
||||
Description: "",
|
||||
Alias: "",
|
||||
Type: string(f.Config.Role),
|
||||
}
|
||||
}
|
||||
|
||||
// getAddress returns the routable address of the machine.
|
||||
func (f *MeshNodeFactory) getAddress(params *mesh.MeshNodeFactoryParams) string {
|
||||
var hostName string = ""
|
||||
|
||||
if params.Endpoint != "" {
|
||||
hostName = params.Endpoint
|
||||
} else if len(f.Config.Endpoint) != 0 {
|
||||
hostName = f.Config.Endpoint
|
||||
} else {
|
||||
ipFunc := lib.GetPublicIP
|
||||
|
||||
if f.Config.IPDiscovery == conf.DNS_IP_DISCOVERY {
|
||||
ipFunc = lib.GetOutboundIP
|
||||
}
|
||||
|
||||
ip, err := ipFunc()
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
hostName = ip.String()
|
||||
}
|
||||
|
||||
return hostName
|
||||
}
|
121
pkg/crdt/g_map.go
Normal file
121
pkg/crdt/g_map.go
Normal file
@ -0,0 +1,121 @@
|
||||
// crdt is a golang implementation of a crdt
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Bucket[D any] struct {
|
||||
Vector uint64
|
||||
Contents D
|
||||
}
|
||||
|
||||
// GMap is a set that can only grow in size
|
||||
type GMap[K comparable, D any] struct {
|
||||
lock sync.RWMutex
|
||||
contents map[K]Bucket[D]
|
||||
getClock func() uint64
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Put(key K, value D) {
|
||||
g.lock.Lock()
|
||||
|
||||
clock := g.getClock() + 1
|
||||
|
||||
g.contents[key] = Bucket[D]{
|
||||
Vector: clock,
|
||||
Contents: value,
|
||||
}
|
||||
|
||||
g.lock.Unlock()
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Contains(key K) bool {
|
||||
g.lock.RLock()
|
||||
|
||||
_, ok := g.contents[key]
|
||||
|
||||
g.lock.RUnlock()
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) put(key K, b Bucket[D]) {
|
||||
g.lock.Lock()
|
||||
|
||||
if g.contents[key].Vector < b.Vector {
|
||||
g.contents[key] = b
|
||||
}
|
||||
|
||||
g.lock.Unlock()
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) get(key K) Bucket[D] {
|
||||
g.lock.RLock()
|
||||
bucket := g.contents[key]
|
||||
g.lock.RUnlock()
|
||||
|
||||
return bucket
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Get(key K) D {
|
||||
return g.get(key).Contents
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Keys() []K {
|
||||
g.lock.RLock()
|
||||
|
||||
contents := make([]K, len(g.contents))
|
||||
index := 0
|
||||
|
||||
for key := range g.contents {
|
||||
contents[index] = key
|
||||
index++
|
||||
}
|
||||
|
||||
g.lock.RUnlock()
|
||||
return contents
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Save() map[K]Bucket[D] {
|
||||
buckets := make(map[K]Bucket[D])
|
||||
g.lock.RLock()
|
||||
|
||||
for key, value := range g.contents {
|
||||
buckets[key] = value
|
||||
}
|
||||
|
||||
g.lock.RUnlock()
|
||||
return buckets
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) SaveWithKeys(keys []K) map[K]Bucket[D] {
|
||||
buckets := make(map[K]Bucket[D])
|
||||
g.lock.RLock()
|
||||
|
||||
for _, key := range keys {
|
||||
buckets[key] = g.contents[key]
|
||||
}
|
||||
|
||||
g.lock.RUnlock()
|
||||
return buckets
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) GetClock() map[K]uint64 {
|
||||
clock := make(map[K]uint64)
|
||||
g.lock.RLock()
|
||||
|
||||
for key, bucket := range g.contents {
|
||||
clock[key] = bucket.Vector
|
||||
}
|
||||
|
||||
g.lock.RUnlock()
|
||||
return clock
|
||||
}
|
||||
|
||||
func NewGMap[K comparable, D any](getClock func() uint64) *GMap[K, D] {
|
||||
return &GMap[K, D]{
|
||||
contents: make(map[K]Bucket[D]),
|
||||
getClock: getClock,
|
||||
}
|
||||
}
|
208
pkg/crdt/two_phase_map.go
Normal file
208
pkg/crdt/two_phase_map.go
Normal file
@ -0,0 +1,208 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
type TwoPhaseMap[K comparable, D any] struct {
|
||||
addMap *GMap[K, D]
|
||||
removeMap *GMap[K, bool]
|
||||
vectors map[K]uint64
|
||||
processId K
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
type TwoPhaseMapSnapshot[K comparable, D any] struct {
|
||||
Add map[K]Bucket[D]
|
||||
Remove map[K]Bucket[bool]
|
||||
}
|
||||
|
||||
// Contains checks whether the value exists in the map
|
||||
func (m *TwoPhaseMap[K, D]) Contains(key K) bool {
|
||||
if !m.addMap.Contains(key) {
|
||||
return false
|
||||
}
|
||||
|
||||
addValue := m.addMap.get(key)
|
||||
|
||||
if !m.removeMap.Contains(key) {
|
||||
return true
|
||||
}
|
||||
|
||||
removeValue := m.removeMap.get(key)
|
||||
|
||||
return addValue.Vector >= removeValue.Vector
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) Get(key K) D {
|
||||
var result D
|
||||
|
||||
if !m.Contains(key) {
|
||||
return result
|
||||
}
|
||||
|
||||
return m.addMap.Get(key)
|
||||
}
|
||||
|
||||
// Put places the key K in the map
|
||||
func (m *TwoPhaseMap[K, D]) Put(key K, data D) {
|
||||
msgSequence := m.incrementClock()
|
||||
|
||||
m.lock.Lock()
|
||||
|
||||
if _, ok := m.vectors[key]; !ok {
|
||||
m.vectors[key] = msgSequence
|
||||
}
|
||||
|
||||
m.lock.Unlock()
|
||||
m.addMap.Put(key, data)
|
||||
}
|
||||
|
||||
// Remove removes the value from the map
|
||||
func (m *TwoPhaseMap[K, D]) Remove(key K) {
|
||||
m.removeMap.Put(key, true)
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) Keys() []K {
|
||||
keys := make([]K, 0)
|
||||
|
||||
addKeys := m.addMap.Keys()
|
||||
|
||||
for _, key := range addKeys {
|
||||
if !m.Contains(key) {
|
||||
continue
|
||||
}
|
||||
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) AsMap() map[K]D {
|
||||
theMap := make(map[K]D)
|
||||
|
||||
keys := m.Keys()
|
||||
|
||||
for _, key := range keys {
|
||||
theMap[key] = m.Get(key)
|
||||
}
|
||||
|
||||
return theMap
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) Snapshot() *TwoPhaseMapSnapshot[K, D] {
|
||||
return &TwoPhaseMapSnapshot[K, D]{
|
||||
Add: m.addMap.Save(),
|
||||
Remove: m.removeMap.Save(),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) SnapShotFromState(state *TwoPhaseMapState[K]) *TwoPhaseMapSnapshot[K, D] {
|
||||
addKeys := lib.MapKeys(state.AddContents)
|
||||
removeKeys := lib.MapKeys(state.RemoveContents)
|
||||
|
||||
return &TwoPhaseMapSnapshot[K, D]{
|
||||
Add: m.addMap.SaveWithKeys(addKeys),
|
||||
Remove: m.removeMap.SaveWithKeys(removeKeys),
|
||||
}
|
||||
}
|
||||
|
||||
type TwoPhaseMapState[K comparable] struct {
|
||||
AddContents map[K]uint64
|
||||
RemoveContents map[K]uint64
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) incrementClock() uint64 {
|
||||
maxClock := uint64(0)
|
||||
m.lock.Lock()
|
||||
|
||||
for _, value := range m.vectors {
|
||||
maxClock = max(maxClock, value)
|
||||
}
|
||||
|
||||
m.vectors[m.processId] = maxClock + 1
|
||||
m.lock.Unlock()
|
||||
return maxClock
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) GetClock() uint64 {
|
||||
maxClock := uint64(0)
|
||||
m.lock.RLock()
|
||||
|
||||
for _, value := range m.vectors {
|
||||
maxClock = max(maxClock, value)
|
||||
}
|
||||
|
||||
m.lock.RUnlock()
|
||||
return maxClock
|
||||
}
|
||||
|
||||
// GetState: get the current vector clock of the add and remove
|
||||
// map
|
||||
func (m *TwoPhaseMap[K, D]) GenerateMessage() *TwoPhaseMapState[K] {
|
||||
addContents := m.addMap.GetClock()
|
||||
removeContents := m.removeMap.GetClock()
|
||||
|
||||
return &TwoPhaseMapState[K]{
|
||||
AddContents: addContents,
|
||||
RemoveContents: removeContents,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMapState[K]) Difference(state *TwoPhaseMapState[K]) *TwoPhaseMapState[K] {
|
||||
mapState := &TwoPhaseMapState[K]{
|
||||
AddContents: make(map[K]uint64),
|
||||
RemoveContents: make(map[K]uint64),
|
||||
}
|
||||
|
||||
for key, value := range state.AddContents {
|
||||
otherValue, ok := m.AddContents[key]
|
||||
|
||||
if !ok || otherValue < value {
|
||||
mapState.AddContents[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
for key, value := range state.AddContents {
|
||||
otherValue, ok := m.RemoveContents[key]
|
||||
|
||||
if !ok || otherValue < value {
|
||||
mapState.RemoveContents[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return mapState
|
||||
}
|
||||
|
||||
func (m *TwoPhaseMap[K, D]) Merge(snapshot TwoPhaseMapSnapshot[K, D]) {
|
||||
m.lock.Lock()
|
||||
|
||||
for key, value := range snapshot.Add {
|
||||
m.addMap.put(key, value)
|
||||
m.vectors[key] = max(value.Vector, m.vectors[key])
|
||||
}
|
||||
|
||||
for key, value := range snapshot.Remove {
|
||||
m.removeMap.put(key, value)
|
||||
m.vectors[key] = max(value.Vector, m.vectors[key])
|
||||
}
|
||||
|
||||
m.lock.Unlock()
|
||||
}
|
||||
|
||||
// NewTwoPhaseMap: create a new two phase map. Consists of two maps
|
||||
// a grow map and a remove map. If both timestamps equal then favour keeping
|
||||
// it in the map
|
||||
func NewTwoPhaseMap[K comparable, D any](processId K) *TwoPhaseMap[K, D] {
|
||||
m := TwoPhaseMap[K, D]{
|
||||
vectors: make(map[K]uint64),
|
||||
processId: processId,
|
||||
}
|
||||
|
||||
m.addMap = NewGMap[K, D](m.incrementClock)
|
||||
m.removeMap = NewGMap[K, bool](m.incrementClock)
|
||||
return &m
|
||||
}
|
145
pkg/crdt/two_phase_map_syncer.go
Normal file
145
pkg/crdt/two_phase_map_syncer.go
Normal file
@ -0,0 +1,145 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
)
|
||||
|
||||
type SyncState int
|
||||
|
||||
const (
|
||||
PREPARE SyncState = iota
|
||||
PRESENT
|
||||
EXCHANGE
|
||||
MERGE
|
||||
FINISHED
|
||||
)
|
||||
|
||||
// TwoPhaseSyncer is a type to sync a TwoPhase data store
|
||||
type TwoPhaseSyncer struct {
|
||||
manager *TwoPhaseStoreMeshManager
|
||||
generateMessageFSM SyncFSM
|
||||
state SyncState
|
||||
mapState *TwoPhaseMapState[string]
|
||||
peerMsg []byte
|
||||
}
|
||||
|
||||
type SyncFSM map[SyncState]func(*TwoPhaseSyncer) ([]byte, bool)
|
||||
|
||||
func prepare(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
var buffer bytes.Buffer
|
||||
enc := gob.NewEncoder(&buffer)
|
||||
|
||||
err := enc.Encode(*syncer.mapState)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof(err.Error())
|
||||
}
|
||||
|
||||
syncer.IncrementState()
|
||||
return buffer.Bytes(), true
|
||||
}
|
||||
|
||||
func present(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
if syncer.peerMsg == nil {
|
||||
panic("peer msg is nil")
|
||||
}
|
||||
|
||||
var recvBuffer = bytes.NewBuffer(syncer.peerMsg)
|
||||
dec := gob.NewDecoder(recvBuffer)
|
||||
|
||||
var mapState TwoPhaseMapState[string]
|
||||
err := dec.Decode(&mapState)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof(err.Error())
|
||||
}
|
||||
|
||||
difference := syncer.mapState.Difference(&mapState)
|
||||
|
||||
var sendBuffer bytes.Buffer
|
||||
enc := gob.NewEncoder(&sendBuffer)
|
||||
enc.Encode(*difference)
|
||||
|
||||
syncer.IncrementState()
|
||||
return sendBuffer.Bytes(), true
|
||||
}
|
||||
|
||||
func exchange(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
if syncer.peerMsg == nil {
|
||||
panic("peer msg is nil")
|
||||
}
|
||||
|
||||
var recvBuffer = bytes.NewBuffer(syncer.peerMsg)
|
||||
dec := gob.NewDecoder(recvBuffer)
|
||||
|
||||
var mapState TwoPhaseMapState[string]
|
||||
dec.Decode(&mapState)
|
||||
|
||||
snapshot := syncer.manager.store.SnapShotFromState(&mapState)
|
||||
|
||||
var sendBuffer bytes.Buffer
|
||||
enc := gob.NewEncoder(&sendBuffer)
|
||||
enc.Encode(*snapshot)
|
||||
|
||||
syncer.IncrementState()
|
||||
return sendBuffer.Bytes(), true
|
||||
}
|
||||
|
||||
func merge(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
if syncer.peerMsg == nil {
|
||||
panic("peer msg is nil")
|
||||
}
|
||||
|
||||
var recvBuffer = bytes.NewBuffer(syncer.peerMsg)
|
||||
dec := gob.NewDecoder(recvBuffer)
|
||||
|
||||
var snapshot TwoPhaseMapSnapshot[string, MeshNode]
|
||||
dec.Decode(&snapshot)
|
||||
|
||||
syncer.manager.store.Merge(snapshot)
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (t *TwoPhaseSyncer) IncrementState() {
|
||||
t.state = min(t.state+1, FINISHED)
|
||||
}
|
||||
|
||||
func (t *TwoPhaseSyncer) GenerateMessage() ([]byte, bool) {
|
||||
fsmFunc, ok := t.generateMessageFSM[t.state]
|
||||
|
||||
if !ok {
|
||||
panic("state not handled")
|
||||
}
|
||||
|
||||
return fsmFunc(t)
|
||||
}
|
||||
|
||||
func (t *TwoPhaseSyncer) RecvMessage(msg []byte) error {
|
||||
t.peerMsg = msg
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TwoPhaseSyncer) Complete() {
|
||||
logging.Log.WriteInfof("SYNC COMPLETED")
|
||||
t.manager.SaveChanges()
|
||||
}
|
||||
|
||||
func NewTwoPhaseSyncer(manager *TwoPhaseStoreMeshManager) *TwoPhaseSyncer {
|
||||
var generateMessageFsm SyncFSM = SyncFSM{
|
||||
PREPARE: prepare,
|
||||
PRESENT: present,
|
||||
EXCHANGE: exchange,
|
||||
MERGE: merge,
|
||||
}
|
||||
|
||||
return &TwoPhaseSyncer{
|
||||
manager: manager,
|
||||
state: PREPARE,
|
||||
mapState: manager.store.GenerateMessage(),
|
||||
generateMessageFSM: generateMessageFsm,
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package ctrlserver
|
||||
|
||||
import (
|
||||
crdt "github.com/tim-beatham/wgmesh/pkg/automerge"
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/conn"
|
||||
"github.com/tim-beatham/wgmesh/pkg/crdt"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ip"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
@ -21,14 +21,15 @@ type NewCtrlServerParams struct {
|
||||
CtrlProvider rpc.MeshCtrlServerServer
|
||||
SyncProvider rpc.SyncServiceServer
|
||||
Querier query.Querier
|
||||
OnDelete func(mesh.MeshProvider)
|
||||
}
|
||||
|
||||
// Create a new instance of the MeshCtrlServer or error if the
|
||||
// operation failed
|
||||
func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
||||
ctrlServer := new(MeshCtrlServer)
|
||||
meshFactory := crdt.CrdtProviderFactory{}
|
||||
nodeFactory := crdt.MeshNodeFactory{
|
||||
meshFactory := &crdt.TwoPhaseMapFactory{}
|
||||
nodeFactory := &crdt.MeshNodeFactory{
|
||||
Config: *params.Conf,
|
||||
}
|
||||
idGenerator := &lib.IDNameGenerator{}
|
||||
@ -40,12 +41,13 @@ func NewCtrlServer(params *NewCtrlServerParams) (*MeshCtrlServer, error) {
|
||||
meshManagerParams := &mesh.NewMeshManagerParams{
|
||||
Conf: *params.Conf,
|
||||
Client: params.Client,
|
||||
MeshProvider: &meshFactory,
|
||||
NodeFactory: &nodeFactory,
|
||||
MeshProvider: meshFactory,
|
||||
NodeFactory: nodeFactory,
|
||||
IdGenerator: idGenerator,
|
||||
IPAllocator: ipAllocator,
|
||||
InterfaceManipulator: interfaceManipulator,
|
||||
ConfigApplyer: configApplyer,
|
||||
OnDelete: params.OnDelete,
|
||||
}
|
||||
|
||||
ctrlServer.MeshManager = mesh.NewMeshManager(meshManagerParams)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ip"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
"github.com/tim-beatham/wgmesh/pkg/route"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
@ -32,10 +33,6 @@ type routeNode struct {
|
||||
route Route
|
||||
}
|
||||
|
||||
func (r *routeNode) equals(route2 *routeNode) bool {
|
||||
return r.gateway == route2.gateway && RouteEquals(r.route, route2.route)
|
||||
}
|
||||
|
||||
func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Device,
|
||||
peerToClients map[string][]net.IPNet,
|
||||
routes map[string][]routeNode) (*wgtypes.PeerConfig, error) {
|
||||
@ -63,9 +60,10 @@ func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Dev
|
||||
|
||||
for _, route := range node.GetRoutes() {
|
||||
bestRoutes := routes[route.GetDestination().String()]
|
||||
var pickedRoute routeNode
|
||||
|
||||
if len(bestRoutes) == 1 {
|
||||
allowedips = append(allowedips, *route.GetDestination())
|
||||
pickedRoute = bestRoutes[0]
|
||||
} else if len(bestRoutes) > 1 {
|
||||
keyFunc := func(mn MeshNode) int {
|
||||
pubKey, _ := mn.GetPublicKey()
|
||||
@ -77,11 +75,11 @@ func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Dev
|
||||
}
|
||||
|
||||
// Else there is more than one candidate so consistently hash
|
||||
pickedRoute := lib.ConsistentHash(bestRoutes, node, bucketFunc, keyFunc)
|
||||
pickedRoute = lib.ConsistentHash(bestRoutes, node, bucketFunc, keyFunc)
|
||||
}
|
||||
|
||||
if pickedRoute.gateway == pubKey.String() {
|
||||
allowedips = append(allowedips, *route.GetDestination())
|
||||
}
|
||||
if pickedRoute.gateway == pubKey.String() {
|
||||
allowedips = append(allowedips, *pickedRoute.route.GetDestination())
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,6 +99,7 @@ func (m *WgMeshConfigApplyer) convertMeshNode(node MeshNode, device *wgtypes.Dev
|
||||
Endpoint: endpoint,
|
||||
AllowedIPs: allowedips,
|
||||
PersistentKeepaliveInterval: &keepAlive,
|
||||
ReplaceAllowedIPs: true,
|
||||
}
|
||||
|
||||
return &peerConfig, nil
|
||||
@ -122,14 +121,9 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
||||
|
||||
for _, node := range mesh.GetNodes() {
|
||||
pubKey, _ := node.GetPublicKey()
|
||||
meshRoutes, _ := meshProvider.GetRoutes(pubKey.String())
|
||||
|
||||
for _, route := range meshRoutes {
|
||||
for _, route := range node.GetRoutes() {
|
||||
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
||||
if prefix == nil || route == nil || route.GetDestination() == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return prefix.Contains(route.GetDestination().IP)
|
||||
}) {
|
||||
continue
|
||||
@ -150,6 +144,8 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
||||
} else if route.GetHopCount() < otherRoute[0].route.GetHopCount() {
|
||||
otherRoute[0] = rn
|
||||
} else if otherRoute[0].route.GetHopCount() == route.GetHopCount() {
|
||||
logging.Log.WriteInfof("Other Route Hop: %d", otherRoute[0].route.GetHopCount())
|
||||
logging.Log.WriteInfof("Route gateway %s, route hop %d", rn.gateway, route.GetHopCount())
|
||||
routes[destination] = append(otherRoute, rn)
|
||||
}
|
||||
}
|
||||
@ -218,7 +214,6 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
|
||||
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
||||
|
||||
if !ipNet.Contains(route.IP) {
|
||||
|
||||
installedRoutes = append(installedRoutes, lib.Route{
|
||||
Gateway: n.GetWgHost().IP,
|
||||
Destination: route,
|
||||
@ -240,13 +235,13 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.routeInstaller.InstallRoutes(dev.Name, installedRoutes...)
|
||||
err = m.meshManager.GetClient().ConfigureDevice(dev.Name, cfg)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return m.meshManager.GetClient().ConfigureDevice(dev.Name, cfg)
|
||||
return m.routeInstaller.InstallRoutes(dev.Name, installedRoutes...)
|
||||
}
|
||||
|
||||
func (m *WgMeshConfigApplyer) ApplyConfig() error {
|
||||
@ -275,8 +270,7 @@ func (m *WgMeshConfigApplyer) RemovePeers(meshId string) error {
|
||||
}
|
||||
|
||||
m.meshManager.GetClient().ConfigureDevice(dev.Name, wgtypes.Config{
|
||||
ReplacePeers: true,
|
||||
Peers: make([]wgtypes.PeerConfig, 0),
|
||||
Peers: make([]wgtypes.PeerConfig, 0),
|
||||
})
|
||||
|
||||
return nil
|
||||
|
@ -3,6 +3,7 @@ package mesh
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ip"
|
||||
@ -18,7 +19,7 @@ type MeshManager interface {
|
||||
AddMesh(params *AddMeshParams) error
|
||||
HasChanges(meshid string) bool
|
||||
GetMesh(meshId string) MeshProvider
|
||||
GetPublicKey(meshId string) (*wgtypes.Key, error)
|
||||
GetPublicKey() *wgtypes.Key
|
||||
AddSelf(params *AddSelfParams) error
|
||||
LeaveMesh(meshId string) error
|
||||
GetSelf(meshId string) (MeshNode, error)
|
||||
@ -38,6 +39,7 @@ type MeshManager interface {
|
||||
}
|
||||
|
||||
type MeshManagerImpl struct {
|
||||
lock sync.RWMutex
|
||||
Meshes map[string]MeshProvider
|
||||
RouteManager RouteManager
|
||||
Client *wgctrl.Client
|
||||
@ -52,6 +54,7 @@ type MeshManagerImpl struct {
|
||||
ipAllocator ip.IPAllocator
|
||||
interfaceManipulator wg.WgInterfaceManipulator
|
||||
Monitor MeshMonitor
|
||||
OnDelete func(MeshProvider)
|
||||
}
|
||||
|
||||
// GetRouteManager implements MeshManager.
|
||||
@ -143,13 +146,16 @@ func (m *MeshManagerImpl) CreateMesh(port int) (string, error) {
|
||||
Conf: m.conf,
|
||||
Client: m.Client,
|
||||
MeshId: meshId,
|
||||
NodeID: m.HostParameters.GetPublicKey(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error creating mesh: %w", err)
|
||||
}
|
||||
|
||||
m.lock.Lock()
|
||||
m.Meshes[meshId] = nodeManager
|
||||
m.lock.Unlock()
|
||||
return meshId, nil
|
||||
}
|
||||
|
||||
@ -178,6 +184,7 @@ func (m *MeshManagerImpl) AddMesh(params *AddMeshParams) error {
|
||||
Conf: m.conf,
|
||||
Client: m.Client,
|
||||
MeshId: params.MeshId,
|
||||
NodeID: m.HostParameters.GetPublicKey(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -190,7 +197,9 @@ func (m *MeshManagerImpl) AddMesh(params *AddMeshParams) error {
|
||||
return err
|
||||
}
|
||||
|
||||
m.lock.Lock()
|
||||
m.Meshes[params.MeshId] = meshProvider
|
||||
m.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -206,25 +215,9 @@ func (m *MeshManagerImpl) GetMesh(meshId string) MeshProvider {
|
||||
}
|
||||
|
||||
// GetPublicKey: Gets the public key of the WireGuard mesh
|
||||
func (s *MeshManagerImpl) GetPublicKey(meshId string) (*wgtypes.Key, error) {
|
||||
if s.conf.StubWg {
|
||||
zeroedKey := make([]byte, wgtypes.KeyLen)
|
||||
return (*wgtypes.Key)(zeroedKey), nil
|
||||
}
|
||||
|
||||
mesh, ok := s.Meshes[meshId]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mesh does not exist")
|
||||
}
|
||||
|
||||
dev, err := mesh.GetDevice()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dev.PublicKey, nil
|
||||
func (s *MeshManagerImpl) GetPublicKey() *wgtypes.Key {
|
||||
key := s.HostParameters.PrivateKey.PublicKey()
|
||||
return &key
|
||||
}
|
||||
|
||||
type AddSelfParams struct {
|
||||
@ -289,14 +282,29 @@ func (s *MeshManagerImpl) AddSelf(params *AddSelfParams) error {
|
||||
|
||||
// LeaveMesh leaves the mesh network
|
||||
func (s *MeshManagerImpl) LeaveMesh(meshId string) error {
|
||||
mesh, exists := s.Meshes[meshId]
|
||||
mesh := s.GetMesh(meshId)
|
||||
|
||||
if !exists {
|
||||
if mesh == nil {
|
||||
return fmt.Errorf("mesh %s does not exist", meshId)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
s.RouteManager.RemoveRoutes(meshId)
|
||||
err = mesh.RemoveNode(s.HostParameters.GetPublicKey())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.OnDelete != nil {
|
||||
s.OnDelete(mesh)
|
||||
}
|
||||
|
||||
s.lock.Lock()
|
||||
delete(s.Meshes, meshId)
|
||||
s.lock.Unlock()
|
||||
|
||||
if !s.conf.StubWg {
|
||||
device, err := mesh.GetDevice()
|
||||
|
||||
@ -311,8 +319,6 @@ func (s *MeshManagerImpl) LeaveMesh(meshId string) error {
|
||||
}
|
||||
}
|
||||
|
||||
err = s.RouteManager.RemoveRoutes(meshId)
|
||||
delete(s.Meshes, meshId)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -348,7 +354,8 @@ func (s *MeshManagerImpl) ApplyConfig() error {
|
||||
}
|
||||
|
||||
func (s *MeshManagerImpl) SetDescription(description string) error {
|
||||
for _, mesh := range s.Meshes {
|
||||
meshes := s.GetMeshes()
|
||||
for _, mesh := range meshes {
|
||||
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
||||
err := mesh.SetDescription(s.HostParameters.GetPublicKey(), description)
|
||||
|
||||
@ -363,7 +370,8 @@ func (s *MeshManagerImpl) SetDescription(description string) error {
|
||||
|
||||
// SetAlias implements MeshManager.
|
||||
func (s *MeshManagerImpl) SetAlias(alias string) error {
|
||||
for _, mesh := range s.Meshes {
|
||||
meshes := s.GetMeshes()
|
||||
for _, mesh := range meshes {
|
||||
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
||||
err := mesh.SetAlias(s.HostParameters.GetPublicKey(), alias)
|
||||
|
||||
@ -377,7 +385,8 @@ func (s *MeshManagerImpl) SetAlias(alias string) error {
|
||||
|
||||
// UpdateTimeStamp updates the timestamp of this node in all meshes
|
||||
func (s *MeshManagerImpl) UpdateTimeStamp() error {
|
||||
for _, mesh := range s.Meshes {
|
||||
meshes := s.GetMeshes()
|
||||
for _, mesh := range meshes {
|
||||
if mesh.NodeExists(s.HostParameters.GetPublicKey()) {
|
||||
err := mesh.UpdateTimeStamp(s.HostParameters.GetPublicKey())
|
||||
|
||||
@ -395,7 +404,16 @@ func (s *MeshManagerImpl) GetClient() *wgctrl.Client {
|
||||
}
|
||||
|
||||
func (s *MeshManagerImpl) GetMeshes() map[string]MeshProvider {
|
||||
return s.Meshes
|
||||
meshes := make(map[string]MeshProvider)
|
||||
|
||||
s.lock.RLock()
|
||||
|
||||
for id, mesh := range s.Meshes {
|
||||
meshes[id] = mesh
|
||||
}
|
||||
|
||||
s.lock.RUnlock()
|
||||
return meshes
|
||||
}
|
||||
|
||||
// Close the mesh manager
|
||||
@ -432,6 +450,7 @@ type NewMeshManagerParams struct {
|
||||
InterfaceManipulator wg.WgInterfaceManipulator
|
||||
ConfigApplyer MeshConfigApplyer
|
||||
RouteManager RouteManager
|
||||
OnDelete func(MeshProvider)
|
||||
}
|
||||
|
||||
// Creates a new instance of a mesh manager with the given parameters
|
||||
@ -466,5 +485,6 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
||||
aliasManager := NewAliasManager()
|
||||
m.Monitor.AddUpdateCallback(aliasManager.AddAliases)
|
||||
m.Monitor.AddRemoveCallback(aliasManager.RemoveAliases)
|
||||
m.OnDelete = params.OnDelete
|
||||
return m
|
||||
}
|
||||
|
@ -81,6 +81,11 @@ type MeshProviderStub struct {
|
||||
snapshot *MeshSnapshotStub
|
||||
}
|
||||
|
||||
// RemoveNode implements MeshProvider.
|
||||
func (*MeshProviderStub) RemoveNode(nodeId string) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (*MeshProviderStub) GetRoutes(targetId string) (map[string]Route, error) {
|
||||
return nil, nil
|
||||
}
|
||||
@ -287,9 +292,9 @@ func (m *MeshManagerStub) GetMesh(meshId string) MeshProvider {
|
||||
snapshot: &MeshSnapshotStub{nodes: make(map[string]MeshNode)}}
|
||||
}
|
||||
|
||||
func (m *MeshManagerStub) GetPublicKey(meshId string) (*wgtypes.Key, error) {
|
||||
func (m *MeshManagerStub) GetPublicKey() *wgtypes.Key {
|
||||
key, _ := wgtypes.GenerateKey()
|
||||
return &key, nil
|
||||
return &key
|
||||
}
|
||||
|
||||
func (m *MeshManagerStub) AddSelf(params *AddSelfParams) error {
|
||||
|
@ -138,6 +138,8 @@ type MeshProvider interface {
|
||||
GetPeers() []string
|
||||
// GetRoutes(): Get all unique routes. Where the route with the least hop count is chosen
|
||||
GetRoutes(targetNode string) (map[string]Route, error)
|
||||
// RemoveNode(): remove the node from the mesh
|
||||
RemoveNode(nodeId string) error
|
||||
}
|
||||
|
||||
// HostParameters contains the IDs of a node
|
||||
@ -157,6 +159,7 @@ type MeshProviderFactoryParams struct {
|
||||
Port int
|
||||
Conf *conf.WgMeshConfiguration
|
||||
Client *wgctrl.Client
|
||||
NodeID string
|
||||
}
|
||||
|
||||
// MeshProviderFactory creates an instance of a mesh provider
|
||||
|
@ -36,28 +36,19 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
|
||||
logging.Log.WriteInfof("UPDATING WG CONF")
|
||||
|
||||
if s.manager.HasChanges(meshId) {
|
||||
err := s.manager.ApplyConfig()
|
||||
s.manager.GetRouteManager().UpdateRoutes()
|
||||
err := s.manager.ApplyConfig()
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof("Failed to update config %w", err)
|
||||
}
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof("Failed to update config %w", err)
|
||||
}
|
||||
|
||||
publicKey := s.manager.GetPublicKey()
|
||||
|
||||
logging.Log.WriteInfof(publicKey.String())
|
||||
|
||||
nodeNames := s.manager.GetMesh(meshId).GetPeers()
|
||||
self, err := s.manager.GetSelf(meshId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
selfPublickey, err := self.GetPublicKey()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
neighbours := s.cluster.GetNeighbours(nodeNames, selfPublickey.String())
|
||||
neighbours := s.cluster.GetNeighbours(nodeNames, publicKey.String())
|
||||
randomSubset := lib.RandomSubsetOfLength(neighbours, s.conf.BranchRate)
|
||||
|
||||
for _, node := range randomSubset {
|
||||
@ -68,7 +59,7 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
|
||||
if len(nodeNames) > s.conf.ClusterSize && rand.Float64() < s.conf.InterClusterChance {
|
||||
logging.Log.WriteInfof("Sending to random cluster")
|
||||
interCluster := s.cluster.GetInterCluster(nodeNames, selfPublickey.String())
|
||||
interCluster := s.cluster.GetInterCluster(nodeNames, publicKey.String())
|
||||
randomSubset = append(randomSubset, interCluster)
|
||||
}
|
||||
|
||||
@ -98,10 +89,6 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
logging.Log.WriteInfof("SYNC COUNT: %d", s.syncCount)
|
||||
|
||||
s.infectionCount = ((s.conf.InfectionCount + s.infectionCount - 1) % s.conf.InfectionCount)
|
||||
|
||||
// Check if any changes have occurred and trigger callbacks
|
||||
// if changes have occurred.
|
||||
// return s.manager.GetMonitor().Trigger()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,6 @@ func syncFunction(syncer Syncer) lib.TimerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func NewSyncScheduler(s *ctrlserver.MeshCtrlServer, syncRequester SyncRequester) *lib.Timer {
|
||||
syncer := NewSyncer(s.MeshManager, s.Conf, syncRequester)
|
||||
func NewSyncScheduler(s *ctrlserver.MeshCtrlServer, syncRequester SyncRequester, syncer Syncer) *lib.Timer {
|
||||
return lib.NewTimer(syncFunction(syncer), int(s.Conf.SyncRate))
|
||||
}
|
||||
|
@ -3,20 +3,14 @@ package timer
|
||||
import (
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
)
|
||||
|
||||
func NewTimestampScheduler(ctrlServer *ctrlserver.MeshCtrlServer) lib.Timer {
|
||||
timerFunc := func() error {
|
||||
logging.Log.WriteInfof("Updated Timestamp")
|
||||
return ctrlServer.MeshManager.UpdateTimeStamp()
|
||||
}
|
||||
|
||||
return *lib.NewTimer(timerFunc, ctrlServer.Conf.KeepAliveTime)
|
||||
}
|
||||
|
||||
func NewRouteScheduler(ctrlServer *ctrlserver.MeshCtrlServer) lib.Timer {
|
||||
timerFunc := func() error {
|
||||
return ctrlServer.MeshManager.GetRouteManager().UpdateRoutes()
|
||||
}
|
||||
|
||||
return *lib.NewTimer(timerFunc, 10)
|
||||
}
|
||||
|
Reference in New Issue
Block a user