diff --git a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs index 2173fd1e..996eef81 100644 --- a/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs +++ b/v2rayN/ServiceLib/Handler/Data/ConfigDbHandler.cs @@ -1388,58 +1388,106 @@ public static class ConfigDbHandler } var dnsClient = preResolver.IsNullOrEmpty() ? null : EndpointResolveManager.BuildDnsClient(preResolver); - var dnsCache = new Dictionary(); + var dnsCache = new ConcurrentDictionary>>(); var countServers = 0; List 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) { - 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 - if (!isSub && (str.StartsWith(Global.HttpsProtocol) || str.StartsWith(Global.HttpProtocol))) + foreach (var str in arrData.Where(t => t.StartsWith(Global.HttpsProtocol) || t.StartsWith(Global.HttpProtocol))) { if (await AddSubItem(str) == 0) { 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>(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 + { + 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) { 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 { EConfigType.VMess => await AddVMessServer(profileItem, false), diff --git a/v2rayN/ServiceLib/Manager/EndpointResolveManager.cs b/v2rayN/ServiceLib/Manager/EndpointResolveManager.cs index 30f85ae0..5c8dbcce 100644 --- a/v2rayN/ServiceLib/Manager/EndpointResolveManager.cs +++ b/v2rayN/ServiceLib/Manager/EndpointResolveManager.cs @@ -4,13 +4,43 @@ namespace ServiceLib.Manager; public class EndpointResolveManager { - public static IDnsResolver BuildDnsClient(string dnsServer) + /// + /// 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. + /// + 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(); + 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" || dnsServer.StartsWith("127.0.0.1") || dnsServer.StartsWith("::1")) @@ -161,7 +191,14 @@ public class EndpointResolveManager var rcode = flags & 0x000F; 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; @@ -409,6 +446,107 @@ public class EndpointResolveManager var results = await Task.WhenAll(ipv4Task, ipv6Task); return results.SelectMany(x => x).ToList(); } + + /// + /// Resolves without throwing; returns an empty list if all servers fail. + /// + public async Task> 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 resolvers) : IDnsResolver + { + public Task> ResolveAsync( + string host, + bool ipv6, + CancellationToken cancellationToken = default) + { + return ResolveCoreAsync((resolver, ct) => resolver.ResolveAsync(host, ipv6, ct), cancellationToken); + } + + public Task> ResolveIpv46Async( + string host, + CancellationToken cancellationToken = default) + { + return ResolveCoreAsync((resolver, ct) => resolver.ResolveIpv46Async(host, ct), cancellationToken); + } + + private async Task> ResolveCoreAsync( + Func>> queryAsync, + CancellationToken cancellationToken) + { + using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + var tasks = resolvers + .Select(resolver => queryAsync(resolver, linkedCts.Token)) + .ToList(); + + try + { + var errors = new List(); + 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 @@ -466,36 +604,52 @@ public class EndpointResolveManager public class DohDnsResolver(HttpClient httpClient, Uri dohServer) : IDnsResolver { + private const int MaxAttempts = 2; + private static readonly TimeSpan DefaultRetryDelay = TimeSpan.FromSeconds(1); + public async Task> ResolveAsync( string host, bool ipv6, CancellationToken cancellationToken = default) { - return await DnsWire.ResolveWithCnameAsync( - host, - ipv6, - async (name, queryType, ct) => + return await DnsWire.ResolveWithCnameAsync(host, ipv6, SendQueryAsync, cancellationToken); + } + + private async Task 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); - - using var request = new HttpRequestMessage(HttpMethod.Post, dohServer); - request.Content = new ByteArrayContent(query) + Headers = { - 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 = - await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); - response.EnsureSuccessStatusCode(); + using var response = + await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); - return await response.Content.ReadAsByteArrayAsync(ct); - }, - cancellationToken); + if (response.StatusCode == HttpStatusCode.TooManyRequests && attempt < MaxAttempts) + { + await Task.Delay(DefaultRetryDelay, cancellationToken); + continue; + } + + response.EnsureSuccessStatusCode(); + + return await response.Content.ReadAsByteArrayAsync(cancellationToken); + } } }