dns~spider-gazelle
DNS
Non-blocking extendable DNS client for crystal lang.
With built in support for UDP, TLS, HTTPS and mDNS resolvers.
Installation
-
Add the dependency to your
shard.yml:dependencies: dns: github: spider-gazelle/dns -
Run
shards install
Usage
A simple query
require "dns"
responses = DNS.query(
"www.google.com",
{
DNS::RecordType::A,
DNS::RecordType::AAAA,
}
)
ips = responses.map(&.ip_address)
How to configure custom resolvers (secure from prying eyes)
require "dns"
require "dns/resolver/https"
DNS.default_resolver = DNS::Resolver::HTTPS.new(["https://1.1.1.1/dns-query"])
# or just for some routes
DNS.resolvers[/.+\.com\.au$/i] = DNS::Resolver::HTTPS.new(["https://1.1.1.1/dns-query"])
# there is a built in resolver to use mDNS for *.local routes
# if you'd prefer to use the system resolver for A and AAAA records
DNS.default_resolver = DNS::Resolver::System.new(
# uses the fallback resolver for other DNS records
fallback: DNS::Resolver::UDP.new
)
# for the TLS resolver you need to specify a domain name for the returned TLS certificate
DNS.default_resolver = DNS::Resolver::TLS.new({
"8.8.8.8" => "dns.google",
})
# or maybe you don't want to use the system defined DNS servers
DNS.default_resolver = DNS::Resolver::UDP.new(["1.1.1.1", "8.8.8.8"])
# it's also possible to create your own resolvers implementing the abstract method in `DNS::Resolver`
By default, the library will attempt to obtain the DNS servers configured on your host. That is using:
/etc/resolv.confon linux / nix* systems- Win32 API calls on Windows
- SystemConfiguration framework on MacOS
NOTE:: currently it does not check if DNS over HTTPS is configured on either Mac or Windows (future work)
If DNS discovery is unsuccessful it falls back to using ["1.1.1.1", "8.8.8.8"].
# to customise the fallback servers
DNS::Servers.fallback = ["192.168.0.1"]
Resolver behaviour
The UDP resolver is modelled on the behaviour of unix getaddrinfo:
- queries are retransmitted with an exponential backoff and failed over across the configured nameservers, rather than failing on a single dropped packet
- a persistently unresponsive nameserver is demoted to the end of the list so later queries prefer healthy servers
resolv.confoptions are honoured:timeout:(default2s, clamped1..30),attempts:(default3, clamped1..5) andndots:(default1, clamped0..15). Set the global base timeout withDNS.timeout. An explicittimeout:inresolv.confor a resolver's server configuration takes precedence.- UDP advertises a 1232-byte EDNS0 payload. When the query has no required EDNS
options or negotiated cookie, the resolver also tries without EDNS if the
server rejects it or stays silent through half the attempt. Truncated (
TC) replies are refetched over TCP within the same deadline.
# tune the base (initial) per-try timeout; backoff doubles it each attempt
DNS.timeout = 1.second
Shared UDP source port
All UDP queries are sent from a single, long-lived source port per address
family (a process-wide DNS::Resolver::UDP::SharedSocket) rather than opening a
fresh socket per query. A background fiber demultiplexes responses back to each
waiting query by transaction id.
Reusing one source port trades some of the source-port entropy recommended by
RFC 5452 for connection reuse,
so every response is strictly validated before it is accepted: the transaction
id (a CSPRNG value) must be outstanding, the QR bit must be set, the source
address must match the queried server, and the echoed question must match the
request. An EDNS rejection without an echoed question can trigger a legacy
retry. This is appropriate for a stub resolver talking to a trusted recursive
resolver. If you operate over an untrusted path, prefer the TLS or HTTPS
resolvers.
UDP negotiates DNS Cookies per upstream and local address. A valid
BADCOOKIE response triggers a retry with the replacement cookie, followed
by TCP if the server challenges the retry. Set resolver.dns_cookies = false
to disable negotiation.
EDNS options and service bindings
UDP, TCP fallback, DNS-over-TLS, and DNS-over-HTTPS can send options configured
on the resolver's edns_options property. Low-level packet callers can pass
a DNS::Packet::EDNS to Question.build_query and read reply options through
packet.edns. Extended DNS Errors are available through
packet.extended_errors or DNS::Packet::Error#extended_errors when a
lookup raises.
DNS::Resource::SVCB and DNS::Resource::HTTPS expose typed ALPN, port, and
address hints. Pass the parameter keys your application can honor to
compatible?(supported_keys) before using a service binding.
Other things to note:
- IPv6 is supported and handled transparently
- the cache keeps complete record sets and expires them using their shortest TTL, including any CNAMEs they depend on
- NXDOMAIN is cached by name and NODATA by name and query type, with expiration derived from the authority section's SOA
- additional records can satisfy later lookups while a cached direct answer takes precedence
- network results are cached by name and resolver. Entries inserted directly into
DNS.cacheby the application are shared. - search candidates use any resolver route that matches their expanded name, including mDNS for
.local require "dns/ext/addrinfo"uses this library for socket name lookups. Hostname-based DoH endpoints are bootstrapped with the system resolver.
Set ipv4_interface to a local IPv4 address or ipv6_interface to an interface
index when a multicast query needs a specific interface.
The specs use local fixtures and run without public DNS. Run the mDNS specs
with crystal spec spec/mdns_resolver_spec.cr. On Linux this includes a multicast
query over the loopback interface.
Contributing
- Fork it (https://github.com/spider-gazelle/dns/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- Stephen von Takach - creator and maintainer