mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-12 06:29:05 +02:00
Compare commits
18 Commits
55-cli-opt
...
66-improve
Author | SHA1 | Date | |
---|---|---|---|
255d3c8b39 | |||
41899c5831 | |||
fe4ca66ff6 | |||
0b91ba744a | |||
67483c2a90 | |||
af26e81bd3 | |||
0cc3141b58 | |||
186acbe915 | |||
ceb43a1db1 | |||
bed59f120f | |||
8aab4e99d8 | |||
cf4be1ccab | |||
6ed32f3a79 | |||
b6199892f0 | |||
ad22f04b0d | |||
092d9a4af5 | |||
19abf712a6 | |||
b296e1f45a |
@ -6,6 +6,8 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/akamensky/argparse"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
graph "github.com/tim-beatham/wgmesh/pkg/dot"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||
)
|
||||
@ -15,7 +17,6 @@ const SockAddr = "/tmp/wgmesh_ipc.sock"
|
||||
type CreateMeshParams struct {
|
||||
Client *ipcRpc.Client
|
||||
Endpoint string
|
||||
Role string
|
||||
WgArgs ipc.WireGuardArgs
|
||||
AdvertiseRoutes bool
|
||||
AdvertiseDefault bool
|
||||
@ -56,7 +57,6 @@ type JoinMeshParams struct {
|
||||
MeshId string
|
||||
IpAddress string
|
||||
Endpoint string
|
||||
Role string
|
||||
WgArgs ipc.WireGuardArgs
|
||||
AdvertiseRoutes bool
|
||||
AdvertiseDefault bool
|
||||
@ -93,17 +93,40 @@ func leaveMesh(client *ipcRpc.Client, meshId string) {
|
||||
fmt.Println(reply)
|
||||
}
|
||||
|
||||
func getGraph(client *ipcRpc.Client, meshId string) {
|
||||
var reply string
|
||||
func getGraph(client *ipcRpc.Client) {
|
||||
listMeshesReply := new(ipc.ListMeshReply)
|
||||
|
||||
err := client.Call("IpcHandler.GetDOT", &meshId, &reply)
|
||||
err := client.Call("IpcHandler.ListMeshes", "", &listMeshesReply)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(reply)
|
||||
meshes := make(map[string][]ctrlserver.MeshNode)
|
||||
|
||||
for _, meshId := range listMeshesReply.Meshes {
|
||||
var meshReply ipc.GetMeshReply
|
||||
|
||||
err := client.Call("IpcHandler.GetMesh", &meshId, &meshReply)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
meshes[meshId] = meshReply.Nodes
|
||||
}
|
||||
|
||||
dotGenerator := graph.NewMeshGraphConverter(meshes)
|
||||
dot, err := dotGenerator.Generate()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(dot)
|
||||
}
|
||||
|
||||
func queryMesh(client *ipcRpc.Client, meshId, query string) {
|
||||
@ -203,6 +226,7 @@ func main() {
|
||||
})
|
||||
|
||||
var newMeshRole *string = newMeshCmd.Selector("r", "role", []string{"peer", "client"}, &argparse.Options{
|
||||
Default: "peer",
|
||||
Help: "Role in the mesh network. A value of peer means that the node is publicly routeable and thus considered" +
|
||||
" in the gossip protocol. Client means that the node is not publicly routeable and is not a candidate in the gossip" +
|
||||
" protocol",
|
||||
@ -235,7 +259,7 @@ func main() {
|
||||
})
|
||||
|
||||
var joinMeshRole *string = joinMeshCmd.Selector("r", "role", []string{"peer", "client"}, &argparse.Options{
|
||||
Default: "Peer",
|
||||
Default: "peer",
|
||||
Help: "Role in the mesh network. A value of peer means that the node is publicly routeable and thus considered" +
|
||||
" in the gossip protocol. Client means that the node is not publicly routeable and is not a candidate in the gossip" +
|
||||
" protocol",
|
||||
@ -259,11 +283,6 @@ func main() {
|
||||
Help: "Advertise ::/0 into the mesh network",
|
||||
})
|
||||
|
||||
var getGraphMeshId *string = getGraphCmd.String("m", "mesh", &argparse.Options{
|
||||
Required: true,
|
||||
Help: "MeshID of the graph to get",
|
||||
})
|
||||
|
||||
var leaveMeshMeshId *string = leaveMeshCmd.String("m", "mesh", &argparse.Options{
|
||||
Required: true,
|
||||
Help: "MeshID of the mesh to leave",
|
||||
@ -319,7 +338,6 @@ func main() {
|
||||
fmt.Println(createMesh(&CreateMeshParams{
|
||||
Client: client,
|
||||
Endpoint: *newMeshEndpoint,
|
||||
Role: *newMeshRole,
|
||||
WgArgs: ipc.WireGuardArgs{
|
||||
Endpoint: *newMeshEndpoint,
|
||||
Role: *newMeshRole,
|
||||
@ -341,7 +359,6 @@ func main() {
|
||||
IpAddress: *joinMeshIpAddress,
|
||||
MeshId: *joinMeshId,
|
||||
Endpoint: *joinMeshEndpoint,
|
||||
Role: *joinMeshRole,
|
||||
WgArgs: ipc.WireGuardArgs{
|
||||
Endpoint: *joinMeshEndpoint,
|
||||
Role: *joinMeshRole,
|
||||
@ -354,7 +371,7 @@ func main() {
|
||||
}
|
||||
|
||||
if getGraphCmd.Happened() {
|
||||
getGraph(client, *getGraphMeshId)
|
||||
getGraph(client)
|
||||
}
|
||||
|
||||
if leaveMeshCmd.Happened() {
|
||||
|
14
go.mod
14
go.mod
@ -4,14 +4,18 @@ go 1.21.3
|
||||
|
||||
require (
|
||||
github.com/akamensky/argparse v1.4.0
|
||||
github.com/anandvarma/namegen v0.0.0-20230727084436-5197c6ea3255
|
||||
github.com/automerge/automerge-go v0.0.0-20230903201930-b80ce8aadbb9
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-playground/validator/v10 v10.16.0
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jmespath/go-jmespath v0.4.0
|
||||
github.com/jsimonetti/rtnetlink v1.3.5
|
||||
github.com/miekg/dns v1.1.57
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
golang.org/x/sys v0.14.0
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
||||
gonum.org/v1/gonum v0.14.0
|
||||
google.golang.org/grpc v1.58.1
|
||||
google.golang.org/protobuf v1.31.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
@ -24,7 +28,6 @@ require (
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
@ -42,10 +45,13 @@ require (
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.13.0 // indirect
|
||||
golang.org/x/net v0.15.0 // indirect
|
||||
golang.org/x/sync v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.14.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
|
||||
golang.org/x/mod v0.12.0 // indirect
|
||||
golang.org/x/net v0.17.0 // indirect
|
||||
golang.org/x/sync v0.4.0 // indirect
|
||||
golang.org/x/text v0.13.0 // indirect
|
||||
golang.org/x/tools v0.13.0 // indirect
|
||||
golang.zx2c4.com/wireguard v0.0.0-20230704135630-469159ecf7d1 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -29,6 +29,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
|
||||
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
|
||||
github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
|
@ -40,7 +40,11 @@ func (c *CrdtMeshManager) AddNode(node mesh.MeshNode) {
|
||||
crdt.Services = make(map[string]string)
|
||||
crdt.Timestamp = time.Now().Unix()
|
||||
|
||||
c.doc.Path("nodes").Map().Set(crdt.PublicKey, crdt)
|
||||
err := c.doc.Path("nodes").Map().Set(crdt.PublicKey, crdt)
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteInfof("error")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CrdtMeshManager) isPeer(nodeId string) bool {
|
||||
@ -161,7 +165,7 @@ func (m *CrdtMeshManager) GetNode(endpoint string) (mesh.MeshNode, error) {
|
||||
node, err := m.doc.Path("nodes").Map().Get(endpoint)
|
||||
|
||||
if node.Kind() != automerge.KindMap {
|
||||
return nil, fmt.Errorf("GetNode: something went wrong %s is not a map type")
|
||||
return nil, fmt.Errorf("getnode: node is not a map")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package automerge
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@ -22,7 +22,7 @@ func setUpTests() *TestParams {
|
||||
DevName: "wg0",
|
||||
Port: 5000,
|
||||
Client: nil,
|
||||
Conf: conf.DaemonConfiguration{},
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
return &TestParams{
|
||||
@ -31,22 +31,26 @@ func setUpTests() *TestParams {
|
||||
}
|
||||
|
||||
func getTestNode() mesh.MeshNode {
|
||||
pubKey, _ := wgtypes.GeneratePrivateKey()
|
||||
|
||||
return &MeshNodeCrdt{
|
||||
HostEndpoint: "public-endpoint:8080",
|
||||
WgEndpoint: "public-endpoint:21906",
|
||||
WgHost: "3e9a:1fb3:5e50:8173:9690:f917:b1ab:d218/128",
|
||||
PublicKey: "AAAAAAAAAAAA",
|
||||
PublicKey: pubKey.String(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Description: "A node that we are adding",
|
||||
}
|
||||
}
|
||||
|
||||
func getTestNode2() mesh.MeshNode {
|
||||
pubKey, _ := wgtypes.GeneratePrivateKey()
|
||||
|
||||
return &MeshNodeCrdt{
|
||||
HostEndpoint: "public-endpoint:8081",
|
||||
WgEndpoint: "public-endpoint:21907",
|
||||
WgHost: "3e9a:1fb3:5e50:8173:9690:f917:b1ab:d219/128",
|
||||
PublicKey: "BBBBBBBBB",
|
||||
PublicKey: pubKey.String(),
|
||||
Timestamp: time.Now().Unix(),
|
||||
Description: "A node that we are adding",
|
||||
}
|
||||
@ -54,9 +58,11 @@ func getTestNode2() mesh.MeshNode {
|
||||
|
||||
func TestAddNodeNodeExists(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
testParams.manager.AddNode(getTestNode())
|
||||
node := getTestNode()
|
||||
testParams.manager.AddNode(node)
|
||||
|
||||
node, err := testParams.manager.GetNode("public-endpoint:8080")
|
||||
pubKey, _ := node.GetPublicKey()
|
||||
node, err := testParams.manager.GetNode(pubKey.String())
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -70,25 +76,28 @@ func TestAddNodeNodeExists(t *testing.T) {
|
||||
func TestAddNodeAddRoute(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
testNode := getTestNode()
|
||||
testParams.manager.AddNode(testNode)
|
||||
testParams.manager.AddRoutes(testNode.GetHostEndpoint(), "fd:1c64:1d00::/48")
|
||||
pubKey, _ := testNode.GetPublicKey()
|
||||
|
||||
updatedNode, err := testParams.manager.GetNode(testNode.GetHostEndpoint())
|
||||
_, destination, _ := net.ParseCIDR("fd:1c64:1d00::/48")
|
||||
|
||||
testParams.manager.AddNode(testNode)
|
||||
testParams.manager.AddRoutes(pubKey.String(), &mesh.RouteStub{
|
||||
Destination: destination,
|
||||
HopCount: 0,
|
||||
Path: make([]string, 0),
|
||||
})
|
||||
updatedNode, err := testParams.manager.GetNode(pubKey.String())
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if updatedNode == nil {
|
||||
t.Fatalf(`Node does not exist in the mesh`)
|
||||
t.Fatalf(`node does not exist in the mesh`)
|
||||
}
|
||||
|
||||
routes := updatedNode.GetRoutes()
|
||||
|
||||
if !slices.Contains(routes, "fd:1c64:1d00::/48") {
|
||||
t.Fatal("Route node not added")
|
||||
}
|
||||
|
||||
if len(routes) != 1 {
|
||||
t.Fatal(`Route length mismatch`)
|
||||
}
|
||||
@ -253,7 +262,9 @@ func TestUpdateTimeStampNodeExists(t *testing.T) {
|
||||
node := getTestNode()
|
||||
|
||||
testParams.manager.AddNode(node)
|
||||
err := testParams.manager.UpdateTimeStamp(node.GetHostEndpoint())
|
||||
pubKey, _ := node.GetPublicKey()
|
||||
|
||||
err := testParams.manager.UpdateTimeStamp(pubKey.String())
|
||||
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -282,7 +293,13 @@ func TestSetDescriptionNodeExists(t *testing.T) {
|
||||
func TestAddRoutesNodeDoesNotExist(t *testing.T) {
|
||||
testParams := setUpTests()
|
||||
|
||||
err := testParams.manager.AddRoutes("AAAAA", "fd:1c64:1d00::/48")
|
||||
_, destination, _ := net.ParseCIDR("fd:1c64:1d00::/48")
|
||||
|
||||
err := testParams.manager.AddRoutes("AAAAA", &mesh.RouteStub{
|
||||
Destination: destination,
|
||||
HopCount: 0,
|
||||
Path: make([]string, 0),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Error(err)
|
||||
@ -293,16 +310,11 @@ func TestCompareComparesByPublicKey(t *testing.T) {
|
||||
node := getTestNode().(*MeshNodeCrdt)
|
||||
node2 := getTestNode2().(*MeshNodeCrdt)
|
||||
|
||||
if node.Compare(node2) != -1 {
|
||||
t.Fatalf(`node is alphabetically before node2`)
|
||||
}
|
||||
pubKey1, _ := node.GetPublicKey()
|
||||
pubKey2, _ := node2.GetPublicKey()
|
||||
|
||||
if node2.Compare(node) != 1 {
|
||||
t.Fatalf(`node is alphabetical;y before node2`)
|
||||
}
|
||||
|
||||
if node.Compare(node) != 0 {
|
||||
t.Fatalf(`node is equal to node`)
|
||||
if node.Compare(node2) != strings.Compare(pubKey1.String(), pubKey2.String()) {
|
||||
t.Fatalf(`compare failed`)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ type MeshNodeFactory struct {
|
||||
func (f *MeshNodeFactory) Build(params *mesh.MeshNodeFactoryParams) mesh.MeshNode {
|
||||
hostName := f.getAddress(params)
|
||||
|
||||
grpcEndpoint := fmt.Sprintf("%s:%s", hostName, f.Config.GrpcPort)
|
||||
grpcEndpoint := fmt.Sprintf("%s:%d", hostName, f.Config.GrpcPort)
|
||||
|
||||
if *params.MeshConfig.Role == conf.CLIENT_ROLE {
|
||||
grpcEndpoint = "-"
|
||||
|
@ -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,13 +1,40 @@
|
||||
package conf
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getExampleConfiguration() *DaemonConfiguration {
|
||||
discovery := PUBLIC_IP_DISCOVERY
|
||||
advertiseRoutes := false
|
||||
advertiseDefaultRoute := false
|
||||
endpoint := "abc.com:123"
|
||||
nodeType := CLIENT_ROLE
|
||||
keepAliveWg := 0
|
||||
|
||||
return &DaemonConfiguration{
|
||||
CertificatePath: "./cert/cert.pem",
|
||||
PrivateKeyPath: "./cert/key.pem",
|
||||
CaCertificatePath: "./cert/ca.pems",
|
||||
CertificatePath: "../../../cert/cert.pem",
|
||||
PrivateKeyPath: "../../../cert/priv.pem",
|
||||
CaCertificatePath: "../../../cert/cacert.pem",
|
||||
SkipCertVerification: true,
|
||||
GrpcPort: 25,
|
||||
Timeout: 5,
|
||||
Profile: false,
|
||||
StubWg: false,
|
||||
SyncRate: 2,
|
||||
KeepAliveTime: 2,
|
||||
ClusterSize: 64,
|
||||
InterClusterChance: 0.15,
|
||||
BranchRate: 3,
|
||||
InfectionCount: 2,
|
||||
BaseConfiguration: WgConfiguration{
|
||||
IPDiscovery: &discovery,
|
||||
AdvertiseRoutes: &advertiseRoutes,
|
||||
AdvertiseDefaultRoute: &advertiseDefaultRoute,
|
||||
Endpoint: &endpoint,
|
||||
Role: &nodeType,
|
||||
KeepAliveWg: &keepAliveWg,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +82,141 @@ func TestConfigurationGrpcPortEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPDiscoveryNotSet(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
ipDiscovery := IPDiscovery("djdsjdskd")
|
||||
conf.BaseConfiguration.IPDiscovery = &ipDiscovery
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdvertiseRoutesNotSet(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.BaseConfiguration.AdvertiseRoutes = nil
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdvertiseDefaultRouteNotSet(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.BaseConfiguration.AdvertiseDefaultRoute = nil
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepAliveWgNegative(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
keepAliveWg := -1
|
||||
conf.BaseConfiguration.KeepAliveWg = &keepAliveWg
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleTypeNotValid(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
role := NodeType("bruhhh")
|
||||
conf.BaseConfiguration.Role = &role
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleTypeNotSpecified(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.BaseConfiguration.Role = nil
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`invalid role type`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBranchRateZero(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.BranchRate = 0
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncRateZero(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.SyncRate = 0
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepAliveTimeZero(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.KeepAliveTime = 0
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClusterSizeZero(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.ClusterSize = 0
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterClusterChanceZero(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.InterClusterChance = 0
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInfectionCountOne(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
conf.InfectionCount = 0
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal(`error should be thrown`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidConfiguration(t *testing.T) {
|
||||
conf := getExampleConfiguration()
|
||||
|
||||
err := ValidateDaemonConfiguration(conf)
|
||||
|
||||
if err != nil {
|
||||
|
@ -55,12 +55,14 @@ func (i *ConnClusterImpl) GetNeighbours(global []string, selfId string) []string
|
||||
// you will communicate with a random node that is not in your cluster.
|
||||
func (i *ConnClusterImpl) GetInterCluster(global []string, selfId string) string {
|
||||
// Doesn't matter if not in it. Get index of where the node 'should' be
|
||||
slices.Sort(global)
|
||||
|
||||
index, _ := binarySearch(global, selfId, 1)
|
||||
numClusters := math.Ceil(float64(len(global)) / float64(i.clusterSize))
|
||||
|
||||
randomCluster := rand.Intn(int(numClusters)-1) + 1
|
||||
randomCluster := rand.Intn(2) + 1
|
||||
|
||||
neighbourIndex := (index + randomCluster) % len(global)
|
||||
// cluster is considered a heap
|
||||
neighbourIndex := (2*index + (randomCluster * i.clusterSize)) % len(global)
|
||||
return global[neighbourIndex]
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
@ -386,7 +386,7 @@ func (m *TwoPhaseStoreMeshManager) SetAlias(nodeId string, alias string) error {
|
||||
}
|
||||
|
||||
node := m.store.Get(nodeId)
|
||||
node.Description = alias
|
||||
node.Alias = alias
|
||||
|
||||
m.store.Put(nodeId, node)
|
||||
return nil
|
||||
|
@ -1,4 +1,4 @@
|
||||
// crdt is a golang implementation of a crdt
|
||||
// crdt provides go implementations for crdts
|
||||
package crdt
|
||||
|
||||
import (
|
||||
@ -65,10 +65,19 @@ func (g *GMap[K, D]) get(key uint64) Bucket[D] {
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Get(key K) D {
|
||||
if !g.Contains(key) {
|
||||
var def D
|
||||
return def
|
||||
}
|
||||
|
||||
return g.get(g.clock.hashFunc(key)).Contents
|
||||
}
|
||||
|
||||
func (g *GMap[K, D]) Mark(key K) {
|
||||
if !g.Contains(key) {
|
||||
return
|
||||
}
|
||||
|
||||
g.lock.Lock()
|
||||
bucket := g.contents[g.clock.hashFunc(key)]
|
||||
bucket.Gravestone = true
|
||||
@ -89,7 +98,6 @@ func (g *GMap[K, D]) IsMarked(key K) bool {
|
||||
}
|
||||
|
||||
g.lock.RUnlock()
|
||||
|
||||
return marked
|
||||
}
|
||||
|
||||
|
224
pkg/crdt/g_map_test.go
Normal file
224
pkg/crdt/g_map_test.go
Normal file
@ -0,0 +1,224 @@
|
||||
// crdt_test unit tests the crdt implementations
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
func NewGmap() *GMap[string, bool] {
|
||||
vectorClock := NewVectorClock("a", func(key string) uint64 {
|
||||
hash := fnv.New64a()
|
||||
hash.Write([]byte(key))
|
||||
return hash.Sum64()
|
||||
}, 1) // 1 second stale time
|
||||
|
||||
gMap := NewGMap[string, bool](vectorClock)
|
||||
return gMap
|
||||
}
|
||||
|
||||
func TestGMapPutInsertsItems(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("bruh1234", true)
|
||||
|
||||
if !gMap.Contains("bruh1234") {
|
||||
t.Fatalf(`value not added to map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGMapPutReplacesItems(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("bruh1234", true)
|
||||
gMap.Put("bruh1234", false)
|
||||
|
||||
value := gMap.Get("bruh1234")
|
||||
|
||||
if value {
|
||||
t.Fatalf(`value should ahve been replaced to false`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsValueNotPresent(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
|
||||
if gMap.Contains("sdhjsdhsdj") {
|
||||
t.Fatalf(`value should not be present in the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainsValuePresent(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
key := "hehehehe"
|
||||
gMap.Put(key, false)
|
||||
|
||||
if !gMap.Contains(key) {
|
||||
t.Fatalf(`%s should not be present in the map`, key)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGMapGetNotPresentReturnsError(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
value := gMap.Get("bruh123")
|
||||
|
||||
if value != false {
|
||||
t.Fatalf(`value should be default type false`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGMapGetReturnsValue(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("bobdylan", true)
|
||||
|
||||
value := gMap.Get("bobdylan")
|
||||
|
||||
if !value {
|
||||
t.Fatalf("value should be true but was false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkMarksTheValue(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("hello123", true)
|
||||
|
||||
gMap.Mark("hello123")
|
||||
|
||||
if !gMap.IsMarked("hello123") {
|
||||
t.Fatal(`hello123 should be marked`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkValueNotPresent(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Mark("ok123456")
|
||||
}
|
||||
|
||||
func TestKeysMapEmpty(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
|
||||
keys := gMap.Keys()
|
||||
|
||||
if len(keys) != 0 {
|
||||
t.Fatal(`list of keys was not empty but should be empty`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeysMapReturnsKeysInMap(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
|
||||
gMap.Put("a", false)
|
||||
gMap.Put("b", false)
|
||||
gMap.Put("c", false)
|
||||
|
||||
keys := gMap.Keys()
|
||||
|
||||
if len(keys) != 3 {
|
||||
t.Fatal(`key length should be 3`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveMapEmptyReturnsEmptyMap(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
|
||||
saveMap := gMap.Save()
|
||||
|
||||
if len(saveMap) != 0 {
|
||||
t.Fatal(`saves should be empty`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveMapReturnsMapOfBuckets(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("a", false)
|
||||
gMap.Put("b", false)
|
||||
gMap.Put("c", false)
|
||||
|
||||
saveMap := gMap.Save()
|
||||
|
||||
if len(saveMap) != 3 {
|
||||
t.Fatalf(`save length should be 3`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveWithKeysNoKeysReturnsEmptyBucket(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("a", false)
|
||||
gMap.Put("b", false)
|
||||
gMap.Put("c", false)
|
||||
|
||||
saveMap := gMap.SaveWithKeys([]uint64{})
|
||||
|
||||
if len(saveMap) != 0 {
|
||||
t.Fatalf(`save map should be empty`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveWithKeysReturnsIntersection(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("a", false)
|
||||
gMap.Put("b", false)
|
||||
gMap.Put("c", false)
|
||||
|
||||
clock := lib.MapKeys(gMap.GetClock())
|
||||
clock = clock[:len(clock)-1]
|
||||
|
||||
values := gMap.SaveWithKeys(clock)
|
||||
if len(values) != len(clock) {
|
||||
t.Fatalf(`intersection not returned`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClockMapEmptyReturnsEmptyClock(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
|
||||
clocks := gMap.GetClock()
|
||||
|
||||
if len(clocks) != 0 {
|
||||
t.Fatalf(`vector clock is not empty`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClockReturnsAllCLocks(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("a", false)
|
||||
gMap.Put("b", false)
|
||||
gMap.Put("c", false)
|
||||
|
||||
clocks := lib.MapValues(gMap.GetClock())
|
||||
slices.Sort(clocks)
|
||||
|
||||
if !slices.Equal([]uint64{0, 1, 2}, clocks) {
|
||||
t.Fatalf(`clocks are invalid`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHashChangesHashOnValueAdded(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.Put("a", false)
|
||||
prevHash := gMap.GetHash()
|
||||
|
||||
gMap.Put("b", true)
|
||||
|
||||
if prevHash == gMap.GetHash() {
|
||||
t.Fatalf(`hash should be different`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPruneGarbageCollectsValuesThatHaveNotBeenUpdated(t *testing.T) {
|
||||
gMap := NewGmap()
|
||||
gMap.clock.Put("c", 12)
|
||||
gMap.Put("c", false)
|
||||
gMap.Put("a", false)
|
||||
|
||||
time.Sleep(4 * time.Second)
|
||||
gMap.Put("a", true)
|
||||
|
||||
gMap.Prune()
|
||||
|
||||
if gMap.Contains("c") {
|
||||
t.Fatalf(`a should have been pruned`)
|
||||
}
|
||||
}
|
@ -68,9 +68,16 @@ func prepare(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Increment the clock here so the clock gets
|
||||
// distributed to everyone else in the mesh
|
||||
syncer.manager.store.Clock.IncrementClock()
|
||||
|
||||
var buffer bytes.Buffer
|
||||
enc := gob.NewEncoder(&buffer)
|
||||
|
||||
mapState := syncer.manager.store.GenerateMessage()
|
||||
|
||||
syncer.mapState = mapState
|
||||
err = enc.Encode(*syncer.mapState)
|
||||
|
||||
if err != nil {
|
||||
@ -94,7 +101,7 @@ func present(syncer *TwoPhaseSyncer) ([]byte, bool) {
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteErrorf(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
difference := syncer.mapState.Difference(syncer.manager.store.Clock.GetStaleCount(), &mapState)
|
||||
syncer.manager.store.Clock.Merge(mapState.Vectors)
|
||||
@ -164,9 +171,6 @@ func (t *TwoPhaseSyncer) RecvMessage(msg []byte) error {
|
||||
|
||||
func (t *TwoPhaseSyncer) Complete() {
|
||||
logging.Log.WriteInfof("SYNC COMPLETED")
|
||||
if t.state >= MERGE {
|
||||
t.manager.store.Clock.IncrementClock()
|
||||
}
|
||||
}
|
||||
|
||||
func NewTwoPhaseSyncer(manager *TwoPhaseStoreMeshManager) *TwoPhaseSyncer {
|
||||
@ -181,7 +185,6 @@ func NewTwoPhaseSyncer(manager *TwoPhaseStoreMeshManager) *TwoPhaseSyncer {
|
||||
return &TwoPhaseSyncer{
|
||||
manager: manager,
|
||||
state: HASH,
|
||||
mapState: manager.store.GenerateMessage(),
|
||||
generateMessageFSM: generateMessageFsm,
|
||||
}
|
||||
}
|
||||
|
214
pkg/crdt/two_phase_map_test.go
Normal file
214
pkg/crdt/two_phase_map_test.go
Normal file
@ -0,0 +1,214 @@
|
||||
package crdt
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func NewMap(processId string) *TwoPhaseMap[string, string] {
|
||||
theMap := NewTwoPhaseMap[string, string](processId, func(key string) uint64 {
|
||||
hash := fnv.New64a()
|
||||
hash.Write([]byte(key))
|
||||
return hash.Sum64()
|
||||
}, 1)
|
||||
return theMap
|
||||
}
|
||||
|
||||
func TestTwoPhaseMapEmpty(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
if theMap.Contains("a") {
|
||||
t.Fatalf(`a should not be present in the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoPhaseMapValuePresent(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
theMap.Put("a", "")
|
||||
|
||||
if !theMap.Contains("a") {
|
||||
t.Fatalf(`should be present within the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoPhaseMapValueNotPresent(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
theMap.Put("b", "")
|
||||
|
||||
if theMap.Contains("a") {
|
||||
t.Fatalf(`a should not be present in the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoPhaseMapPutThenRemove(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
theMap.Put("a", "")
|
||||
theMap.Remove("a")
|
||||
|
||||
if theMap.Contains("a") {
|
||||
t.Fatalf(`a should not be present within the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoPhaseMapPutThenRemoveThenPut(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
theMap.Put("a", "")
|
||||
theMap.Remove("a")
|
||||
theMap.Put("a", "")
|
||||
|
||||
if !theMap.Contains("a") {
|
||||
t.Fatalf(`a should be present within the map`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkMarksTheValueIn2PMap(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
theMap.Put("a", "")
|
||||
theMap.Mark("a")
|
||||
|
||||
if !theMap.IsMarked("a") {
|
||||
t.Fatalf(`a should be marked`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAsListReturnsItemsInList(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
theMap.Put("a", "bob")
|
||||
theMap.Put("b", "dylan")
|
||||
|
||||
keys := theMap.AsList()
|
||||
slices.Sort(keys)
|
||||
|
||||
if !slices.Equal([]string{"bob", "dylan"}, keys) {
|
||||
t.Fatalf(`values should be bob, dylan`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapShotRemoveMapEmpty(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
theMap.Put("a", "bob")
|
||||
theMap.Put("b", "dylan")
|
||||
|
||||
snapshot := theMap.Snapshot()
|
||||
|
||||
if len(snapshot.Add) != 2 {
|
||||
t.Fatalf(`add values length should be 2`)
|
||||
}
|
||||
|
||||
if len(snapshot.Remove) != 0 {
|
||||
t.Fatalf(`remove map length should be 0`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotMapEmpty(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
snapshot := theMap.Snapshot()
|
||||
|
||||
if len(snapshot.Add) != 0 || len(snapshot.Remove) != 0 {
|
||||
t.Fatalf(`snapshot length should be 0`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapShotFromStateReturnsIntersection(t *testing.T) {
|
||||
map1 := NewMap("a")
|
||||
map1.Put("a", "heyy")
|
||||
|
||||
map2 := NewMap("b")
|
||||
map2.Put("b", "hmmm")
|
||||
|
||||
message := map2.GenerateMessage()
|
||||
|
||||
snapShot := map1.SnapShotFromState(message)
|
||||
|
||||
if len(snapShot.Add) != 1 {
|
||||
t.Fatalf(`add length should be 1`)
|
||||
}
|
||||
|
||||
if len(snapShot.Remove) != 0 {
|
||||
t.Fatalf(`remove length should be 0`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHashDifferentOnChange(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
|
||||
prevHash := theMap.GetHash()
|
||||
|
||||
theMap.Put("b", "hmmhmhmh")
|
||||
|
||||
if prevHash == theMap.GetHash() {
|
||||
t.Fatalf(`hashes should not be the same`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMessageReturnsClocks(t *testing.T) {
|
||||
theMap := NewMap("a")
|
||||
theMap.Put("a", "hmm")
|
||||
theMap.Put("b", "hmm")
|
||||
theMap.Remove("a")
|
||||
|
||||
message := theMap.GenerateMessage()
|
||||
|
||||
if len(message.AddContents) != 2 {
|
||||
t.Fatalf(`two items added add should be 2`)
|
||||
}
|
||||
|
||||
if len(message.RemoveContents) != 1 {
|
||||
t.Fatalf(`a was removed remove map should be length 1`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDifferenceReturnsDifferenceOfMaps(t *testing.T) {
|
||||
map1 := NewMap("a")
|
||||
map1.Put("a", "ssms")
|
||||
map1.Put("b", "sdmdsmd")
|
||||
|
||||
map2 := NewMap("b")
|
||||
map2.Put("d", "eek")
|
||||
map2.Put("c", "meh")
|
||||
|
||||
message1 := map1.GenerateMessage()
|
||||
message2 := map2.GenerateMessage()
|
||||
|
||||
difference := message1.Difference(0, message2)
|
||||
|
||||
if len(difference.AddContents) != 2 {
|
||||
t.Fatalf(`d and c are not in map1 they should be in add contents`)
|
||||
}
|
||||
|
||||
if len(difference.RemoveContents) != 0 {
|
||||
t.Fatalf(`remove should be empty`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeMergesValuesThatAreGreaterThanCurrentClock(t *testing.T) {
|
||||
map1 := NewMap("a")
|
||||
map1.Put("a", "ssms")
|
||||
map1.Put("b", "sdmdsmd")
|
||||
|
||||
map2 := NewMap("b")
|
||||
map2.Put("d", "eek")
|
||||
map2.Put("c", "meh")
|
||||
|
||||
message1 := map1.GenerateMessage()
|
||||
message2 := map2.GenerateMessage()
|
||||
|
||||
difference := message1.Difference(0, message2)
|
||||
state := map2.SnapShotFromState(difference)
|
||||
|
||||
map1.Merge(*state)
|
||||
|
||||
if !map1.Contains("d") {
|
||||
t.Fatalf(`d should be in the map`)
|
||||
}
|
||||
|
||||
if !map2.Contains("c") {
|
||||
t.Fatalf(`c should be in the map`)
|
||||
}
|
||||
}
|
227
pkg/dot/dot.go
Normal file
227
pkg/dot/dot.go
Normal file
@ -0,0 +1,227 @@
|
||||
// Graph allows the definition of a DOT graph in golang
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"strings"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
type GraphType string
|
||||
type Shape string
|
||||
|
||||
const (
|
||||
GRAPH GraphType = "graph"
|
||||
DIGRAPH GraphType = "digraph"
|
||||
)
|
||||
|
||||
const (
|
||||
CIRCLE Shape = "circle"
|
||||
STAR Shape = "star"
|
||||
HEXAGON Shape = "hexagon"
|
||||
PARALLELOGRAM Shape = "parallelogram"
|
||||
)
|
||||
|
||||
type Graph interface {
|
||||
Dottable
|
||||
GetType() GraphType
|
||||
}
|
||||
|
||||
type Cluster struct {
|
||||
Type GraphType
|
||||
Name string
|
||||
Label string
|
||||
nodes map[string]*Node
|
||||
edges map[string]Edge
|
||||
}
|
||||
|
||||
type RootGraph struct {
|
||||
Type GraphType
|
||||
Label string
|
||||
nodes map[string]*Node
|
||||
clusters map[string]*Cluster
|
||||
edges map[string]Edge
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Label string
|
||||
Shape Shape
|
||||
Size int
|
||||
}
|
||||
|
||||
type Edge interface {
|
||||
Dottable
|
||||
}
|
||||
|
||||
type DirectedEdge struct {
|
||||
Name string
|
||||
Label string
|
||||
From string
|
||||
To string
|
||||
}
|
||||
|
||||
type UndirectedEdge struct {
|
||||
Name string
|
||||
Label string
|
||||
From string
|
||||
To string
|
||||
}
|
||||
|
||||
// Dottable means an implementer can convert the struct to DOT representation
|
||||
type Dottable interface {
|
||||
GetDOT() (string, error)
|
||||
}
|
||||
|
||||
func NewGraph(label string, graphType GraphType) *RootGraph {
|
||||
return &RootGraph{Type: graphType, Label: label, clusters: map[string]*Cluster{}, nodes: make(map[string]*Node), edges: make(map[string]Edge)}
|
||||
}
|
||||
|
||||
// PutNode: puts a node in the graph
|
||||
func (g *RootGraph) PutNode(name, label string, size int, shape Shape) error {
|
||||
_, exists := g.nodes[name]
|
||||
|
||||
if exists {
|
||||
// If exists no need to add the ndoe
|
||||
return nil
|
||||
}
|
||||
|
||||
g.nodes[name] = &Node{Name: name, Label: label, Size: size, Shape: shape}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *RootGraph) PutCluster(graph *Cluster) {
|
||||
g.clusters[graph.Label] = graph
|
||||
}
|
||||
|
||||
func writeContituents[D Dottable](result *strings.Builder, elements ...D) error {
|
||||
for _, node := range elements {
|
||||
dot, err := node.GetDOT()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = result.WriteString(dot)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *RootGraph) GetDOT() (string, error) {
|
||||
var result strings.Builder
|
||||
|
||||
result.WriteString(fmt.Sprintf("%s {\n", g.Type))
|
||||
result.WriteString("node [colorscheme=set312];\n")
|
||||
result.WriteString("layout = fdp;\n")
|
||||
nodes := lib.MapValues(g.nodes)
|
||||
edges := lib.MapValues(g.edges)
|
||||
writeContituents(&result, nodes...)
|
||||
writeContituents(&result, edges...)
|
||||
|
||||
for _, cluster := range g.clusters {
|
||||
clusterDOT, err := cluster.GetDOT()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
result.WriteString(clusterDOT)
|
||||
}
|
||||
|
||||
result.WriteString("}")
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
// GetType implements Graph.
|
||||
func (r *RootGraph) GetType() GraphType {
|
||||
return r.Type
|
||||
}
|
||||
|
||||
func constructEdge(graph Graph, name, label, from, to string) Edge {
|
||||
switch graph.GetType() {
|
||||
case DIGRAPH:
|
||||
return &DirectedEdge{Name: name, Label: label, From: from, To: to}
|
||||
default:
|
||||
return &UndirectedEdge{Name: name, Label: label, From: from, To: to}
|
||||
}
|
||||
}
|
||||
|
||||
// AddEdge: adds an edge between two nodes in the graph
|
||||
func (g *RootGraph) AddEdge(name string, label string, from string, to string) error {
|
||||
g.edges[name] = constructEdge(g, name, label, from, to)
|
||||
return nil
|
||||
}
|
||||
|
||||
const numColours = 12
|
||||
|
||||
func (n *Node) hash() int {
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(n.Name))
|
||||
return (int(h.Sum32()) % numColours) + 1
|
||||
}
|
||||
|
||||
func (n *Node) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("node[label=\"%s\",shape=%s, style=\"filled\", fillcolor=%d, width=%d, height=%d, fixedsize=true] \"%s\";\n",
|
||||
n.Label, n.Shape, n.hash(), n.Size, n.Size, n.Name), nil
|
||||
}
|
||||
|
||||
func (e *DirectedEdge) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("\"%s\" -> \"%s\" [label=\"%s\"];\n", e.From, e.To, e.Label), nil
|
||||
}
|
||||
|
||||
func (e *UndirectedEdge) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("\"%s\" -- \"%s\" [label=\"%s\"];\n", e.From, e.To, e.Label), nil
|
||||
}
|
||||
|
||||
// AddEdge: adds an edge between two nodes in the graph
|
||||
func (g *Cluster) AddEdge(name string, label string, from string, to string) error {
|
||||
g.edges[name] = constructEdge(g, name, label, from, to)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutNode: puts a node in the graph
|
||||
func (g *Cluster) PutNode(name, label string, size int, shape Shape) error {
|
||||
_, exists := g.nodes[name]
|
||||
|
||||
if exists {
|
||||
// If exists no need to add the ndoe
|
||||
return nil
|
||||
}
|
||||
|
||||
g.nodes[name] = &Node{Name: name, Label: label, Shape: shape, Size: size}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Cluster) GetDOT() (string, error) {
|
||||
var builder strings.Builder
|
||||
|
||||
builder.WriteString(fmt.Sprintf("subgraph \"cluster%s\" {\n", g.Label))
|
||||
builder.WriteString(fmt.Sprintf("label = \"%s\"\n", g.Label))
|
||||
nodes := lib.MapValues(g.nodes)
|
||||
edges := lib.MapValues(g.edges)
|
||||
writeContituents(&builder, nodes...)
|
||||
writeContituents(&builder, edges...)
|
||||
|
||||
builder.WriteString("}\n")
|
||||
return builder.String(), nil
|
||||
}
|
||||
|
||||
func (g *Cluster) GetType() GraphType {
|
||||
return g.Type
|
||||
}
|
||||
|
||||
func NewSubGraph(name string, label string, graphType GraphType) *Cluster {
|
||||
return &Cluster{
|
||||
Label: name,
|
||||
Type: graphType,
|
||||
Name: name,
|
||||
nodes: make(map[string]*Node),
|
||||
edges: make(map[string]Edge),
|
||||
}
|
||||
}
|
116
pkg/dot/wg.go
Normal file
116
pkg/dot/wg.go
Normal file
@ -0,0 +1,116 @@
|
||||
package graph
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
)
|
||||
|
||||
// MeshGraphConverter converts a mesh to a graph
|
||||
type MeshGraphConverter interface {
|
||||
// convert the mesh to textual form
|
||||
Generate() (string, error)
|
||||
}
|
||||
|
||||
type MeshDOTConverter struct {
|
||||
meshes map[string][]ctrlserver.MeshNode
|
||||
destinations map[string]interface{}
|
||||
}
|
||||
|
||||
func (c *MeshDOTConverter) Generate() (string, error) {
|
||||
g := NewGraph("Smegmesh", GRAPH)
|
||||
|
||||
for meshId := range c.meshes {
|
||||
err := c.generateMesh(g, meshId)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
for mesh := range c.meshes {
|
||||
g.PutNode(mesh, mesh, 1, CIRCLE)
|
||||
}
|
||||
|
||||
for destination := range c.destinations {
|
||||
g.PutNode(destination, destination, 1, HEXAGON)
|
||||
}
|
||||
|
||||
return g.GetDOT()
|
||||
}
|
||||
|
||||
func (c *MeshDOTConverter) generateMesh(g *RootGraph, meshId string) error {
|
||||
nodes := c.meshes[meshId]
|
||||
|
||||
g.PutNode(meshId, meshId, 1, CIRCLE)
|
||||
|
||||
for _, node := range nodes {
|
||||
c.graphNode(g, node, meshId)
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
g.AddEdge(fmt.Sprintf("%s to %s", node.PublicKey, meshId), "", node.PublicKey, meshId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// graphNode: graphs a node within the mesh
|
||||
func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, meshId string) {
|
||||
alias := node.Alias
|
||||
|
||||
if alias == "" {
|
||||
alias = node.WgHost[1:len(node.WgHost)-20] + "\\n" + node.WgHost[len(node.WgHost)-20:len(node.WgHost)]
|
||||
}
|
||||
|
||||
g.PutNode(node.PublicKey, alias, 2, CIRCLE)
|
||||
|
||||
for _, route := range node.Routes {
|
||||
if len(route.Path) == 0 {
|
||||
g.AddEdge(route.Destination, "", node.PublicKey, route.Destination)
|
||||
continue
|
||||
}
|
||||
|
||||
reversedPath := slices.Clone(route.Path)
|
||||
slices.Reverse(reversedPath)
|
||||
|
||||
g.AddEdge(fmt.Sprintf("%s to %s", node.PublicKey, reversedPath[0]), "", node.PublicKey, reversedPath[0])
|
||||
|
||||
for _, mesh := range route.Path {
|
||||
if _, ok := c.meshes[mesh]; !ok {
|
||||
c.destinations[mesh] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for index := range reversedPath[0 : len(reversedPath)-1] {
|
||||
routeID := fmt.Sprintf("%s to %s", reversedPath[index], reversedPath[index+1])
|
||||
g.AddEdge(routeID, "", reversedPath[index], reversedPath[index+1])
|
||||
}
|
||||
|
||||
if route.Destination == "::/0" {
|
||||
c.destinations[route.Destination] = struct{}{}
|
||||
lastMesh := reversedPath[len(reversedPath)-1]
|
||||
routeID := fmt.Sprintf("%s to %s", lastMesh, route.Destination)
|
||||
g.AddEdge(routeID, "", lastMesh, route.Destination)
|
||||
}
|
||||
}
|
||||
|
||||
for service := range node.Services {
|
||||
c.putService(g, service, meshId, node)
|
||||
}
|
||||
}
|
||||
|
||||
// putService: construct a service node and a link between the nodes
|
||||
func (c *MeshDOTConverter) putService(g *RootGraph, key, meshId string, node ctrlserver.MeshNode) {
|
||||
serviceID := fmt.Sprintf("%s%s%s", key, node.PublicKey, meshId)
|
||||
g.PutNode(serviceID, key, 1, PARALLELOGRAM)
|
||||
g.AddEdge(fmt.Sprintf("%s to %s", node.PublicKey, serviceID), "", node.PublicKey, serviceID)
|
||||
}
|
||||
|
||||
func NewMeshGraphConverter(meshes map[string][]ctrlserver.MeshNode) MeshGraphConverter {
|
||||
return &MeshDOTConverter{
|
||||
meshes: meshes,
|
||||
destinations: make(map[string]interface{}),
|
||||
}
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
// Graph allows the definition of a DOT graph in golang
|
||||
package graph
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"strings"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
type GraphType string
|
||||
type Shape string
|
||||
|
||||
const (
|
||||
GRAPH GraphType = "graph"
|
||||
DIGRAPH = "digraph"
|
||||
)
|
||||
|
||||
const (
|
||||
CIRCLE Shape = "circle"
|
||||
STAR Shape = "star"
|
||||
HEXAGON Shape = "hexagon"
|
||||
)
|
||||
|
||||
type Graph struct {
|
||||
Type GraphType
|
||||
Label string
|
||||
nodes map[string]*Node
|
||||
edges []Edge
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Shape Shape
|
||||
}
|
||||
|
||||
type Edge interface {
|
||||
Dottable
|
||||
}
|
||||
|
||||
type DirectedEdge struct {
|
||||
Label string
|
||||
From *Node
|
||||
To *Node
|
||||
}
|
||||
|
||||
type UndirectedEdge struct {
|
||||
Label string
|
||||
From *Node
|
||||
To *Node
|
||||
}
|
||||
|
||||
// Dottable means an implementer can convert the struct to DOT representation
|
||||
type Dottable interface {
|
||||
GetDOT() (string, error)
|
||||
}
|
||||
|
||||
func NewGraph(label string, graphType GraphType) *Graph {
|
||||
return &Graph{Type: graphType, Label: label, nodes: make(map[string]*Node), edges: make([]Edge, 0)}
|
||||
}
|
||||
|
||||
// PutNode: puts a node in the graph
|
||||
func (g *Graph) PutNode(label string, shape Shape) error {
|
||||
_, exists := g.nodes[label]
|
||||
|
||||
if exists {
|
||||
// If exists no need to add the ndoe
|
||||
return nil
|
||||
}
|
||||
|
||||
g.nodes[label] = &Node{Name: label, Shape: shape}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeContituents[D Dottable](result *strings.Builder, elements ...D) error {
|
||||
for _, node := range elements {
|
||||
dot, err := node.GetDOT()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = result.WriteString(dot)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Graph) GetDOT() (string, error) {
|
||||
var result strings.Builder
|
||||
|
||||
_, err := result.WriteString(fmt.Sprintf("%s {\n", g.Type))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = result.WriteString("node [colorscheme=set312];\n")
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
nodes := lib.MapValues(g.nodes)
|
||||
|
||||
err = writeContituents(&result, nodes...)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = writeContituents(&result, g.edges...)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = result.WriteString("}")
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return result.String(), nil
|
||||
}
|
||||
|
||||
func (g *Graph) constructEdge(label string, from *Node, to *Node) Edge {
|
||||
switch g.Type {
|
||||
case DIGRAPH:
|
||||
return &DirectedEdge{Label: label, From: from, To: to}
|
||||
default:
|
||||
return &UndirectedEdge{Label: label, From: from, To: to}
|
||||
}
|
||||
}
|
||||
|
||||
// AddEdge: adds an edge between two nodes in the graph
|
||||
func (g *Graph) AddEdge(label string, from string, to string) error {
|
||||
fromNode, exists := g.nodes[from]
|
||||
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("Node %s does not exist", from))
|
||||
}
|
||||
|
||||
toNode, exists := g.nodes[to]
|
||||
|
||||
if !exists {
|
||||
return errors.New(fmt.Sprintf("Node %s does not exist", to))
|
||||
}
|
||||
|
||||
g.edges = append(g.edges, g.constructEdge(label, fromNode, toNode))
|
||||
return nil
|
||||
}
|
||||
|
||||
const numColours = 12
|
||||
|
||||
func (n *Node) hash() int {
|
||||
h := fnv.New32a()
|
||||
h.Write([]byte(n.Name))
|
||||
return (int(h.Sum32()) % numColours) + 1
|
||||
}
|
||||
|
||||
func (n *Node) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("node[shape=%s, style=\"filled\", fillcolor=%d] %s;\n",
|
||||
n.Shape, n.hash(), n.Name), nil
|
||||
}
|
||||
|
||||
func (e *DirectedEdge) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("%s -> %s;\n", e.From.Name, e.To.Name), nil
|
||||
}
|
||||
|
||||
func (e *UndirectedEdge) GetDOT() (string, error) {
|
||||
return fmt.Sprintf("%s -- %s;\n", e.From.Name, e.To.Name), nil
|
||||
}
|
@ -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)}
|
||||
}
|
@ -68,7 +68,6 @@ type MeshIpc interface {
|
||||
JoinMesh(args JoinMeshArgs, reply *string) error
|
||||
LeaveMesh(meshId string, reply *string) error
|
||||
GetMesh(meshId string, reply *GetMeshReply) error
|
||||
GetDOT(meshId string, reply *string) error
|
||||
Query(query QueryMesh, reply *string) error
|
||||
PutDescription(description string, reply *string) error
|
||||
PutAlias(alias string, reply *string) error
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/graph"
|
||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||
)
|
||||
|
||||
// MeshGraphConverter converts a mesh to a graph
|
||||
type MeshGraphConverter interface {
|
||||
// convert the mesh to textual form
|
||||
Generate(meshId string) (string, error)
|
||||
}
|
||||
|
||||
type MeshDOTConverter struct {
|
||||
manager MeshManager
|
||||
}
|
||||
|
||||
func (c *MeshDOTConverter) Generate(meshId string) (string, error) {
|
||||
mesh := c.manager.GetMesh(meshId)
|
||||
|
||||
if mesh == nil {
|
||||
return "", errors.New("mesh does not exist")
|
||||
}
|
||||
|
||||
g := graph.NewGraph(meshId, graph.GRAPH)
|
||||
|
||||
snapshot, err := mesh.GetMesh()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, node := range snapshot.GetNodes() {
|
||||
c.graphNode(g, node, meshId)
|
||||
}
|
||||
|
||||
nodes := lib.MapValues(snapshot.GetNodes())
|
||||
|
||||
for i, node1 := range nodes[:len(nodes)-1] {
|
||||
for _, node2 := range nodes[i+1:] {
|
||||
if node1.GetWgEndpoint() == node2.GetWgEndpoint() {
|
||||
continue
|
||||
}
|
||||
|
||||
node1Id := fmt.Sprintf("\"%s\"", node1.GetIdentifier())
|
||||
node2Id := fmt.Sprintf("\"%s\"", node2.GetIdentifier())
|
||||
g.AddEdge(fmt.Sprintf("%s to %s", node1Id, node2Id), node1Id, node2Id)
|
||||
}
|
||||
}
|
||||
|
||||
return g.GetDOT()
|
||||
}
|
||||
|
||||
// graphNode: graphs a node within the mesh
|
||||
func (c *MeshDOTConverter) graphNode(g *graph.Graph, node MeshNode, meshId string) {
|
||||
nodeId := fmt.Sprintf("\"%s\"", node.GetIdentifier())
|
||||
g.PutNode(nodeId, graph.CIRCLE)
|
||||
|
||||
self, _ := c.manager.GetSelf(meshId)
|
||||
|
||||
if NodeEquals(self, node) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, route := range node.GetRoutes() {
|
||||
routeId := fmt.Sprintf("\"%s\"", route)
|
||||
g.PutNode(routeId, graph.HEXAGON)
|
||||
g.AddEdge(fmt.Sprintf("%s to %s", nodeId, routeId), nodeId, routeId)
|
||||
}
|
||||
}
|
||||
|
||||
func NewMeshDotConverter(m MeshManager) MeshGraphConverter {
|
||||
return &MeshDOTConverter{manager: m}
|
||||
}
|
@ -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: true,
|
||||
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")
|
||||
|
@ -20,6 +20,12 @@ type Route interface {
|
||||
GetPath() []string
|
||||
}
|
||||
|
||||
func RouteEqual(r1 Route, r2 Route) bool {
|
||||
return r1.GetDestination().IP.Equal(r2.GetDestination().IP) &&
|
||||
r1.GetHopCount() == r2.GetHopCount() &&
|
||||
slices.Equal(r1.GetPath(), r2.GetPath())
|
||||
}
|
||||
|
||||
func RouteEquals(r1, r2 Route) bool {
|
||||
return r1.GetDestination().String() == r2.GetDestination().String() &&
|
||||
r1.GetHopCount() == r2.GetHopCount() &&
|
||||
|
@ -182,19 +182,6 @@ func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *IpcHandler) GetDOT(meshId string, reply *string) error {
|
||||
g := mesh.NewMeshDotConverter(n.Server.GetMeshManager())
|
||||
|
||||
result, err := g.Generate(meshId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*reply = result
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error {
|
||||
queryResponse, err := n.Server.GetQuerier().Query(params.MeshId, params.Query)
|
||||
|
||||
|
@ -3,6 +3,7 @@ package robin
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ipc"
|
||||
"github.com/tim-beatham/wgmesh/pkg/mesh"
|
||||
@ -17,9 +18,11 @@ func TestCreateMeshRepliesMeshId(t *testing.T) {
|
||||
requester := getRequester()
|
||||
|
||||
err := requester.CreateMesh(&ipc.NewMeshArgs{
|
||||
IfName: "wg0",
|
||||
WgPort: 5000,
|
||||
Endpoint: "abc.com",
|
||||
WgArgs: ipc.WireGuardArgs{
|
||||
WgPort: 500,
|
||||
Endpoint: "abc.com:1234",
|
||||
Role: "peer",
|
||||
},
|
||||
}, &reply)
|
||||
|
||||
if err != nil {
|
||||
@ -52,9 +55,8 @@ func TestListMeshesMeshesNotEmpty(t *testing.T) {
|
||||
|
||||
requester.Server.GetMeshManager().AddMesh(&mesh.AddMeshParams{
|
||||
MeshId: "tim123",
|
||||
DevName: "wg0",
|
||||
WgPort: 5000,
|
||||
MeshBytes: make([]byte, 0),
|
||||
Conf: &conf.WgConfiguration{},
|
||||
})
|
||||
|
||||
err := requester.ListMeshes("", &reply)
|
||||
|
@ -1,6 +1,8 @@
|
||||
package sync
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"time"
|
||||
@ -25,7 +27,7 @@ type SyncerImpl struct {
|
||||
syncCount int
|
||||
cluster conn.ConnCluster
|
||||
conf *conf.DaemonConfiguration
|
||||
lastSync uint64
|
||||
lastSync map[string]uint64
|
||||
}
|
||||
|
||||
// Sync: Sync random nodes
|
||||
@ -39,6 +41,12 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
|
||||
if self != nil && self.GetType() == conf.PEER_ROLE && !s.manager.HasChanges(meshId) && s.infectionCount == 0 {
|
||||
logging.Log.WriteInfof("No changes for %s", meshId)
|
||||
|
||||
// If not synchronised in certain pull from random neighbour
|
||||
if uint64(time.Now().Unix())-s.lastSync[meshId] > 20 {
|
||||
return s.Pull(meshId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -110,7 +118,7 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
}
|
||||
|
||||
s.manager.GetMesh(meshId).SaveChanges()
|
||||
s.lastSync = uint64(time.Now().Unix())
|
||||
s.lastSync[meshId] = uint64(time.Now().Unix())
|
||||
|
||||
logging.Log.WriteInfof("UPDATING WG CONF")
|
||||
err := s.manager.ApplyConfig()
|
||||
@ -122,6 +130,51 @@ func (s *SyncerImpl) Sync(meshId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pull one node in the cluster, if there has not been message dissemination
|
||||
// in a certain period of time pull a random node within the cluster
|
||||
func (s *SyncerImpl) Pull(meshId string) error {
|
||||
mesh := s.manager.GetMesh(meshId)
|
||||
self, err := s.manager.GetSelf(meshId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pubKey, _ := self.GetPublicKey()
|
||||
|
||||
if mesh == nil {
|
||||
return errors.New("mesh is nil, invalid operation")
|
||||
}
|
||||
|
||||
peers := mesh.GetPeers()
|
||||
neighbours := s.cluster.GetNeighbours(peers, pubKey.String())
|
||||
neighbour := lib.RandomSubsetOfLength(neighbours, 1)
|
||||
|
||||
if len(neighbour) == 0 {
|
||||
logging.Log.WriteInfof("no neighbours")
|
||||
return nil
|
||||
}
|
||||
|
||||
logging.Log.WriteInfof("PULLING from node %s", neighbour[0])
|
||||
|
||||
pullNode, err := mesh.GetNode(neighbour[0])
|
||||
|
||||
if err != nil || pullNode == nil {
|
||||
return fmt.Errorf("node %s does not exist in the mesh", neighbour[0])
|
||||
}
|
||||
|
||||
err = s.requester.SyncMesh(meshId, pullNode)
|
||||
|
||||
if err == nil || err == io.EOF {
|
||||
s.lastSync[meshId] = uint64(time.Now().Unix())
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
|
||||
s.syncCount++
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncMeshes: Sync all meshes
|
||||
func (s *SyncerImpl) SyncMeshes() error {
|
||||
for meshId := range s.manager.GetMeshes() {
|
||||
@ -143,5 +196,6 @@ func NewSyncer(m mesh.MeshManager, conf *conf.DaemonConfiguration, r SyncRequest
|
||||
requester: r,
|
||||
infectionCount: 0,
|
||||
syncCount: 0,
|
||||
cluster: cluster}
|
||||
cluster: cluster,
|
||||
lastSync: make(map[string]uint64)}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user