Simplified code

This commit is contained in:
DHR60 2026-01-08 13:14:38 +08:00
parent 48d549fd14
commit d4a077f730
2 changed files with 41 additions and 34 deletions

View file

@ -134,24 +134,20 @@ public partial class CoreConfigSingboxService
} }
// ech // ech
if (node?.StreamSecurity == Global.StreamSecurity var (_, dnsServer) = ParseEchParam(node?.EchConfigList);
&& node?.EchConfigList?.Contains("://") == true) if (dnsServer is not null)
{ {
// example.com+https://1.1.1.1/dns-query dnsServer.tag = Global.SingboxEchDNSTag;
var idx = node.EchConfigList.IndexOf('+'); if (dnsServer.server is not null
var echDnsServer = idx > 0 ? node.EchConfigList[(idx + 1)..] : node.EchConfigList; && hostsDns.predefined.ContainsKey(dnsServer.server))
var echDnsObject = ParseDnsAddress(echDnsServer);
echDnsObject.tag = Global.SingboxEchDNSTag;
if (echDnsObject.server is not null
&& hostsDns.predefined.ContainsKey(echDnsObject.server))
{ {
echDnsObject.domain_resolver = Global.SingboxHostsDNSTag; dnsServer.domain_resolver = Global.SingboxHostsDNSTag;
} }
else else
{ {
echDnsObject.domain_resolver = Global.SingboxLocalDNSTag; dnsServer.domain_resolver = Global.SingboxLocalDNSTag;
} }
singboxConfig.dns.servers.Add(echDnsObject); singboxConfig.dns.servers.Add(dnsServer);
} }
else if (node?.ConfigType.IsGroupType() == true) else if (node?.ConfigType.IsGroupType() == true)
{ {
@ -195,16 +191,15 @@ public partial class CoreConfigSingboxService
} }
}); });
if (node?.StreamSecurity == Global.StreamSecurity var (ech, _) = ParseEchParam(node?.EchConfigList);
&& node?.EchConfigList?.Contains("://") == true) if (ech is not null)
{ {
var idx = node.EchConfigList.IndexOf('+'); var echDomain = ech.query_server_name ?? node?.Sni;
List<string> queryServerNames = [(idx > 0 ? node.EchConfigList[..idx] : node.Sni)];
singboxConfig.dns.rules.Add(new() singboxConfig.dns.rules.Add(new()
{ {
query_type = new List<int> { 64, 65 }, query_type = new List<int> { 64, 65 },
server = Global.SingboxEchDNSTag, server = Global.SingboxEchDNSTag,
domain = queryServerNames, domain = echDomain is not null ? new List<string> { echDomain } : null,
}); });
} }
else if (node?.ConfigType.IsGroupType() == true) else if (node?.ConfigType.IsGroupType() == true)

View file

@ -334,24 +334,9 @@ public partial class CoreConfigSingboxService
}; };
tls.insecure = false; tls.insecure = false;
} }
var (ech, _) = ParseEchParam(node.EchConfigList);
if (!node.EchConfigList.IsNullOrEmpty()) if (ech is not null)
{ {
var ech = new Ech4Sbox()
{
enabled = true,
};
if (node.EchConfigList.Contains("://"))
{
var idx = node.EchConfigList.IndexOf('+');
ech.query_server_name = idx > 0 ? node.EchConfigList[..idx] : null;
}
else
{
ech.config = [$"-----BEGIN ECH CONFIGS-----\n" +
$"{node.EchConfigList}\n" +
$"-----END ECH CONFIGS-----"];
}
tls.ech = ech; tls.ech = ech;
} }
outbound.tls = tls; outbound.tls = tls;
@ -924,4 +909,31 @@ public partial class CoreConfigSingboxService
} }
return await Task.FromResult(0); return await Task.FromResult(0);
} }
private (Ech4Sbox? ech, Server4Sbox? dnsServer) ParseEchParam(string? echConfig)
{
if (echConfig.IsNullOrEmpty())
{
return (null, null);
}
if (!echConfig.Contains("://"))
{
return (new Ech4Sbox()
{
enabled = true,
config = [$"-----BEGIN ECH CONFIGS-----\n" +
$"{echConfig}\n" +
$"-----END ECH CONFIGS-----"],
}, null);
}
var idx = echConfig.IndexOf('+');
// NOTE: query_server_name, since sing-box 1.13.0
//var queryServerName = idx > 0 ? echConfig[..idx] : null;
var echDnsServer = idx > 0 ? echConfig[(idx + 1)..] : echConfig;
return (new Ech4Sbox()
{
enabled = true,
query_server_name = null,
}, ParseDnsAddress(echDnsServer));
}
} }