Skip to content

Commit

Permalink
Code examples
Browse files Browse the repository at this point in the history
Implements WireGuard#37
  • Loading branch information
muhlemmer committed Mar 27, 2020
1 parent ceff612 commit 69374ad
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
109 changes: 109 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -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
// <nil>
}

// 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)
}
}
78 changes: 78 additions & 0 deletions wgtypes/doc_test.go
Original file line number Diff line number Diff line change
@@ -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=
}

0 comments on commit 69374ad

Please sign in to comment.