Add Ansible dump feature

master
Ambrose Chua 2020-01-05 03:14:39 +00:00
parent 8a75196ae3
commit 2fd1a77ad1
5 changed files with 83 additions and 76 deletions

View File

@ -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/<interface>.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
}

View File

@ -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
}

82
cmd/dump.go Normal file
View File

@ -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/<interface>.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
}

View File

@ -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
}

View File

@ -16,9 +16,8 @@ func main() {
Flags: []cli.Flag{}, Flags: []cli.Flag{},
Commands: []*cli.Command{ Commands: []*cli.Command{
cmd.CmdServer, cmd.CmdServer,
cmd.CmdList,
cmd.CmdApprove,
cmd.CmdRequest, cmd.CmdRequest,
cmd.CmdDump,
}, },
} }