From 9d95143d85ade850f7014e5193bd896d860237d8 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 11 Apr 2025 20:55:15 +0800 Subject: [PATCH] Refactors DNS address parsing --- .../CoreConfig/CoreConfigSingboxService.cs | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs index f417ac1d..815ab873 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/CoreConfigSingboxService.cs @@ -1453,45 +1453,50 @@ public class CoreConfigSingboxService var localDnsAddress = string.IsNullOrEmpty(dNSItem?.DomainDNSAddress) ? Global.SingboxDomainDNSAddress.FirstOrDefault() : dNSItem?.DomainDNSAddress; string? localDnsType = null; string? dhcpDnsInterface = null; + var dnsProtocols = new List + { + "dhcp", + "https", + "tcp", + "tls", + "quic", + "h3", + "udp" + }; if (localDnsAddress == "local") { localDnsType = "local"; localDnsAddress = null; } - else if (localDnsAddress.StartsWith("dhcp") && localDnsAddress.Length > 7) + else if (dnsProtocols.Any(protocol => localDnsAddress.StartsWith(protocol))) { - localDnsType = "dhcp"; - // dhcp:// - dhcpDnsInterface = localDnsAddress.Substring(7); - if (dhcpDnsInterface == "auto") + var protocol = dnsProtocols.First(p => localDnsAddress.StartsWith(p)); + localDnsType = protocol; + // +3 for "://" + if (localDnsAddress.Length > protocol.Length + 3) { - dhcpDnsInterface = null; + localDnsAddress = localDnsAddress.Substring(protocol.Length + 3); + if (protocol == "dhcp") + { + dhcpDnsInterface = localDnsAddress; + if (dhcpDnsInterface == "auto") + { + dhcpDnsInterface = null; + } + localDnsAddress = null; + } + else if (protocol is "https" or "h3") + { + if (localDnsAddress.Contains('/')) + { + localDnsAddress = localDnsAddress.Substring(0, localDnsAddress.IndexOf('/')); + } + } + } + else + { + localDnsAddress = null; } - localDnsAddress = null; - } - else if (localDnsAddress.StartsWith("tcp") && localDnsAddress.Length > 6) - { - localDnsType = "tcp"; - // tcp:// - localDnsAddress = localDnsAddress.Substring(6); - } - else if (localDnsAddress.StartsWith("tls") && localDnsAddress.Length > 6) - { - localDnsType = "tls"; - // tls:// - localDnsAddress = localDnsAddress.Substring(6); - } - else if (localDnsAddress.StartsWith("https") && localDnsAddress.Length > 8) - { - localDnsType = "https"; - // https:// - localDnsAddress = localDnsAddress.Substring(8); - } - else if (localDnsAddress.StartsWith("quic") && localDnsAddress.Length > 7) - { - localDnsType = "quic"; - // quic:// - localDnsAddress = localDnsAddress.Substring(7); } else {