awgctrl

package module
v0.0.0-...-d49fff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2025 License: MIT Imports: 15 Imported by: 0

README

awgctrl

Package awgctrl enables control of amneziawg devices on Linux.

For more information on amneziawg, please see https://github.com/amnezia-vpn/amneziawg-go.

MIT Licensed.

Overview

awgctrl can control amneziawg devices on Linux via generic netlink.

If you are aware of any efforts on this front, please file an issue.

This package implements amneziawg configuration protocol operations, enabling the configuration of existing amneziawg devices. Operations such as creating amneziawg devices, or applying IP addresses to those devices, are out of scope for this package.

Documentation

Overview

package awgctrl provides internal access to Linux's WireGuard generic netlink interface.

Index

Constants

View Source
const (
	WGDEVICE_A_JC   = iota + 1 + unix.WGDEVICE_A_PEERS // uint16
	WGDEVICE_A_JMIN                                    // uint16
	WGDEVICE_A_JMAX                                    // uint16
	WGDEVICE_A_S1                                      // uint16
	WGDEVICE_A_S2                                      // uint16
	WGDEVICE_A_H1                                      // uint32
	WGDEVICE_A_H2                                      // uint32
	WGDEVICE_A_H3                                      // uint32
	WGDEVICE_A_H4                                      // uint32
)

Additional attribute types added in amneziawg.

View Source
const KeyLen = 32

KeyLen is the expected key length for a WireGuard key.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

A Client provides access to Linux WireGuard netlink information.

func New

func New() (*Client, bool, error)

New creates a new Client and returns whether or not the generic netlink interface is available.

func (*Client) Close

func (c *Client) Close() error

Close closes the connection.

func (*Client) ConfigureDevice

func (c *Client) ConfigureDevice(name string, cfg Config) error

ConfigureDevice configures a WireGuard device.

func (*Client) Device

func (c *Client) Device(name string) (*Device, error)

Device implements wginternal.Client.

func (*Client) Devices

func (c *Client) Devices() ([]*Device, error)

Devices returns a list of WireGuard devices.

type Config

type Config struct {
	// PrivateKey specifies a private key configuration, if not nil.
	//
	// A non-nil, zero-value Key will clear the private key.
	PrivateKey *Key

	// ListenPort specifies a device's listening port, if not nil.
	ListenPort *uint16

	// FirewallMark specifies a device's firewall mark, if not nil.
	//
	// If non-nil and set to 0, the firewall mark will be cleared.
	FirewallMark *uint32

	// JunkCount sets the number of junk packets, if not nil.
	//
	// Values: 1-128
	// Recommended: 4-12
	JunkCount *uint16

	// JunkMin specifies the minimum size of junk packets, if not nil.
	//
	// Values: JunkMax > JunkMin < MTU
	// Recommended: 8
	JunkMin *uint16

	// JunkMax specifies the maximum size of junk packets, if not nil.
	//
	// Values: JunkMin < JunkMax <= MTU
	// Recommended: 80
	JunkMax *uint16

	// S1 specifies init packet junk size, if not nil.
	// Values: S1 <= (MTU - 148) && S1 + 56 != S2
	// Recommended: 15-150
	S1 *uint16

	// S2 specifies response packet junk size, if not nil.
	// Values: S2 <= (MTU - 92)
	// Recommended: 15-150
	S2 *uint16

	// H* specifies packet magic header, if not nil.
	// H1 - init | H2 - response | H3 - underload | H4 - transport.
	// Values: (H* <= MaxInt32); unique among each other
	// Recommended: 5-MaxInt32
	H1, H2, H3, H4 *uint32

	// ReplacePeers specifies if the Peers in this configuration should replace
	// the existing peer list, instead of appending them to the existing list.
	ReplacePeers bool

	// Peers specifies a list of peer configurations to apply to a device.
	Peers []PeerConfig
}

A Config is a WireGuard device configuration.

Because the zero value of some Go types may be significant to WireGuard for Config fields, pointer types are used for some of these fields. Only pointer fields which are not nil will be applied when configuring a device.

type Device

type Device struct {
	// Name is the name of the device.
	Name string

	// PrivateKey is the device's private key.
	PrivateKey Key

	// PublicKey is the device's public key, computed from its PrivateKey.
	PublicKey Key

	// ListenPort is the device's network listening port.
	ListenPort uint16

	// FirewallMark is the device's current firewall mark.
	//
	// The firewall mark can be used in conjunction with firewall software to
	// take action on outgoing WireGuard packets.
	FirewallMark uint32

	// JunkCount is a number of junk packets.
	JunkCount uint16

	// JunkMin and JunkMax specify the minimum and maximum size of junk packets.
	JunkMin, JunkMax uint16

	// S1 and S2 is a init and response junk packet sizes.
	S1, S2 uint16

	// H* is a packet magic header.
	// H1 - init | H2 - response | H3 - underload | H4 - transport.
	H1, H2, H3, H4 uint32

	// Peers is the list of network peers associated with this device.
	Peers []Peer
}

Device is a WireGuard device.

func (*Device) Decode

func (d *Device) Decode(ad *netlink.AttributeDecoder)

Decode decodes a Device from a single generic netlink message under the attribute decoder.

type Encoder

type Encoder struct{ *netlink.AttributeEncoder }

Encoder is a netlink attribute encoder but encodes only not nil values.

func NewEncoder

func NewEncoder() Encoder

NewEncoder returns a new Encoder.

func (Encoder) Duration

func (e Encoder) Duration(typ uint16, d *time.Duration)

func (Encoder) Flag

func (e Encoder) Flag(typ uint16, cond bool, flag uint32)

func (Encoder) Key

func (e Encoder) Key(typ uint16, value *Key)

func (Encoder) Uint16

func (e Encoder) Uint16(typ uint16, value *uint16)

func (Encoder) Uint32

func (e Encoder) Uint32(typ uint16, value *uint32)

type Key

type Key [KeyLen]byte

A Key is a public, private, or pre-shared secret key. The Key constructor functions in this package can be used to create Keys suitable for each of these applications.

func GenerateKey

func GenerateKey() (k Key)

GenerateKey generates a Key suitable for use as a pre-shared secret key from a cryptographically safe source.

The output Key should not be used as a private key; use GeneratePrivateKey instead.

func GeneratePrivateKey

func GeneratePrivateKey() Key

GeneratePrivateKey generates a Key suitable for use as a private key from a cryptographically safe source.

func NewKey

func NewKey(b []byte) (Key, error)

NewKey creates a Key from an existing byte slice. The byte slice must be exactly 32 bytes in length.

func ParseKey

func ParseKey(s string) (Key, error)

ParseKey parses a Key from a base64-encoded string, as produced by the Key.String method.

func (*Key) Decode

func (key *Key) Decode(b []byte) error

Decode decodes a Key from a byte slice.

func (Key) PublicKey

func (k Key) PublicKey() Key

PublicKey computes a public key from the private key k.

PublicKey should only be called when k is a private key.

func (Key) String

func (k Key) String() string

String returns the base64-encoded string representation of a Key.

ParseKey can be used to produce a new Key from this string.

type Peer

type Peer struct {
	// PublicKey is the public key of a peer, computed from its private key.
	//
	// PublicKey is always present in a Peer.
	PublicKey Key

	// PresharedKey is an optional preshared key which may be used as an
	// additional layer of security for peer communications.
	//
	// A zero-value Key means no preshared key is configured.
	PresharedKey Key

	// Endpoint is the most recent source address used for communication by
	// this Peer.
	Endpoint *net.UDPAddr

	// PersistentKeepaliveInterval specifies how often an "empty" packet is sent
	// to a peer to keep a connection alive.
	//
	// A value of 0 indicates that persistent keepalives are disabled.
	PersistentKeepaliveInterval time.Duration

	// LastHandshakeTime indicates the most recent time a handshake was performed
	// with this peer.
	//
	// A zero-value time.Time indicates that no handshake has taken place with
	// this peer.
	LastHandshakeTime time.Time

	// ReceiveBytes indicates the number of bytes received from this peer.
	ReceiveBytes int64

	// TransmitBytes indicates the number of bytes transmitted to this peer.
	TransmitBytes int64

	// AllowedIPs specifies which IPv4 and IPv6 addresses this peer is allowed
	// to communicate on.
	//
	// 0.0.0.0/0 indicates that all IPv4 addresses are allowed, and ::/0
	// indicates that all IPv6 addresses are allowed.
	AllowedIPs []net.IPNet

	// ProtocolVersion specifies which version of the WireGuard protocol is used
	// for this Peer.
	//
	// A value of 0 indicates that the most recent protocol version will be used.
	ProtocolVersion int
}

A Peer is a WireGuard peer to a Device.

func (*Peer) Decode

func (p *Peer) Decode(ad *netlink.AttributeDecoder)

Decode decodes a Peer from a netlink attribute payload.

type PeerConfig

type PeerConfig struct {
	// PublicKey specifies the public key of this peer.  PublicKey is a
	// mandatory field for all PeerConfigs.
	PublicKey Key

	// Remove specifies if the peer with this public key should be removed
	// from a device's peer list.
	Remove bool

	// UpdateOnly specifies that an operation will only occur on this peer
	// if the peer already exists as part of the interface.
	UpdateOnly bool

	// PresharedKey specifies a peer's preshared key configuration, if not nil.
	//
	// A non-nil, zero-value Key will clear the preshared key.
	PresharedKey *Key

	// Endpoint specifies the endpoint of this peer entry, if not nil.
	Endpoint *net.UDPAddr

	// PersistentKeepaliveInterval specifies the persistent keepalive interval
	// for this peer, if not nil.
	//
	// A non-nil value of 0 will clear the persistent keepalive interval.
	PersistentKeepaliveInterval *time.Duration

	// ReplaceAllowedIPs specifies if the allowed IPs specified in this peer
	// configuration should replace any existing ones, instead of appending them
	// to the allowed IPs list.
	ReplaceAllowedIPs bool

	// AllowedIPs specifies a list of allowed IP addresses in CIDR notation
	// for this peer.
	AllowedIPs []net.IPNet
}

A PeerConfig is a WireGuard device peer configuration.

Because the zero value of some Go types may be significant to WireGuard for PeerConfig fields, pointer types are used for some of these fields. Only pointer fields which are not nil will be applied when configuring a peer.

func (PeerConfig) Encode

func (p PeerConfig) Encode(nae *netlink.AttributeEncoder) error

Encode encodes the PeerConfig into a netlink format.

Directories

Path Synopsis
cmd
awgctrl command
Command awgctrl is a testing utility for interacting with amneziawg via package awgctrl.
Command awgctrl is a testing utility for interacting with amneziawg via package awgctrl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL