Files
Zakk c1e65ca44a 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.
2026-09-20 18:44:58 +10:00

113 lines
3.2 KiB
Go

package controller
import (
"errors"
"fmt"
"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"
)
// DnsConfigResponse 是新 DNS 配置的 API 响应结构。
type DnsConfigResponse struct {
UseNewModule bool `json:"useNewModule"`
Listener string `json:"listener"`
Rules []configure.DnsRule `json:"rules"`
}
// PutDnsRules 处理 PUT /api/dns 请求,保存 DNS 规则配置。
// 支持新格式和旧格式请求,向后兼容。
func PutDnsRules(ctx *gin.Context) {
release, ok := beginMutation(ctx)
if !ok {
return
}
defer release()
var rules []configure.DnsRule
if err := ctx.ShouldBindJSON(&rules); err != nil {
common.ResponseError(ctx, badRequest("DNS rules", fmt.Errorf("request body must be a JSON array of DNS rules: %w", err)))
return
}
for i, rule := range rules {
// 检查上游地址:优先使用 Upstream 字段,回退到 Server
upstream := rule.Upstream
if upstream == "" {
upstream = rule.Server
}
if upstream == "" {
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
}
if rule.Upstream == "" && rule.Server != "" {
rule.Upstream = rule.Server
}
// 同步 Domain 和 Domains
if rule.Domains == "" && rule.Domain != "" {
rule.Domains = rule.Domain
}
if rule.Domain == "" && rule.Domains != "" {
rule.Domain = rule.Domains
}
if rule.Outbound == "" {
rule.Outbound = "direct"
}
rules[i] = rule
}
// 执行迁移以确保新字段有默认值
migrated := configure.MigrateDnsRules(rules)
err := service.ApplyCoreConfig(func() func() error {
previous := configure.GetDnsRulesNotNil()
return func() error { return configure.SetDnsRules(previous) }
}, func() error {
return configure.SetDnsRules(migrated)
})
if err != nil {
var failure *service.ApplyCoreConfigError
if errors.As(err, &failure) {
err = failure.UpdateErr
if failure.RestoreStoreErr != nil {
err = fmt.Errorf("%w; restoring DNS rules failed: %v", err, failure.RestoreStoreErr)
} else if failure.RestoreUpdateErr != nil {
err = fmt.Errorf("%w; restarting with previous DNS rules failed: %v", err, failure.RestoreUpdateErr)
}
}
common.ResponseError(ctx, logError(err))
return
}
common.ResponseSuccess(ctx, nil)
}
// GetDnsRules 处理 GET /api/dns 请求,返回 DNS 规则配置和状态。
// 返回新格式配置,同时保持向后兼容。
func GetDnsRules(ctx *gin.Context) {
rules := configure.GetDnsRulesNotNil()
migrated := configure.MigrateDnsRules(rules)
// 获取当前设置以返回监听地址等信息
setting := service.GetSetting()
listener := ""
if setting != nil {
listener = setting.DnsListenAddr
}
common.ResponseSuccess(ctx, DnsConfigResponse{
UseNewModule: true,
Listener: listener,
Rules: migrated,
})
}