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 19, 2020
1 parent e35592f commit a713d1a
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
102 changes: 102 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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
client, err = wgctrl.New()
if err != nil {
log.Fatalf("examples init: %v", err)
}
}

func ExampleClient_Device() {
dev, err := client.Device("wg-foobar")
if os.IsNotExist(err) {
fmt.Println(err)
} else {
log.Fatalf("client.Device() expected: %v, got : %v", os.ErrNotExist, err)
}
fmt.Println(dev)
// Output:
// file does not exist
// <nil>
}

func ExampleClient_ConfigureDevice() {
// More key generation examples in wgtypes sub-package
// Error checks omitted for brevity
pri, _ := wgtypes.ParseKey("Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9Hixkk=")
pub, _ := wgtypes.ParseKey("C9VaGN9qYYWPi4IKnbM9uv75E6iL9pBqY+i+XjUc13o=") // Should be generated by peer
psk, _ := wgtypes.GenerateKey()

port := 51800
ka := 20 * time.Second

conf := wgtypes.Config{
PrivateKey: &pri,
ListenPort: &port,
ReplacePeers: false,
Peers: []wgtypes.PeerConfig{{
PublicKey: pub,
Remove: false,
UpdateOnly: true,
PresharedKey: &psk,
Endpoint: &net.UDPAddr{
IP: net.ParseIP("192.168.22.44"),
Port: 51800,
},
PersistentKeepaliveInterval: &ka,
ReplaceAllowedIPs: false,
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 a713d1a

Please sign in to comment.