DNS — Xray-core
dns 块用于配置 Xray 的内部解析器:一份服务器列表、可选的静态 hosts,以及一组缓存 / 回落开关。DNS 引擎自身也以内部入站形式存在(默认 tag 为 dns),因此 DNS 流量可以像普通连接一样被路由。
顶层选项
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
servers | []*NameServerConfig | [] | [NameServerConfig | <string>] | 解析器列表。每个条目要么是地址字符串(8.8.8.8、https://dns.google/dns-query、tcp+local://、fakedns 等),要么是完整的 NameServerConfig 对象。 |
script | string | (unset) | <path to .lua file> | 由 Lua 脚本接管服务器选择:hosts 未应答的每次查询都交给脚本的 HandleDNSQuery 函数。不能与 disableFallback、disableFallbackIfMatch、enableParallelQuery 或服务器的 domains / skipFallback / finalQuery 同时使用 —— 否则启动失败。见下方「Lua 脚本」一节。 |
hosts | *HostsWrapper | {} | {<domain or pattern>: <IP | [IP, …]>} | 静态 hosts。键支持与路由规则相同的 full:、domain:、regexp:、keyword:、geosite: 前缀。 |
clientIp | *Address | (unset) | <IP> | 出站查询中携带的 ECS(EDNS Client Subnet)。可让支持 CDN 调度的解析器返回地理上更近的应答。 |
tag | string | dns | <inbound tag> | DNS 引擎内部入站的 tag。被路由规则用来单独识别 DNS 流量。 |
queryStrategy | string | UseIP | UseIP | UseIPv4 | UseIPv6 | 在未设定单服务器策略时使用的默认查询策略。UseIP 同时查询 A 与 AAAA;UseIPv4 / UseIPv6 跳过另一族。 |
disableCache | bool | false | true | false | 禁用内存中的应答缓存。每次查询都会重新发起。 |
serveStale | bool | false | true | false | 后台刷新过程中先返回已过期的缓存条目,改善感知延迟。 |
serveExpiredTTL | uint32 | 0 | <seconds> | serveStale 启用时,过期条目可继续被使用的最长秒数。0 表示无上限。 |
disableFallback | bool | false | true | false | 当第一台服务器没有返回可用答案时,不要回落到后续服务器。 |
disableFallbackIfMatch | bool | false | true | false | 与上一项类似,但仅当命中的服务器自身带有 domains 列表时才禁用回落 —— 即按域名命中的服务器永远不会触发回落。 |
enableParallelQuery | bool | false | true | false | 并行查询所有匹配的服务器,首个有效答案获胜。 |
useSystemHosts | bool | false | true | false | 在走网络之前也查询操作系统的 /etc/hosts(或平台等价文件)。 |
源码: infra/conf/dns.go:164-178 · 锚定版本 v26.11.9 (676eaf6)
servers[] —— NameServerConfig
每个条目要么是裸地址字符串(被解析进 address),要么是完整对象形式:
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
id | string | (unset) | <string> | Lua 脚本看到的该服务器名称:即 xray.dns 模块 Servers 列表中的 ID,供 DNS 脚本或路由脚本挑选该服务器。没有脚本时不起作用。 |
address | *Address | (required) | <DNS URL> | 解析器地址。裸 IP(8.8.8.8 = UDP/53)、tcp://、tls://、https://、quic://、tcp+local://...(本地 DNS,不走代理)、localhost 或 fakedns。 |
clientIp | *Address | (inherit) | <IP> | 单服务器的 ECS 覆盖。 |
port | uint16 | 53 | <port> | 服务器端口(仅对裸 IP 地址生效)。 |
skipFallback | bool | false | true | false | 为 true 时,此服务器返回空答案不会触发回落链 —— 查询直接失败。 |
domains | StringList | [] | <domain pattern> | 该服务器应当应答的域名。语法同路由规则。非空时,此服务器仅就这些域名被询问。 |
expectedIPs | StringList | [] | <IP / CIDR / geoip:> | 只有解析结果匹配这些模式时才接受该服务器的应答。用于过滤被审查或被劫持的响应。 |
expectIPs | StringList | (alias) | (alias of expectedIPs) | expectedIPs 的兼容别名。 |
queryStrategy | string | (inherit) | UseIP | UseIPv4 | UseIPv6 | 单服务器的策略覆盖。 |
tag | string | (unset) | <string> | 服务器 tag —— 被路由规则用来路由解析器自身的出站流量。 |
timeoutMs | uint64 | 4000 | <ms> | 单次查询的超时(毫秒)。 |
disableCache | *bool | (inherit) | true | false | 单服务器的缓存覆盖。 |
serveStale | *bool | (inherit) | true | false | 单服务器的 serve-stale 覆盖。 |
serveExpiredTTL | *uint32 | (inherit) | <seconds> | 单服务器的过期 TTL 上限。 |
finalQuery | bool | false | true | false | 把该服务器标记为最终回落 —— 一旦它有应答,就不再向后解析。 |
unexpectedIPs | StringList | [] | <IP / CIDR / geoip:> | 解析结果匹配这些模式时拒绝该服务器的应答。expectedIPs 的反向版。 |
源码: infra/conf/dns.go:19-36 · 锚定版本 v26.11.9 (676eaf6)
hosts
hosts 是扁平 map。键支持与路由规则相同的前缀;值要么是单个 IP,要么是 IP 数组。
{
"dns": {
"hosts": {
"domain:example.com": "203.0.113.10",
"domain:internal.corp": ["10.0.0.1", "10.0.0.2"],
"geosite:cn": "8.8.8.8"
}
}
}把 IP 值写成 "fakedns",会让 Xray 返回 FakeDNS 池 中的伪地址。
Lua 脚本(script)
script 把服务器选择交给 Lua。静态 hosts(含 useSystemHosts)仍最先应答;其余每次查询都会调用脚本的 HandleDNSQuery,由它自行查询已配置的服务器并返回答案。每台服务器保留自己的 address、clientIp、queryStrategy、expectedIPs / unexpectedIPs、timeoutMs 与缓存设置;而决定由哪台服务器应答的选项(disableFallback、disableFallbackIfMatch、enableParallelQuery,以及单服务器的 domains、skipFallback、finalQuery)会在启动时被拒绝。
取值为 .lua 文件路径 —— 绝对路径,或依次在 XRAY_LOCATION_CONFDIR / XRAY_LOCATION_CONFIG 指定的目录、工作目录与可执行文件所在目录中查找。不接受内联的 base64: 脚本(启动失败)。文件必须定义:
HandleDNSQuery(domain, ipv4, ipv6, fake)——domain为小写且不带末尾的点;ipv4/ipv6表示需要哪些地址族(已按queryStrategy收窄);fake表示是否接受 FakeDNS 应答。返回ips, ttl, err:IP 列表须原样来自某台服务器的Query(或 IP 匹配器的FilterIPs),TTL 以秒计,err为nil或错误信息。return nil, 0表示空应答。
require 可加载以下模块:
xray.dns——Servers:按顺序排列的已配置服务器,每项含ID(即其id)与Query(domain, ipv4, ipv6, fake),以server:Query(…)调用,返回ips, ttl, err。xray.geodata——BuildDomainMatcher(…)与BuildIPMatcher(…)接受geosite:cn、geoip:private这类规则字符串;域名匹配器提供MatchAny(domain),IP 匹配器提供AnyMatch(ips)与FilterIPs(ips)(返回命中与未命中两个列表)。xray.log——Debug、Info、Warning、Error写入 Xray 的错误日志。
脚本在沙箱中运行:只有去掉 load、dofile、print、getmetatable / setmetatable 与 rawget / rawset 后的 Lua 基础函数,加上 table、string、math 以及 os 的时间函数 —— 无法访问文件、网络或进程。单次调用超过 6 秒即被中止。并发查询运行在各自独立的 Lua 状态中,因此全局变量不会在它们之间共享。
仅当国内解析器的结果指向中国境内时才采用,否则询问远端解析器:
{
"dns": {
"script": "dns.lua",
"servers": [
{ "id": "domestic", "address": "223.5.5.5" },
{ "id": "remote", "address": "https://1.1.1.1/dns-query" }
]
}
}local servers = require("xray.dns").Servers
local cn = require("xray.geodata").BuildIPMatcher("geoip:cn")
local byID = {}
for _, server in ipairs(servers) do
byID[server.ID] = server
end
function HandleDNSQuery(domain, ipv4, ipv6, fake)
local ips, ttl, err = byID.domestic:Query(domain, ipv4, ipv6, fake)
if not err and cn:AnyMatch(ips) then
return ips, ttl, nil
end
return byID.remote:Query(domain, ipv4, ipv6, fake)
end路由脚本 同样可以加载 xray.dns。
示例
标准分流解析 —— CN 域名走本地 DoH,其余走 Cloudflare DoH:
{
"dns": {
"servers": [
{
"address": "https://doh.pub/dns-query",
"domains": ["geosite:cn"],
"expectedIPs": ["geoip:cn"]
},
"https://1.1.1.1/dns-query",
{
"address": "tcp+local://223.5.5.5",
"domains": ["geosite:cn"]
}
],
"hosts": {
"domain:example.com": "203.0.113.10"
},
"queryStrategy": "UseIP",
"disableFallbackIfMatch": true,
"enableParallelQuery": false
}
}配合 FakeDNS 的路由:
{
"dns": {
"servers": ["fakedns", "1.1.1.1"]
},
"fakeDns": { "ipPool": "198.18.0.0/15", "poolSize": 32768 },
"inbounds": [{
"port": 1080,
"protocol": "socks",
"sniffing": {
"enabled": true,
"destOverride": ["http", "tls", "fakedns"]
}
}]
}说明
servers[]中裸8.8.8.8字符串是{"address": "8.8.8.8"}的简写 ——NameServerConfig的UnmarshalJSON(infra/conf/dns.go:37-44)接受两种形态。address: "localhost"使用操作系统解析器(在带 stub 解析器的平台如 systemd-resolved 上很方便)。address: "tcp+local://..."通过 TCP 查询并 绕过 路由引擎 —— 适合作为整个 DNS 体系所依赖的引导解析器。domains模式:裸字符串默认按后缀匹配;带full:为精确匹配、regexp:为正则、keyword:为子串、geosite:为 GeoSite 类目。expectedIPs/unexpectedIPs联用实现「拒绝中毒应答」模式 —— 在国内解析器上设expectedIPs: ["geoip:cn"],在公网解析器上设unexpectedIPs: ["geoip:cn"]。clientIp(含按服务器的clientIp)设置 EDNS Client Subnet 选项。设置成真实值(例如你的某个公网 IP)可获得 CDN 调度的答案。finalQuery: true将服务器标记为终端 —— 一旦它有应答,就不再跑回落。
dns 出站
与上面的 dns 块不同,Xray 还提供一个 dns 出站("protocol": "dns")。把 DNS 流量路由给它,它要么把每条查询转发到固定解析器,要么按规则列表来应答:它具有目的地 改写 字段与 rules 引擎,而 nonIPQuery / blockTypes 旋钮已弃用,推荐改用 rules。
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
rewriteNetwork | Network | (unset) | tcp | udp | tcp,udp | 查询改写到的传输。旧式 network 的规范名。 |
rewriteAddress | *Address | (unset) | <host> | 被拦截的 DNS 查询改写到的解析器。旧式 address 的规范名。 |
rewritePort | uint16 | 0 | <port> | 查询改写到的端口。旧式 port 的规范名。 |
network | Network | (alias) | (alias of rewriteNetwork) | rewriteNetwork 的旧式别名。 |
address | *Address | (alias) | (alias of rewriteAddress) | rewriteAddress 的旧式别名。 |
port | uint16 | (alias) | (alias of rewritePort) | rewritePort 的旧式别名。 |
userLevel | uint32 | 0 | <uint32> | 应用于该出站连接的策略等级。 |
rules | []*DNSOutboundRuleConfig | (unset) | [DNSOutboundRuleConfig] | 按查询的规则,按顺序求值;第一条命中决定查询的处理方式。 |
nonIPQuery | *string | (legacy) | reject | drop | skip | 旧式的非 IP 查询处理。已弃用,改用 rules,且不能与之同时使用。 |
blockTypes | *[]int32 | (legacy) | [<qType number>] | 旧式的要拦截的 DNS 查询类型编号列表。已弃用,改用 rules。 |
源码: infra/conf/dns_proxy.go:60-71 · 锚定版本 v26.11.9 (676eaf6)
rules[]
| 字段 | 类型 | 默认值 | 允许值 | 描述 |
|---|---|---|---|---|
action | string | (required) | direct | drop | return | hijack | 对匹配查询的处理方式。direct 原样转发;drop 丢弃;return 用 rCode 本地应答;hijack 将其重定向到改写服务器。 |
qType | *PortList | (any) | <DNS type number / range> | 按编号匹配这些 DNS 查询类型(1 = A,28 = AAAA,65 = HTTPS 等)。接受逗号 / 范围列表。 |
domain | *StringList | (any) | <domain pattern> | 匹配这些域名(默认按子串;与路由规则相同的模式前缀)。 |
rCode | uint32 | 0 | 0-65535 | 当 action 为 return 时返回的响应码(例如 5 = REFUSED,3 = NXDOMAIN)。 |
源码: infra/conf/dns_proxy.go:13-18 · 锚定版本 v26.11.9 (676eaf6)
把 A/AAAA 劫持到固定解析器,丢弃 HTTPS 记录(类型 65)查询,其余一律拒绝:
{
"outbounds": [
{
"tag": "dns-out",
"protocol": "dns",
"settings": {
"rewriteAddress": "1.1.1.1",
"rewritePort": 53,
"rewriteNetwork": "udp",
"rules": [
{ "action": "hijack", "qType": "1,28" },
{ "action": "drop", "qType": "65" },
{ "action": "return", "rCode": 5 }
]
}
}
]
}nonIPQuery 与 blockTypes 不能与 rules 混用 —— 这么做会触发启动错误。请把旧式配置迁移到 rules。
跨内核说明
- sing-box 使用按 type 多态的服务器 schema(
type: "https"、type: "tls"、type: "fakeip"等),并配合带action: "route"|"reject"|"predefined"的结构化 DNS 规则。参见 DNS — sing-box。 - mihomo 用扁平
dns:块,含独立的nameserver/fallback列表,再加强大的nameserver-policy映射做按域路由。参见 DNS — mihomo。
源码: infra/conf/dns.go:19-178 · v26.11.9 (676eaf6)
