1
0
forked from extern/smegmesh
smegmesh/cmd/wg-mesh/main.go

136 lines
2.9 KiB
Go
Raw Normal View History

2023-09-18 13:59:28 +02:00
package main
import (
"fmt"
2023-09-28 17:55:37 +02:00
"log"
ipcRpc "net/rpc"
"os"
"github.com/akamensky/argparse"
2023-09-19 14:45:49 +02:00
"github.com/tim-beatham/wgmesh/pkg/ctrlserver"
"github.com/tim-beatham/wgmesh/pkg/ipc"
2023-09-18 13:59:28 +02:00
)
2023-09-18 18:00:43 +02:00
const SockAddr = "/tmp/wgmesh_ipc.sock"
2023-09-20 00:50:44 +02:00
func createNewMesh(client *ipcRpc.Client) string {
2023-09-18 18:00:43 +02:00
var reply string
err := client.Call("Mesh.CreateNewMesh", "", &reply)
if err != nil {
2023-09-20 00:50:44 +02:00
return err.Error()
2023-09-18 18:00:43 +02:00
}
2023-09-20 00:50:44 +02:00
return reply
2023-09-18 18:00:43 +02:00
}
func listMeshes(client *ipcRpc.Client) {
2023-09-19 14:45:49 +02:00
var reply map[string]ctrlserver.Mesh
err := client.Call("Mesh.ListMeshes", "", &reply)
if err != nil {
return
}
for sharedKey := range reply {
fmt.Println(sharedKey)
}
}
2023-09-20 00:50:44 +02:00
func joinMesh(client *ipcRpc.Client, meshId string, ipAddress string) string {
var reply string
args := ipc.JoinMeshArgs{MeshId: meshId, IpAdress: ipAddress}
err := client.Call("Mesh.JoinMesh", &args, &reply)
2023-09-20 00:50:44 +02:00
if err != nil {
return err.Error()
}
return reply
}
func getMesh(client *ipcRpc.Client, meshId string) {
reply := new(ipc.GetMeshReply)
err := client.Call("Mesh.GetMesh", &meshId, &reply)
if err != nil {
2023-09-28 17:55:37 +02:00
log.Panic(err.Error())
return
}
2023-09-20 00:50:44 +02:00
for _, node := range reply.Nodes {
fmt.Println("Public Key: " + node.PublicKey)
fmt.Println("WireGuard Endpoint: " + node.HostEndpoint)
fmt.Println("Control Endpoint: " + node.WgEndpoint)
fmt.Println("Wg IP: " + node.WgHost)
fmt.Println("---")
}
2023-09-19 14:45:49 +02:00
}
2023-09-21 19:43:29 +02:00
func enableInterface(client *ipcRpc.Client, meshId string) {
var reply string
err := client.Call("Mesh.EnableInterface", &meshId, &reply)
if err != nil {
2023-09-28 17:55:37 +02:00
(err.Error())
2023-09-21 19:43:29 +02:00
return
}
fmt.Println(reply)
}
func main() {
parser := argparse.NewParser("wg-mesh",
"wg-mesh Manipulate WireGuard meshes")
newMeshCmd := parser.NewCommand("new-mesh", "Create a new mesh")
2023-09-19 14:45:49 +02:00
listMeshCmd := parser.NewCommand("list-meshes", "List meshes the node is connected to")
joinMeshCmd := parser.NewCommand("join-mesh", "Join a mesh network")
2023-09-20 00:50:44 +02:00
getMeshCmd := parser.NewCommand("get-mesh", "Get a mesh network")
2023-09-21 19:43:29 +02:00
enableInterfaceCmd := parser.NewCommand("enable-interface", "Enable A Specific Mesh Interface")
2023-09-19 14:45:49 +02:00
var meshId *string = joinMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
var ipAddress *string = joinMeshCmd.String("i", "ip", &argparse.Options{Required: true})
2023-09-19 14:45:49 +02:00
2023-09-20 00:50:44 +02:00
var getMeshId *string = getMeshCmd.String("m", "mesh", &argparse.Options{Required: true})
2023-09-21 19:43:29 +02:00
var enableInterfaceMeshId *string = enableInterfaceCmd.String("m", "mesh", &argparse.Options{Required: true})
2023-09-20 00:50:44 +02:00
err := parser.Parse(os.Args)
2023-09-18 13:59:28 +02:00
if err != nil {
fmt.Print(parser.Usage(err))
return
}
2023-09-18 13:59:28 +02:00
client, err := ipcRpc.DialHTTP("unix", SockAddr)
2023-09-18 18:00:43 +02:00
if err != nil {
fmt.Println(err.Error())
return
}
2023-09-18 13:59:28 +02:00
2023-09-18 18:00:43 +02:00
if newMeshCmd.Happened() {
2023-09-20 00:50:44 +02:00
fmt.Println(createNewMesh(client))
}
2023-09-19 14:45:49 +02:00
if listMeshCmd.Happened() {
listMeshes(client)
}
if joinMeshCmd.Happened() {
2023-09-20 00:50:44 +02:00
fmt.Println(joinMesh(client, *meshId, *ipAddress))
}
if getMeshCmd.Happened() {
getMesh(client, *getMeshId)
2023-09-19 14:45:49 +02:00
}
2023-09-21 19:43:29 +02:00
if enableInterfaceCmd.Happened() {
enableInterface(client, *enableInterfaceMeshId)
}
}