From c1e65ca44a0d554459031f79fd515e6fc26eded0 Mon Sep 17 00:00:00 2001 From: Zakk Date: Sun, 20 Sep 2026 18:44:58 +1000 Subject: [PATCH] fix(dns): refuse DoH and DoQ upstreams on save instead of failing every query The DNS form's placeholder offered https://dns.google/dns-query, the service turned https:// into protocol "https" and quic:// into "quic", and the DNS module handed those to a miekg/dns client, which speaks udp, tcp and tcp-tls only. Saving succeeded and every query on that rule then failed, visible only in the log. CheckDnsUpstream in kernel/v2ray accepts an address, tcp://, udp:// and tls:// (DNS over TLS, which the module does support) and rejects https:// and quic:// with a message that names the alternatives; PutDnsRules returns it as a 400 before anything is stored. The placeholder in the six locales shows the supported forms and the docs say which schemes work. Verified on the VM: https:// and quic:// rules come back BAD_REQUEST with the message, tls://1.1.1.1 saves and the module logs addr=1.1.1.1:853 protocol=tcp-tls; the previous rules restore unchanged. --- gui/src/docs/en/transparent-proxy.md | 2 +- gui/src/docs/zh/transparent-proxy.md | 2 +- gui/src/locales/en.js | 3 ++- gui/src/locales/fa-ir.js | 3 ++- gui/src/locales/ko.js | 3 ++- gui/src/locales/pt-br.js | 3 ++- gui/src/locales/ru.js | 3 ++- gui/src/locales/zh.js | 3 ++- service/kernel/v2ray/template_dns.go | 24 +++++++++++++++++++ .../v2ray/template_dns_upstream_test.go | 18 ++++++++++++++ service/server/controller/dnslist.go | 5 ++++ 11 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 service/kernel/v2ray/template_dns_upstream_test.go diff --git a/gui/src/docs/en/transparent-proxy.md b/gui/src/docs/en/transparent-proxy.md index 5d2f1339..4308b1b2 100644 --- a/gui/src/docs/en/transparent-proxy.md +++ b/gui/src/docs/en/transparent-proxy.md @@ -36,7 +36,7 @@ Known limitation: on Windows and macOS an application that queries a LAN resolve ## DNS -**Settings → DNS Settings** holds the rules the core's DNS module follows: which upstream answers which domains, and whether the query goes out directly. The defaults send private names to `127.0.0.1:53` (`localhost`, which needs a resolver listening there), `geosite:cn` to `223.5.5.5` directly, and everything else to `1.0.0.1` through the proxy. An outbound of `direct` queries directly; any other value sends the query through the local SOCKS inbound, so it is routed like SOCKS traffic. The rule with an empty domain list answers every domain no other rule names. +**Settings → DNS Settings** holds the rules the core's DNS module follows: which upstream answers which domains, and whether the query goes out directly. The defaults send private names to `127.0.0.1:53` (`localhost`, which needs a resolver listening there), `geosite:cn` to `223.5.5.5` directly, and everything else to `1.0.0.1` through the proxy. An outbound of `direct` queries directly; any other value sends the query through the local SOCKS inbound, so it is routed like SOCKS traffic. The rule with an empty domain list answers every domain no other rule names. An upstream is an address (`8.8.8.8`, `dns.google`), `tcp://host` or `tls://host` for DNS over TLS; DNS over HTTPS and QUIC are not supported and are refused on save. ## Sharing with the LAN diff --git a/gui/src/docs/zh/transparent-proxy.md b/gui/src/docs/zh/transparent-proxy.md index 206b3eb1..1c68178d 100644 --- a/gui/src/docs/zh/transparent-proxy.md +++ b/gui/src/docs/zh/transparent-proxy.md @@ -36,7 +36,7 @@ ## DNS -**设置 → DNS 设置**保存内核 DNS 模块遵循的规则:哪个上游回答哪些域名,查询是否直连。默认规则把私有域名交给 `127.0.0.1:53`(`localhost`,需要本机有解析器监听),`geosite:cn` 直连查询 `223.5.5.5`,其余经代理查询 `1.0.0.1`。出站为 `direct` 时直连查询;其他值都经本机的 SOCKS 入站发出,分流与 SOCKS 流量相同。域名列表为空的规则回答其他规则未命名的全部域名。 +**设置 → DNS 设置**保存内核 DNS 模块遵循的规则:哪个上游回答哪些域名,查询是否直连。默认规则把私有域名交给 `127.0.0.1:53`(`localhost`,需要本机有解析器监听),`geosite:cn` 直连查询 `223.5.5.5`,其余经代理查询 `1.0.0.1`。出站为 `direct` 时直连查询;其他值都经本机的 SOCKS 入站发出,分流与 SOCKS 流量相同。域名列表为空的规则回答其他规则未命名的全部域名。上游写地址(`8.8.8.8`、`dns.google`)、`tcp://host`,或 `tls://host` 表示 DNS over TLS;不支持 DNS over HTTPS 与 QUIC,保存时会拒绝。 ## 局域网共享 diff --git a/gui/src/locales/en.js b/gui/src/locales/en.js index dc9b964e..7888b115 100644 --- a/gui/src/locales/en.js +++ b/gui/src/locales/en.js @@ -313,7 +313,8 @@ export default { colServer: "DNS Server", colDomains: "Domain List", colOutbound: "Outbound", - serverPlaceholder: "e.g. 8.8.8.8 or https://dns.google/dns-query", + serverPlaceholder: + "e.g. 8.8.8.8, tcp://8.8.8.8 or tls://dns.google (DNS over TLS)", domainsPlaceholder: "One per line, e.g. geosite:cn\nLeave empty for fallback DNS", addRule: "Add Rule", diff --git a/gui/src/locales/fa-ir.js b/gui/src/locales/fa-ir.js index 201448cd..6f8590f8 100644 --- a/gui/src/locales/fa-ir.js +++ b/gui/src/locales/fa-ir.js @@ -310,7 +310,8 @@ export default { colServer: "سرور DNS", colDomains: "فهرست دامنه‌ها", colOutbound: "خروجی", - serverPlaceholder: "برای نمونه 8.8.8.8 یا https://dns.google/dns-query", + serverPlaceholder: + "برای نمونه 8.8.8.8، tcp://8.8.8.8 یا tls://dns.google (DNS over TLS)", domainsPlaceholder: "هر مورد در یک خط؛ برای نمونه geosite:cn\nبرای DNS جایگزین خالی بگذارید", addRule: "افزودن قانون", diff --git a/gui/src/locales/ko.js b/gui/src/locales/ko.js index 0814efa4..aa4ac5df 100644 --- a/gui/src/locales/ko.js +++ b/gui/src/locales/ko.js @@ -309,7 +309,8 @@ export default { colServer: "DNS 서버", colDomains: "도메인 목록", colOutbound: "아웃바운드", - serverPlaceholder: "예: 8.8.8.8 또는 https://dns.google/dns-query", + serverPlaceholder: + "예: 8.8.8.8, tcp://8.8.8.8 또는 tls://dns.google (DNS over TLS)", domainsPlaceholder: "한 줄에 하나, 예: geosite:cn\n비우면 폴백 DNS 사용", addRule: "규칙 추가", rule: "규칙 {n}", diff --git a/gui/src/locales/pt-br.js b/gui/src/locales/pt-br.js index d412fa69..85856a1a 100644 --- a/gui/src/locales/pt-br.js +++ b/gui/src/locales/pt-br.js @@ -317,7 +317,8 @@ export default { colServer: "Servidor DNS", colDomains: "Lista de domínios", colOutbound: "Saída", - serverPlaceholder: "por exemplo, 8.8.8.8 ou https://dns.google/dns-query", + serverPlaceholder: + "por exemplo, 8.8.8.8, tcp://8.8.8.8 ou tls://dns.google (DNS over TLS)", domainsPlaceholder: "Um por linha, por exemplo, geosite:cn\nDeixe vazio para usar o DNS de fallback", addRule: "Adicionar regra", diff --git a/gui/src/locales/ru.js b/gui/src/locales/ru.js index be85e4b5..a850a88b 100644 --- a/gui/src/locales/ru.js +++ b/gui/src/locales/ru.js @@ -313,7 +313,8 @@ export default { colServer: "DNS-сервер", colDomains: "Список доменов", colOutbound: "Исходящий трафик", - serverPlaceholder: "например, 8.8.8.8 или https://dns.google/dns-query", + serverPlaceholder: + "например, 8.8.8.8, tcp://8.8.8.8 или tls://dns.google (DNS over TLS)", domainsPlaceholder: "По одному в строке, например geosite:cn\nОставьте пустым для резервного DNS", addRule: "Добавить правило", diff --git a/gui/src/locales/zh.js b/gui/src/locales/zh.js index 695bbd6b..8ff7c21a 100644 --- a/gui/src/locales/zh.js +++ b/gui/src/locales/zh.js @@ -303,7 +303,8 @@ export default { colServer: "DNS 服务器", colDomains: "域名列表", colOutbound: "出口", - serverPlaceholder: "如 8.8.8.8 或 https://dns.google/dns-query", + serverPlaceholder: + "如 8.8.8.8、tcp://8.8.8.8 或 tls://dns.google(DNS over TLS)", domainsPlaceholder: "每行一个,如 geosite:cn\n留空表示兜底 DNS", addRule: "添加规则", rule: "规则 {n}", diff --git a/service/kernel/v2ray/template_dns.go b/service/kernel/v2ray/template_dns.go index 7a78784c..a9581aa9 100644 --- a/service/kernel/v2ray/template_dns.go +++ b/service/kernel/v2ray/template_dns.go @@ -143,6 +143,30 @@ func dnsModuleExtraListenAddrs(setting *configure.Setting) []string { return append(addrs, "127.2.0.17:53") } +// CheckDnsUpstream rejects an upstream the DNS module cannot query. The +// module speaks plain UDP and TCP and DNS over TLS; https:// (DoH) and +// quic:// (DoQ) used to be accepted here and then failed on every query. +func CheckDnsUpstream(upstream string) error { + scheme, rest, found := strings.Cut(upstream, "://") + if !found { + if strings.TrimSpace(upstream) == "" { + return fmt.Errorf("DNS upstream is empty") + } + return nil + } + switch strings.ToLower(scheme) { + case "udp", "tcp", "tls": + if rest == "" { + return fmt.Errorf("DNS upstream %q has no address after the scheme", upstream) + } + return nil + case "https", "quic": + return fmt.Errorf("DNS upstream %q: DNS over HTTPS and DNS over QUIC are not supported; use an address (8.8.8.8), tcp://host or tls://host for DNS over TLS", upstream) + default: + return fmt.Errorf("DNS upstream %q: unknown scheme %q; use an address, tcp:// or tls://", upstream, scheme) + } +} + // generateDnsModuleConfig 生成新 DNS 模块的 JSON 配置,嵌入 xray JSON 配置文件。 // v2raya-core 启动时解析此配置并启动独立 DNS 监听器,v2rayA 不参与 DNS 查询处理。 // diff --git a/service/kernel/v2ray/template_dns_upstream_test.go b/service/kernel/v2ray/template_dns_upstream_test.go new file mode 100644 index 00000000..dc2a1e27 --- /dev/null +++ b/service/kernel/v2ray/template_dns_upstream_test.go @@ -0,0 +1,18 @@ +package v2ray + +import "testing" + +func TestCheckDnsUpstream(t *testing.T) { + ok := []string{"8.8.8.8", "8.8.8.8:53", "localhost", "dns.google", "tcp://8.8.8.8", "tls://dns.google", "tls://1.1.1.1:853", "udp://[2001:4860:4860::8888]:53"} + for _, u := range ok { + if err := CheckDnsUpstream(u); err != nil { + t.Errorf("%q rejected: %v", u, err) + } + } + bad := []string{"", "https://dns.google/dns-query", "quic://dns.adguard.com", "ftp://x", "tls://"} + for _, u := range bad { + if err := CheckDnsUpstream(u); err == nil { + t.Errorf("%q accepted", u) + } + } +} diff --git a/service/server/controller/dnslist.go b/service/server/controller/dnslist.go index 429ec457..c3e2ff42 100644 --- a/service/server/controller/dnslist.go +++ b/service/server/controller/dnslist.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "github.com/v2rayA/v2rayA/common" "github.com/v2rayA/v2rayA/db/configure" + "github.com/v2rayA/v2rayA/kernel/v2ray" "github.com/v2rayA/v2rayA/server/service" ) @@ -40,6 +41,10 @@ func PutDnsRules(ctx *gin.Context) { common.ResponseError(ctx, logError(fmt.Errorf("DNS rule %d has no upstream server", i+1))) return } + if err := v2ray.CheckDnsUpstream(upstream); err != nil { + common.ResponseError(ctx, badRequest("DNS rules", fmt.Errorf("DNS rule %d: %w", i+1, err))) + return + } // 同步新旧字段:确保 Server 和 Upstream 至少有一个有值 if rule.Server == "" && rule.Upstream != "" { rule.Server = rule.Upstream