Optimize resolver (#10625)

* Resolve concurrently

* Override the host and sni if empty
This commit is contained in:
DHR60
2026-11-18 02:54:39 +00:00
committed by GitHub
parent 99e2e61bfa
commit 2049271f0b
2 changed files with 260 additions and 58 deletions
@@ -1388,58 +1388,106 @@ public static class ConfigDbHandler
} }
var dnsClient = preResolver.IsNullOrEmpty() ? null : EndpointResolveManager.BuildDnsClient(preResolver); var dnsClient = preResolver.IsNullOrEmpty() ? null : EndpointResolveManager.BuildDnsClient(preResolver);
var dnsCache = new Dictionary<string, string>(); var dnsCache = new ConcurrentDictionary<string, Lazy<Task<string>>>();
var countServers = 0; var countServers = 0;
List<ProfileItem> lstAdd = []; List<ProfileItem> lstAdd = [];
var arrData = strData.Split(Environment.NewLine.ToCharArray()).Where(t => !t.IsNullOrEmpty()); var arrData = strData.Split(Environment.NewLine.ToCharArray()).Where(t => !t.IsNullOrEmpty()).ToList();
if (isSub) if (isSub)
{ {
arrData = arrData.Distinct(); arrData = arrData.Distinct().ToList();
} }
foreach (var str in arrData)
// Subscriptions are added sequentially because they write to the database.
if (!isSub)
{ {
//maybe sub foreach (var str in arrData.Where(t => t.StartsWith(Global.HttpsProtocol) || t.StartsWith(Global.HttpProtocol)))
if (!isSub && (str.StartsWith(Global.HttpsProtocol) || str.StartsWith(Global.HttpProtocol)))
{ {
if (await AddSubItem(str) == 0) if (await AddSubItem(str) == 0)
{ {
countServers++; countServers++;
} }
continue;
} }
var profileItem = FmtHandler.ResolveConfig(str, out var msg); }
var profileItems = new ProfileItem?[arrData.Count];
await Parallel.ForEachAsync(
Enumerable.Range(0, arrData.Count),
new ParallelOptions { MaxDegreeOfParallelism = 8 },
async (index, cancellationToken) =>
{
var str = arrData[index];
//maybe sub
if (!isSub && (str.StartsWith(Global.HttpsProtocol) || str.StartsWith(Global.HttpProtocol)))
{
return;
}
var profileItem = FmtHandler.ResolveConfig(str, out _);
if (profileItem is null)
{
return;
}
//exist sub items //filter
if (isSub && subid.IsNotEmpty() && subFilter.IsNotEmpty())
{
if (!Utils.IsRegexMatch(profileItem.Remarks, subFilter))
{
return;
}
}
profileItem.Subid = subid;
profileItem.IsSub = isSub;
if (dnsClient is not null && profileItem.Address.IsNotEmpty() && !Utils.IsIpAddress(profileItem.Address))
{
var address = profileItem.Address;
var resolvedAddress = await dnsCache.GetOrAdd(
address,
key => new Lazy<Task<string>>(async () =>
{
var ipList = await dnsClient.TryResolveIpv46Async(key, cancellationToken);
return ipList.FirstOrDefault()?.ToString() ?? key;
})).Value;
if (!string.IsNullOrEmpty(resolvedAddress))
{
profileItem.Address = resolvedAddress;
}
// host -> serverName -> address
if (!profileItem.StreamSecurity.IsNullOrEmpty()
&& profileItem.Sni.IsNullOrEmpty())
{
profileItem.Sni = profileItem.Address;
}
var requestHostTransport = new HashSet<string>
{
nameof(ETransport.ws),
nameof(ETransport.httpupgrade),
nameof(ETransport.xhttp),
//nameof(ETransport.grpc),
};
if (requestHostTransport.Contains(profileItem.GetNetwork())
&& profileItem.GetTransportExtra().Host.IsNullOrEmpty())
{
profileItem.SetTransportExtra(profileItem.GetTransportExtra() with
{
Host = profileItem.Sni,
});
}
}
profileItems[index] = profileItem;
});
// Profile validation and sorting mutate shared state, so they stay sequential.
foreach (var profileItem in profileItems)
{
if (profileItem is null) if (profileItem is null)
{ {
continue; continue;
} }
//exist sub items //filter
if (isSub && subid.IsNotEmpty() && subFilter.IsNotEmpty())
{
if (!Utils.IsRegexMatch(profileItem.Remarks, subFilter))
{
continue;
}
}
profileItem.Subid = subid;
profileItem.IsSub = isSub;
if (dnsClient is not null && profileItem.Address.IsNotEmpty() && !Utils.IsIpAddress(profileItem.Address))
{
if (!dnsCache.TryGetValue(profileItem.Address, out var resolvedAddress))
{
var ipList = await dnsClient.ResolveIpv46Async(profileItem.Address);
resolvedAddress = ipList.FirstOrDefault()?.ToString() ?? profileItem.Address;
dnsCache[profileItem.Address] = resolvedAddress;
}
if (!resolvedAddress.IsNullOrEmpty())
{
profileItem.Address = resolvedAddress;
}
// TODO: Override the host and sni if empty
}
var addStatus = profileItem.ConfigType switch var addStatus = profileItem.ConfigType switch
{ {
EConfigType.VMess => await AddVMessServer(profileItem, false), EConfigType.VMess => await AddVMessServer(profileItem, false),
@@ -4,13 +4,43 @@ namespace ServiceLib.Manager;
public class EndpointResolveManager public class EndpointResolveManager
{ {
public static IDnsResolver BuildDnsClient(string dnsServer) /// <summary>
/// Builds a resolver from one or more DNS servers separated by commas.
/// Multiple servers are queried concurrently; the first successful response wins.
/// Invalid entries are skipped, and null is returned when no valid server remains.
/// </summary>
public static IDnsResolver? BuildDnsClient(string dnsServer)
{ {
if (string.IsNullOrEmpty(dnsServer)) if (string.IsNullOrWhiteSpace(dnsServer))
{ {
throw new ArgumentNullException(nameof(dnsServer)); return null;
} }
var resolvers = new List<IDnsResolver>();
foreach (var server in dnsServer
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Distinct(StringComparer.OrdinalIgnoreCase))
{
try
{
resolvers.Add(BuildSingleDnsClient(server));
}
catch
{
// Skip invalid DNS server entries.
}
}
return resolvers.Count switch
{
0 => null,
1 => resolvers[0],
_ => new MultiDnsResolver(resolvers),
};
}
private static IDnsResolver BuildSingleDnsClient(string dnsServer)
{
if (dnsServer is "local" or "localhost" or "system" or "dhcp" || if (dnsServer is "local" or "localhost" or "system" or "dhcp" ||
dnsServer.StartsWith("127.0.0.1") || dnsServer.StartsWith("127.0.0.1") ||
dnsServer.StartsWith("::1")) dnsServer.StartsWith("::1"))
@@ -161,7 +191,14 @@ public class EndpointResolveManager
var rcode = flags & 0x000F; var rcode = flags & 0x000F;
if (rcode != 0) if (rcode != 0)
{ {
return new DnsResponse([], null); // NXDOMAIN is a definitive answer; other codes (SERVFAIL, REFUSED, ...)
// mean the server failed or rejected the query, e.g. anti-abuse/rate limiting.
if (rcode == 3)
{
return new DnsResponse([], null);
}
throw new DnsResolveException($"DNS server returned rcode {rcode}.");
} }
var offset = 12; var offset = 12;
@@ -409,6 +446,107 @@ public class EndpointResolveManager
var results = await Task.WhenAll(ipv4Task, ipv6Task); var results = await Task.WhenAll(ipv4Task, ipv6Task);
return results.SelectMany(x => x).ToList(); return results.SelectMany(x => x).ToList();
} }
/// <summary>
/// Resolves without throwing; returns an empty list if all servers fail.
/// </summary>
public async Task<List<IPAddress>> TryResolveIpv46Async(
string host,
CancellationToken cancellationToken = default)
{
try
{
return await ResolveIpv46Async(host, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch
{
return [];
}
}
}
public class DnsResolveException(string message) : Exception(message);
public class MultiDnsResolver(IReadOnlyList<IDnsResolver> resolvers) : IDnsResolver
{
public Task<List<IPAddress>> ResolveAsync(
string host,
bool ipv6,
CancellationToken cancellationToken = default)
{
return ResolveCoreAsync((resolver, ct) => resolver.ResolveAsync(host, ipv6, ct), cancellationToken);
}
public Task<List<IPAddress>> ResolveIpv46Async(
string host,
CancellationToken cancellationToken = default)
{
return ResolveCoreAsync((resolver, ct) => resolver.ResolveIpv46Async(host, ct), cancellationToken);
}
private async Task<List<IPAddress>> ResolveCoreAsync(
Func<IDnsResolver, CancellationToken, Task<List<IPAddress>>> queryAsync,
CancellationToken cancellationToken)
{
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var tasks = resolvers
.Select(resolver => queryAsync(resolver, linkedCts.Token))
.ToList();
try
{
var errors = new List<Exception>();
var hasResponse = false;
await foreach (var task in Task.WhenEach(tasks).WithCancellation(linkedCts.Token))
{
try
{
var addresses = await task;
hasResponse = true;
if (addresses.Count > 0)
{
return addresses;
}
}
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
{
// Ignore cancellation of the queries that lost the race.
}
catch (Exception ex)
{
errors.Add(ex);
}
}
cancellationToken.ThrowIfCancellationRequested();
if (!hasResponse && errors.Count > 0)
{
throw new AggregateException("All DNS servers failed to resolve the endpoint.", errors);
}
return [];
}
finally
{
await linkedCts.CancelAsync();
try
{
await Task.WhenAll(tasks);
}
catch
{
// Results and errors were already handled above.
}
}
}
} }
public class UdpDnsResolver(IPEndPoint dnsServer) : IDnsResolver public class UdpDnsResolver(IPEndPoint dnsServer) : IDnsResolver
@@ -466,36 +604,52 @@ public class EndpointResolveManager
public class DohDnsResolver(HttpClient httpClient, Uri dohServer) : IDnsResolver public class DohDnsResolver(HttpClient httpClient, Uri dohServer) : IDnsResolver
{ {
private const int MaxAttempts = 2;
private static readonly TimeSpan DefaultRetryDelay = TimeSpan.FromSeconds(1);
public async Task<List<IPAddress>> ResolveAsync( public async Task<List<IPAddress>> ResolveAsync(
string host, string host,
bool ipv6, bool ipv6,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
return await DnsWire.ResolveWithCnameAsync( return await DnsWire.ResolveWithCnameAsync(host, ipv6, SendQueryAsync, cancellationToken);
host, }
ipv6,
async (name, queryType, ct) => private async Task<byte[]> SendQueryAsync(
string name,
ushort queryType,
CancellationToken cancellationToken)
{
for (var attempt = 1;; attempt++)
{
var query = DnsWire.BuildQuery(name, queryType);
using var request = new HttpRequestMessage(HttpMethod.Post, dohServer);
request.Version = HttpVersion.Version20;
request.VersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
request.Content = new ByteArrayContent(query)
{ {
var query = DnsWire.BuildQuery(name, queryType); Headers =
using var request = new HttpRequestMessage(HttpMethod.Post, dohServer);
request.Content = new ByteArrayContent(query)
{ {
Headers = ContentType = new MediaTypeHeaderValue("application/dns-message"),
{ },
ContentType = new MediaTypeHeaderValue("application/dns-message"), };
},
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/dns-message")); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/dns-message"));
using var response = using var response =
await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsByteArrayAsync(ct); if (response.StatusCode == HttpStatusCode.TooManyRequests && attempt < MaxAttempts)
}, {
cancellationToken); await Task.Delay(DefaultRetryDelay, cancellationToken);
continue;
}
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsByteArrayAsync(cancellationToken);
}
} }
} }