oracle-watchdog

wgresolver

import "github.com/afreidah/oracle-watchdog/internal/wgresolver"

Index

Variables

ErrNoIPv4 is returned when DNS resolution returns no IPv4 addresses for the configured endpoint hostname.

var ErrNoIPv4 = errors.New("no IPv4 address resolved for endpoint")

ErrPeerNotFound is returned when the configured peer pubkey is absent from the WireGuard device’s peer list.

var ErrPeerNotFound = errors.New("peer not found on interface")

type EndpointResolver

EndpointResolver maintains a fresh peer endpoint by polling DNS and updating the kernel WireGuard configuration when the resolved IP drifts.

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

func New

func New(cfg config.WireguardConfig, opts ...Option) (*EndpointResolver, error)

New constructs a resolver from the given config. The default wgctrl client is opened immediately so configuration errors surface at construction time; the default DNS resolver is the Go stdlib resolver.

func (*EndpointResolver) Close

func (r *EndpointResolver) Close() error

Close releases resources held by the default wgctrl client. Safe to call when a custom client was injected via WithWGClient (no-op in that case).

func (*EndpointResolver) Run

func (r *EndpointResolver) Run(ctx context.Context)

Run drives the resolve loop until ctx is cancelled. Performs an initial tick on entry so failover-driven endpoint changes are picked up promptly after the resolver starts.

type Option

Option configures the EndpointResolver. Used to inject test doubles for the wgctrl client and DNS resolver.

type Option func(*EndpointResolver)

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger overrides the default scoped logger.

func WithResolver

func WithResolver(d Resolver) Option

WithResolver overrides the default DNS resolver.

func WithWGClient

func WithWGClient(c WGClient) Option

WithWGClient overrides the default wgctrl client. The supplied client’s Close method is not called by the resolver.

type Resolver

Resolver narrows the DNS surface so tests can substitute fakes without hitting real DNS infrastructure.

type Resolver interface {
    // LookupIP behaves like net.Resolver.LookupIP. The endpoint resolver
    // always passes "ip4" for the network argument.
    LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
}

type WGClient

WGClient narrows the wgctrl surface so tests can substitute fakes without requiring a real WireGuard interface or root privileges.

type WGClient interface {
    // Device returns the WireGuard device state for the named interface.
    Device(name string) (*wgtypes.Device, error)

    // ConfigureDevice applies the supplied configuration to the named
    // interface. The endpoint resolver only sets per-peer Endpoint fields
    // with UpdateOnly=true.
    ConfigureDevice(name string, cfg wgtypes.Config) error

    // Close releases the underlying netlink socket.
    Close() error
}

Generated by gomarkdoc