2023-10-26 17:53:12 +02:00
|
|
|
// mesh provides implementation agnostic logic for managing
|
|
|
|
// the mesh
|
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
type Route interface {
|
|
|
|
// GetDestination: returns the destination of the route
|
|
|
|
GetDestination() *net.IPNet
|
|
|
|
// GetHopCount: get the total hopcount of the prefix
|
|
|
|
GetHopCount() int
|
2023-11-27 19:55:41 +01:00
|
|
|
// GetPath: get a list of AS paths to get to the destination
|
|
|
|
GetPath() []string
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type RouteStub struct {
|
|
|
|
Destination *net.IPNet
|
|
|
|
HopCount int
|
2023-11-27 19:55:41 +01:00
|
|
|
Path []string
|
2023-11-25 04:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RouteStub) GetDestination() *net.IPNet {
|
|
|
|
return r.Destination
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RouteStub) GetHopCount() int {
|
|
|
|
return r.HopCount
|
|
|
|
}
|
|
|
|
|
2023-11-27 19:55:41 +01:00
|
|
|
func (r *RouteStub) GetPath() []string {
|
|
|
|
return r.Path
|
|
|
|
}
|
|
|
|
|
2023-10-26 17:53:12 +02:00
|
|
|
// MeshNode represents an implementation of a node in a mesh
|
|
|
|
type MeshNode interface {
|
|
|
|
// GetHostEndpoint: gets the gRPC endpoint of the node
|
|
|
|
GetHostEndpoint() string
|
|
|
|
// GetPublicKey: gets the public key of the node
|
|
|
|
GetPublicKey() (wgtypes.Key, error)
|
|
|
|
// GetWgEndpoint(): get IP and port of the wireguard endpoint
|
|
|
|
GetWgEndpoint() string
|
|
|
|
// GetWgHost: get the IP address of the WireGuard node
|
|
|
|
GetWgHost() *net.IPNet
|
|
|
|
// GetTimestamp: get the UNIX time stamp of the ndoe
|
|
|
|
GetTimeStamp() int64
|
|
|
|
// GetRoutes: returns the routes that the nodes provides
|
2023-11-25 04:15:58 +01:00
|
|
|
GetRoutes() []Route
|
2023-10-27 18:49:18 +02:00
|
|
|
// GetIdentifier: returns the identifier of the node
|
|
|
|
GetIdentifier() string
|
2023-11-01 12:58:10 +01:00
|
|
|
// GetDescription: returns the description for this node
|
|
|
|
GetDescription() string
|
2023-11-17 20:05:21 +01:00
|
|
|
// GetAlias: associates the node with an alias. Potentially used
|
|
|
|
// for DNS and so forth.
|
|
|
|
GetAlias() string
|
2023-11-17 23:13:51 +01:00
|
|
|
// GetServices: returns a list of services offered by the node
|
|
|
|
GetServices() map[string]string
|
2023-11-21 17:42:49 +01:00
|
|
|
GetType() conf.NodeType
|
2023-11-17 23:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeEquals: determines if two mesh nodes are equivalent to one another
|
|
|
|
func NodeEquals(node1, node2 MeshNode) bool {
|
2023-11-24 18:49:06 +01:00
|
|
|
key1, _ := node1.GetPublicKey()
|
|
|
|
key2, _ := node2.GetPublicKey()
|
|
|
|
|
|
|
|
return key1.String() == key2.String()
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:56:30 +01:00
|
|
|
func RouteEquals(route1, route2 Route) bool {
|
|
|
|
return route1.GetDestination().String() == route2.GetDestination().String() &&
|
|
|
|
route1.GetHopCount() == route2.GetHopCount()
|
|
|
|
}
|
|
|
|
|
2023-11-24 18:49:06 +01:00
|
|
|
func NodeID(node MeshNode) string {
|
|
|
|
key, _ := node.GetPublicKey()
|
|
|
|
return key.String()
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MeshSnapshot interface {
|
|
|
|
// GetNodes() returns the nodes in the mesh
|
|
|
|
GetNodes() map[string]MeshNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeshSyncer syncs two meshes
|
|
|
|
type MeshSyncer interface {
|
|
|
|
GenerateMessage() ([]byte, bool)
|
|
|
|
RecvMessage(mesg []byte) error
|
|
|
|
Complete()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mesh: Represents an implementation of a mesh
|
|
|
|
type MeshProvider interface {
|
|
|
|
// AddNode() adds a node to the mesh
|
|
|
|
AddNode(node MeshNode)
|
2023-11-20 12:28:12 +01:00
|
|
|
// GetMesh() returns a snapshot of the mesh provided by the mesh provider.
|
2023-10-26 17:53:12 +02:00
|
|
|
GetMesh() (MeshSnapshot, error)
|
|
|
|
// GetMeshId() returns the ID of the mesh network
|
|
|
|
GetMeshId() string
|
|
|
|
// Save() saves the mesh network
|
|
|
|
Save() []byte
|
|
|
|
// Load() loads a mesh network
|
|
|
|
Load([]byte) error
|
|
|
|
// GetDevice() get the device corresponding with the mesh
|
|
|
|
GetDevice() (*wgtypes.Device, error)
|
|
|
|
// HasChanges returns true if we have changes since last time we synced
|
|
|
|
HasChanges() bool
|
2023-11-06 10:54:06 +01:00
|
|
|
// Record that we have changes and save the corresponding changes
|
2023-10-26 17:53:12 +02:00
|
|
|
SaveChanges()
|
|
|
|
// UpdateTimeStamp: update the timestamp of the given node
|
|
|
|
UpdateTimeStamp(nodeId string) error
|
|
|
|
// AddRoutes: adds routes to the given node
|
2023-11-25 04:15:58 +01:00
|
|
|
AddRoutes(nodeId string, route ...Route) error
|
2023-11-07 20:48:53 +01:00
|
|
|
// DeleteRoutes: deletes the routes from the node
|
|
|
|
RemoveRoutes(nodeId string, route ...string) error
|
2023-11-06 10:54:06 +01:00
|
|
|
// GetSyncer: returns the automerge syncer for sync
|
2023-10-26 17:53:12 +02:00
|
|
|
GetSyncer() MeshSyncer
|
2023-11-20 12:28:12 +01:00
|
|
|
// GetNode get a particular not within the mesh
|
|
|
|
GetNode(string) (MeshNode, error)
|
|
|
|
// NodeExists: returns true if a particular node exists false otherwise
|
|
|
|
NodeExists(string) bool
|
2023-11-06 10:54:06 +01:00
|
|
|
// SetDescription: sets the description of this automerge data type
|
2023-11-01 12:58:10 +01:00
|
|
|
SetDescription(nodeId string, description string) error
|
2023-11-17 20:05:21 +01:00
|
|
|
// SetAlias: set the alias of the nodeId
|
|
|
|
SetAlias(nodeId string, alias string) error
|
2023-11-17 23:13:51 +01:00
|
|
|
// AddService: adds the service to the given node
|
|
|
|
AddService(nodeId, key, value string) error
|
|
|
|
// RemoveService: removes the service form the node. throws an error if the service does not exist
|
|
|
|
RemoveService(nodeId, key string) error
|
2023-12-07 02:44:54 +01:00
|
|
|
// Prune: prunes all nodes that have not updated their
|
|
|
|
// vector clock
|
|
|
|
Prune() error
|
2023-11-24 13:37:54 +01:00
|
|
|
// GetPeers: get a list of contactable peers
|
2023-11-21 17:42:49 +01:00
|
|
|
GetPeers() []string
|
2023-11-25 04:15:58 +01:00
|
|
|
// GetRoutes(): Get all unique routes. Where the route with the least hop count is chosen
|
|
|
|
GetRoutes(targetNode string) (map[string]Route, error)
|
2023-11-28 15:42:09 +01:00
|
|
|
// RemoveNode(): remove the node from the mesh
|
|
|
|
RemoveNode(nodeId string) error
|
2023-12-06 23:45:04 +01:00
|
|
|
// Mark: marks the node as unreachable. This is not broadcast to the entire
|
|
|
|
// this is not considered when syncing node state
|
|
|
|
Mark(nodeId string)
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HostParameters contains the IDs of a node
|
|
|
|
type HostParameters struct {
|
2023-11-24 13:07:03 +01:00
|
|
|
PrivateKey *wgtypes.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPublicKey: gets the public key of the node
|
|
|
|
func (h *HostParameters) GetPublicKey() string {
|
|
|
|
return h.PrivateKey.PublicKey().String()
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeshProviderFactoryParams parameters required to build a mesh provider
|
|
|
|
type MeshProviderFactoryParams struct {
|
|
|
|
DevName string
|
|
|
|
MeshId string
|
|
|
|
Port int
|
|
|
|
Conf *conf.WgMeshConfiguration
|
|
|
|
Client *wgctrl.Client
|
2023-11-30 03:02:38 +01:00
|
|
|
NodeID string
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeshProviderFactory creates an instance of a mesh provider
|
|
|
|
type MeshProviderFactory interface {
|
|
|
|
CreateMesh(params *MeshProviderFactoryParams) (MeshProvider, error)
|
|
|
|
}
|
2023-10-28 17:38:25 +02:00
|
|
|
|
|
|
|
// MeshNodeFactoryParams are the parameters required to construct
|
|
|
|
// a mesh node
|
|
|
|
type MeshNodeFactoryParams struct {
|
2023-12-07 19:18:13 +01:00
|
|
|
PublicKey *wgtypes.Key
|
2023-10-28 17:38:25 +02:00
|
|
|
NodeIP net.IP
|
|
|
|
WgPort int
|
|
|
|
Endpoint string
|
|
|
|
}
|
|
|
|
|
|
|
|
// MeshBuilder build the hosts mesh node for it to be added to the mesh
|
|
|
|
type MeshNodeFactory interface {
|
|
|
|
Build(params *MeshNodeFactoryParams) MeshNode
|
|
|
|
}
|