From 255d3c8b391e8077204df06378d90cb6f553a398 Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Mon, 25 Dec 2023 01:25:20 +0000 Subject: [PATCH] 66-improve-graph-dot-tool - Showing services a node provides - Showing all meshes not just one - Showing the default route --- cmd/wg-mesh/main.go | 40 ++++++++++++++++++++++++++++++---------- pkg/dot/dot.go | 7 ++++--- pkg/dot/wg.go | 15 +++++++-------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/cmd/wg-mesh/main.go b/cmd/wg-mesh/main.go index cfe56d2..7f879f7 100644 --- a/cmd/wg-mesh/main.go +++ b/cmd/wg-mesh/main.go @@ -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" ) @@ -91,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) { @@ -258,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", @@ -351,7 +371,7 @@ func main() { } if getGraphCmd.Happened() { - getGraph(client, *getGraphMeshId) + getGraph(client) } if leaveMeshCmd.Happened() { diff --git a/pkg/dot/dot.go b/pkg/dot/dot.go index b5aedaf..8df6173 100644 --- a/pkg/dot/dot.go +++ b/pkg/dot/dot.go @@ -18,9 +18,10 @@ const ( ) const ( - CIRCLE Shape = "circle" - STAR Shape = "star" - HEXAGON Shape = "hexagon" + CIRCLE Shape = "circle" + STAR Shape = "star" + HEXAGON Shape = "hexagon" + PARALLELOGRAM Shape = "parallelogram" ) type Graph interface { diff --git a/pkg/dot/wg.go b/pkg/dot/wg.go index c94de1e..d592047 100644 --- a/pkg/dot/wg.go +++ b/pkg/dot/wg.go @@ -34,7 +34,7 @@ func (c *MeshDOTConverter) Generate() (string, error) { } for destination := range c.destinations { - g.PutNode(destination, destination, 3, CIRCLE) + g.PutNode(destination, destination, 1, HEXAGON) } return g.GetDOT() @@ -67,7 +67,7 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes g.PutNode(node.PublicKey, alias, 2, CIRCLE) for _, route := range node.Routes { - if len(route.Path) == 0 && route.Destination == "::/0" { + if len(route.Path) == 0 { g.AddEdge(route.Destination, "", node.PublicKey, route.Destination) continue } @@ -88,7 +88,7 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes g.AddEdge(routeID, "", reversedPath[index], reversedPath[index+1]) } - if reversedPath[len(reversedPath)-1] == "::/0" { + if route.Destination == "::/0" { c.destinations[route.Destination] = struct{}{} lastMesh := reversedPath[len(reversedPath)-1] routeID := fmt.Sprintf("%s to %s", lastMesh, route.Destination) @@ -97,15 +97,14 @@ func (c *MeshDOTConverter) graphNode(g *RootGraph, node ctrlserver.MeshNode, mes } for service := range node.Services { - c.putService(g, service, node) + c.putService(g, service, meshId, node) } } // putService: construct a service node and a link between the nodes -func (c *MeshDOTConverter) putService(g *RootGraph, key string, node ctrlserver.MeshNode) { - serviceID := fmt.Sprintf("%s%s", key, node.PublicKey) - - g.PutNode(serviceID, key, 1, CIRCLE) +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) }