From dcc9c9fa140404b03dd7bb1cc16f7421f826a54c Mon Sep 17 00:00:00 2001 From: DHR60 Date: Thu, 14 Aug 2025 17:32:48 +0800 Subject: [PATCH] Fixes DNS (#7757) * Adds properties * Adds DNS routing --- v2rayN/ServiceLib/Global.cs | 1 + v2rayN/ServiceLib/Models/V2rayConfig.cs | 15 +++++++- .../CoreConfig/CoreConfigV2rayService.cs | 35 +++++++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/v2rayN/ServiceLib/Global.cs b/v2rayN/ServiceLib/Global.cs index 3e7b8858..ffab8d77 100644 --- a/v2rayN/ServiceLib/Global.cs +++ b/v2rayN/ServiceLib/Global.cs @@ -48,6 +48,7 @@ public class Global public const string ProxyTag = "proxy"; public const string DirectTag = "direct"; public const string BlockTag = "block"; + public const string DnsTag = "dns-module"; public const string StreamSecurity = "tls"; public const string StreamSecurityReality = "reality"; public const string Loopback = "127.0.0.1"; diff --git a/v2rayN/ServiceLib/Models/V2rayConfig.cs b/v2rayN/ServiceLib/Models/V2rayConfig.cs index 354e33dc..702bbae1 100644 --- a/v2rayN/ServiceLib/Models/V2rayConfig.cs +++ b/v2rayN/ServiceLib/Models/V2rayConfig.cs @@ -203,8 +203,15 @@ public class Response4Ray public class Dns4Ray { - public Dictionary>? hosts { get; set; } + public Dictionary? hosts { get; set; } public List servers { get; set; } + public string? clientIp { get; set; } + public string? queryStrategy { get; set; } + public bool? disableCache { get; set; } + public bool? disableFallback { get; set; } + public bool? disableFallbackIfMatch { get; set; } + public bool? useSystemHosts { get; set; } + public string? tag { get; set; } } public class DnsServer4Ray @@ -214,6 +221,12 @@ public class DnsServer4Ray public bool? skipFallback { get; set; } public List? expectedIPs { get; set; } public List? unexpectedIPs { get; set; } + public string? clientIp { get; set; } + public string? queryStrategy { get; set; } + public int? timeoutMs { get; set; } + public bool? disableCache { get; set; } + public bool? finalQuery { get; set; } + public string? tag { get; set; } } public class Routing4Ray diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs index e6784db2..62bf2b22 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigV2rayService.cs @@ -1143,7 +1143,21 @@ public class CoreConfigV2rayService var item = await AppHandler.Instance.GetDNSItem(ECoreType.Xray); if (item != null && item.Enabled == true) { - return await GenDnsCompatible(node, v2rayConfig); + var result = await GenDnsCompatible(node, v2rayConfig); + + if (v2rayConfig.routing.domainStrategy == "IPIfNonMatch") + { + // DNS routing + v2rayConfig.dns.tag = Global.DnsTag; + v2rayConfig.routing.rules.Add(new RulesItem4Ray + { + type = "field", + inboundTag = new List { Global.DnsTag }, + outboundTag = Global.ProxyTag, + }); + } + + return result; } var simpleDNSItem = _config.SimpleDNSItem; var domainStrategy4Freedom = simpleDNSItem?.RayStrategy4Freedom; @@ -1164,6 +1178,18 @@ public class CoreConfigV2rayService await GenDnsServers(node, v2rayConfig, simpleDNSItem); await GenDnsHosts(v2rayConfig, simpleDNSItem); + + if (v2rayConfig.routing.domainStrategy == "IPIfNonMatch") + { + // DNS routing + v2rayConfig.dns.tag = Global.DnsTag; + v2rayConfig.routing.rules.Add(new RulesItem4Ray + { + type = "field", + inboundTag = new List { Global.DnsTag }, + outboundTag = Global.ProxyTag, + }); + } } catch (Exception ex) { @@ -1340,10 +1366,13 @@ public class CoreConfigV2rayService return await Task.FromResult(0); } v2rayConfig.dns ??= new Dns4Ray(); - v2rayConfig.dns.hosts ??= new Dictionary>(); + v2rayConfig.dns.hosts ??= new Dictionary(); if (simpleDNSItem.AddCommonHosts == true) { - v2rayConfig.dns.hosts = Global.PredefinedHosts; + v2rayConfig.dns.hosts = Global.PredefinedHosts.ToDictionary( + kvp => kvp.Key, + kvp => (object)kvp.Value + ); } if (simpleDNSItem.UseSystemHosts == true)