Add Ansible dump feature
parent
8a75196ae3
commit
2fd1a77ad1
|
@ -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
|
|
||||||
}
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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
|
||||||
|
}
|
17
cmd/list.go
17
cmd/list.go
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue