Skip to content

Add an optional DNS cache ([Resolve] CacheTTL) - #230

Open
dividedby wants to merge 7 commits into
windtf:masterfrom
dividedby:feat/dns-cache
Open

dividedby wants to merge 7 commits into
windtf:masterfrom
dividedby:feat/dns-cache

Conversation

@dividedby

Copy link
Copy Markdown

Summary

An opt-in DNS cache: [Resolve] CacheTTL = <seconds> (unset or 0 keeps today's behaviour).

Every proxied connection to a hostname currently does a fresh lookup, through the tunnel's DNS servers (or the system resolver), which adds a tunnel round trip to each new connection. With CacheTTL = 60, fresh-connection latency through the HTTP proxy dropped by about 150 ms in our testing against a remote WireGuard peer, matching a kernel-WireGuard client on the same link.

  • Successful answers are cached by lowercased hostname; errors and empty answers are not. The raw answer is cached before shuffling and ResolveStrategy, so per-request ordering still spreads load. The map is bounded (1024 names, expired entries swept).
  • HTTP, SNI and split-routing SOCKS5 dials are covered. Those paths passed hostnames straight to Tnet.Dial, which resolves inside netstack and never reached LookupAddr. They now resolve through the cache and dial each address in order, matching netstack's own loop (family filter, per-address deadline split, first error returned, cancellation reported as *net.OpError).
  • Behaviour change (fix): with no DNS set, tunnelled hostname dials on those paths failed with cannot marshal DNS message; they now resolve through the system resolver, as the SOCKS5 legacy path already did. The README notes that this resolves outside the tunnel.
  • Bug fix: a [Resolve] section without ResolveStrategy (e.g. only CacheTTL) left the strategy empty instead of auto, so the SOCKS5 legacy and TCP-tunnel resolvers found no address. It now defaults to auto, and unknown values are rejected at parse.

Test plan

  • go test -race ./..., go vet ./..., gofmt -l ., golangci-lint (fork CI green).
  • Unit tests with injected lookup and dial functions (no network): hits, expiry, case-insensitivity, copy safety, errors and empty answers not cached, bound and sweep, concurrency, family filter, fallback to the next address, deadline split, cancellation, RoutedDial going through the cache, a real-netstack test with system DNS, and ResolveStrategy parsing.

🤖 Generated with Claude Code

dividedby and others added 7 commits November 19, 2026 20:52
Every proxied connection to a hostname currently does a fresh
LookupAddr, over the tunnel or via the system resolver. Add
`CacheTTL` (seconds) to the [Resolve] section to cache successful
lookups by hostname.

- Unset or 0 keeps today's behaviour (no cache); negative or
  non-numeric values are rejected at config parse.
- The raw answer is cached before shuffling and strategy selection,
  so per-request shuffling still spreads load across records.
- Applies to both the tunnel and SystemDNS paths. Errors and empty
  answers are not cached.
- Entries expire lazily on read; the map is capped at 1024 names and
  swept for expired entries when it reaches the cap.
- The cache is a pointer on VirtualTun, so all copies share it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The HTTP proxy, SNI proxy and split-routing SOCKS5 dial tunnelled
hosts with Tnet.Dial/DialContext, whose netstack-internal lookup
bypasses VirtualTun.LookupAddr, so CacheTTL had no effect there.

Add VirtualTun.tunnelDial, used by RoutedDial and routedSocks5Dial.
With a cache and a non-literal host it resolves through the cache and
dials each address in order (filtered by network family), mirroring
netstack's loop. A nil cache, SystemDNS, IP literals and unparsable
addresses use Tnet.DialContext unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Check the context before each address, share the remaining deadline
across the addresses still to try (2s floor, capped at what remains),
and return the first dial error rather than the last. Tidy the cache
tests (close both net.Pipe ends, drop a pointer-copy test) and make
the README coverage sentence precise.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
netstack's DialContext wraps these errors in &net.OpError{Op: "dial"}, so
callers that inspect the error type (or print it) see the same shape
whether or not the dial went through dialCached. Return the bare
ctx.Err() / context.DeadlineExceeded wrapped the same way.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Without a DNS setting netstack has no DNS servers, so Tnet.DialContext on
a hostname failed with "cannot marshal DNS message", and tunnelDial worked
around it by skipping the cache. LookupAddr already uses the system
resolver in that mode.

Always go through dialCached with d.lookupAddr: the system resolver
without DNS =, the tunnel's DNS otherwise. This makes HTTP/SNI hostname
dials work without DNS =, applies CacheTTL in both modes, and keeps the
cache consistent with LookupAddr, which uses the same function.

dialCached no longer short-circuits on a nil cache: the nil cache looks
the name up on every call, so IP literals and unparsable addresses are
still the only addresses passed to dial unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Without a DNS setting, hostnames for tunnelled connections are resolved by
the host's resolver outside the tunnel; say so next to the DNS option.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
parseResolveConfig built an empty ResolveConfig and copied ResolveStrategy
from the key even when it was absent, so a [Resolve] section with only
CacheTTL left the strategy empty. ResolveAddrWithContext then returned "no
address found" for every name, breaking SOCKS5 and TCP tunnels.

Default to "auto" when the key is missing, normalise the value to lower
case, and return a parse error for anything other than auto, ipv4 or ipv6.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant