mesh

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package mesh owns the WireGuard overlay (ADR-0003). The Manager interface is consumed by the daemon wiring and the agent; implementations:

  • device manager with wireguard-go / kernel WG (tasks T-18..T-21, T-57/58)
  • Disabled (single-node mode, returned by NewDisabled)
  • testutil fake (simcluster)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscoKey

func DiscoKey(pub wgtypes.Key, caHash []byte) []byte

DiscoKey derives the shared disco HMAC key for a node.

func EnsureNodeKey

func EnsureNodeKey(path string) (string, error)

EnsureNodeKey loads or creates the WG private key at path and returns its public key (base64). A joining node calls this to advertise its public key in the join request before the device is brought up; Up reuses the same path so the advertised key matches the running device.

func RunPeerSync

func RunPeerSync(ctx context.Context, cfg PeerSyncConfig) error

RunPeerSync keeps a WatchPeers stream open and applies every pushed PeerSet to the mesh device, reconnecting with backoff. Blocks until ctx is canceled.

Types

type DeviceManager

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

DeviceManager implements mesh.Manager over a real WireGuard device.

func NewDeviceManager

func NewDeviceManager(log *slog.Logger) *DeviceManager

NewDeviceManager builds a mesh manager backed by a real WG device. The kernel-vs-userspace choice is made at Up time (kernel preferred on Linux).

func (*DeviceManager) ApplyPeers

func (dm *DeviceManager) ApplyPeers(_ context.Context, peers *clusterv1.PeerSet) error

ApplyPeers reconciles the device's peer set to exactly `peers`.

func (*DeviceManager) Down

func (dm *DeviceManager) Down(_ context.Context) error

Down tears the device down.

func (*DeviceManager) LastHandshakes

func (dm *DeviceManager) LastHandshakes() (map[string]int64, error)

LastHandshakes returns the last-handshake time (unix seconds, 0 = never) per peer, keyed by hex public key. Useful for path diagnostics and integration tests.

func (*DeviceManager) PublicKey

func (dm *DeviceManager) PublicKey() (string, error)

PublicKey returns the node's WG public key, generating the keypair on first use. Valid before Up (the key path is taken from the last Up config or, if Up has not run, this returns an error asking for a path).

func (*DeviceManager) Status

func (dm *DeviceManager) Status() Status

Status reports the current device state.

func (*DeviceManager) Up

func (dm *DeviceManager) Up(_ context.Context, cfg NodeConfig) error

Up creates and configures the WG device (idempotent). It requires root/CAP_NET_ADMIN.

type Disabled

type Disabled struct{}

Disabled is the single-node no-mesh implementation: everything is a no-op and Status reports Enabled=false. The daemon then binds all internal services to 127.0.0.1.

func NewDisabled

func NewDisabled() Disabled

func (Disabled) ApplyPeers

func (Disabled) Down

func (Disabled) Down(context.Context) error

func (Disabled) PublicKey

func (Disabled) PublicKey() (string, error)

func (Disabled) Status

func (Disabled) Status() Status

func (Disabled) Up

type DiscoPinger

type DiscoPinger struct {
	NodeID   string
	Key      []byte
	Controls []string // control disco endpoints "ip:port"
	Interval time.Duration
	Clock    clock.Clock
	Report   func(observedEndpoint string)
	Logger   *slog.Logger
	// contains filtered or unexported fields
}

DiscoPinger is the node-side prober. It pings each control's disco endpoint and reports the reflexive address the control observed.

func (*DiscoPinger) Run

func (p *DiscoPinger) Run(ctx context.Context)

Run probes every Interval until ctx is canceled.

type DiscoResponder

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

DiscoResponder is the control-side echo server. For each valid ping it returns a pong carrying the observed UDP source address.

func NewDiscoResponder

func NewDiscoResponder(addr string, keyForNode func(string) ([]byte, bool), log *slog.Logger) (*DiscoResponder, error)

NewDiscoResponder binds a UDP listener on addr ("ip:port").

func (*DiscoResponder) Addr

func (r *DiscoResponder) Addr() string

Addr is the bound listen address.

func (*DiscoResponder) Close

func (r *DiscoResponder) Close() error

Close stops the responder.

func (*DiscoResponder) Serve

func (r *DiscoResponder) Serve(ctx context.Context)

Serve reads and answers pings until ctx is canceled.

type Manager

type Manager interface {
	// Up creates/configures the WG device. Idempotent.
	Up(ctx context.Context, cfg NodeConfig) error
	// ApplyPeers reconciles the device's peer set to exactly `peers`
	// (declarative full set — implementations diff internally).
	ApplyPeers(ctx context.Context, peers *clusterv1.PeerSet) error
	// Down tears the device down.
	Down(ctx context.Context) error
	Status() Status
	// PublicKey returns the node's WG public key (generating a keypair on
	// first use), valid before Up.
	PublicKey() (string, error)
}

Manager is the per-node mesh controller.

type NodeConfig

type NodeConfig struct {
	// PrivateKeyPath stores the WG private key (0600). Generated when absent.
	PrivateKeyPath string
	MeshIP         netip.Addr
	// ListenPort for WireGuard UDP (default 51820).
	ListenPort uint16
	// InterfaceName defaults to "zt0" (utunN chosen automatically on darwin).
	InterfaceName string
}

NodeConfig is what a node needs to bring its mesh interface up.

type PeerSyncConfig

type PeerSyncConfig struct {
	NodeID  string
	Manager Manager
	Clock   clock.Clock
	Logger  *slog.Logger
	// Dial opens a connection to a control node's MeshService. Called on every
	// (re)connect; the returned closer is invoked when the stream ends.
	Dial func(ctx context.Context) (grpc.ClientConnInterface, func() error, error)
}

PeerSyncConfig configures the node-side peer synchronizer.

type Status

type Status struct {
	Enabled   bool
	Up        bool
	MeshIP    netip.Addr
	PublicKey string
	// PeerPaths maps node id → path in use: "direct" | "punched" | "relay" | "hub".
	PeerPaths map[string]string
}

Status describes the current mesh device state.

Jump to

Keyboard shortcuts

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