tunnel

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package tunnel defines the TunnelBackend abstraction: the seam between domain services and the AmneziaWG implementation (ADR-0001 — AWG is driven through its pinned CLI as a subprocess; nothing AWG-specific leaks above this interface). internal/tunnel/amneziawg implements it against the real CLI in Phase 2; tunnel/fake is the in-memory implementation for tests and dev without root.

Index

Constants

This section is empty.

Variables

View Source
var ErrInterfaceNotFound = errors.New("tunnel: interface not found")

ErrInterfaceNotFound is returned when the named interface does not exist in the backend (distinct from DB-not-found domain errors).

Functions

func GeneratePresharedKey

func GeneratePresharedKey() (string, error)

GeneratePresharedKey returns 32 random bytes, base64 — exactly what `awg genpsk` produces.

func PublicKeyFromPrivate

func PublicKeyFromPrivate(b64 string) (string, error)

PublicKeyFromPrivate derives the public key for a base64 private key.

func ValidateEncodedKey added in v0.1.9

func ValidateEncodedKey(value string) error

ValidateEncodedKey accepts the canonical 32-byte base64 form used for keys/PSKs.

func ValidateKeyPair added in v0.1.9

func ValidateKeyPair(private, public string) error

ValidateKeyPair checks canonical storage and that the public identity belongs to this private key. Errors never contain either key.

func ValidatePrivateKey

func ValidatePrivateKey(b64 string) error

ValidatePrivateKey checks base64 form and 32-byte length.

func ValidatePublicKey

func ValidatePublicKey(b64 string) error

ValidatePublicKey checks base64 form and 32-byte length.

Types

type Backend

type Backend interface {
	// ListInterfaces returns the names of interfaces visible to the backend.
	ListInterfaces(ctx context.Context) ([]string, error)
	// CreateInterface brings up a new interface with the given spec (link
	// + config, no peers).
	CreateInterface(ctx context.Context, spec InterfaceSpec) error
	// RemoveInterface tears down and removes the interface.
	RemoveInterface(ctx context.Context, name string) error
	// ApplyInterfaceConfig fully replaces the interface configuration
	// (setconf semantics; active handshakes may reset).
	ApplyInterfaceConfig(ctx context.Context, name string, cfg InterfaceConfig) error
	// SyncPeers diff-applies the peer list without disturbing the interface
	// (syncconf semantics; active sessions survive).
	SyncPeers(ctx context.Context, name string, peers []PeerConfig) error
	// Dump returns the observed state (stats + handshakes + config snapshot).
	Dump(ctx context.Context, name string) (InterfaceState, error)
}

Backend is the control-plane surface WG-Guard needs from the tunnel engine. Implementations must be safe for concurrent use.

type InterfaceConfig

type InterfaceConfig struct {
	PrivateKey  string
	ListenPort  int
	Fwmark      string
	Obfuscation Obfuscation
	Peers       []PeerConfig
}

InterfaceConfig is a full interface configuration: setconf semantics (complete replace).

type InterfaceSpec

type InterfaceSpec struct {
	Name        string
	BackendMode string // kernel (default) or userspace
	PrivateKey  string // base64
	ListenPort  int
	Fwmark      string // "" = off
	MTU         int
	Address     string   // interface gateway CIDR, e.g. "10.8.0.1/24"
	Addresses   []string // all managed gateway CIDRs; Address is the primary
	Obfuscation Obfuscation
}

InterfaceSpec is the identity-level configuration of a backend interface. Address was added in Phase 2: a backend that creates the link (kernel path) must assign the interface gateway address at bring-up, and the spec is the only state the backend receives — the phase-1 draft omitted it.

type InterfaceState

type InterfaceState struct {
	Name        string
	BackendMode string // observed mode when the real backend can identify it
	PublicKey   string
	ListenPort  int
	Obfuscation Obfuscation
	Peers       []PeerState
}

InterfaceState is the observed state of one interface (dump semantics).

type KeyPair

type KeyPair struct {
	Private string // base64, 32 bytes
	Public  string // base64, 32 bytes
}

KeyPair is a Curve25519 keypair in WireGuard's base64 encoding. Generation uses the standard library (crypto/ecdh): WireGuard/AWG keys *are* X25519 keys — this is standard cryptography, not a reinvented scheme. The `awg genkey` CLI remains the pinned engine's own generator for parity checks in Phase 2; services only need correct key material.

func GenerateKeyPair

func GenerateKeyPair() (KeyPair, error)

GenerateKeyPair creates a fresh Curve25519 keypair.

type Obfuscation

type Obfuscation struct {
	Enabled            bool
	Jc                 int
	Jmin, Jmax         int
	S1, S2             int
	H1, H2, H3, H4     awgparam.U32Range
	I1, I2, I3, I4, I5 string // hex blobs, "" = unset (client-side only params)

	S3, S4                 int               // plain u16 when set
	HeaderProtectionKey    string            // base64 32-byte key, "" = disabled
	ContentPaddingAddition awgparam.U16Range // zero = disabled
	RekeyAfterTime         awgparam.U16Range // zero = upstream default
	RekeyTimeout           awgparam.U16Range
	RejectAfterTime        awgparam.U16Range
	KeepaliveTimeout       awgparam.U16Range
	MaxHandshakeAttempts   awgparam.U16Range
	RandomTrailers         bool // rendered as "on" (upstream panic history — default off)
	DisableCookies         bool // rendered as "on" (security implications — default off)
}

Obfuscation carries the AWG obfuscation parameters for one interface (one profile per interface — ADR-0002). Enabled=false means a plain WireGuard configuration: every parameter is omitted upstream. Field semantics and constraints are pinned in docs/integrations/amneziawg.md; validation of the kernel-README constraint set happens in the interface service before anything reaches a backend.

The 2.0/3.x-generation fields below are explicit advanced options: the pinned tools, kernel, and userspace runtimes apply and dump them, while client compatibility still varies by option. They render only when set, and a runtime that silently ignores them surfaces through verify-after-apply.

type PeerConfig

type PeerConfig struct {
	PublicKey           string // base64
	PresharedKey        string // base64, "" = none
	AllowedIPs          []string
	Endpoint            string            // host:port, "" = none (server side: usually unset)
	PersistentKeepalive awgparam.U16Range // zero = off
}

PeerConfig is the desired state of one peer.

type PeerState

type PeerState struct {
	PublicKey           string
	PresharedKeySet     bool
	Endpoint            string
	AllowedIPs          []string
	LastHandshake       time.Time // zero = never
	RXBytes             uint64
	TXBytes             uint64
	PersistentKeepalive awgparam.U16Range
}

PeerState is one peer as observed from the backend.

Directories

Path Synopsis
Package amneziawg implements tunnel.Backend against the pinned awg CLI (docs/integrations/amneziawg.md — nothing here may assume upstream behavior that document does not verify).
Package amneziawg implements tunnel.Backend against the pinned awg CLI (docs/integrations/amneziawg.md — nothing here may assume upstream behavior that document does not verify).
Package fake is an in-memory TunnelBackend for tests and development without root or the pinned AWG tooling.
Package fake is an in-memory TunnelBackend for tests and development without root or the pinned AWG tooling.

Jump to

Keyboard shortcuts

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