Skip to content

Commit 8c12626

Browse files
authored
fix: prevent resolver from overwriting DNS cache hostname entries (#11)
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.
1 parent 3d16189 commit 8c12626

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

internal/greyproxy/plugins/resolver.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ func (r *Resolver) Resolve(ctx context.Context, network, host string, opts ...re
4040
return nil, fmt.Errorf("resolve %s: no results", host)
4141
}
4242

43-
// Cache all resolved IPs -> hostname
44-
ipStrs := make([]string, len(ips))
45-
for i, ip := range ips {
46-
ipStrs[i] = ip.String()
43+
// Cache all resolved IPs -> hostname, but only when host is an actual
44+
// hostname (not an IP). When gost calls the resolver with a raw IP,
45+
// LookupIP returns the IP itself; registering it would overwrite
46+
// a correct hostname entry previously populated by the DNS handler.
47+
if net.ParseIP(host) == nil {
48+
ipStrs := make([]string, len(ips))
49+
for i, ip := range ips {
50+
ipStrs[i] = ip.String()
51+
}
52+
r.cache.RegisterIPs(host, ipStrs)
4753
}
48-
r.cache.RegisterIPs(host, ipStrs)
4954

50-
r.log.Debugf("resolved %s -> %v", host, ipStrs)
55+
r.log.Debugf("resolved %s -> %v", host, ips)
5156
return ips, nil
5257
}
5358

0 commit comments

Comments
 (0)