diff --git a/doc_test.go b/doc_test.go new file mode 100644 index 0000000..e032427 --- /dev/null +++ b/doc_test.go @@ -0,0 +1,109 @@ +package wgctrl_test + +// This file contains documentation examples + +import ( + "fmt" + "log" + "net" + "os" + "time" + + "golang.zx2c4.com/wireguard/wgctrl" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" +) + +func ExampleNew() { + client, err := wgctrl.New() + if err != nil { + log.Fatalf("failed to open client: %v", err) + } + defer client.Close() + +} + +var ( + client *wgctrl.Client +) + +func init() { + var err error + if client, err = wgctrl.New(); err != nil { + log.Fatalf("examples init: %v", err) + } + + // Make sure `wg-foobar` really does not exist` + if _, err := client.Device("wg-foobar"); !os.IsNotExist(err) { + log.Fatalf("init: client.Device() expected err: %v, got : %v", os.ErrNotExist, err) + } +} + +// Following example expects the "wg-foobar" interface not to exist. +func ExampleClient_Device() { + dev, err := client.Device("wg-foobar") + if os.IsNotExist(err) { + fmt.Println(err) + } else { + log.Fatalf("client.Device() expected err: %v, got : %v", os.ErrNotExist, err) + } + fmt.Println(dev) + // Output: + // file does not exist + // +} + +// Following example expects the "wg-foobar" interface not to exist. +// However, the provided config is valid. +// Key related error checks omitted for brevity. +// More key generation examples in "wgtypes" sub-package. +func ExampleClient_ConfigureDevice() { + pri, _ := wgtypes.ParseKey("Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk=") + pub, _ := wgtypes.ParseKey("C9VaGN9qYYWPi4IKnbM9uv75E6iL9pBqY+i+XjUc13o=") // Should be generated by the remote peer + psk, _ := wgtypes.GenerateKey() + + port := 51800 + ka := 20 * time.Second + + conf := wgtypes.Config{ + PrivateKey: &pri, + ListenPort: &port, + ReplacePeers: true, + Peers: []wgtypes.PeerConfig{{ + PublicKey: pub, + Remove: false, + UpdateOnly: false, + PresharedKey: &psk, + Endpoint: &net.UDPAddr{ + IP: net.ParseIP("192.168.22.44"), + Port: 51800, + }, + PersistentKeepaliveInterval: &ka, + ReplaceAllowedIPs: true, + AllowedIPs: []net.IPNet{{ + IP: net.ParseIP("0.0.0.0"), + Mask: net.ParseIP("0.0.0.0").DefaultMask(), + }}, + }}, + } + + if err := client.ConfigureDevice("wg-foobar", conf); err != nil { + if os.IsNotExist(err) { + fmt.Println(err) + } else { + log.Fatalf("Unknown config error: %v", err) + } + } + // Output: + // file does not exist +} + +func ExampleClient_Devices() { + devs, err := client.Devices() + if err != nil { + log.Fatal(err) + } + + for _, d := range devs { + fmt.Printf("Interface %s is of type %s", d.Name, d.Type) + } +} diff --git a/wgtypes/doc_test.go b/wgtypes/doc_test.go new file mode 100644 index 0000000..249ccd5 --- /dev/null +++ b/wgtypes/doc_test.go @@ -0,0 +1,78 @@ +package wgtypes_test + +import ( + "fmt" + "log" + "math/rand" + + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" +) + +// This file contains documentation examples + +func ExampleGenerateKey() { + psk, err := wgtypes.GenerateKey() + if err != nil { + log.Fatal(err) + } + + pc := wgtypes.PeerConfig{ + PresharedKey: &psk, + } + fmt.Println(pc) +} + +func ExampleGeneratePrivateKey() { + priv, err := wgtypes.GeneratePrivateKey() + if err != nil { + log.Fatal(err) + } + + conf := wgtypes.Config{ + PrivateKey: &priv, + } + fmt.Println(conf) +} + +func init() { + rand.Seed(1) +} + +func ExampleNewKey() { + bs := make([]byte, 32) + + _, err := rand.Read(bs) + if err != nil { + log.Fatal(err) + } + + key, err := wgtypes.NewKey(bs) + if err != nil { + log.Fatal(err) + } + fmt.Println(key) + // Output: + // Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk= +} + +func ExampleParseKey() { + key, err := wgtypes.ParseKey("Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk=") + if err != nil { + log.Fatal(err) + } + fmt.Println(key) + // Output: + // Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk= +} + +func ExampleKey_PublicKey() { + priv, err := wgtypes.ParseKey("Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk=") + if err != nil { + log.Fatal(err) + } + pub := priv.PublicKey() + + fmt.Println(pub) + // Output: + // ZP/Mzlvt9BwNH9oqtuL0ZP8OW1foBBWfE8R6nSrM/nk= +}