dns

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dns manages a host's DNS resolver configuration through an injected exec.Runner.

Build a Manager for an explicit backend and call its methods:

r, _ := exec.NewRunner(exec.Direct) // the agent runs as root
m, err := dns.New(dns.Resolved, r)
if err != nil { ... }
err = m.Apply(ctx, dns.Config{Nameservers: []string{"1.1.1.1"}, SearchDomains: []string{"corp.example"}})

Two backends are implemented:

  • Resolved (systemd-resolved via resolvectl) — supports a host-global config (an empty Config.Interface writes the managed /etc/systemd/resolved.conf.d drop-in and restarts the service) and per-link config (a set Interface uses runtime `resolvectl dns/domain <iface>`).
  • NetworkManager (nmcli) — connection-scoped: it configures DNS on the active connection of Config.Interface, so Interface is REQUIRED. There is no clean host-global DNS via nmcli; use the Resolved backend for that.

Mutations escalate through the Runner; reads are unprivileged. Detect lists the backends whose tool is present on PATH; the consumer picks one and passes it to New (no auto-detection, no global state).

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidConfig = errors.New("dns: invalid config")

ErrInvalidConfig is returned when a Config field is unsafe or malformed for a resolver backend. Apply validates before touching any backend, so a bad config is rejected without side effects.

View Source
var ErrUnknownBackend = errors.New("dns: unknown backend")

ErrUnknownBackend is returned by New for the zero value or any Backend the SDK does not implement.

Functions

This section is empty.

Types

type Backend

type Backend int

Backend selects the resolver framework a Manager drives. The zero value is invalid (New → ErrUnknownBackend); valid values start at 1. Only implemented backends exist — a new value is appended if and when a real implementation is written (resolvconf/dnsmasq are intentionally absent, not stubbed).

const (
	// Resolved is systemd-resolved (resolvectl + resolved.conf.d drop-in).
	Resolved Backend = iota + 1
	// NetworkManager configures DNS on a connection via nmcli.
	NetworkManager
)

func Detect

func Detect(ctx context.Context) []Backend

Detect reports the DNS backends usable on THIS host: Resolved when resolvectl is on PATH, NetworkManager when nmcli is. It lists; it never picks and never constructs a Manager — the consumer reads the list, picks a backend explicitly, and passes it to New. An empty slice means no usable DNS manager.

The ctx is accepted for signature uniformity with the other capability Detect functions; the probe itself is a pure PATH lookup.

func (Backend) String

func (b Backend) String() string

String renders the backend as its canonical tool/framework name.

type Config

type Config struct {
	// Nameservers are resolver IP addresses (v4 or v6 literals).
	Nameservers []string
	// SearchDomains are DNS search domains.
	SearchDomains []string
	// Interface scopes the configuration to one link. Empty means host-global
	// (supported only by the Resolved backend; NetworkManager requires it).
	Interface string
}

Config is the desired resolver configuration to Apply.

type Manager

type Manager interface {
	// Get reads the active resolver configuration.
	Get(ctx context.Context) (State, error)
	// Apply installs cfg. It validates cfg (rejecting non-IP nameservers,
	// malformed/flag-shaped search domains, and bad interface names) before
	// touching any backend, so an invalid config has no side effects.
	Apply(ctx context.Context, cfg Config) error
}

Manager is the DNS configuration surface. Every method takes a context.

func New

func New(b Backend, runner exec.Runner) (Manager, error)

New returns a Manager for the named backend, driven by runner. Pure: it validates the backend is known and does NOT probe the host (use Detect). The zero value and any unimplemented backend are rejected with ErrUnknownBackend; a nil runner is rejected.

Example

ExampleNew shows the construct-a-handle flow: pick a backend (here systemd-resolved), build a Manager over a Runner, and apply a host-global resolver config.

package main

import (
	"context"
	"log"

	"github.com/manchtools/power-manage-sdk/sys/dns"
	"github.com/manchtools/power-manage-sdk/sys/exec"
)

func main() {
	r, err := exec.NewRunner(exec.Direct) // the agent runs as root
	if err != nil {
		log.Fatal(err)
	}
	m, err := dns.New(dns.Resolved, r)
	if err != nil {
		log.Fatal(err)
	}
	if err := m.Apply(context.Background(), dns.Config{
		Nameservers:   []string{"1.1.1.1", "9.9.9.9"},
		SearchDomains: []string{"corp.example"},
	}); err != nil {
		log.Fatal(err)
	}
}

type State

type State struct {
	Nameservers   []string
	SearchDomains []string
}

State is the resolver configuration read back from the host.

Jump to

Keyboard shortcuts

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