Skip to content

fix: prevent resolver from overwriting DNS cache hostname entries#11

Merged
tito merged 1 commit intomainfrom
mathieu/fix-resolver-dns-cache-overwrite
Mar 17, 2026
Merged

fix: prevent resolver from overwriting DNS cache hostname entries#11
tito merged 1 commit intomainfrom
mathieu/fix-resolver-dns-cache-overwrite

Conversation

@tito
Copy link
Copy Markdown
Contributor

@tito tito commented Mar 17, 2026

Summary

  • When gost calls the resolver plugin with a raw IP address (common with tun2socks transparent proxying), net.LookupIP returns the IP itself. RegisterIPs then overwrites the correct hostname mapping (e.g. 160.79.104.10 -> api.anthropic.com) with a self-referential one (160.79.104.10 -> 160.79.104.10).
  • This caused pending requests and request logs to progressively show raw IPs instead of hostnames, as each allowed connection corrupted its own cache entry.
  • Fix: skip RegisterIPs when the host is already an IP address.

Root cause

The DNS cache is populated correctly by the DNS handler wrapper (dns_cache_handler.go) when a DNS query flows through the proxy. However, once the connection is allowed and gost dials the destination, gost calls the resolver plugin to "resolve" the target. For IP destinations, LookupIP("160.79.104.10") trivially returns [160.79.104.10], and RegisterIPs("160.79.104.10", ...) overwrites the correct entry.

Reproduction:

  1. DNS query through proxy: api.anthropic.com -> 160.79.104.10 (cache correct)
  2. First SOCKS5 connection to 160.79.104.10: cache HIT, shows api.anthropic.com (correct)
  3. Resolver called during dial: overwrites cache with 160.79.104.10 -> 160.79.104.10
  4. Second SOCKS5 connection to 160.79.104.10: cache HIT, shows 160.79.104.10 (wrong)

Test plan

  • go test ./internal/greyproxy/... passes
  • Manual test: DNS query + 3 sequential SOCKS5 connections all show correct hostname after fix (previously the 2nd+ connections showed raw IP)

When gost calls the resolver with a raw IP (e.g. from tun2socks),
LookupIP returns the IP itself. RegisterIPs then overwrites the
correct hostname entry (e.g. 160.79.104.10 -> api.anthropic.com)
with a self-referential mapping (160.79.104.10 -> 160.79.104.10).

This caused pending requests and logs to show raw IPs instead of
hostnames, worsening over time as each connection corrupted its
cache entry.

Skip cache registration when the host is already an IP address.
@tito tito merged commit 8c12626 into main Mar 17, 2026
@tito tito deleted the mathieu/fix-resolver-dns-cache-overwrite branch March 17, 2026 15:06
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