DanielLavrushin:b4 的 DNS 全匹配集应暂时放在其他集合之后

DanielLavrushin 报告,b4 文档中的 regexp:.* DNS 集若放在前面,可能先匹配其他集的正则域名,再因 53 端口过滤不符而丢弃匹配,TLS 连接因此没有集合和策略;普通域名优先匹配,不受同样影响。他在 d17aaa3bd 的网络命名空间中测得该现象,QUIC 只核对了代码路径,未实测。作者建议把端口提前纳入选集条件;在修复前,文档两种语言都应注明将全匹配集放在最后。

作者原文

Sending every DNS query to DoH in the docs gives a set with the target regexp:.*, port filters of 53 and a DoH resolver. It says: "its port filters of 53 keep it off TLS and QUIC connections". Placed above other sets, the set takes their regexp: names away over TLS: the connection gets no set and no strategy.

Measured

Network namespace, main (d17aaa3bd); the branch of #672 gives the same results. Three sets: the catch-all from the docs (with a plain resolver), one with regexp:(^|\.)rx\.example$, one with plain.example. The last two have no resolver. Only the order differs.

| | Catch-all first | Catch-all last |
| --- | --- | --- |
| TLS to a.rx.example | no set | the rx set |
| TLS to b.plain.example | the plain set | the plain set |
| 5 DNS queries for a.rx.example | the catch-all's resolver | the resolver that was asked |
| 5 DNS queries for b.plain.example | the resolver that was asked | the resolver that was asked |

Names listed as plain domains are not affected: they are matched before any regular expression. The sentence "Domains listed in other sets keep their own sets' resolvers" in the same section holds for them, but not for regexp: names.

GeoSite categories bring such names in without the user writing one: in the v2fly lists, openai has 1 regular expression and netflix 4, and b4 loads them as regexp: targets.

Cause

  • matchRegexWithSourceTLS collects the sets whose regular expression fits, in the order of the sets. selectSetBySourceAndTLS returns the first that fits the device and the TLS version. Neither looks at the port.
  • The port filter is read afterwards (src/nfq/handler.go). When it does not fit, the match is dropped and no other set is tried.

The QUIC path reads the same (L1312), but only TLS was measured. So does a plain domain that two sets list, one of them limited to a port. #686 describes the same shape for the TLS Version Filter.

Direction

Give the match the port, so that a set whose port filter leaves it out is not a candidate. MatchSNIWithSourceTLS already takes filters (SetFilter), which the client scope uses.

To check:

  • Other callers ask for the set of a name too, and not all of them know a port (src/nfq/hosthint.go, src/nfq/ipbans.go).
  • A connection that passes unchanged today, because the first set's port filter leaves it out, would go to the next set. #686 asks for the same decision.

In the docs, both languages: until this is fixed, the section should say to place the catch-all last. regexp: names of other sets then behave like their plain domains: they stay with their own set, and go over DoH only when that set has a resolver.

Found while working on #672, which leaves this as it was.