diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs index a0d172ba..3a7cb189 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Singbox/SingboxDnsService.cs @@ -298,7 +298,7 @@ public partial class CoreConfigSingboxService var rules = JsonUtils.Deserialize>(routing.RuleSet) ?? []; var expectedIPCidr = new List(); var expectedIPsRegions = new List(); - var regionNames = new HashSet(); + var regionName = string.Empty; if (!string.IsNullOrEmpty(simpleDnsItem?.DirectExpectedIPs)) { @@ -313,13 +313,13 @@ public partial class CoreConfigSingboxService if (ip.StartsWith("geoip:", StringComparison.OrdinalIgnoreCase)) { var region = ip["geoip:".Length..]; - if (!string.IsNullOrEmpty(region)) + if (string.IsNullOrEmpty(region)) { - expectedIPsRegions.Add(region); - regionNames.Add(region); - regionNames.Add($"geolocation-{region}"); - regionNames.Add($"tld-{region}"); + continue; } + + expectedIPsRegions.Add(region); + regionName = region; } else { @@ -352,19 +352,24 @@ public partial class CoreConfigSingboxService rule.server = Global.SingboxDirectDNSTag; rule.strategy = Utils.DomainStrategy4Sbox(simpleDnsItem.Strategy4Freedom); - if (expectedIPsRegions.Count > 0 && rule.geosite?.Count > 0) + if (expectedIPsRegions.Count > 0 && rule.geosite?.Count > 0 && !regionName.IsNullOrEmpty()) { - var geositeSet = new HashSet(rule.geosite); - if (regionNames.Intersect(geositeSet).Any()) + var regionGeosite = rule.geosite.Where(g => g.EndsWith($"-{regionName}", StringComparison.OrdinalIgnoreCase) + || g.EndsWith($"@{regionName}", StringComparison.OrdinalIgnoreCase)).ToList(); + if (regionGeosite.Count > 0) { + rule.geosite.RemoveAll(regionGeosite.Contains); + var rule4ExpectedIPs = JsonUtils.DeepCopy(rule); + rule4ExpectedIPs.geosite = regionGeosite; if (expectedIPsRegions.Count > 0) { - rule.geoip = expectedIPsRegions; + rule4ExpectedIPs.geoip = expectedIPsRegions; } if (expectedIPCidr.Count > 0) { - rule.ip_cidr = expectedIPCidr; + rule4ExpectedIPs.ip_cidr = expectedIPCidr; } + _coreConfig.dns.rules.Add(rule4ExpectedIPs); } } } diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs index 7faae5b1..0bfed021 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayDnsService.cs @@ -121,7 +121,7 @@ public partial class CoreConfigV2rayService var proxyGeositeList = new List(); var expectedDomainList = new List(); var expectedIPs = new List(); - var regionNames = new HashSet(); + var regionName = string.Empty; var bootstrapDNSAddress = ParseDnsAddresses(simpleDNSItem?.BootstrapDNS, Global.DomainPureIPDNSAddress.First()); var dnsServerDomains = new List(); @@ -160,18 +160,14 @@ public partial class CoreConfigV2rayService .Where(s => !string.IsNullOrEmpty(s)) .ToList(); - foreach (var ip in expectedIPs) + foreach (var region in from ip in expectedIPs + where ip.StartsWith("geoip:", StringComparison.OrdinalIgnoreCase) + select ip["geoip:".Length..] + into region + where !string.IsNullOrEmpty(region) + select region) { - if (ip.StartsWith("geoip:", StringComparison.OrdinalIgnoreCase)) - { - var region = ip["geoip:".Length..]; - if (!string.IsNullOrEmpty(region)) - { - regionNames.Add($"geosite:{region}"); - regionNames.Add($"geosite:geolocation-{region}"); - regionNames.Add($"geosite:tld-{region}"); - } - } + regionName = region; } } @@ -203,7 +199,11 @@ public partial class CoreConfigV2rayService { if (normalizedDomain.StartsWith("geosite:") || normalizedDomain.StartsWith("ext:")) { - (regionNames.Contains(normalizedDomain) ? expectedDomainList : directGeositeList).Add(normalizedDomain); + var isExpectedDomain = !regionName.IsNullOrEmpty() + || normalizedDomain.EndsWith($"-{regionName}") + || normalizedDomain.EndsWith($"@{regionName}"); + var targetList = isExpectedDomain ? expectedDomainList : directGeositeList; + targetList.Add(normalizedDomain); } else {