dns: Add namespace and parallel support for evaluate

This commit is contained in:
世界
2026-07-23 12:09:10 +08:00
parent 52e72736ad
commit 5d744adaa3
12 changed files with 1383 additions and 129 deletions
+5 -1
View File
@@ -562,7 +562,11 @@ Enable response-based matching. When enabled, this rule matches against the eval
(set by a preceding [`evaluate`](/configuration/dns/rule_action/#evaluate) action)
instead of only matching the original query.
The evaluated response can also be returned directly by a later [`respond`](/configuration/dns/rule_action/#respond) action.
`true` or the `tag` of an `evaluate` action: `true` matches against the response of the latest
`evaluate` action without `tag`; a tag matches against the response of the `evaluate` action with the tag.
The evaluated response can also be returned directly by a later [`respond`](/configuration/dns/rule_action/#respond) action;
in a rule with a `match_response` tag, `respond` returns the tagged response.
Required for Response Match Fields (`response_rcode`, `response_answer`, `response_ns`, `response_extra`).
Also required for `ip_cidr`, `ip_is_private`, and `ip_accept_any` when used with `evaluate` or Response Match Fields.
+3 -1
View File
@@ -552,7 +552,9 @@ Available values: `wifi`, `cellular`, `ethernet` and `other`.
启用响应匹配。启用后,此规则将匹配已评估的响应(由前序 [`evaluate`](/zh/configuration/dns/rule_action/#evaluate) 动作设置),而不仅是匹配原始查询。
该已评估的响应也可以被后续的 [`respond`](/zh/configuration/dns/rule_action/#respond) 动作直接返回
可以为 `true``evaluate` 动作的 `tag``true` 匹配最近一条无 `tag``evaluate` 动作的响应;标签则匹配对应 `evaluate` 动作的响应
该已评估的响应也可以被后续的 [`respond`](/zh/configuration/dns/rule_action/#respond) 动作直接返回;在带 `match_response` 标签的规则中,`respond` 返回该标签的响应。
响应匹配字段(`response_rcode``response_answer``response_ns``response_extra`)需要此选项。
当与 `evaluate` 或响应匹配字段一起使用时,`ip_cidr``ip_is_private``ip_accept_any` 也需要此选项。
+76 -2
View File
@@ -8,7 +8,9 @@ icon: material/new-box
:material-plus: [evaluate](#evaluate)
:material-plus: [respond](#respond)
:material-plus: [disable_optimistic_cache](#disable_optimistic_cache)
:material-plus: [timeout](#timeout)
:material-plus: [timeout](#timeout)
:material-plus: [race](#race)
:material-plus: [speculative](#speculative)
!!! quote "Changes in sing-box 1.12.0"
@@ -17,12 +19,50 @@ icon: material/new-box
!!! question "Since sing-box 1.11.0"
### Structure
```json
{
"action": "",
"race": false,
... // Action Fields
}
```
#### action
The action to perform. `route` will be used by default.
#### race
!!! question "Since sing-box 1.14.0"
Only available with `route`, `respond`, `reject` and `predefined` actions.
Requires [`match_response`](/configuration/dns/rule/#match_response) (for logical rules, in sub-rules).
Conflict with `speculative`.
By default, rules are matched one after another in listed order: a rule with `match_response`
waits for its referenced responses, and no later rule is matched until it has been judged.
A rule with `race` enabled does not hold this order: rule matching continues past it while its
referenced responses are still pending, so the matching of race rules runs in parallel — with
each other and with the rules after them. Each race rule is judged once its referenced
responses are available, and the first race rule that matches terminates rule evaluation
immediately; the remaining queries are canceled.
Rules without `race` still take effect strictly in listed order: while a preceding race rule is
not yet judged, the action of any other matched rule is held until none of the race rules
matched. The result may therefore depend on server speed only among race rules.
### route
```json
{
"action": "route", // default
"server": "",
"speculative": false,
"strategy": "",
"disable_cache": false,
"disable_optimistic_cache": false,
@@ -40,6 +80,19 @@ icon: material/new-box
Tag of target server.
#### speculative
!!! question "Since sing-box 1.14.0"
Conflict with `race`. Has no effect without a preceding `race` rule.
By default, no query is sent in parallel with pending race rules: a matched `route` action
holds its query until none of the race rules matched.
When `speculative` is enabled, the query is sent as soon as the rule matches, in parallel with
the pending race rules, and may be wasted: its response is still used only after none of the
race rules matched.
#### strategy
!!! question "Since sing-box 1.12.0"
@@ -90,6 +143,8 @@ Will override `dns.client_subnet`.
{
"action": "evaluate",
"server": "",
"tag": "",
"speculative": false,
"disable_cache": false,
"disable_optimistic_cache": false,
"rewrite_ttl": null,
@@ -113,6 +168,25 @@ does not satisfy this requirement, because matching happens before the action ru
Tag of target server.
#### tag
Tag of the evaluated response.
A tagged response is only referenced via [`match_response`](/configuration/dns/rule/#match_response) with the tag;
`match_response: true` references the response of the latest `evaluate` action without `tag`.
#### speculative
!!! question "Since sing-box 1.14.0"
Has no effect without a preceding `race` rule.
By default, no query is sent in parallel with pending race rules: a matched `evaluate` action
holds its query, and rule matching stops there, until none of the race rules matched.
When `speculative` is enabled, the query is sent as soon as the rule matches, in parallel with
the pending race rules, and may be wasted: rule matching continues without waiting for them.
#### disable_cache
Disable cache and save cache in this query.
@@ -155,7 +229,7 @@ Will override `dns.client_subnet`.
`respond` terminates rule evaluation and returns the evaluated response from a preceding [`evaluate`](/configuration/dns/rule_action/#evaluate) action.
This action does not send a new DNS query and has no extra options.
This action does not send a new DNS query.
Only allowed after a preceding top-level `evaluate` rule. If the action is reached without an evaluated response at runtime, the request fails with an error instead of falling through to later rules.
+64 -2
View File
@@ -8,7 +8,9 @@ icon: material/new-box
:material-plus: [evaluate](#evaluate)
:material-plus: [respond](#respond)
:material-plus: [disable_optimistic_cache](#disable_optimistic_cache)
:material-plus: [timeout](#timeout)
:material-plus: [timeout](#timeout)
:material-plus: [race](#race)
:material-plus: [speculative](#speculative)
!!! quote "sing-box 1.12.0 中的更改"
@@ -17,12 +19,43 @@ icon: material/new-box
!!! question "自 sing-box 1.11.0 起"
### 结构
```json
{
"action": "",
"race": false,
... // 动作字段
}
```
#### action
要执行的动作。默认使用 `route`
#### race
!!! question "自 sing-box 1.14.0 起"
仅可用于 `route``respond``reject``predefined` 动作。
需要 [`match_response`](/zh/configuration/dns/rule/#match_response)(对 logical 规则,位于子规则中)。
`speculative` 冲突。
默认情况下,规则逐条按顺序匹配:带 `match_response` 的规则等待其引用的响应,在它被判定之前不会匹配任何后续规则。
启用 `race` 的规则成为竞态规则,不再保持这一顺序:其引用的响应尚未到达时,规则匹配会越过它继续进行,因此竞态规则的匹配相互并行、也与后续规则并行。每条竞态规则在其引用的响应可用时被判定,首个匹配的竞态规则立即终止规则评估,其余查询将被取消。
未启用 `race` 的规则仍严格按顺序生效:只要前面还有未判定的竞态规则,其他已匹配规则的动作就被扣住,直到所有竞态规则均未匹配。因此只有竞态规则之间的结果取决于服务器速度。
### route
```json
{
"action": "route", // 默认
"server": "",
"speculative": false,
"strategy": "",
"disable_cache": false,
"disable_optimistic_cache": false,
@@ -40,6 +73,16 @@ icon: material/new-box
目标 DNS 服务器的标签。
#### speculative
!!! question "自 sing-box 1.14.0 起"
`race` 冲突。没有前序竞态规则时无效果。
默认情况下,查询决不与未判定的竞态规则并行发出:已匹配的 `route` 动作扣住其查询,直到所有竞态规则均未匹配后才发送。
启用 `speculative` 后,查询成为投机查询:在规则匹配时立即发出、与未判定的竞态规则并行,且可能被浪费;其响应仍仅在所有竞态规则均未匹配后才被使用。
#### strategy
!!! question "自 sing-box 1.12.0 起"
@@ -90,6 +133,8 @@ icon: material/new-box
{
"action": "evaluate",
"server": "",
"tag": "",
"speculative": false,
"disable_cache": false,
"disable_optimistic_cache": false,
"rewrite_ttl": null,
@@ -111,6 +156,23 @@ icon: material/new-box
目标 DNS 服务器的标签。
#### tag
已评估响应的标签。
带标签的响应仅能通过 [`match_response`](/zh/configuration/dns/rule/#match_response) 以标签引用;
`match_response: true` 引用最近一条无 `tag``evaluate` 动作的响应。
#### speculative
!!! question "自 sing-box 1.14.0 起"
没有前序竞态规则时无效果。
默认情况下,查询决不与未判定的竞态规则并行发出:已匹配的 `evaluate` 动作扣住其查询,规则匹配在此处停止,直到所有竞态规则均未匹配。
启用 `speculative` 后,查询成为投机查询:在规则匹配时立即发出、与未判定的竞态规则并行,且可能被浪费;规则匹配继续进行而不等待竞态规则。
#### disable_cache
在此查询中禁用缓存。
@@ -153,7 +215,7 @@ icon: material/new-box
`respond` 会终止规则评估,并直接返回前序 [`evaluate`](/zh/configuration/dns/rule_action/#evaluate) 动作保存的已评估的响应。
此动作不会发起新的 DNS 查询,也没有额外选项
此动作不会发起新的 DNS 查询。
只能用于前面已有顶层 `evaluate` 规则的场景。如果运行时命中该动作时没有已评估的响应,则请求会直接返回错误,而不是继续匹配后续规则。