Files
v2rayA/core/dns/socket_unix_test.go
Zakk a6c2d5f1d2 feat(core): bind the DNS module's own sockets to an egress interface
On Linux the module's upstream, bootstrap and health-check sockets carry
SO_MARK 0x80, which the transparent-proxy rules exempt. Windows and macOS
have no mark: once a TUN holds the default route, those sockets would send
the module's own queries into the TUN, where the core's port-53
interception hands them straight back to the module. The module config
gains egress_interface; when set, every dialer created by
newMarkedDnsClient binds to it with IP_UNICAST_IF / IPV6_UNICAST_IF on
Windows and IP_BOUND_IF / IPV6_BOUND_IF on macOS. Unset, the dialers are
unchanged, and Linux keeps the mark.

The darwin and windows tests dial through the dialer and read the option
back; they run on those platforms. The interface lookup itself is tested
everywhere.
2026-09-19 10:34:55 +10:00

33 lines
1.1 KiB
Go

//go:build !windows
package dns
import (
"testing"
)
// On Unix, markedDialer() sets Dialer.Control so every socket created by the
// dialer carries SO_MARK=0x80 (the fwmark that makes iptables skip the DNS
// redirect rules). Without Control the upstream queries would be hijacked
// back into this module — an infinite loop and unbounded memory growth.
// Windows and macOS have no SO_MARK; their Control binds the socket to the
// egress interface instead (IP_UNICAST_IF, IP_BOUND_IF), so these
// assertions are platform-specific and live in a !windows file.
func TestMarkedDnsClientHasControl(t *testing.T) {
udpClient := newMarkedDnsClient("udp")
if udpClient.Dialer == nil {
t.Fatal("Dialer is nil")
}
if udpClient.Dialer.Control == nil {
t.Error("Dialer.Control is nil — UDP sockets would not carry SO_MARK=0x80 and queries could loop back into the DNS module")
}
tcpClient := newMarkedDnsClient("tcp")
if tcpClient.Dialer == nil {
t.Fatal("Dialer is nil")
}
if tcpClient.Dialer.Control == nil {
t.Error("Dialer.Control is nil — TCP sockets would not carry SO_MARK=0x80")
}
}