forked from WireGuard/wgctrl-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements WireGuard#37
- Loading branch information
Showing
2 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= | ||
} |