DHR60 2025-07-12 16:59:52 +08:00
parent dff67e9d78
commit 97bd64ee9a
4 changed files with 107 additions and 54 deletions

View File

@ -245,7 +245,7 @@ public class CoreConfigSingboxService
singboxConfig.route.rules.Add(rule); singboxConfig.route.rules.Add(rule);
} }
await GenDns(singboxConfig); await GenDnsDomains(singboxConfig, _config.DNSItem);
//var dnsServer = singboxConfig.dns?.servers.FirstOrDefault(); //var dnsServer = singboxConfig.dns?.servers.FirstOrDefault();
//if (dnsServer != null) //if (dnsServer != null)
//{ //{
@ -317,7 +317,7 @@ public class CoreConfigSingboxService
await GenOutbound(node, singboxConfig.outbounds.First()); await GenOutbound(node, singboxConfig.outbounds.First());
} }
await GenMoreOutbounds(node, singboxConfig); await GenMoreOutbounds(node, singboxConfig);
await GenDns(singboxConfig); await GenDnsDomains(singboxConfig, _config.DNSItem);
singboxConfig.route.rules.Clear(); singboxConfig.route.rules.Clear();
singboxConfig.inbounds.Clear(); singboxConfig.inbounds.Clear();
@ -1230,8 +1230,7 @@ public class CoreConfigSingboxService
{ {
server = "outbound_resolver", server = "outbound_resolver",
strategy = item.SingboxStrategy4Direct strategy = item.SingboxStrategy4Direct
} };
;
if (_config.TunModeItem.EnableTun) if (_config.TunModeItem.EnableTun)
{ {
@ -1582,7 +1581,7 @@ public class CoreConfigSingboxService
singboxConfig.dns ??= new Dns4Sbox(); singboxConfig.dns ??= new Dns4Sbox();
singboxConfig.dns.independent_cache = true; singboxConfig.dns.independent_cache = true;
singboxConfig.dns.final = "dns_remote"; // TODO singboxConfig.dns.final = "dns_remote"; // TODO: Select fallback DNS server based on routing rules
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1593,8 +1592,7 @@ public class CoreConfigSingboxService
private async Task<int> GenDnsServers(SingboxConfig singboxConfig, DNSItem dNSItem) private async Task<int> GenDnsServers(SingboxConfig singboxConfig, DNSItem dNSItem)
{ {
var finalDns = ParseDnsAddress(dNSItem.SingboxFinalResolveDNS); var finalDns = await GenDnsDomains(singboxConfig, dNSItem);
finalDns.tag = "final_resolver";
var directDns = ParseDnsAddress(dNSItem.DirectDNS); var directDns = ParseDnsAddress(dNSItem.DirectDNS);
directDns.tag = "dns_direct"; directDns.tag = "dns_direct";
@ -1668,7 +1666,6 @@ public class CoreConfigSingboxService
singboxConfig.dns.servers ??= new List<Server4Sbox>(); singboxConfig.dns.servers ??= new List<Server4Sbox>();
singboxConfig.dns.servers.Add(remoteDns); singboxConfig.dns.servers.Add(remoteDns);
singboxConfig.dns.servers.Add(directDns); singboxConfig.dns.servers.Add(directDns);
singboxConfig.dns.servers.Add(finalDns);
singboxConfig.dns.servers.Add(resolverDns); singboxConfig.dns.servers.Add(resolverDns);
singboxConfig.dns.servers.Add(hostsDns); singboxConfig.dns.servers.Add(hostsDns);
@ -1688,6 +1685,16 @@ public class CoreConfigSingboxService
return await Task.FromResult(0); return await Task.FromResult(0);
} }
private async Task<Server4Sbox> GenDnsDomains(SingboxConfig singboxConfig, DNSItem? dNSItem)
{
var finalDns = ParseDnsAddress(dNSItem.SingboxFinalResolveDNS);
finalDns.tag = "final_resolver";
singboxConfig.dns ??= new Dns4Sbox();
singboxConfig.dns.servers ??= new List<Server4Sbox>();
singboxConfig.dns.servers.Add(finalDns);
return await Task.FromResult(finalDns);
}
private async Task<int> GenDnsRules(SingboxConfig singboxConfig, DNSItem dNSItem) private async Task<int> GenDnsRules(SingboxConfig singboxConfig, DNSItem dNSItem)
{ {
singboxConfig.dns ??= new Dns4Sbox(); singboxConfig.dns ??= new Dns4Sbox();
@ -1778,41 +1785,42 @@ public class CoreConfigSingboxService
private static Server4Sbox? ParseDnsAddress(string address) private static Server4Sbox? ParseDnsAddress(string address)
{ {
if (string.IsNullOrEmpty(address)) var addressFirst = address?.Split(address.Contains(',') ? ',' : ';').FirstOrDefault()?.Trim();
if (string.IsNullOrEmpty(addressFirst))
{ {
return null; return null;
} }
var server = new Server4Sbox(); var server = new Server4Sbox();
if (address is "local" or "localhost") if (addressFirst is "local" or "localhost")
{ {
server.type = "local"; server.type = "local";
return server; return server;
} }
if (address.StartsWith("dhcp://", StringComparison.OrdinalIgnoreCase)) if (addressFirst.StartsWith("dhcp://", StringComparison.OrdinalIgnoreCase))
{ {
var interface_name = address.Substring(7); var interface_name = addressFirst.Substring(7);
server.type = "dhcp"; server.type = "dhcp";
server.Interface = interface_name == "auto" ? null : interface_name; server.Interface = interface_name == "auto" ? null : interface_name;
return server; return server;
} }
if (!address.Contains("://")) if (!addressFirst.Contains("://"))
{ {
// udp dns // udp dns
server.type = "udp"; server.type = "udp";
server.server = address; server.server = addressFirst;
return server; return server;
} }
try try
{ {
var protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); var protocolEndIndex = addressFirst.IndexOf("://", StringComparison.Ordinal);
server.type = address.Substring(0, protocolEndIndex).ToLower(); server.type = addressFirst.Substring(0, protocolEndIndex).ToLower();
var uri = new Uri(address); var uri = new Uri(addressFirst);
server.server = uri.Host; server.server = uri.Host;
if (!uri.IsDefaultPort) if (!uri.IsDefaultPort)
@ -1827,11 +1835,11 @@ public class CoreConfigSingboxService
} }
catch (UriFormatException) catch (UriFormatException)
{ {
var protocolEndIndex = address.IndexOf("://", StringComparison.Ordinal); var protocolEndIndex = addressFirst.IndexOf("://", StringComparison.Ordinal);
if (protocolEndIndex > 0) if (protocolEndIndex > 0)
{ {
server.type = address.Substring(0, protocolEndIndex).ToLower(); server.type = addressFirst.Substring(0, protocolEndIndex).ToLower();
var remaining = address.Substring(protocolEndIndex + 3); var remaining = addressFirst.Substring(protocolEndIndex + 3);
var portIndex = remaining.IndexOf(':'); var portIndex = remaining.IndexOf(':');
var pathIndex = remaining.IndexOf('/'); var pathIndex = remaining.IndexOf('/');
@ -1884,7 +1892,8 @@ public class CoreConfigSingboxService
singboxConfig.experimental.cache_file = new CacheFile4Sbox() singboxConfig.experimental.cache_file = new CacheFile4Sbox()
{ {
enabled = true, enabled = true,
path = Utils.GetBinPath("cache.db") path = Utils.GetBinPath("cache.db"),
store_fakeip = _config.DNSItem.FakeIP == true
}; };
} }

View File

@ -1164,6 +1164,32 @@ public class CoreConfigV2rayService
private async Task<int> GenDnsServers(ProfileItem? node, V2rayConfig v2rayConfig, DNSItem dNSItem) private async Task<int> GenDnsServers(ProfileItem? node, V2rayConfig v2rayConfig, DNSItem dNSItem)
{ {
var directDNSAddress = dNSItem?.DirectDNS?
.Split(dNSItem.DirectDNS?.Contains(',') == true ? ',' : ';')
.Select(addr => addr.Trim())
.Where(addr => !string.IsNullOrEmpty(addr))
.Select(addr => addr.StartsWith("dhcp", StringComparison.OrdinalIgnoreCase) ? "localhost" : addr)
.Distinct()
.ToList();
if (directDNSAddress != null && directDNSAddress.Count == 0)
{
directDNSAddress = new() { Global.DomainDirectDNSAddress.FirstOrDefault() };
}
var remoteDNSAddress = dNSItem?.RemoteDNS?
.Split(dNSItem.RemoteDNS?.Contains(',') == true ? ',' : ';')
.Select(addr => addr.Trim())
.Where(addr => !string.IsNullOrEmpty(addr))
.Select(addr => addr.StartsWith("dhcp", StringComparison.OrdinalIgnoreCase) ? "localhost" : addr)
.Distinct()
.ToList();
if (remoteDNSAddress != null && remoteDNSAddress.Count == 0)
{
remoteDNSAddress = new() { Global.DomainRemoteDNSAddress.FirstOrDefault() };
}
var directDomainList = new List<string>(); var directDomainList = new List<string>();
var directGeositeList = new List<string>(); var directGeositeList = new List<string>();
var proxyDomainList = new List<string>(); var proxyDomainList = new List<string>();
@ -1258,45 +1284,63 @@ public class CoreConfigV2rayService
if (proxyDomainList.Count > 0) if (proxyDomainList.Count > 0)
{ {
var dnsServer = new DnsServer4Ray() foreach (var dnsDomain in remoteDNSAddress)
{ {
address = dNSItem.RemoteDNS, var dnsServer = new DnsServer4Ray()
skipFallback = true, {
domains = proxyDomainList address = dnsDomain,
}; skipFallback = true,
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); domains = proxyDomainList
} };
if (proxyGeositeList.Count > 0) v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer));
{ }
var dnsServer = new DnsServer4Ray()
{
address = dNSItem.RemoteDNS,
skipFallback = true,
domains = proxyGeositeList
};
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer));
} }
if (directDomainList.Count > 0) if (directDomainList.Count > 0)
{ {
var dnsServer = new DnsServer4Ray() foreach (var dnsDomain in directDNSAddress)
{ {
address = dNSItem.DirectDNS, var dnsServer = new DnsServer4Ray()
skipFallback = true, {
domains = directDomainList address = dnsDomain,
}; skipFallback = true,
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); domains = directDomainList
};
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer));
}
}
if (proxyGeositeList.Count > 0)
{
foreach (var dnsDomain in remoteDNSAddress)
{
var dnsServer = new DnsServer4Ray()
{
address = dnsDomain,
skipFallback = true,
domains = proxyGeositeList
};
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer));
}
} }
if (directGeositeList.Count > 0) if (directGeositeList.Count > 0)
{ {
var dnsServer = new DnsServer4Ray() foreach (var dnsDomain in directDNSAddress)
{ {
address = dNSItem.DirectDNS, var dnsServer = new DnsServer4Ray()
skipFallback = true, {
domains = directGeositeList address = dnsDomain,
}; skipFallback = true,
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer)); domains = directGeositeList
};
v2rayConfig.dns.servers.Add(JsonUtils.SerializeToNode(dnsServer));
}
}
// fallback DNS server
// TODO: Select fallback DNS server based on routing rules
foreach (var dnsDomain in remoteDNSAddress)
{
v2rayConfig.dns.servers.Add(dnsDomain);
} }
v2rayConfig.dns.servers.Add(dNSItem.RemoteDNS); // fallback DNS server
return await Task.FromResult(0); return await Task.FromResult(0);
} }

View File

@ -21,9 +21,9 @@ public partial class DNSSettingWindow : WindowBase<DNSSettingViewModel>
cmbSBDirectDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out; cmbSBDirectDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out;
cmbSBRemoteDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out; cmbSBRemoteDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out;
cmbDirectDNS.ItemsSource = Global.DomainDirectDNSAddress; cmbDirectDNS.ItemsSource = Global.DomainDirectDNSAddress;
cmbSBResolverDNS.ItemsSource = Global.DomainDirectDNSAddress.Concat(new[] { "dhcp://auto" }); cmbSBResolverDNS.ItemsSource = Global.DomainDirectDNSAddress.Concat(new[] { "dhcp://auto,localhost" });
cmbRemoteDNS.ItemsSource = Global.DomainRemoteDNSAddress; cmbRemoteDNS.ItemsSource = Global.DomainRemoteDNSAddress;
cmbSBFinalResolverDNS.ItemsSource = Global.DomainPureIPDNSAddress; cmbSBFinalResolverDNS.ItemsSource = Global.DomainPureIPDNSAddress.Concat(new[] { "dhcp://auto,localhost" });
this.WhenActivated(disposables => this.WhenActivated(disposables =>
{ {

View File

@ -21,9 +21,9 @@ public partial class DNSSettingWindow
cmbSBDirectDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out; cmbSBDirectDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out;
cmbSBRemoteDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out; cmbSBRemoteDNSStrategy.ItemsSource = Global.SingboxDomainStrategy4Out;
cmbDirectDNS.ItemsSource = Global.DomainDirectDNSAddress; cmbDirectDNS.ItemsSource = Global.DomainDirectDNSAddress;
cmbSBResolverDNS.ItemsSource = Global.DomainDirectDNSAddress.Concat(new[] { "dhcp://auto" }); cmbSBResolverDNS.ItemsSource = Global.DomainDirectDNSAddress.Concat(new[] { "dhcp://auto,localhost" });
cmbRemoteDNS.ItemsSource = Global.DomainRemoteDNSAddress; cmbRemoteDNS.ItemsSource = Global.DomainRemoteDNSAddress;
cmbSBFinalResolverDNS.ItemsSource = Global.DomainPureIPDNSAddress; cmbSBFinalResolverDNS.ItemsSource = Global.DomainPureIPDNSAddress.Concat(new[] { "dhcp://auto,localhost" });
this.WhenActivated(disposables => this.WhenActivated(disposables =>
{ {