fix(xray): allow a private DoH server on the port Xray dials (#6918)

The DNS allow rule opened port 53 for a private server given as https://
or h2c://, but Xray dials remote DoH on the URL's port, 443 by default,
so the rule never matched and the private-IP block dropped the queries.

Remote DoH now takes its port from the URL and ignores an object entry's
port field, which Xray ignores as well; the +local forms skip routing and
keep their previous handling.
This commit is contained in:
Jay Hemnani
2026-10-24 23:10:00 +05:30
committed by GitHub
parent f105132866
commit 180cc37503
2 changed files with 61 additions and 17 deletions
@@ -183,6 +183,34 @@ func TestEnsureDnsServerRouting_StripsSchemePortAndPath(t *testing.T) {
}
}
func TestEnsureDnsServerRouting_RemoteDohUsesHttpsPort(t *testing.T) {
in := `{
"dns": {"servers": [
"https://10.255.255.1/dns-query",
"h2c://10.255.255.2/dns-query",
"https://10.255.255.3:8443/dns-query"
]},
"routing": {"rules": [{"type":"field","outboundTag":"blocked","ip":["geoip:private"]}]}
}`
out, err := EnsureDnsServerRouting(in)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
rules := rulesFromRaw(t, out)
if len(rules) != 3 {
t.Fatalf("got %d rules, want 2 allow rules + block: %v", len(rules), rules)
}
if p, _ := rules[0]["port"].(string); p != "443" {
t.Fatalf("rules[0] port = %v, want \"443\"", rules[0]["port"])
}
if ips := readRuleIPs(rules[0]["ip"]); len(ips) != 2 || ips[0] != "10.255.255.1" || ips[1] != "10.255.255.2" {
t.Fatalf("rules[0] ip = %v, want [10.255.255.1 10.255.255.2]", ips)
}
if p, _ := rules[1]["port"].(string); p != "8443" {
t.Fatalf("rules[1] port = %v, want \"8443\"", rules[1]["port"])
}
}
func TestEnsureDnsServerRouting_SkipsSpecialAndDomainAddresses(t *testing.T) {
in := `{
"dns": {"servers": ["localhost", "fakedns", "dns.google", "8.8.8.8"]},
@@ -465,3 +493,20 @@ func TestEnsureDnsServerRouting_InvalidJsonReturnsAsIs(t *testing.T) {
t.Fatalf("expected raw passthrough on error, got %q", out)
}
}
// Xray dials a DoH server from its URL alone; the object's "port" (53 in entries saved by older
// forms) never reaches it, so the allow rule must still open the URL's port.
func TestEnsureDnsServerRouting_RemoteDohIgnoresObjectPort(t *testing.T) {
in := `{
"dns": {"servers": [{"address": "https://10.255.255.4/dns-query", "port": 53, "domains": ["lan"]}]},
"routing": {"rules": [{"type":"field","outboundTag":"blocked","ip":["geoip:private"]}]}
}`
out, err := EnsureDnsServerRouting(in)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
rules := rulesFromRaw(t, out)
if p, _ := rules[0]["port"].(string); p != "443" {
t.Fatalf("rules[0] port = %v, want \"443\"", rules[0]["port"])
}
}