Documentation ¶
Overview ¶
Package wgconf provides a limited set of WireGuard configuration types that can be marshaled as systemd netdev configuration.
Example ¶
package main import ( "fmt" "net" "github.com/gentlemanautomaton/wgconf" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) func main() { peers := wgconf.PeerList{ { Name: "Laptop1", Description: "alice.laptop", PublicKey: ParseKey("lO/VBDYf0zKo4N+RwnjNsBYMb8Wuw8WUZP00C7CviS0="), AllowedIPs: []net.IPNet{ParseIPNet("10.0.0.1/32")}, }, { Name: "Laptop2", Description: "bob.laptop", PublicKey: ParseKey("uEVNLxM71801qc3xOYsgvoKjX3AaK6+CV3c8tzjR0iE="), AllowedIPs: []net.IPNet{ParseIPNet("10.0.0.2/32"), ParseIPNet("192.168.0.254/32")}, }, } fmt.Print(peers.NetDev()) } func ParseKey(key string) wgtypes.Key { k, err := wgtypes.ParseKey(key) if err != nil { panic(err) } return k } func ParseIPNet(cidr string) net.IPNet { _, v, err := net.ParseCIDR(cidr) if err != nil { panic(err) } return *v }
Output: # Laptop1 (alice.laptop) [WireGuardPeer] PublicKey=lO/VBDYf0zKo4N+RwnjNsBYMb8Wuw8WUZP00C7CviS0= AllowedIPs=10.0.0.1/32 # Laptop2 (bob.laptop) [WireGuardPeer] PublicKey=uEVNLxM71801qc3xOYsgvoKjX3AaK6+CV3c8tzjR0iE= AllowedIPs=10.0.0.2/32,192.168.0.254/32
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
Compare provides a comparison function for peers. It is used by PeerList to determine its sort order. It returns the following values:
-1: Peer a is less than peer b 0: Peer a and b are equivalent 1: Peer a is greater than peer b
Peers are by their allowed IP addresses, in ascending order. The public key is used as a tie breaker for peers lacking addresses.
func ReconcilePeers ¶
ReconcilePeers updates the peer list configuration for the given WireGuard device.
The difference between the old peer list and the new peer list is used to determine the set of peer list changes that should be issued. Peers present in the old list but not present in the new list will be removed. Peers that are not present in either list will not be modified.
Types ¶
type AllowedIPs ¶
AllowedIPs is a slice of network addresses that are assigned to a WireGuard peers.
func (AllowedIPs) String ¶
func (ipnets AllowedIPs) String() string
String returns a comma-separated AllowedIPs string for the given IP networks. Invalid networks will be omitted.
type Peer ¶
type Peer struct { Name string Description string PublicKey Key AllowedIPs AllowedIPs }
Peer is a WireGuard peer.
type PeerFilter ¶
PeerFilter is a filter that can be applied to peers.
type PeerList ¶
type PeerList []Peer
PeerList is a list of WireGuard peers.
func CollectPeers ¶
CollectPeers returns the current set of WireGuard peers for a device.
func CompareLists ¶
CompareLists compares a with b and determines the differences.
Peers are uniquely identified by their public key.
func (PeerList) Less ¶
Less reports whether the peer with index i must sort before the peer with index j. The order is based on the Compare function.
func (PeerList) Match ¶
func (list PeerList) Match(filter PeerFilter) PeerList
Match returns the set of peers that match the given filter.