From 917efefd5a2ddb971db58201bdff3959f25ea4f6 Mon Sep 17 00:00:00 2001 From: Desaichk <127425055+Desai0@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:06:41 +0300 Subject: [PATCH] feat(dns): expose Xray fallback restriction for matched domains (#10602) --- .../CoreConfig/V2ray/DnsFallbackTests.cs | 107 ++++++++++++++++++ .../ServiceLib/Models/Configs/ConfigItems.cs | 1 + .../Models/CoreConfigs/V2rayConfig.cs | 1 + v2rayN/ServiceLib/Resx/ResUI.Designer.cs | 18 +++ v2rayN/ServiceLib/Resx/ResUI.resx | 6 + v2rayN/ServiceLib/Resx/ResUI.ru.resx | 6 + v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx | 6 + .../CoreConfig/V2ray/V2rayDnsService.cs | 1 + .../ViewModels/DNSSettingViewModel.cs | 3 + .../Views/DNSSettingWindow.axaml | 25 +++- .../Views/DNSSettingWindow.axaml.cs | 2 + v2rayN/v2rayN/Views/DNSSettingWindow.xaml | 24 ++++ v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 2 + 13 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 v2rayN/ServiceLib.Tests/CoreConfig/V2ray/DnsFallbackTests.cs diff --git a/v2rayN/ServiceLib.Tests/CoreConfig/V2ray/DnsFallbackTests.cs b/v2rayN/ServiceLib.Tests/CoreConfig/V2ray/DnsFallbackTests.cs new file mode 100644 index 00000000..1d2a29b5 --- /dev/null +++ b/v2rayN/ServiceLib.Tests/CoreConfig/V2ray/DnsFallbackTests.cs @@ -0,0 +1,107 @@ +namespace ServiceLib.Tests.CoreConfig.V2ray; + +public class DnsFallbackTests +{ + [Test] + [Arguments(null, false, false)] + [Arguments(false, false, false)] + [Arguments(true, false, false)] + [Arguments(null, true, false)] + [Arguments(false, true, false)] + [Arguments(true, true, false)] + [Arguments(null, false, true)] + [Arguments(false, false, true)] + [Arguments(true, false, true)] + [Arguments(null, true, true)] + [Arguments(false, true, true)] + [Arguments(true, true, true)] + public async Task SimpleDns_ShouldPersistAndApplyFallbackChoice(bool? disabled, bool parallel, bool tun) + { + var config = CoreConfigTestFactory.CreateConfig(); + config.SimpleDNSItem.DisableFallbackIfMatch = disabled; + config.SimpleDNSItem.ParallelQuery = parallel; + config.SimpleDNSItem.DirectDNS = "8.8.8.8,8.8.4.4"; + config.SimpleDNSItem.RemoteDNS = "1.1.1.1"; + config.TunModeItem.EnableTun = tun; + config = JsonUtils.Deserialize(JsonUtils.Serialize(config))!; + await config.SimpleDNSItem.DisableFallbackIfMatch.Should().BeEqualTo(disabled); + CoreConfigTestFactory.BindAppManagerConfig(config); + var node = CoreConfigTestFactory.CreateSocksNode(ECoreType.Xray); + var context = CoreConfigTestFactory.CreateContext(config, node, ECoreType.Xray) with + { + RoutingItem = new RoutingItem + { + DomainStrategy = Global.AsIs, + RuleSet = JsonUtils.Serialize(new List + { + new() { RuleType = ERuleType.DNS, Domain = ["domain:direct.example"], OutboundTag = Global.DirectTag }, + }), + }, + }; + + var result = new CoreConfigV2rayService(context).GenerateClientConfigContent(); + + await result.Success.Should().BeTrue(); + var dns = JsonNode.Parse(result.Data!.ToString()!)!["dns"]!; + await (dns["disableFallbackIfMatch"]?.GetValue()).Should().BeEqualTo(disabled == true ? true : (bool?)null); + await (dns["enableParallelQuery"]?.GetValue() == true).Should().BeEqualTo(parallel); + var servers = dns["servers"]!.AsArray(); + var matched = servers.OfType().Where(s => s["domains"] != null).ToList(); + await matched.Count.Should().BeEqualTo(2); + await matched.All(s => s["skipFallback"]!.GetValue()).Should().BeTrue(); + await servers.Last()!.GetValue().Should().BeEqualTo("1.1.1.1"); + } + + [Test] + [Arguments(false, true, false)] + [Arguments(true, false, false)] + [Arguments(false, true, true)] + [Arguments(true, false, true)] + public async Task RawDns_ShouldUseItsOwnFallbackChoice(bool simpleChoice, bool rawChoice, bool tun) + { + var config = CoreConfigTestFactory.CreateConfig(); + config.SimpleDNSItem.DisableFallbackIfMatch = simpleChoice; + config.TunModeItem.EnableTun = tun; + CoreConfigTestFactory.BindAppManagerConfig(config); + var raw = new JsonObject + { + ["servers"] = new JsonArray("1.1.1.1"), + ["disableFallbackIfMatch"] = rawChoice, + }.ToJsonString(); + var node = CoreConfigTestFactory.CreateSocksNode(ECoreType.Xray); + var context = CoreConfigTestFactory.CreateContext(config, node, ECoreType.Xray) with + { + RawDnsItem = new DNSItem { Enabled = true, NormalDNS = raw, TunDNS = raw }, + }; + + var result = new CoreConfigV2rayService(context).GenerateClientConfigContent(); + + await result.Success.Should().BeTrue(); + var dns = JsonNode.Parse(result.Data!.ToString()!)!["dns"]!; + await dns["disableFallbackIfMatch"]!.GetValue().Should().BeEqualTo(rawChoice); + } + + [Test] + public async Task ExistingSettings_ShouldKeepFallbackEnabledByDefault() + { + var simple = JsonUtils.Deserialize("{\"DirectDNS\":\"8.8.8.8\",\"RemoteDNS\":\"1.1.1.1\"}")!; + await simple.DisableFallbackIfMatch.Should().BeNull(); + } + + [Test] + public async Task Singbox_ShouldGenerateTheSameDnsForBothFallbackChoices() + { + var config = CoreConfigTestFactory.CreateConfig(ECoreType.sing_box); + CoreConfigTestFactory.BindAppManagerConfig(config); + var node = CoreConfigTestFactory.CreateSocksNode(ECoreType.sing_box); + var context = CoreConfigTestFactory.CreateContext(config, node, ECoreType.sing_box); + var before = new CoreConfigSingboxService(context).GenerateClientConfigContent(); + config.SimpleDNSItem.DisableFallbackIfMatch = true; + var after = new CoreConfigSingboxService(context).GenerateClientConfigContent(); + + await before.Success.Should().BeTrue(); + await after.Success.Should().BeTrue(); + await JsonNode.DeepEquals(JsonNode.Parse(before.Data!.ToString()!)!["dns"], + JsonNode.Parse(after.Data!.ToString()!)!["dns"]).Should().BeTrue(); + } +} diff --git a/v2rayN/ServiceLib/Models/Configs/ConfigItems.cs b/v2rayN/ServiceLib/Models/Configs/ConfigItems.cs index 2747796a..d706c054 100644 --- a/v2rayN/ServiceLib/Models/Configs/ConfigItems.cs +++ b/v2rayN/ServiceLib/Models/Configs/ConfigItems.cs @@ -268,6 +268,7 @@ public class SimpleDNSItem public string? Strategy4ProxyDial { get; set; } public bool? ServeStale { get; set; } public bool? ParallelQuery { get; set; } + public bool? DisableFallbackIfMatch { get; set; } public string? Hosts { get; set; } public string? DirectExpectedIPs { get; set; } public bool? EnableHappyEyeballs { get; set; } diff --git a/v2rayN/ServiceLib/Models/CoreConfigs/V2rayConfig.cs b/v2rayN/ServiceLib/Models/CoreConfigs/V2rayConfig.cs index 6b2c65ac..a98754eb 100644 --- a/v2rayN/ServiceLib/Models/CoreConfigs/V2rayConfig.cs +++ b/v2rayN/ServiceLib/Models/CoreConfigs/V2rayConfig.cs @@ -203,6 +203,7 @@ public class Dns4Ray public List servers { get; set; } public bool? serveStale { get; set; } public bool? enableParallelQuery { get; set; } + public bool? disableFallbackIfMatch { get; set; } public string? queryStrategy { get; set; } public string? tag { get; set; } } diff --git a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs index a57dd707..2370b5f8 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.Designer.cs +++ b/v2rayN/ServiceLib/Resx/ResUI.Designer.cs @@ -3636,6 +3636,24 @@ namespace ServiceLib.Resx { } } + /// + /// Looks up the localized DNS fallback setting label. + /// + public static string TbDisableFallbackIfMatch { + get { + return ResourceManager.GetString("TbDisableFallbackIfMatch", resourceCulture); + } + } + + /// + /// Looks up the localized DNS fallback setting explanation. + /// + public static string TbDisableFallbackIfMatchTip { + get { + return ResourceManager.GetString("TbDisableFallbackIfMatchTip", resourceCulture); + } + } + /// /// 查找类似 Parallel Query 的本地化字符串。 /// diff --git a/v2rayN/ServiceLib/Resx/ResUI.resx b/v2rayN/ServiceLib/Resx/ResUI.resx index 5c7ec11d..b1db22dd 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.resx @@ -1974,4 +1974,10 @@ The "Get Certificate" action may fail if a self-signed certificate is used or if XDRIVE remote folder + + Restrict DNS fallback on domain match (Xray) + + + Use only DNS servers with matching domain rules, including bootstrap. If all fail, resolution fails. Other domains use the default DNS servers. + diff --git a/v2rayN/ServiceLib/Resx/ResUI.ru.resx b/v2rayN/ServiceLib/Resx/ResUI.ru.resx index 79bdcf1f..295f056d 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.ru.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.ru.resx @@ -1974,4 +1974,10 @@ Удаленная папка XDRIVE + + Ограничить резервные DNS при совпадении домена (Xray) + + + Только DNS с совпавшими доменными правилами, включая bootstrap. Если все они не сработают, разрешение завершится ошибкой. Для остальных доменов — DNS по умолчанию. + diff --git a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx index 27c68c37..cb95c4da 100644 --- a/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx @@ -1966,4 +1966,10 @@ XDRIVE 远端目录 + + 域名匹配时限制 DNS 回退(Xray) + + + 仅使用域名规则匹配的 DNS(包括引导 DNS)。若全部失败,解析失败。其他域名使用默认 DNS。 + diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs index ccccd8aa..c287cb0b 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs @@ -81,6 +81,7 @@ public partial class CoreConfigV2rayService dnsItem.serveStale = simpleDnsItem?.ServeStale is true ? true : null; dnsItem.enableParallelQuery = simpleDnsItem?.ParallelQuery is true ? true : null; + dnsItem.disableFallbackIfMatch = simpleDnsItem?.DisableFallbackIfMatch is true ? true : null; if (simpleDnsItem.BlockAAAAQuery == true) { diff --git a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs index f092f1c4..569a370a 100644 --- a/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/DNSSettingViewModel.cs @@ -23,6 +23,7 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable [Reactive] public partial string Hosts { get; set; } [Reactive] public partial string DirectExpectedIPs { get; set; } [Reactive] public partial bool ParallelQuery { get; set; } + [Reactive] public partial bool DisableFallbackIfMatch { get; set; } [Reactive] public partial bool ServeStale { get; set; } [Reactive] public partial bool EnableHappyEyeballs { get; set; } @@ -101,6 +102,7 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable Hosts = item.Hosts ?? string.Empty; DirectExpectedIPs = item.DirectExpectedIPs ?? string.Empty; ParallelQuery = item.ParallelQuery ?? false; + DisableFallbackIfMatch = item.DisableFallbackIfMatch ?? false; ServeStale = item.ServeStale ?? false; EnableHappyEyeballs = item.EnableHappyEyeballs ?? false; var item1 = await ConfigDbQuery.GetDNSItem(ECoreType.Xray); @@ -145,6 +147,7 @@ public partial class DNSSettingViewModel : MyReactiveObject, ICloseable _config.SimpleDNSItem.Hosts = Hosts; _config.SimpleDNSItem.DirectExpectedIPs = DirectExpectedIPs; _config.SimpleDNSItem.ParallelQuery = ParallelQuery; + _config.SimpleDNSItem.DisableFallbackIfMatch = DisableFallbackIfMatch; _config.SimpleDNSItem.ServeStale = ServeStale; _config.SimpleDNSItem.EnableHappyEyeballs = EnableHappyEyeballs; if (NormalDNSCompatible.IsNotEmpty()) diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml index 87488574..065f05eb 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml @@ -42,7 +42,7 @@ x:Name="gridBasicDNSSettings" Margin="{StaticResource Margin8}" ColumnDefinitions="Auto,Auto,*" - RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"> + RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto"> + + + diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs index 7d4b8b57..15cdc461 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs @@ -49,6 +49,8 @@ public partial class DNSSettingWindow : WindowBase this.Bind(ViewModel, vm => vm.Hosts, v => v.txtHosts.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.DirectExpectedIPs, v => v.cmbDirectExpectedIPs.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.ParallelQuery, v => v.togParallelQuery.IsChecked).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.DisableFallbackIfMatch, v => v.togDisableFallbackIfMatch.IsChecked).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.RayCustomDNSEnableCompatible, v => v.togDisableFallbackIfMatch.IsEnabled, enabled => !enabled).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.ServeStale, v => v.togServeStale.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.EnableHappyEyeballs, v => v.togEnableHappyEyeballs.IsChecked).DisposeWith(disposables); diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml index bbe141c4..a34a70ac 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml @@ -54,6 +54,7 @@ + @@ -264,6 +265,29 @@ Style="{StaticResource ToolbarTextBlock}" Text="{x:Static resx:ResUI.TbEnableHappyEyeballsTip}" TextWrapping="Wrap" /> + + + diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index 52e79907..d5390138 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -45,6 +45,8 @@ public partial class DNSSettingWindow this.Bind(ViewModel, vm => vm.Hosts, v => v.txtHosts.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.DirectExpectedIPs, v => v.cmbDirectExpectedIPs.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.ParallelQuery, v => v.togParallelQuery.IsChecked).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.DisableFallbackIfMatch, v => v.togDisableFallbackIfMatch.IsChecked).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.RayCustomDNSEnableCompatible, v => v.togDisableFallbackIfMatch.IsEnabled, enabled => !enabled).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.ServeStale, v => v.togServeStale.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.EnableHappyEyeballs, v => v.togEnableHappyEyeballs.IsChecked).DisposeWith(disposables);