1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-10 06:02:24 -05:00
bitwarden/src/Core/Services/Implementations/DnsResolverService.cs
SmithThe4th 53b9d52125
[PM-1675] Timeout or transient error when verifying domains (#2835)
* Increased timeout of the dns resolve method and changed the lifetime of the dnsResolverService to scoped

* Reverted to using singleton as this was recommneded on the docs and also registered lookup client as a singleton

* Registerered a singleton service of ILookupClient

* replaced unused serviceProvider with a discard
2023-04-06 15:31:45 -04:00

27 lines
772 B
C#

using Bit.Core.Exceptions;
using DnsClient;
namespace Bit.Core.Services;
public class DnsResolverService : IDnsResolverService
{
private readonly ILookupClient _client;
public DnsResolverService(ILookupClient client)
{
_client = client;
}
public async Task<bool> ResolveAsync(string domain, string txtRecord, CancellationToken cancellationToken = default)
{
var result = await _client.QueryAsync(new DnsQuestion(domain, QueryType.TXT), cancellationToken);
if (!result.HasError)
{
return result.Answers.TxtRecords()
.Select(t => t?.EscapedText?.FirstOrDefault())
.Any(t => t == txtRecord);
}
throw new DnsQueryException(result.ErrorMessage);
}
}