From cddf88730f570f8249f81d9da68c69932e6f336b Mon Sep 17 00:00:00 2001 From: DHR60 Date: Tue, 26 Aug 2025 17:34:12 +0800 Subject: [PATCH] Fix dns (#7834) --- .../CoreConfig/Singbox/SingboxDnsService.cs | 52 ++++++++----------- .../CoreConfig/V2ray/V2rayDnsService.cs | 48 +++++++---------- 2 files changed, 39 insertions(+), 61 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs index 15f2c43b..2f0f8015 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs @@ -80,25 +80,31 @@ public partial class CoreConfigSingboxService hostsDns.predefined = Global.PredefinedHosts; } + if (simpleDNSItem.UseSystemHosts == true) + { + var systemHosts = Utils.GetSystemHosts(); + if (systemHosts != null && systemHosts.Count > 0) + { + foreach (var host in systemHosts) + { + hostsDns.predefined.TryAdd(host.Key, new List { host.Value }); + } + } + } + if (!simpleDNSItem.Hosts.IsNullOrEmpty()) { - var userHostsMap = simpleDNSItem.Hosts? + var userHostsMap = simpleDNSItem.Hosts .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) - .Where(line => !string.IsNullOrWhiteSpace(line)) - .Where(line => line.Contains(' ')) + .Select(line => line.Trim()) + .Where(line => !string.IsNullOrWhiteSpace(line) && line.Contains(' ')) + .Select(line => line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)) + .Where(parts => parts.Length >= 2) + .GroupBy(parts => parts[0]) .ToDictionary( - line => - { - var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - return parts[0]; - }, - line => - { - var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - var values = parts.Skip(1).ToList(); - return values; - } - ) ?? new Dictionary>(); + group => group.Key, + group => group.SelectMany(parts => parts.Skip(1)).ToList() + ); foreach (var kvp in userHostsMap) { @@ -106,22 +112,6 @@ public partial class CoreConfigSingboxService } } - if (simpleDNSItem.UseSystemHosts == true) - { - var systemHosts = Utils.GetSystemHosts(); - if (systemHosts.Count > 0) - { - foreach (var host in systemHosts) - { - if (hostsDns.predefined[host.Key] != null) - { - continue; - } - hostsDns.predefined[host.Key] = new List { host.Value }; - } - } - } - foreach (var host in hostsDns.predefined) { if (finalDns.server == host.Key) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs index 9faf4df3..6f64e923 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs @@ -248,43 +248,31 @@ public partial class CoreConfigV2rayService if (simpleDNSItem.UseSystemHosts == true) { var systemHosts = Utils.GetSystemHosts(); - if (systemHosts.Count > 0) + var normalHost = v2rayConfig?.dns?.hosts; + + if (normalHost != null && systemHosts?.Count > 0) { - var normalHost = v2rayConfig.dns.hosts; - if (normalHost != null) + foreach (var host in systemHosts) { - foreach (var host in systemHosts) - { - if (normalHost[host.Key] != null) - { - continue; - } - normalHost[host.Key] = new List { host.Value }; - } + normalHost.TryAdd(host.Key, new List { host.Value }); } } } - var userHostsMap = simpleDNSItem.Hosts? - .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) - .Where(line => !string.IsNullOrWhiteSpace(line)) - .Where(line => line.Contains(' ')) - .ToDictionary( - line => - { - var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - return parts[0]; - }, - line => - { - var parts = line.Trim().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); - var values = parts.Skip(1).ToList(); - return values; - } - ); - - if (userHostsMap != null) + if (!simpleDNSItem.Hosts.IsNullOrEmpty()) { + var userHostsMap = simpleDNSItem.Hosts + .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) + .Select(line => line.Trim()) + .Where(line => !string.IsNullOrWhiteSpace(line) && line.Contains(' ')) + .Select(line => line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)) + .Where(parts => parts.Length >= 2) + .GroupBy(parts => parts[0]) + .ToDictionary( + group => group.Key, + group => group.SelectMany(parts => parts.Skip(1)).ToList() + ); + foreach (var kvp in userHostsMap) { v2rayConfig.dns.hosts[kvp.Key] = kvp.Value;