pull/7852/head
DHR60 2025-08-26 17:34:12 +08:00 committed by GitHub
parent 3eb49aa24c
commit cddf88730f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 61 deletions

View File

@ -80,25 +80,31 @@ public partial class CoreConfigSingboxService
hostsDns.predefined = Global.PredefinedHosts; 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<string> { host.Value });
}
}
}
if (!simpleDNSItem.Hosts.IsNullOrEmpty()) if (!simpleDNSItem.Hosts.IsNullOrEmpty())
{ {
var userHostsMap = simpleDNSItem.Hosts? var userHostsMap = simpleDNSItem.Hosts
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Where(line => !string.IsNullOrWhiteSpace(line)) .Select(line => line.Trim())
.Where(line => line.Contains(' ')) .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( .ToDictionary(
line => group => group.Key,
{ group => group.SelectMany(parts => parts.Skip(1)).ToList()
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<string, List<string>>();
foreach (var kvp in userHostsMap) 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<string> { host.Value };
}
}
}
foreach (var host in hostsDns.predefined) foreach (var host in hostsDns.predefined)
{ {
if (finalDns.server == host.Key) if (finalDns.server == host.Key)

View File

@ -248,43 +248,31 @@ public partial class CoreConfigV2rayService
if (simpleDNSItem.UseSystemHosts == true) if (simpleDNSItem.UseSystemHosts == true)
{ {
var systemHosts = Utils.GetSystemHosts(); var systemHosts = Utils.GetSystemHosts();
if (systemHosts.Count > 0) var normalHost = v2rayConfig?.dns?.hosts;
{
var normalHost = v2rayConfig.dns.hosts; if (normalHost != null && systemHosts?.Count > 0)
if (normalHost != null)
{ {
foreach (var host in systemHosts) foreach (var host in systemHosts)
{ {
if (normalHost[host.Key] != null) normalHost.TryAdd(host.Key, new List<string> { host.Value });
{
continue;
}
normalHost[host.Key] = new List<string> { host.Value };
}
} }
} }
} }
var userHostsMap = simpleDNSItem.Hosts? if (!simpleDNSItem.Hosts.IsNullOrEmpty())
{
var userHostsMap = simpleDNSItem.Hosts
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Where(line => !string.IsNullOrWhiteSpace(line)) .Select(line => line.Trim())
.Where(line => line.Contains(' ')) .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( .ToDictionary(
line => group => group.Key,
{ group => group.SelectMany(parts => parts.Skip(1)).ToList()
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)
{
foreach (var kvp in userHostsMap) foreach (var kvp in userHostsMap)
{ {
v2rayConfig.dns.hosts[kvp.Key] = kvp.Value; v2rayConfig.dns.hosts[kvp.Key] = kvp.Value;