RFC 7871 requires that non-globally-routable client addresses are not
sent as EDNS Client Subnet. Sending ECS with loopback/private addresses
(e.g. 127.0.0.1 from the local listeners) breaks split-horizon zones:
AliDNS returned only the CNAME without the target A record for such
queries, so Cloudflare-fronted proxy servers could never be resolved.
Proxy server domains previously fell through to the default (proxy)
upstream: the upstream query is dispatched through the proxy outbound,
but dialing the proxy outbound itself requires resolving the server
domain, creating a circular dependency that left the proxy channel
unusable ("dns read length via dispatcher: io: read/write on closed
pipe").
Generate a highest-priority DNS module rule matching all node server
hostnames against a dedicated direct upstream (system DNS when
available). The upstream is prepended, not appended, because the module
treats the last non-bootstrap upstream as the default.
iptables requires -p tcp or -p udp when using the REDIRECT target with
--to-port. The generated rule lacked the protocol match, so on legacy
iptables the command failed with "Need TCP, UDP, SCTP or DCCP with port
specification", aborting transparent proxy startup entirely.
Review (goal verification) found that socket_test.go asserted
Dialer.Control != nil without build tags, so 'go test ./core/dns/'
fails on Windows where markedDialer() deliberately omits Control
(SO_MARK is a Linux-only option; markFd is a no-op there).
Move the Control assertions to socket_unix_test.go (//go:build !windows);
socket_test.go keeps the platform-neutral assertions (Net/UDPSize/timeouts/
Dialer non-nil). Verified: Linux go build + go test all pass, and
GOOS=windows go vet compiles the test files cleanly.
Follow-up review of 525654e found:
1. Log gating was incomplete: only the direct/route path was gated;
cache-hit/cache-miss, proxy and dispatcher per-query logs were still
unconditional log.Printf (the hottest paths under load). Gate them
behind V2RAYA_DNS_DEBUG=1 too; error logs stay visible.
2. Stopping the transparent proxy left the direct nat OUTPUT/PREROUTING
REDIRECT --to-port 52353 rules and the fwmark 0x80 exemption rules in
place (Tproxy/Redirect GetCleanCommands only clean their own chains).
Once the DNS module exits, local DNS queries hit a dead port and DNS
fails. Add cleanDnsRedirectRules() and run it on the normal stop path.
3. module.go bootstrap client inlined its own markFd + marked dialer;
reuse newMarkedDnsClient (keeping the 3s fail-fast timeout).
4. dnsLogf/dnsDebugEnabled were duplicated in socket_unix.go /
socket_windows.go; move them to a common debug.go.
5. No unit tests for newMarkedDnsClient; add tests asserting
Net/UDPSize/Timeout and a non-nil Dialer.Control.
Verified: go build ./dns/... ./kernel/v2ray/... ; go test ./dns/...
Fixes defects in the v2.4.8+ transparent-proxy DNS path that cause the
core to exhaust memory within seconds on low-RAM hosts (see discussion
#1915: '2.4.9 内存占用过高' / '有严重的内存泄漏,别用了。本地DNS完全废掉了'):
1. core/dns: upstream queries from NewUpstreamManager /
GetOrCreateUpstream / getClientForProtocol used bare &dns.Client{}
with no Dialer configured, so their sockets never carried
SO_MARK=0x80. The unmarked queries were hijacked by the module's own
transparent-proxy DNS redirect rules and looped back into :52353 —
an infinite self-query loop and memory snowball.
2. service/kernel/v2ray/transparent.go: the unconditional DNS hijack
rules (REDIRECT --to-port 52353) had no fwmark-0x80 exemption, and
being inserted with -I they sat at the head of nat OUTPUT/PREROUTING,
so marked traffic was looped back as well.
3. core/dns: per-query info logging (query/route/upstream) produced
thousands of lines/second under load, amplifying CPU/IO/memory
pressure.
Fix:
- Route every upstream client through newMarkedDnsClient, whose
net.Dialer sets SO_MARK=0x80 on all sockets. miekg/dns dials UDP
through net.Dialer too, so Dialer.Control covers UDP sockets — no
manual socket path is needed. UDPSize=4096 keeps a sane receive
buffer for queries without an EDNS0 OPT record.
- Append the DNS redirects with -A and insert the mark exemption with
-I so the exemption is matched first; also delete the exemption
rules in the residual-rule cleanup so they don't accumulate across
restarts.
- redirect mode: insert the DNS_REDIRECT jumps after the TP_PRE/TP_OUT
jumps so they actually take precedence (-I inserts at chain head),
keeping local and LAN TCP DNS flowing to the DNS module (:52353)
instead of the proxy inbound (:52345).
- Gate per-query info logs behind V2RAYA_DNS_DEBUG=1; error logs stay
always visible.
Verified: unpatched v2.4.11 core RSS 230MB->1GB in ~6s then OOM/hang;
patched build stable at ~25MB idle / ~243MB loaded, DNS and SOCKS proxy
working.
Fix a bug existed in the main branch where any modifications to node content could not be saved via WebUI.
The patch has been compiled and tested on version 2.4.9, and the save function works properly.
Further testing is still required as potential side effects introduced by this patch remain unknown.
Signed-off-by: haotiangood <haotiangood2008@gmail.com>
Locally originated DNS queries are marked 0x40 by DNS_MARK in mangle OUTPUT, then nat OUTPUT REDIRECT rewrites their destination port from 53 to 52353. When the packet re-enters PREROUTING via loopback, the dport-53 TPROXY rule no longer matches, so the generic mark-0x40 rule diverts it to the transparent proxy port 52345 where it is dropped. All local UDP DNS times out and transparent proxy appears completely broken (TCP DNS survives because the proxied connection still reaches 52353).
Add TP_PRE RETURN rules for dport 52353 ahead of the TPROXY rules in the legacy IPv4, legacy IPv6, and nftables variants so already-redirected DNS traffic is delivered to the DNS module untouched.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>