Skip to content

Add DNS routing#8841

Merged
2dust merged 1 commit into2dust:masterfrom
DHR60:fix3
Mar 1, 2026
Merged

Add DNS routing#8841
2dust merged 1 commit into2dust:masterfrom
DHR60:fix3

Conversation

@DHR60
Copy link
Contributor

@DHR60 DHR60 commented Feb 25, 2026

  • 最终路由规则
  • DNS 直连保护规则

@DHR60 DHR60 changed the title Fix3 Fix routing Feb 25, 2026
@DHR60 DHR60 force-pushed the fix3 branch 2 times, most recently from 780ba84 to 5548511 Compare February 26, 2026 06:03
@2dust
Copy link
Owner

2dust commented Feb 26, 2026

在现有内置路由规则集下, 下面的是黑名单产生的规则,是否有点问题?
是否不应该由代码添加最终路由?


 {
        "type": "field",
        "port": "0-65535",
        "outboundTag": "direct"
      },
      {
        "type": "field",
        "network": "tcp,udp",
        "outboundTag": "proxy"
      },
      {
        "type": "field",
        "inboundTag": [
          "direct-dns-1",
          "direct-dns-2"
        ],
        "outboundTag": "direct"
      },
      {
        "type": "field",
        "inboundTag": [
          "dns-module"
        ],
        "outboundTag": "proxy"
      }

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

内置的规则集虽然叫黑白名单,但是实际上个人的路由规则可能不同,确实不应该改变

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

规则生成上确实有点 bug,我修下

内置的规则集虽然叫黑白名单,但是实际上个人的路由规则可能不同,确实不应该改变

倒不是改变规则的问题

是如果多出口的话必须指定 balancer 出口,否则全部走 outbounds 第一个节点了就。
包括 DNS 流量

单节点出口跳过这个规则可能确实更合理

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

打算是改成这样

[
  {
    "type": "field",
    "port": "0-65535",
    "outboundTag": "direct"
  },
  {
    "type": "field",
    "inboundTag": [
      "direct-dns-1",
      "direct-dns-2"
    ],
    "outboundTag": "direct"
  },
  {
    "type": "field",
    "inboundTag": [
      "dns-module"
    ],
    "balancerTag": "proxy-round"
  },
  {
    "type": "field",
    "network": "tcp,udp",
    "balancerTag": "proxy-round"
  }
]

可能看上去还是有点怪吧,尤其是 黑名单,dns-module proxy 完全不起作用

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

或者 DNS 直连保护规则提到最前面

但是会导致这个问题:2dust/v2rayNG#4905

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

修成这样了 #8841 (comment)

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

@2dust 这个不合吗?

那所有 Xray 的策略组配置生成都是有问题的,只会走第一个节点

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

还没有看明白,等下一版吧

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

@2dust 这个不合吗?

那所有 Xray 的策略组配置生成都是有问题的,只会走第一个节点

是否简单的修复方式?
实话说 xray 的策略组非常的拉,要不是你 PR 是不会计划做的。。。

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

是否简单的修复方式?

白名单最后添加一个 "network": "tcp,udp" -> proxy?

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

或者 DNS 直连保护规则提到最前面

但是会导致这个问题:2dust/v2rayNG#4905

我是推荐这个,并且和 sing-box 的逻辑相同

正常分流就是应该哪里落地用哪里的 DNS,用自建的 DNS 是不合理的

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

白名单最后添加一个 "network": "tcp,udp" -> proxy?

如果能解决问题,可以这样做。
你的意思是代码添加,还是在规则集里面添加?
如果是代码添加,如何判断且添加在哪里?

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

我认为应该在代码添加,直接 Append 到最后

举个最简单例子,规则集所有规则都删掉。结果策略组直接不能用了。这明显不符合预期

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

可以,能否简单的写一个 PR 过来,针对 策略组 强制添加规则。
只做最简单的,我们可以在里面继续讨论

@DHR60
Copy link
Contributor Author

DHR60 commented Feb 26, 2026

简单的就第一个 commit 吧

12a4699

没问题的话我就开个新 pr

@2dust
Copy link
Owner

2dust commented Feb 26, 2026

简单的就第一个 commit 吧

12a4699

没问题的话我就开个新 pr

行, 把策略组的路由 bug 修复下先

@DHR60 DHR60 changed the title Fix routing Add DNS routing Feb 27, 2026
@DHR60
Copy link
Contributor Author

DHR60 commented Feb 28, 2026

翻了下我一年前提交给 v2rayNG 的 pr ,想起来为什么要 加 DNS routing 了
2dust/v2rayNG#4838

对于从 DNS 模块发出的路由会绕过 IP 规则,但是 IPIfNonMatch 的最终规则是 0.0.0.0/0 的 IP 匹配,这就导致远程 DNS 匹配不到任何规则,回落到 outbounds 第一个节点,但如果第一个节点恰好失效或延迟很高,那整个远程 DNS 模块都无法正常查询。

总而言之,如果不加的话,所有 Xray 的策略组配置的远程 DNS 路由都是有问题的

@DHR60 DHR60 mentioned this pull request Mar 1, 2026
@2dust 2dust requested a review from Copilot March 1, 2026 06:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds DNS routing support to the V2ray/Xray config generation so that “direct DNS” servers are explicitly routed through the direct outbound, providing stronger protection for direct/whitelisted DNS resolution paths.

Changes:

  • Add tagging for “direct DNS” server entries and generate a corresponding routing rule to force those tagged DNS queries through the direct outbound.
  • Adjust DNS server generation so direct DNS servers can be emitted as structured server objects (with tags) when needed.
  • Introduce Global.DirectDnsTag and simplify DnsServer4Ray by removing unused properties.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs Tags direct DNS servers and adds routing rules to ensure direct-DNS queries egress via direct.
v2rayN/ServiceLib/Models/V2rayConfig.cs Removes unused DNS server model fields, keeping only properties used by current generation logic.
v2rayN/ServiceLib/Global.cs Adds DirectDnsTag constant used for tagging and routing matching.
Comments suppressed due to low confidence (2)

v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs:271

  • JsonSerializerOptions is being instantiated inside the foreach loop. Consider reusing a single options instance (e.g., a local static/readonly variable) to avoid repeated allocations and ensure consistent serialization settings across all DNS server entries.
                var dnsServer = CreateDnsServer(dns, []);
                dnsServer.tag = $"{Global.DirectDnsTag}-{directDnsTagIndex++}";
                dnsServer.skipFallback = false;
                dnsItem.servers.Add(JsonUtils.SerializeToNode(dnsServer,
                    new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }));
            }

v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs:327

  • JsonSerializerOptions is created for every DNS server added. Consider caching/reusing a single options instance for this method to reduce per-server allocations (especially when domain lists are large).
                var dnsServer = CreateDnsServer(dnsAddress, domains, expectedIPs);
                if (isDirectDns)
                {
                    dnsServer.tag = $"{Global.DirectDnsTag}-{directDnsTagIndex++}";
                }
                var dnsServerNode = JsonUtils.SerializeToNode(dnsServer,
                    new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
                dnsItem.servers.Add(dnsServerNode);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@2dust 2dust merged commit 99d67ca into 2dust:master Mar 1, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants