mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-04 21:50:49 +01:00
61-improve-unit-testing-coverage
- Got unit tests passing - Improved manager unit tests
This commit is contained in:
parent
bed59f120f
commit
ceb43a1db1
@ -26,8 +26,8 @@ const (
|
||||
type IPDiscovery string
|
||||
|
||||
const (
|
||||
PUBLIC_IP_DISCOVERY = "public"
|
||||
DNS_IP_DISCOVERY = "dns"
|
||||
PUBLIC_IP_DISCOVERY IPDiscovery = "public"
|
||||
DNS_IP_DISCOVERY IPDiscovery = "dns"
|
||||
)
|
||||
|
||||
// WgConfiguration contains per-mesh WireGuard configuration. Contains poitner types only so we can
|
||||
@ -61,11 +61,11 @@ type WgConfiguration struct {
|
||||
|
||||
type DaemonConfiguration struct {
|
||||
// CertificatePath is the path to the certificate to use in mTLS
|
||||
CertificatePath string `yaml:"certificatePath" validate:"required,file"`
|
||||
CertificatePath string `yaml:"certificatePath" validate:"required"`
|
||||
// PrivateKeypath is the path to the clients private key in mTLS
|
||||
PrivateKeyPath string `yaml:"privateKeyPath" validate:"required,file"`
|
||||
PrivateKeyPath string `yaml:"privateKeyPath" validate:"required"`
|
||||
// CaCeritifcatePath path to the certificate of the trust certificate authority
|
||||
CaCertificatePath string `yaml:"caCertificatePath" validate:"required,file"`
|
||||
CaCertificatePath string `yaml:"caCertificatePath" validate:"required"`
|
||||
// SkipCertVerification specify to skip certificate verification. Should only be used
|
||||
// in test environments
|
||||
SkipCertVerification bool `yaml:"skipCertVerification"`
|
||||
@ -83,9 +83,9 @@ type DaemonConfiguration struct {
|
||||
// send to every member in the mesh
|
||||
KeepAliveTime int `yaml:"keepAliveTime" validate:"required,gte=1"`
|
||||
// ClusterSize specifies how many neighbours you should synchronise with per round
|
||||
ClusterSize int `yaml:"clusterSize" valdiate:"required,gt=0"`
|
||||
ClusterSize int `yaml:"clusterSize" validate:"gte=1"`
|
||||
// InterClusterChance specifies the probabilityof inter-cluster communication in a sync round
|
||||
InterClusterChance float64 `yaml:"interClusterChance" valdiate:"required,gt=0"`
|
||||
InterClusterChance float64 `yaml:"interClusterChance" validate:"gt=0"`
|
||||
// BranchRate specifies the number of nodes to synchronise with when a node has
|
||||
// new changes to send to the mesh
|
||||
BranchRate int `yaml:"branchRate" validate:"required,gte=1"`
|
||||
|
@ -1,84 +0,0 @@
|
||||
package conn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
// ConnectionWindow maintains a sliding window of connections between users
|
||||
type ConnectionWindow interface {
|
||||
// GetWindow is a list of connections to choose from
|
||||
GetWindow() []string
|
||||
// SlideConnection removes a node from the window and adds a random node
|
||||
// not already in the window. connList represents the list of possible
|
||||
// connections to choose from
|
||||
SlideConnection(connList []string) error
|
||||
// PushConneciton is used when connection list less than window size.
|
||||
PutConnection(conn []string) error
|
||||
// IsFull returns true if the window is full. In which case we must slide the window
|
||||
IsFull() bool
|
||||
}
|
||||
|
||||
type ConnectionWindowImpl struct {
|
||||
window []string
|
||||
windowSize int
|
||||
}
|
||||
|
||||
// GetWindow gets the current list of active connections in
|
||||
// the window
|
||||
func (c *ConnectionWindowImpl) GetWindow() []string {
|
||||
return c.window
|
||||
}
|
||||
|
||||
// SlideConnection slides the connection window by one shuffling items
|
||||
// in the windows
|
||||
func (c *ConnectionWindowImpl) SlideConnection(connList []string) error {
|
||||
// If the number of peer connections is less than the length of the window
|
||||
// then exit early. Can't slide the window it should contain all nodes!
|
||||
if len(c.window) < c.windowSize {
|
||||
return nil
|
||||
}
|
||||
|
||||
filter := func(node string) bool {
|
||||
return !slices.Contains(c.window, node)
|
||||
}
|
||||
|
||||
pool := lib.Filter(connList, filter)
|
||||
newNode := lib.RandomSubsetOfLength(pool, 1)
|
||||
|
||||
if len(newNode) == 0 {
|
||||
return errors.New("could not slide window")
|
||||
}
|
||||
|
||||
for i := len(c.window) - 1; i >= 1; i-- {
|
||||
c.window[i] = c.window[i-1]
|
||||
}
|
||||
|
||||
c.window[0] = newNode[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutConnection put random connections in the connection
|
||||
func (c *ConnectionWindowImpl) PutConnection(connList []string) error {
|
||||
if len(c.window) >= c.windowSize {
|
||||
return errors.New("cannot place connection. Window full need to slide")
|
||||
}
|
||||
|
||||
c.window = lib.RandomSubsetOfLength(connList, c.windowSize)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ConnectionWindowImpl) IsFull() bool {
|
||||
return len(c.window) >= c.windowSize
|
||||
}
|
||||
|
||||
func NewConnectionWindow(windowLength int) ConnectionWindow {
|
||||
window := &ConnectionWindowImpl{
|
||||
window: make([]string, 0),
|
||||
windowSize: windowLength,
|
||||
}
|
||||
|
||||
return window
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
// hosts: utility for modifying the /etc/hosts file
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HOSTS_FILE is the hosts file location
|
||||
const HOSTS_FILE = "/etc/hosts"
|
||||
|
||||
const DOMAIN_HEADER = "#WG AUTO GENERATED HOSTS"
|
||||
const DOMAIN_TRAILER = "#WG AUTO GENERATED HOSTS END"
|
||||
|
||||
type HostsEntry struct {
|
||||
Alias string
|
||||
Ip net.IP
|
||||
}
|
||||
|
||||
// Generic interface to manipulate /etc/hosts file
|
||||
type HostsManipulator interface {
|
||||
// AddrAddr associates an aliasd with a given IP address
|
||||
AddAddr(hosts ...HostsEntry)
|
||||
// Remove deletes the entry from /etc/hosts
|
||||
Remove(hosts ...HostsEntry)
|
||||
// Writes the changes to /etc/hosts file
|
||||
Write() error
|
||||
}
|
||||
|
||||
type HostsManipulatorImpl struct {
|
||||
hosts map[string]HostsEntry
|
||||
}
|
||||
|
||||
// AddAddr implements HostsManipulator.
|
||||
func (m *HostsManipulatorImpl) AddAddr(hosts ...HostsEntry) {
|
||||
changed := false
|
||||
|
||||
for _, host := range hosts {
|
||||
prev, ok := m.hosts[host.Ip.String()]
|
||||
|
||||
if !ok || prev.Alias != host.Alias {
|
||||
changed = true
|
||||
}
|
||||
|
||||
m.hosts[host.Ip.String()] = host
|
||||
}
|
||||
|
||||
if changed {
|
||||
m.Write()
|
||||
}
|
||||
}
|
||||
|
||||
// Remove implements HostsManipulator.
|
||||
func (m *HostsManipulatorImpl) Remove(hosts ...HostsEntry) {
|
||||
lenBefore := len(m.hosts)
|
||||
|
||||
for _, host := range hosts {
|
||||
delete(m.hosts, host.Alias)
|
||||
}
|
||||
|
||||
if lenBefore != len(m.hosts) {
|
||||
m.Write()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *HostsManipulatorImpl) removeHosts() string {
|
||||
hostsFile, err := os.ReadFile(HOSTS_FILE)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var contents strings.Builder
|
||||
|
||||
scanner := bufio.NewScanner(bytes.NewReader(hostsFile))
|
||||
|
||||
hostsSection := false
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if !hostsSection && strings.Contains(line, DOMAIN_HEADER) {
|
||||
hostsSection = true
|
||||
}
|
||||
|
||||
if !hostsSection {
|
||||
contents.WriteString(line + "\n")
|
||||
}
|
||||
|
||||
if hostsSection && strings.Contains(line, DOMAIN_TRAILER) {
|
||||
hostsSection = false
|
||||
}
|
||||
}
|
||||
|
||||
if scanner.Err() != nil && scanner.Err() != io.EOF {
|
||||
return ""
|
||||
}
|
||||
|
||||
return contents.String()
|
||||
}
|
||||
|
||||
// Write implements HostsManipulator
|
||||
func (m *HostsManipulatorImpl) Write() error {
|
||||
contents := m.removeHosts()
|
||||
|
||||
var nextHosts strings.Builder
|
||||
nextHosts.WriteString(contents)
|
||||
|
||||
nextHosts.WriteString(DOMAIN_HEADER + "\n")
|
||||
|
||||
for _, host := range m.hosts {
|
||||
nextHosts.WriteString(fmt.Sprintf("%s\t%s\n", host.Ip.String(), host.Alias))
|
||||
}
|
||||
|
||||
nextHosts.WriteString(DOMAIN_TRAILER + "\n")
|
||||
return os.WriteFile(HOSTS_FILE, []byte(nextHosts.String()), 0644)
|
||||
}
|
||||
|
||||
func NewHostsManipulator() HostsManipulator {
|
||||
return &HostsManipulatorImpl{hosts: make(map[string]HostsEntry)}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
// lib contains helper functions for the implementation
|
||||
package lib
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"math"
|
||||
|
||||
"gonum.org/v1/gonum/stat"
|
||||
"gonum.org/v1/gonum/stat/distuv"
|
||||
)
|
||||
|
||||
// Modelling the distribution using a normal distribution get the count
|
||||
// of the outliers
|
||||
func GetOutliers[K cmp.Ordered](counts map[K]uint64, alpha float64) []K {
|
||||
n := float64(len(counts))
|
||||
|
||||
keys := MapKeys(counts)
|
||||
values := make([]float64, len(keys))
|
||||
|
||||
for index, key := range keys {
|
||||
values[index] = float64(counts[key])
|
||||
}
|
||||
|
||||
mean := stat.Mean(values, nil)
|
||||
stdDev := stat.StdDev(values, nil)
|
||||
|
||||
moe := distuv.Normal{Mu: 0, Sigma: 1}.Quantile(1-alpha/2) * (stdDev / math.Sqrt(n))
|
||||
|
||||
lowerBound := mean - moe
|
||||
|
||||
var outliers []K
|
||||
|
||||
for i, count := range values {
|
||||
if count < lowerBound {
|
||||
outliers = append(outliers, keys[i])
|
||||
}
|
||||
}
|
||||
|
||||
return outliers
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/hosts"
|
||||
)
|
||||
|
||||
type MeshAliasManager interface {
|
||||
AddAliases(nodes []MeshNode)
|
||||
RemoveAliases(node []MeshNode)
|
||||
}
|
||||
|
||||
type AliasManager struct {
|
||||
hosts hosts.HostsManipulator
|
||||
}
|
||||
|
||||
// AddAliases: on node update or change add aliases to the hosts file
|
||||
func (a *AliasManager) AddAliases(nodes []MeshNode) {
|
||||
for _, node := range nodes {
|
||||
if node.GetAlias() != "" {
|
||||
a.hosts.AddAddr(hosts.HostsEntry{
|
||||
Alias: fmt.Sprintf("%s.smeg", node.GetAlias()),
|
||||
Ip: node.GetWgHost().IP,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveAliases: on node remove remove aliases from the hosts file
|
||||
func (a *AliasManager) RemoveAliases(nodes []MeshNode) {
|
||||
for _, node := range nodes {
|
||||
if node.GetAlias() != "" {
|
||||
a.hosts.Remove(hosts.HostsEntry{
|
||||
Alias: fmt.Sprintf("%s.smeg", node.GetAlias()),
|
||||
Ip: node.GetWgHost().IP,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewAliasManager() MeshAliasManager {
|
||||
return &AliasManager{
|
||||
hosts: hosts.NewHostsManipulator(),
|
||||
}
|
||||
}
|
@ -32,7 +32,6 @@ type MeshManager interface {
|
||||
GetClient() *wgctrl.Client
|
||||
GetMeshes() map[string]MeshProvider
|
||||
Close() error
|
||||
GetMonitor() MeshMonitor
|
||||
GetNode(string, string) MeshNode
|
||||
GetRouteManager() RouteManager
|
||||
}
|
||||
@ -52,7 +51,6 @@ type MeshManagerImpl struct {
|
||||
idGenerator lib.IdGenerator
|
||||
ipAllocator ip.IPAllocator
|
||||
interfaceManipulator wg.WgInterfaceManipulator
|
||||
Monitor MeshMonitor
|
||||
cmdRunner cmd.CmdRunner
|
||||
OnDelete func(MeshProvider)
|
||||
}
|
||||
@ -104,11 +102,6 @@ func (m *MeshManagerImpl) GetNode(meshid, nodeId string) MeshNode {
|
||||
return node
|
||||
}
|
||||
|
||||
// GetMonitor implements MeshManager.
|
||||
func (m *MeshManagerImpl) GetMonitor() MeshMonitor {
|
||||
return m.Monitor
|
||||
}
|
||||
|
||||
// CreateMeshParams contains the parameters required to create a mesh
|
||||
type CreateMeshParams struct {
|
||||
Port int
|
||||
@ -521,11 +514,6 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
||||
m.ipAllocator = params.IPAllocator
|
||||
m.interfaceManipulator = params.InterfaceManipulator
|
||||
|
||||
m.Monitor = NewMeshMonitor(m)
|
||||
|
||||
aliasManager := NewAliasManager()
|
||||
m.Monitor.AddUpdateCallback(aliasManager.AddAliases)
|
||||
m.Monitor.AddRemoveCallback(aliasManager.RemoveAliases)
|
||||
m.OnDelete = params.OnDelete
|
||||
return m
|
||||
}
|
||||
|
@ -10,8 +10,32 @@ import (
|
||||
)
|
||||
|
||||
func getMeshConfiguration() *conf.DaemonConfiguration {
|
||||
advertiseRoutes := true
|
||||
advertiseDefaultRoute := true
|
||||
ipDiscovery := conf.PUBLIC_IP_DISCOVERY
|
||||
role := conf.PEER_ROLE
|
||||
|
||||
return &conf.DaemonConfiguration{
|
||||
GrpcPort: 8080,
|
||||
GrpcPort: 8080,
|
||||
CertificatePath: "./somecertificatepath",
|
||||
PrivateKeyPath: "./someprivatekeypath",
|
||||
CaCertificatePath: "./somecacertificatepath",
|
||||
SkipCertVerification: true,
|
||||
Timeout: 5,
|
||||
Profile: false,
|
||||
StubWg: false,
|
||||
SyncRate: 2,
|
||||
KeepAliveTime: 60,
|
||||
ClusterSize: 64,
|
||||
InterClusterChance: 0.15,
|
||||
BranchRate: 3,
|
||||
InfectionCount: 3,
|
||||
BaseConfiguration: conf.WgConfiguration{
|
||||
IPDiscovery: &ipDiscovery,
|
||||
AdvertiseRoutes: &advertiseRoutes,
|
||||
AdvertiseDefaultRoute: &advertiseDefaultRoute,
|
||||
Role: &role,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,7 +58,10 @@ func getMeshManager() MeshManager {
|
||||
func TestCreateMeshCreatesANewMeshProvider(t *testing.T) {
|
||||
manager := getMeshManager()
|
||||
|
||||
meshId, err := manager.CreateMesh("wg0", 5000)
|
||||
meshId, err := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 0,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -121,7 +148,7 @@ func TestAddSelfAddsSelfToTheMesh(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, ok := mesh.GetNodes()["abc.com"]
|
||||
_, ok := mesh.GetNodes()[manager.GetPublicKey().String()]
|
||||
|
||||
if !ok {
|
||||
t.Fatalf(`node has not been added`)
|
||||
@ -186,12 +213,51 @@ func TestLeaveMeshDeletesMesh(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAlias(t *testing.T) {
|
||||
manager := getMeshManager()
|
||||
alias := "Firpo"
|
||||
|
||||
meshId, _ := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 5000,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
manager.AddSelf(&AddSelfParams{
|
||||
MeshId: meshId,
|
||||
WgPort: 5000,
|
||||
Endpoint: "abc.com:8080",
|
||||
})
|
||||
|
||||
err := manager.SetAlias(alias)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`failed to set the alias`)
|
||||
}
|
||||
|
||||
self, err := manager.GetSelf(meshId)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`failed to set the alias err: %s`, err.Error())
|
||||
}
|
||||
|
||||
if alias != self.GetAlias() {
|
||||
t.Fatalf(`alias should be %s was %s`, alias, self.GetAlias())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDescription(t *testing.T) {
|
||||
manager := getMeshManager()
|
||||
description := "wooooo"
|
||||
|
||||
meshId1, _ := manager.CreateMesh(5000)
|
||||
meshId2, _ := manager.CreateMesh(5001)
|
||||
meshId1, _ := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 5000,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
meshId2, _ := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 5001,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
manager.AddSelf(&AddSelfParams{
|
||||
MeshId: meshId1,
|
||||
@ -209,13 +275,40 @@ func TestSetDescription(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf(`failed to set the descriptions`)
|
||||
}
|
||||
|
||||
self1, err := manager.GetSelf(meshId1)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`failed to set the description`)
|
||||
}
|
||||
|
||||
if description != self1.GetDescription() {
|
||||
t.Fatalf(`description should be %s was %s`, description, self1.GetDescription())
|
||||
}
|
||||
|
||||
self2, err := manager.GetSelf(meshId2)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf(`failed to set the description`)
|
||||
}
|
||||
|
||||
if description != self2.GetDescription() {
|
||||
t.Fatalf(`description should be %s was %s`, description, self2.GetDescription())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTimeStampUpdatesAllMeshes(t *testing.T) {
|
||||
manager := getMeshManager()
|
||||
|
||||
meshId1, _ := manager.CreateMesh(5000)
|
||||
meshId2, _ := manager.CreateMesh(5001)
|
||||
meshId1, _ := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 5000,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
meshId2, _ := manager.CreateMesh(&CreateMeshParams{
|
||||
Port: 5001,
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
manager.AddSelf(&AddSelfParams{
|
||||
MeshId: meshId1,
|
||||
|
@ -1,81 +0,0 @@
|
||||
package mesh
|
||||
|
||||
type OnChange = func([]MeshNode)
|
||||
|
||||
type MeshMonitor interface {
|
||||
AddUpdateCallback(cb OnChange)
|
||||
AddRemoveCallback(cb OnChange)
|
||||
Trigger() error
|
||||
}
|
||||
|
||||
type MeshMonitorImpl struct {
|
||||
updateCbs []OnChange
|
||||
removeCbs []OnChange
|
||||
nodes map[string]MeshNode
|
||||
manager MeshManager
|
||||
}
|
||||
|
||||
// Trigger causes the mesh monitor to trigger all of
|
||||
// the callbacks.
|
||||
func (m *MeshMonitorImpl) Trigger() error {
|
||||
changedNodes := make([]MeshNode, 0)
|
||||
removedNodes := make([]MeshNode, 0)
|
||||
|
||||
nodes := make(map[string]MeshNode)
|
||||
|
||||
for _, mesh := range m.manager.GetMeshes() {
|
||||
snapshot, err := mesh.GetMesh()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, node := range snapshot.GetNodes() {
|
||||
previous, exists := m.nodes[node.GetWgHost().String()]
|
||||
|
||||
if !exists || !NodeEquals(previous, node) {
|
||||
changedNodes = append(changedNodes, node)
|
||||
}
|
||||
|
||||
nodes[node.GetWgHost().String()] = node
|
||||
}
|
||||
}
|
||||
|
||||
for _, previous := range m.nodes {
|
||||
_, ok := nodes[previous.GetWgHost().String()]
|
||||
|
||||
if !ok {
|
||||
removedNodes = append(removedNodes, previous)
|
||||
}
|
||||
}
|
||||
|
||||
if len(removedNodes) > 0 {
|
||||
for _, cb := range m.removeCbs {
|
||||
cb(removedNodes)
|
||||
}
|
||||
}
|
||||
|
||||
if len(changedNodes) > 0 {
|
||||
for _, cb := range m.updateCbs {
|
||||
cb(changedNodes)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MeshMonitorImpl) AddUpdateCallback(cb OnChange) {
|
||||
m.updateCbs = append(m.updateCbs, cb)
|
||||
}
|
||||
|
||||
func (m *MeshMonitorImpl) AddRemoveCallback(cb OnChange) {
|
||||
m.removeCbs = append(m.removeCbs, cb)
|
||||
}
|
||||
|
||||
func NewMeshMonitor(manager MeshManager) MeshMonitor {
|
||||
return &MeshMonitorImpl{
|
||||
updateCbs: make([]OnChange, 0),
|
||||
nodes: make(map[string]MeshNode),
|
||||
manager: manager,
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
"golang.zx2c4.com/wireguard/wgctrl"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
@ -19,6 +20,8 @@ type MeshNodeStub struct {
|
||||
routes []Route
|
||||
identifier string
|
||||
description string
|
||||
alias string
|
||||
services map[string]string
|
||||
}
|
||||
|
||||
// GetType implements MeshNode.
|
||||
@ -32,8 +35,8 @@ func (*MeshNodeStub) GetServices() map[string]string {
|
||||
}
|
||||
|
||||
// GetAlias implements MeshNode.
|
||||
func (*MeshNodeStub) GetAlias() string {
|
||||
return ""
|
||||
func (s *MeshNodeStub) GetAlias() string {
|
||||
return s.alias
|
||||
}
|
||||
|
||||
func (m *MeshNodeStub) GetHostEndpoint() string {
|
||||
@ -83,17 +86,26 @@ type MeshProviderStub struct {
|
||||
|
||||
// GetConfiguration implements MeshProvider.
|
||||
func (*MeshProviderStub) GetConfiguration() *conf.WgConfiguration {
|
||||
panic("unimplemented")
|
||||
advertiseRoutes := true
|
||||
advertiseDefaultRoute := true
|
||||
ipDiscovery := conf.PUBLIC_IP_DISCOVERY
|
||||
role := conf.PEER_ROLE
|
||||
|
||||
return &conf.WgConfiguration{
|
||||
IPDiscovery: &ipDiscovery,
|
||||
AdvertiseRoutes: &advertiseRoutes,
|
||||
AdvertiseDefaultRoute: &advertiseDefaultRoute,
|
||||
Role: &role,
|
||||
}
|
||||
}
|
||||
|
||||
// Mark implements MeshProvider.
|
||||
func (*MeshProviderStub) Mark(nodeId string) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RemoveNode implements MeshProvider.
|
||||
func (*MeshProviderStub) RemoveNode(nodeId string) error {
|
||||
panic("unimplemented")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*MeshProviderStub) GetRoutes(targetId string) (map[string]Route, error) {
|
||||
@ -106,32 +118,53 @@ func (*MeshProviderStub) GetPeers() []string {
|
||||
}
|
||||
|
||||
// GetNode implements MeshProvider.
|
||||
func (*MeshProviderStub) GetNode(string) (MeshNode, error) {
|
||||
return nil, nil
|
||||
func (m *MeshProviderStub) GetNode(nodeId string) (MeshNode, error) {
|
||||
return m.snapshot.nodes[nodeId], nil
|
||||
}
|
||||
|
||||
// NodeExists implements MeshProvider.
|
||||
func (*MeshProviderStub) NodeExists(string) bool {
|
||||
return false
|
||||
func (m *MeshProviderStub) NodeExists(nodeId string) bool {
|
||||
return m.snapshot.nodes[nodeId] != nil
|
||||
}
|
||||
|
||||
// AddService implements MeshProvider.
|
||||
func (*MeshProviderStub) AddService(nodeId string, key string, value string) error {
|
||||
func (m *MeshProviderStub) AddService(nodeId string, key string, value string) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
node.services[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveService implements MeshProvider.
|
||||
func (*MeshProviderStub) RemoveService(nodeId string, key string) error {
|
||||
func (m *MeshProviderStub) RemoveService(nodeId string, key string) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
delete(node.services, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetAlias implements MeshProvider.
|
||||
func (*MeshProviderStub) SetAlias(nodeId string, alias string) error {
|
||||
func (m *MeshProviderStub) SetAlias(nodeId string, alias string) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
node.alias = alias
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRoutes implements
|
||||
func (m *MeshProviderStub) AddRoutes(nodeId string, route ...Route) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
node.routes = append(node.routes, route...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveRoutes implements MeshProvider.
|
||||
func (*MeshProviderStub) RemoveRoutes(nodeId string, route ...Route) error {
|
||||
func (m *MeshProviderStub) RemoveRoutes(nodeId string, route ...Route) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
|
||||
newRoutes := lib.Filter(node.routes, func(r1 Route) bool {
|
||||
return !lib.Contains(route, func(r2 Route) bool {
|
||||
return RouteEqual(r1, r2)
|
||||
})
|
||||
})
|
||||
node.routes = newRoutes
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -141,12 +174,15 @@ func (*MeshProviderStub) Prune() error {
|
||||
}
|
||||
|
||||
// UpdateTimeStamp implements MeshProvider.
|
||||
func (*MeshProviderStub) UpdateTimeStamp(nodeId string) error {
|
||||
func (m *MeshProviderStub) UpdateTimeStamp(nodeId string) error {
|
||||
node := (m.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
node.timeStamp = time.Now().Unix()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MeshProviderStub) AddNode(node MeshNode) {
|
||||
s.snapshot.nodes[node.GetHostEndpoint()] = node
|
||||
pubKey, _ := node.GetPublicKey()
|
||||
s.snapshot.nodes[pubKey.String()] = node
|
||||
}
|
||||
|
||||
func (s *MeshProviderStub) GetMesh() (MeshSnapshot, error) {
|
||||
@ -178,15 +214,13 @@ func (s *MeshProviderStub) HasChanges() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *MeshProviderStub) AddRoutes(nodeId string, route ...Route) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MeshProviderStub) GetSyncer() MeshSyncer {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MeshProviderStub) SetDescription(nodeId string, description string) error {
|
||||
meshNode := (s.snapshot.nodes[nodeId]).(*MeshNodeStub)
|
||||
meshNode.description = description
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -209,7 +243,7 @@ func (s *StubNodeFactory) Build(params *MeshNodeFactoryParams) MeshNode {
|
||||
return &MeshNodeStub{
|
||||
hostEndpoint: params.Endpoint,
|
||||
publicKey: *params.PublicKey,
|
||||
wgEndpoint: fmt.Sprintf("%s:%s", params.Endpoint, s.Config.GrpcPort),
|
||||
wgEndpoint: fmt.Sprintf("%s:%d", params.Endpoint, s.Config.GrpcPort),
|
||||
wgHost: wgHost,
|
||||
timeStamp: time.Now().Unix(),
|
||||
routes: make([]Route, 0),
|
||||
@ -255,11 +289,6 @@ func (*MeshManagerStub) SetService(service string, value string) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// GetMonitor implements MeshManager.
|
||||
func (*MeshManagerStub) GetMonitor() MeshMonitor {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// SetAlias implements MeshManager.
|
||||
func (*MeshManagerStub) SetAlias(alias string) error {
|
||||
panic("unimplemented")
|
||||
|
@ -1,15 +1,20 @@
|
||||
package wg
|
||||
|
||||
import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
|
||||
type WgInterfaceManipulatorStub struct{}
|
||||
|
||||
func (i *WgInterfaceManipulatorStub) CreateInterface(port int) (string, error) {
|
||||
return "", nil
|
||||
// CreateInterface creates a WireGuard interface
|
||||
func (w *WgInterfaceManipulatorStub) CreateInterface(port int, privateKey *wgtypes.Key) (string, error) {
|
||||
return "aninterface", nil
|
||||
}
|
||||
|
||||
func (i *WgInterfaceManipulatorStub) AddAddress(ifName string, addr string) error {
|
||||
// AddAddress adds an address to the given interface name
|
||||
func (w *WgInterfaceManipulatorStub) AddAddress(ifName string, addr string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *WgInterfaceManipulatorStub) RemoveInterface(ifName string) error {
|
||||
// RemoveInterface removes the specified interface
|
||||
func (w *WgInterfaceManipulatorStub) RemoveInterface(ifName string) error {
|
||||
return nil
|
||||
}
|
||||
|
@ -2,14 +2,6 @@ package wg
|
||||
|
||||
import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
|
||||
type WgError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (m *WgError) Error() string {
|
||||
return m.msg
|
||||
}
|
||||
|
||||
type WgInterfaceManipulator interface {
|
||||
// CreateInterface creates a WireGuard interface
|
||||
CreateInterface(port int, privateKey *wgtypes.Key) (string, error)
|
||||
@ -18,3 +10,11 @@ type WgInterfaceManipulator interface {
|
||||
// RemoveInterface removes the specified interface
|
||||
RemoveInterface(ifName string) error
|
||||
}
|
||||
|
||||
type WgError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (m *WgError) Error() string {
|
||||
return m.msg
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user