diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs index ad97d246..ceffc0f1 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs @@ -306,8 +306,8 @@ public partial class CoreConfigSingboxService } var rules = JsonUtils.Deserialize>(routing.RuleSet) ?? []; - var expectedIPCidr = new List(); - var expectedIPsRegions = new List(); + var expectedIPCidr = new HashSet(); + var expectedIPsRegions = new HashSet(); var regionName = string.Empty; if (!string.IsNullOrEmpty(simpleDnsItem?.DirectExpectedIPs)) @@ -316,7 +316,7 @@ public partial class CoreConfigSingboxService .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .Where(s => !string.IsNullOrEmpty(s)) - .ToList(); + .ToHashSet(); foreach (var ip in ipItems) { @@ -374,11 +374,11 @@ public partial class CoreConfigSingboxService rule4ExpectedIPs.geosite = regionGeosite; if (expectedIPsRegions.Count > 0) { - rule4ExpectedIPs.geoip = expectedIPsRegions; + rule4ExpectedIPs.geoip = expectedIPsRegions.ToList(); } if (expectedIPCidr.Count > 0) { - rule4ExpectedIPs.ip_cidr = expectedIPCidr; + rule4ExpectedIPs.ip_cidr = expectedIPCidr.ToList(); } _coreConfig.dns.rules.Add(rule4ExpectedIPs); } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs index dbb5b5e8..ff8e5df2 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs @@ -159,16 +159,16 @@ public partial class CoreConfigV2rayService var directDNSAddress = ParseDnsAddresses(simpleDNSItem?.DirectDNS, Global.DomainDirectDNSAddress.First()); var remoteDNSAddress = ParseDnsAddresses(simpleDNSItem?.RemoteDNS, Global.DomainRemoteDNSAddress.First()); - var directDomainList = new List(); - var directGeositeList = new List(); - var proxyDomainList = new List(); - var proxyGeositeList = new List(); - var expectedDomainList = new List(); - var expectedIPs = new List(); + var directDomainList = new HashSet(); + var directGeositeList = new HashSet(); + var proxyDomainList = new HashSet(); + var proxyGeositeList = new HashSet(); + var expectedDomainList = new HashSet(); + var expectedIPs = new HashSet(); var regionName = string.Empty; var bootstrapDNSAddress = ParseDnsAddresses(simpleDNSItem?.BootstrapDNS, Global.DomainPureIPDNSAddress.First()); - var dnsServerDomains = new List(); + var dnsServerDomains = new HashSet(); foreach (var dns in directDNSAddress) { @@ -194,7 +194,6 @@ public partial class CoreConfigV2rayService dnsServerDomains.Add($"full:{domain}"); } } - dnsServerDomains = dnsServerDomains.Distinct().ToList(); if (!string.IsNullOrEmpty(simpleDNSItem?.DirectExpectedIPs)) { @@ -202,7 +201,7 @@ public partial class CoreConfigV2rayService .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .Where(s => !string.IsNullOrEmpty(s)) - .ToList(); + .ToHashSet(); foreach (var region in from ip in expectedIPs where ip.StartsWith(Global.GeoIPPrefix, StringComparison.OrdinalIgnoreCase) @@ -269,15 +268,19 @@ public partial class CoreConfigV2rayService } } - if (context.ProtectDomainList.Count > 0) - { - directDomainList.AddRange(context.ProtectDomainList); - } - dnsItem.servers ??= []; var directDnsTagIndex = 1; + if (dnsServerDomains.Count > 0) + { + AddDnsServers(bootstrapDNSAddress, dnsServerDomains); + } + if (context.ProtectDomainList.Count > 0) + { + AddDnsServers(directDNSAddress, context.ProtectDomainList, true); + } + if (simpleDNSItem.FakeIP == true) { var fakeIPMatchDomain = new HashSet(proxyDomainList); @@ -291,7 +294,7 @@ public partial class CoreConfigV2rayService if (fakeIPMatchDomain.Count > 0) { GenFakeDns(); - AddDnsServers(["fakedns"], fakeIPMatchDomain.ToList()); + AddDnsServers(["fakedns"], fakeIPMatchDomain); } } @@ -300,10 +303,6 @@ public partial class CoreConfigV2rayService AddDnsServers(remoteDNSAddress, proxyGeositeList); AddDnsServers(directDNSAddress, directGeositeList, true); AddDnsServers(directDNSAddress, expectedDomainList, true, expectedIPs); - if (dnsServerDomains.Count > 0) - { - AddDnsServers(bootstrapDNSAddress, dnsServerDomains); - } var useDirectDns = false; @@ -345,7 +344,7 @@ public partial class CoreConfigV2rayService return addresses.Count > 0 ? addresses : new List { defaultAddress }; } - static DnsServer4Ray CreateDnsServer(string dnsAddress, List domains, List? expectedIPs = null) + static DnsServer4Ray CreateDnsServer(string dnsAddress, HashSet domains, HashSet? expectedIPs = null) { var (domain, scheme, port, path) = Utils.ParseUrl(dnsAddress); var domainFinal = dnsAddress; @@ -365,13 +364,13 @@ public partial class CoreConfigV2rayService address = domainFinal, port = portFinal, skipFallback = true, - domains = domains.Count > 0 ? domains : null, - expectedIPs = expectedIPs?.Count > 0 ? expectedIPs : null + domains = domains.Count > 0 ? domains.ToList() : null, + expectedIPs = expectedIPs?.Count > 0 ? expectedIPs.ToList() : null }; return dnsServer; } - void AddDnsServers(List dnsAddresses, List domains, bool isDirectDns = false, List? expectedIPs = null) + void AddDnsServers(List dnsAddresses, HashSet domains, bool isDirectDns = false, HashSet? expectedIPs = null) { if (domains.Count <= 0) {