Documentation
¶
Index ¶
- Constants
- Variables
- func EncodePrivateKey(key NoisePrivateKey) string
- func EncodePublicKey(key NoisePublicKey) string
- func GenerateConfig(device *Device, interfaceAddrs []string) string
- type Config
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Read(b []byte) (n int, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(b []byte) (n int, err error)
- type Device
- func (device *Device) AddPeer(publicKey NoisePublicKey, presharedKey NoiseSymmetricKey, endpoint string, ...) (*Peer, error)
- func (device *Device) Bind(addr string) error
- func (device *Device) Close() error
- func (device *Device) Conn() *net.UDPConn
- func (device *Device) GetPeer(publicKey NoisePublicKey) (*Peer, error)
- func (device *Device) IsClosed() bool
- func (device *Device) LookupPeer(ip net.IP) *Peer
- func (device *Device) Peers() []*Peer
- func (device *Device) PublicKey() NoisePublicKey
- func (device *Device) Read(peer *Peer, data []byte) (int, error)
- func (device *Device) ReceivePacket(peer *Peer, timeout time.Duration) ([]byte, error)
- func (device *Device) RemovePeer(publicKey NoisePublicKey) error
- func (device *Device) SendPacket(data []byte, dstIP net.IP) error
- func (device *Device) SetPacketHandler(handler func([]byte))
- func (device *Device) Start() error
- func (device *Device) Stats() (txBytes, rxBytes, txPackets, rxPackets uint64)
- func (device *Device) Write(peer *Peer, data []byte) (int, error)
- type Dialer
- func (d *Dialer) Close() error
- func (d *Dialer) Dial(network, address string) (net.Conn, error)
- func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error)
- func (d *Dialer) SetDevice(device *Device)
- func (d *Dialer) SetKeepalive(keepalive time.Duration)
- func (d *Dialer) SetPeer(peer *Peer)
- func (d *Dialer) SetTimeout(timeout time.Duration)
- type Keypair
- type NoisePrivateKey
- type NoisePublicKey
- type NoiseSymmetricKey
- type Peer
- func (peer *Peer) AddAllowedIP(ipnet net.IPNet)
- func (peer *Peer) ContainsIP(ip net.IP) bool
- func (peer *Peer) CreateMessageResponse(receiverIndex uint32) (*messageResponse, error)
- func (peer *Peer) DecryptPacket(data []byte) ([]byte, error)
- func (peer *Peer) EncryptPacket(plaintext []byte) ([]byte, error)
- func (peer *Peer) GetEndpoint() *net.UDPAddr
- func (peer *Peer) GetKeypair() *Keypair
- func (peer *Peer) PublicKey() NoisePublicKey
- func (peer *Peer) SetEndpoint(addr *net.UDPAddr)
- type PeerConfig
Constants ¶
const ( DefaultMTU = 1420 MinMTU = 576 // Minimum IPv4 MTU MaxMTU = 65535 IPv4HeaderSize = 20 IPv6HeaderSize = 40 InvalidIndex = 0 // Invalid peer index value )
const ( MessageInitiationType = 1 MessageResponseType = 2 MessageCookieReplyType = 3 MessageTransportDataType = 4 MessageInitiationSize = 148 MessageResponseSize = 92 MessageCookieReplySize = 64 MessageTransportHeaderSize = 16 MessageKeepaliveSize = MessageTransportHeaderSize MessageTransportMaxSize = 65536 )
Variables ¶
var ( ErrInvalidPublicKey = errors.New("invalid public key") ErrInvalidPrivateKey = errors.New("invalid private key") ErrDecryptionFailed = errors.New("decryption failed") )
var ( ErrDeviceClosed = errors.New("device closed") ErrNoRoute = errors.New("no route to host") )
var ( ErrDialerClosed = errors.New("dialer closed") ErrNoPeerSpecified = errors.New("no peer specified") ErrNoDeviceSpecified = errors.New("no device specified") )
var ( ErrInvalidMessageSize = errors.New("invalid message size") ErrInvalidMessageType = errors.New("invalid message type") ErrInvalidMessageFormat = errors.New("invalid message format") )
var ( ErrPeerNotFound = errors.New("peer not found") ErrHandshakeTimeout = errors.New("handshake timeout") ErrInvalidHandshake = errors.New("invalid handshake") ErrCounterExhausted = errors.New("counter exhausted") )
var (
ErrInvalidConfig = errors.New("invalid configuration")
)
Functions ¶
func EncodePrivateKey ¶
func EncodePrivateKey(key NoisePrivateKey) string
EncodePrivateKey encodes a private key to base64
func EncodePublicKey ¶
func EncodePublicKey(key NoisePublicKey) string
EncodePublicKey encodes a public key to base64
func GenerateConfig ¶
GenerateConfig generates a configuration string
Types ¶
type Config ¶
type Config struct {
PrivateKey string
ListenPort int
Address []string
MTU int
DNS []string
Peers []PeerConfig
}
Config represents a WireGuard configuration
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represents a WireGuard connection
func (*Conn) SetDeadline ¶
SetDeadline implements net.Conn
func (*Conn) SetReadDeadline ¶
SetReadDeadline implements net.Conn
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements net.Conn
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device represents a WireGuard device
func CreateDeviceFromConfig ¶
CreateDeviceFromConfig creates a device from a configuration
func NewDevice ¶
func NewDevice(privateKey NoisePrivateKey) (*Device, error)
NewDevice creates a new WireGuard device
func (*Device) AddPeer ¶
func (device *Device) AddPeer(publicKey NoisePublicKey, presharedKey NoiseSymmetricKey, endpoint string, allowedIPs []string) (*Peer, error)
AddPeer adds a peer to the device
func (*Device) GetPeer ¶
func (device *Device) GetPeer(publicKey NoisePublicKey) (*Peer, error)
GetPeer returns a peer by public key
func (*Device) LookupPeer ¶
LookupPeer finds a peer by destination IP
func (*Device) PublicKey ¶
func (device *Device) PublicKey() NoisePublicKey
PublicKey returns the device's public key
func (*Device) ReceivePacket ¶
ReceivePacket receives a packet from a peer
func (*Device) RemovePeer ¶
func (device *Device) RemovePeer(publicKey NoisePublicKey) error
RemovePeer removes a peer
func (*Device) SendPacket ¶
SendPacket sends a packet through the device
func (*Device) SetPacketHandler ¶
SetPacketHandler sets the handler function for received packets
type Dialer ¶
type Dialer struct {
// contains filtered or unexported fields
}
Dialer implements a net.Dialer-like interface for WireGuard connections
func (*Dialer) DialContext ¶
DialContext establishes a connection with context
func (*Dialer) SetKeepalive ¶
SetKeepalive sets the keepalive interval
func (*Dialer) SetTimeout ¶
SetTimeout sets the connection timeout
type Keypair ¶
type Keypair struct {
// contains filtered or unexported fields
}
Keypair represents a symmetric Keypair for transport
func (*Keypair) NextSendNonce ¶
func (*Keypair) ValidateReceiveNonce ¶
type NoisePrivateKey ¶
type NoisePrivateKey [noisePrivateKeySize]byte
NoisePrivateKey represents a Curve25519 private key
func DecodePrivateKey ¶
func DecodePrivateKey(s string) (NoisePrivateKey, error)
DecodePrivateKey decodes a base64 private key
func GeneratePrivateKey ¶
func GeneratePrivateKey() (NoisePrivateKey, error)
GeneratePrivateKey generates a new Curve25519 private key
func (*NoisePrivateKey) PublicKey ¶
func (key *NoisePrivateKey) PublicKey() NoisePublicKey
PublicKey derives the public key from a private key
func (*NoisePrivateKey) SharedSecret ¶
func (key *NoisePrivateKey) SharedSecret(pub NoisePublicKey) (NoiseSymmetricKey, error)
SharedSecret computes the DH shared secret
type NoisePublicKey ¶
type NoisePublicKey [noisePublicKeySize]byte
NoisePublicKey represents a Curve25519 public key
func DecodePublicKey ¶
func DecodePublicKey(s string) (NoisePublicKey, error)
DecodePublicKey decodes a base64 public key
func (*NoisePublicKey) IsZero ¶
func (key *NoisePublicKey) IsZero() bool
IsZero checks if the key is all zeros
type NoiseSymmetricKey ¶
type NoiseSymmetricKey [noiseSymmetricKeySize]byte
NoiseSymmetricKey represents a symmetric encryption key
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a WireGuard peer
func NewPeer ¶
func NewPeer(device *Device, publicKey NoisePublicKey, presharedKey NoiseSymmetricKey, endpoint *net.UDPAddr) *Peer
func (*Peer) AddAllowedIP ¶
AddAllowedIP adds an allowed IP range
func (*Peer) ContainsIP ¶
ContainsIP checks if an IP is in the allowed range
func (*Peer) CreateMessageResponse ¶
CreateMessageResponse creates a handshake response message (public interface)
func (*Peer) DecryptPacket ¶
DecryptPacket decrypts a packet
func (*Peer) EncryptPacket ¶
EncryptPacket encrypts a packet with the current keypair
func (*Peer) GetEndpoint ¶
GetEndpoint returns the peer's endpoint
func (*Peer) GetKeypair ¶
GetKeypair returns the current keypair
func (*Peer) PublicKey ¶
func (peer *Peer) PublicKey() NoisePublicKey
PublicKey returns the peer's public key
func (*Peer) SetEndpoint ¶
SetEndpoint updates the peer's endpoint