From 2fd1a77ad109942a2e1286e5719cca16c1d0bc90 Mon Sep 17 00:00:00 2001 From: Ambrose Chua Date: Sun, 5 Jan 2020 03:14:39 +0000 Subject: [PATCH] Add Ansible dump feature --- cmd/ansibleinventory.go | 40 -------------------- cmd/approve.go | 17 --------- cmd/dump.go | 82 +++++++++++++++++++++++++++++++++++++++++ cmd/list.go | 17 --------- main.go | 3 +- 5 files changed, 83 insertions(+), 76 deletions(-) delete mode 100644 cmd/ansibleinventory.go delete mode 100644 cmd/approve.go create mode 100644 cmd/dump.go delete mode 100644 cmd/list.go diff --git a/cmd/ansibleinventory.go b/cmd/ansibleinventory.go deleted file mode 100644 index 413be3e..0000000 --- a/cmd/ansibleinventory.go +++ /dev/null @@ -1,40 +0,0 @@ -package cmd - -import ( - "log" - - "github.com/urfave/cli/v2" -) - -var CmdAnsibleInventory = &cli.Command{ - Name: "ansible-inventory", - Usage: "Dump WireGuard configuration as Ansible inventory", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "interface", - Aliases: []string{"i"}, - Value: "wg0", - Usage: "Read default configuration path for the interface", - }, - &cli.StringFlag{ - Name: "config", - Aliases: []string{"c"}, - Value: "", - DefaultText: "/etc/wireguard/.conf", - Usage: "Path to the existing WireGuard configuration file", - }, - }, - Action: runAnsibleInventory, -} - -func runAnsibleInventory(ctx *cli.Context) error { - inter := ctx.String("interface") - config := ctx.String("config") - if !ctx.IsSet("config") { - config = "/etc/wireguard/" + inter + ".conf" - } - - log.Println(config) - - return nil -} diff --git a/cmd/approve.go b/cmd/approve.go deleted file mode 100644 index 87f50d9..0000000 --- a/cmd/approve.go +++ /dev/null @@ -1,17 +0,0 @@ -package cmd - -import ( - //"github.com/serverwentdown/wireguard-negotiator/lib" - "github.com/urfave/cli/v2" -) - -var CmdApprove = &cli.Command{ - Name: "approve", - Usage: "Approve pending negotiations", - Action: runApprove, -} - -func runApprove(ctx *cli.Context) error { - //client := lib.NewClient(ctx.String("server"), ctx.Bool("insecure")) - return nil -} diff --git a/cmd/dump.go b/cmd/dump.go new file mode 100644 index 0000000..850133f --- /dev/null +++ b/cmd/dump.go @@ -0,0 +1,82 @@ +package cmd + +import ( + "bytes" + "fmt" + "os" + + "github.com/serverwentdown/wireguard-negotiator/lib" + "github.com/urfave/cli/v2" +) + +var CmdDump = &cli.Command{ + Name: "dump", + Usage: "Dump WireGuard configuration as a list of IPs, useful for Ansible inventories. Dumps only the first address in allowedIP, taking into account the mask", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "interface", + Aliases: []string{"i"}, + Value: "wg0", + Usage: "Read default configuration path for the interface", + }, + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Value: "", + DefaultText: "/etc/wireguard/.conf", + Usage: "Path to the existing WireGuard configuration file", + }, + }, + Action: runDump, +} + +func runDump(ctx *cli.Context) error { + inter := ctx.String("interface") + config := ctx.String("config") + if !ctx.IsSet("config") { + config = "/etc/wireguard/" + inter + ".conf" + } + + // Open config + file, err := os.Open(config) + defer file.Close() + if err != nil { + return err + } + + // Read configuration + device, _, err := lib.ReadConfig(file) + if err != nil { + return err + } + + empty4 := []byte{0, 0, 0, 0} + empty6 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + + // Dump hosts by first allowedIPs + for _, peer := range device.Peers { + fmt.Printf("# %v\n", peer.PublicKey) + + dumped := false + // Choose the first non-zero host address + for _, allowedIP := range peer.AllowedIPs { + ip4, ip6 := allowedIP.IP.To4(), allowedIP.IP.To16() + if bytes.Equal(ip4, empty4) { + continue + } + if bytes.Equal(ip6, empty6) { + continue + } + // Assume the first host in the network is the same as the given IP + // Dump the IP + fmt.Println(allowedIP.IP.String()) + dumped = true + break + } + if !dumped { + fmt.Println("# no address found") + } + } + + return nil +} diff --git a/cmd/list.go b/cmd/list.go deleted file mode 100644 index 1ebe8f6..0000000 --- a/cmd/list.go +++ /dev/null @@ -1,17 +0,0 @@ -package cmd - -import ( - //"github.com/serverwentdown/wireguard-negotiator/lib" - "github.com/urfave/cli/v2" -) - -var CmdList = &cli.Command{ - Name: "list", - Usage: "List all pending negotiations", - Action: runList, -} - -func runList(ctx *cli.Context) error { - //client := lib.NewClient(ctx.String("server"), ctx.Bool("insecure")) - return nil -} diff --git a/main.go b/main.go index f41b22b..9860df6 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,8 @@ func main() { Flags: []cli.Flag{}, Commands: []*cli.Command{ cmd.CmdServer, - cmd.CmdList, - cmd.CmdApprove, cmd.CmdRequest, + cmd.CmdDump, }, }