Skip to content

Commit

Permalink
Add DnsHostEntryVerifier
Browse files Browse the repository at this point in the history
  • Loading branch information
thohng committed Jan 14, 2024
1 parent a307e8f commit 5b5d32f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions EchoServiceApi/Controllers/DiagnosticsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace EchoServiceApi.Controllers
{
[Route("[controller]/[action]")]
[Route("diag/[action]")]
public class DiagnosticsController : ControllerBase
{
public IActionResult GetInfo([FromServices] NetLah.Diagnostics.IAssemblyInfo appInfo)
Expand Down Expand Up @@ -194,5 +195,18 @@ public async Task<IActionResult> CertificateAsync([FromServices] CertificateVeri
return Ok(VerifyResult.Failed(ex));
}
}

public async Task<IActionResult> DnsHostEntryAsync([FromServices] DnsHostEntryVerifier dnsHostEntryVerifier, string? host)
{
try
{
var result = await dnsHostEntryVerifier.VerifyAsync(host);
return Ok(result);
}
catch (Exception ex)
{
return Ok(VerifyResult.Failed(ex));
}
}
}
}
1 change: 1 addition & 0 deletions EchoServiceApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
builder.Services.AddScoped<DirVerifier>();
builder.Services.AddScoped<ServiceBusVerifier>();
builder.Services.AddScoped<CertificateVerifier>();
builder.Services.AddScoped<DnsHostEntryVerifier>();
builder.Services.AddHttpClient<HttpVerifier>();

builder.Services.AddHttpContextAccessor();
Expand Down
31 changes: 31 additions & 0 deletions EchoServiceApi/Verifiers/DnsHostEntryVerifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net;

namespace EchoServiceApi.Verifiers
{
public class DnsHostEntryVerifier : BaseVerifier
{
public DnsHostEntryVerifier(IServiceProvider serviceProvider) : base(serviceProvider) { }

public Task<VerifyResult> VerifyAsync(string? host)
{
host = string.IsNullOrWhiteSpace(host) ? string.Empty : host.Trim();

Logger.LogInformation("DnsHostEntryVerifier: host={host}", host);

var ipHostEntry = Dns.GetHostEntry(host);
var addressList = ipHostEntry.AddressList?.Select(ip => ip.ToString()).ToArray();
var result = new
{
AddressList = addressList,
ipHostEntry.HostName,
ipHostEntry.Aliases,
};

return Task.FromResult<VerifyResult>(new VerifySuccess<object>
{
Message = $"DNS Lookup {host}",
Value = result
});
}
}
}

0 comments on commit 5b5d32f

Please sign in to comment.