Why Does DNS Still Leak When You're Behind a Proxy?
This is the written version of my video on DNS leaks. The animations in the video are easier to follow; this page is for looking up and copying the configs.
TL;DR
- Whoever asks the DNS question is who the authoritative server sees. Public resolvers may also forward your IP subnet in EDNS.
- A proxy doesn't stop this by itself. When a rule needs an IP (
GEOIP, for example), the core has to resolve the domain locally first. If that query goes out directly, it exposes your IP and what you visited. - The fixes: use fake-ip where you can, write your rules as a whitelist, move
GEOIPto the bottom or addno-resolve, and on phones, route by process name. - Then run the DNS leak test to check.
1. What happens when you open a website
You type https://google.com and press Enter. All the browser has is a name, so before anything else it sends a DNS query:
you → DNS server: google.com A ?
DNS server → you: A 142.250.196.110
Only then does the familiar part start: the TCP handshake, then TLS (with google.com in the ClientHello SNI), then HTTP/2 to fetch the page.
Everything after that is encrypted, but by default the first step is a plaintext DNS query over UDP 53, and it goes out before anything else. That's where the whole problem starts.
2. What DNS servers can see
Three kinds of DNS server
Say you buy example.com and point its NS record at your own ns1.example.com. That machine is the domain's authoritative server: it's the one that answers what IP example.com has.
But not every user can ask the authoritative server directly, and it only knows its own domains. So the work is split across three kinds of server:
| Kind | Role |
|---|---|
| Recursive resolver | The DNS you configure on your device, such as 114.114.114.114, 223.5.5.5, or 8.8.8.8. It runs the whole lookup for you |
| Root server | Tells the resolver who is in charge of a domain |
| Authoritative server | Gives the final answer |
The resolver caches answers for their TTL, so repeat lookups don't touch the root or authoritative servers at all.
One random subdomain is enough
Caching leaves a gap: the client can ask for a name nobody has asked before, like x7k2q9.example.com. It can't be in the cache, so the resolver has to go to the authoritative server.
Now that server's log has a new line: source IP 202.106.0.20. That's the resolver, not you. But ISP resolvers are usually assigned by region, so a geo lookup on that address says "Beijing, China Unicom", and now they roughly know where you are.
What about a public resolver?
Switch to 8.8.8.8 and the source becomes a Google address in the US. Safe?
Not quite. To help CDNs pick a nearby server, public resolvers can attach EDNS Client Subnet (ECS, RFC 7871), which forwards your subnet to the authoritative server:
OPT pseudo-record (RFC 6891)
└─ EDNS Client Subnet
FAMILY 1 (IPv4)
SOURCE PREFIX 24
ADDRESS 123.118.45.0 ← your subnet
The source IP is in the US, but the ECS field still says 123.118.45.0/24: Beijing.
Bottom line: if you query DNS with your own IP, you can give that IP away.
3. Why DNS still leaks behind a proxy
Let's use Clash / Mihomo as the example.
Three modes
- Global: everything goes through the proxy.
- Direct: everything connects directly.
- Rule: traffic is routed by rules, e.g. YouTube through an overseas proxy and Bilibili direct. The policy groups in your subscription ("Proxy", "Domestic", "Final") exist for these rules.
Most people use Rule mode, and that's where the trouble is.
How rules are matched
A common config:
rules:
# loopback
- IP-CIDR,127.0.0.0/8,DIRECT,no-resolve
# ads
- GEOSITE,ads,REJECT
# Chinese sites go direct
- GEOSITE,CN,DIRECT
# sites blocked by the GFW
- GEOSITE,gfw,proxy
# other Chinese services
- GEOIP,CN,DIRECT
- MATCH,proxy
Each rule is type,parameter,policy. They're checked top to bottom, and the first match wins. Say proxy is your chosen node and you're in Beijing:
bilibili.com: not loopback, not an ad, matches GEOSITE,CN and goes DIRECT. It asks the Beijing resolver for the address, then connects straight to Bilibili's Shanghai server. All normal.
google.com: skips the first three and matches GEOSITE,gfw, so it goes to proxy. Both the DNS and the request go to the Hong Kong node; the Beijing resolver never sees it.
example.com, a small site nobody's heard of: none of the first four rules recognize it, so it falls through to GEOIP,CN. To decide whether it's a domestic service, the core has to know its IP first.
That's the leak. If DNS isn't set up properly, the core sends example.com A ? straight to the local resolver. Now:
- China Unicom's Beijing resolver logs
123.118.45.67 queried example.com; example.com's authoritative server sees source202.106.0.20 · Beijing Unicom, and possibly123.118.45.0/24in ECS.
There's one more case: if the config has no MATCH at the end, anything that matches nothing goes direct by default and leaks the same way.
That's a DNS leak caused by misconfiguration.
"My important services already have rules"
Some people say: I've written rules for Claude already, I don't care if anything else leaks.
But suppose the service one day registers a domain your rules have never seen, has the page request it, and happens to host the server in mainland China. That domain falls through to GEOIP, triggers a local DNS lookup, and their authoritative server sees your resolver and ECS subnet: your real location.
You can't write rules for domains you don't know about, and the resulting risk flag may be unavoidable.
4. How to prevent DNS leaks
1. Global mode: simplest, but only for short-term use
If everything goes through the proxy, nothing leaks. The cost is that some domestic services break, and toggling it on and off keeps changing your exit IP, which raises risk flags of its own. Use it only temporarily, for high-risk tasks.
2. fake-ip mode
If you can edit your own config, prefer fake-ip. When an app asks for DNS, the core immediately returns a fake IP and matches rules by domain when the connection is made. For proxied domains, the node does the resolving. Two benefits:
- it's fast, since nothing waits on real resolution;
- services that hit the proxy use DNS that's consistent with the node they connect through.
3. Whitelist rules
Write rules for domestic sites first and send them direct, then let everything else fall through to your usual overseas node:
rules:
# domestic sites use domestic services
- GEOSITE,private,DIRECT
- GEOSITE,CN,DIRECT
# everything else: your usual overseas node
- MATCH,proxy
A few domestic apps may get slower or trigger their own risk checks, but your overseas traffic stays safe.
4. Put GEOIP at the bottom, or add no-resolve
Whether you use a whitelist or a blacklist, always put GEOIP at the very bottom and use it only when needed, or add no-resolve:
- GEOIP,CN,DIRECT,no-resolve
With no-resolve, the rule only applies when the connection is already an IP. For a domain, the rule is simply skipped instead of triggering a lookup.
5. Process-name rules: the most effective fix
Route by app. This is especially handy on phones: domestic banking and video apps (Bilibili, say) go direct or through a domestic node, and overseas apps go through your usual overseas node. Apps you've configured this way basically can't cause a DNS leak.
rules:
- PROCESS-NAME,tv.danmaku.bili,DIRECT
- PROCESS-NAME,com.openai.chatgpt,proxy
An app could get sneaky and call an external
curlto reach some address, so the process name no longer matches. That's rare, and it can be handled too, but the setup is complicated and out of scope here.
6. Finally: test it
After changing your config, open the DNS leak test and start a test. It uses the exact trick described above: it generates 10 random subdomains, asks its own authoritative server who actually queried them, and lists the resolver countries and EDNS subnet countries.
If your local ISP shows up, or the EDNS subnet lands in your city, some queries are still leaking directly. Go back through the steps above.
All IPs, domains and server locations in this post are examples.