dohclient

package
v0.0.0-...-bf4ebcc Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCertPinMismatch = errors.New("dohclient: DoH endpoint cert SPKI not in pinned set")
	ErrUnreachable     = errors.New("dohclient: DoH endpoint unreachable")
	ErrEmptyResponse   = errors.New("dohclient: DoH returned no answers")
	ErrBadResponse     = errors.New("dohclient: DoH response malformed")
)

Sentinel errors so callers can branch via errors.Is.

Functions

func ResolveMultiaddr

func ResolveMultiaddr(ctx context.Context, c *Client, m multiaddr.Multiaddr) ([]multiaddr.Multiaddr, error)

ResolveMultiaddr replaces /dns4 or /dns6 components in m with the resolved IP components via c. Returns the new multiaddr(s) — one per resolved IP. Multiaddrs without a /dns* component are returned unchanged (single-element slice). Errors propagate from c.Resolve.

Only the leading /dnsX/<hostname> component is rewritten — the rest of the multiaddr (transport, port, /p2p/<peer-id>, etc.) is preserved verbatim. If m starts with /dns4 but contains a /ip4 elsewhere (rare but possible), only the /dns4 is rewritten.

func SPKISHA256

func SPKISHA256(cert *x509.Certificate) string

SPKISHA256 computes the base64-encoded SHA-256 of a parsed cert's SPKI. Exported for tests + operators computing the pin from a known cert.

Types

type Client

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

Client is the DoH resolver. Safe for concurrent use.

func New

func New(cfg Config) (*Client, error)

New constructs a Client. Returns an error on empty Endpoint.

When cfg.HTTPClient is nil, New builds a default *http.Client whose Transport's TLSClientConfig has a VerifyPeerCertificate callback that rejects connections whose leaf cert SPKI doesn't match any PinnedSPKISHA256 entry. When cfg.PinnedSPKISHA256 is empty, the callback returns nil (no pin check) — only safe for tests.

func (*Client) Resolve

func (c *Client) Resolve(ctx context.Context, hostname string) ([]net.IP, error)

Resolve returns the IPv4 + IPv6 addresses for hostname via DoH. Returns ErrCertPinMismatch (wrapped via the HTTP transport's VerifyPeerCertificate) if the DoH endpoint's cert SPKI doesn't match the pinned set. Returns ErrUnreachable on transport failure. ErrEmptyResponse when the DoH server returns NOERROR + 0 answers.

Both A and AAAA queries are issued; results are combined. If either query fails with a transport error, that error is returned (no partial results). If one query returns NXDOMAIN but the other has answers, the answers are returned. If both queries return NOERROR/0, ErrEmptyResponse.

type Config

type Config struct {
	// Endpoint is the DoH URL, e.g. "https://1.1.1.1/dns-query" or an
	// in-house URL like "https://doh.tx-rails.internal/dns-query".
	Endpoint string

	// PinnedSPKISHA256 is the set of acceptable SPKI fingerprints
	// (base64-stdencoded SHA-256). At least one must match the leaf
	// cert's SPKI for the request to proceed. Empty = NO pinning —
	// only useful for tests against in-process servers.
	PinnedSPKISHA256 []string

	// Timeout caps each DoH request. Default 5s.
	Timeout time.Duration

	// HTTPClient is optional. nil = construct a default with the
	// cert-pinning verifier installed via TLSClientConfig. Tests can
	// supply a pre-built client (e.g. httptest.Server.Client()) with
	// an already-trusting transport.
	HTTPClient *http.Client
}

Config holds the runtime knobs for a DoH Client.

Jump to

Keyboard shortcuts

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