DEV Community

IPYE
IPYE

Posted on

A Local Caching DNS Resolver for Scraping Hosts with Unbound

Disclosure up front: I run IPYE, a company that leases IP space. This is a practical how-to, and IPYE comes up where it helps, with a short call to action at the end.

Scrapers and proxy hosts make huge numbers of DNS lookups, often for the same few thousand hostnames. Sending them all to a public resolver adds latency to every request, can run into the resolver's rate limits, and spreads information about your crawl across third parties. A local Unbound resolver on each host, or one per site, caches answers and validates DNSSEC. It also keeps proxied DNS traffic under your control. This guide sets one up for scraping hosts on a range from IPYE. The examples use the documentation prefix 203.0.113.0/24, with the resolver listening on 127.0.0.1 and ::1.

Install and configure

apt install unbound
Enter fullscreen mode Exit fullscreen mode

/etc/unbound/unbound.conf.d/local.conf:

server:
    interface: 127.0.0.1
    interface: ::1
    access-control: 127.0.0.0/8 allow
    access-control: ::1/128 allow
    outgoing-interface: 203.0.113.5
    num-threads: 4
    msg-cache-size: 128m
    rrset-cache-size: 256m
    prefetch: yes
    serve-expired: yes
    serve-expired-ttl: 3600
    cache-min-ttl: 60
    hide-identity: yes
    hide-version: yes
    qname-minimisation: yes
Enter fullscreen mode Exit fullscreen mode

What the important settings do:

  • interface on loopback only means the resolver isn't reachable from outside. An open resolver gets abused for amplification attacks, and gets your range listed.
  • outgoing-interface sets the address Unbound sends queries from. Use a dedicated infrastructure address, not one of your scraping exits, so that DNS traffic doesn't mix with crawler reputation.
  • prefetch refreshes popular records before they expire, so busy hostnames stay hot in the cache.
  • serve-expired answers from stale cache while it refreshes, which smooths over brief upstream DNS problems.
  • cache-min-ttl: 60 stops very short TTLs from causing repeat lookups. Don't set it much higher, or you'll keep following CDN changes late.

Size the caches to your memory. The rrset cache should be about twice the message cache.

Point the system and the scraper at it

# /etc/resolv.conf (or systemd-resolved DNS=127.0.0.1)
nameserver 127.0.0.1
nameserver ::1
Enter fullscreen mode Exit fullscreen mode

Squid, Dante and most HTTP libraries use the system resolver. For Squid, you can set dns_nameservers 127.0.0.1 explicitly.

Recursive or forwarding?

The configuration above does full recursion from the root servers, which needs no third party. If your hosts have restricted outbound access, forward to resolvers you trust instead:

forward-zone:
    name: "."
    forward-tls-upstream: yes
    forward-addr: 2001:db8:53::1@853#dns.example.net
Enter fullscreen mode Exit fullscreen mode

Check it's working

unbound-control status
dig @127.0.0.1 example.com +stats | grep 'Query time'
dig @127.0.0.1 example.com +stats | grep 'Query time'
unbound-control stats_noreset | grep -E 'total.num.(queries|cachehits)'
Enter fullscreen mode Exit fullscreen mode

The second dig should show a query time near 0 ms, which means a cache hit. Once a crawl is running, a cache hit rate above 80 percent is typical.

Firewall

Allow outbound DNS only from the resolver's outgoing address, and block it from your scraping exits:

ip saddr 203.0.113.5 udp dport 53 accept
ip saddr 203.0.113.5 tcp dport 53 accept
ip saddr 203.0.113.0/24 udp dport 53 drop
Enter fullscreen mode Exit fullscreen mode

Logging

For troubleshooting, log-queries: yes logs every query. It's very verbose, so enable it only briefly. For ongoing visibility, export Unbound's statistics to your monitoring with unbound_exporter, or a script around unbound-control stats.

Where the address comes from

Give infrastructure services such as the resolver their own address in your range, separate from the exits. A leased /24 from IPYE includes reverse DNS, so the resolver's address can have a clear PTR record, plus a LOA and ROA.

Next step

Install Unbound on each scraping host, bind it to loopback, and set up your range and its infrastructure addresses in the IPYE portal.

About IPYE

IPYE leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS, and ASN and region changes apply automatically. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. More at ipye.com. Questions: ipye@turnel.com.

Disclosure: I run IPYE

Top comments (0)