Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DNS-BLM.Api/Controllers/DnsBlocklistScanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task<string> ScanConfiguredDomains()

if (domains == null) throw new Exception("Domains not found");

return await mediator.Send(new ScanBlacklistCommand(domains, false));
return await mediator.Send(new ScanBlacklistCommand(domains));
}

// [HttpGet("ScanCustomDomain", Name = "ScanCustomDomain")]
Expand Down
3 changes: 1 addition & 2 deletions src/DNS-BLM.Application/Commands/ScanBlacklistCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public async Task<string> Handle(ScanBlacklistCommand request, CancellationToken
var results = messageService.GetResults();
messageService.Clear();

if (request.SendMail && results is not null)
if (request.SendMail && !string.IsNullOrWhiteSpace(results))
{
await notificationService.Notify("DNS-BLM Scanning Results", results);
}
else
{
if (results is null)
results = "No blacklisted Domains.";
logger.LogInformation("Mail notification skipped as per request");
}

return results;
Expand Down
2 changes: 2 additions & 0 deletions src/DNS-BLM.Domain/Configuration/MailConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public class MailConfiguration

[Required]
public bool EnableSsl { get; init; } = true;
public string MailTemplate { get; init; } = string.Empty;

}
1 change: 1 addition & 0 deletions src/DNS-BLM.Infrastructure/DNS-BLM.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
<PackageReference Include="MailKit" Version="4.12.1" />
<PackageReference Include="Sentry.AspNetCore" Version="5.10.0" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/DNS-BLM.Infrastructure/ModuleRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static void AddInfrastructureModule(this IServiceCollection services, App
services.AddSingleton<INotificationService, MailNotificationService>();
services.AddScoped<MessageService>();
services.AddSingleton<RetryService>();
services.AddSingleton<TemplateService>();

string? virusTotalApiKey = appConfiguration.ApiCredentials.VirusTotal;

Expand Down
20 changes: 5 additions & 15 deletions src/DNS-BLM.Infrastructure/Services/MessageService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Collections.Concurrent;
using DNS_BLM.Domain.Configuration;
using DNS_BLM.Infrastructure.Dtos;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace DNS_BLM.Infrastructure.Services;

public class MessageService(ILogger<MessageService> logger)
public class MessageService(ILogger<MessageService> logger, TemplateService templateService, IOptions<AppConfiguration> appConfiguration)
{
private ConcurrentBag<ScanResult> _results = [];
private HashSet<string> _domains = [];
Expand Down Expand Up @@ -34,21 +36,9 @@ public void AddResult(ScanResult result)
{
logger.LogDebug($"{result.Domain}: {result.ScannerName} - {result.IsBlacklisted}");
}

List<string> allResults = new List<string>();
foreach (var domain in _domains)
{
var domainResults = _results.Where(r => r.Domain == domain && r.IsBlacklisted).ToArray();
var result = ArrangeResults(domainResults);
if (result == null) continue;
allResults.Add(result);
}

var results = string.Join(Environment.NewLine, allResults);
if (String.IsNullOrWhiteSpace(results))
logger.LogError("No results available to return, despite passing previous checks!");
ArgumentException.ThrowIfNullOrWhiteSpace(results, nameof(results));

var domainResults = _results.Where(r => r.IsBlacklisted).ToArray();
var results = templateService.RenderTemplate(domainResults.ToList(), appConfiguration.Value.Mail.MailTemplate);
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task Notify(string subject, string message)
mailMessage.Subject = subject;

var builder = new BodyBuilder ();
builder.HtmlBody = message.Replace("\n", "<br/>");
builder.HtmlBody = message;

mailMessage.Body = builder.ToMessageBody();

Expand Down
97 changes: 97 additions & 0 deletions src/DNS-BLM.Infrastructure/Services/TemplateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Collections.Concurrent;
using DNS_BLM.Infrastructure.Dtos;
using HandlebarsDotNet;
using Microsoft.Extensions.Logging;

namespace DNS_BLM.Infrastructure.Services;

public class TemplateService() // ILogger<TemplateService> logger
{
private readonly ConcurrentDictionary<string, HandlebarsTemplate<object, object>> _compiledTemplates = new();

public string RenderTemplate(List<ScanResult> model, string? template = null)
{
string defaultTemplate =
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>DNS-BLM Results</title>
</head>
<body style="margin:0; padding:0; background-color:#f4f4f4;">
<center>
<table width="100%" bgcolor="#f4f4f4" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
<table width="600" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff" style="border:1px solid #ddd;">
<tr>
<td align="center" bgcolor="#004080" style="padding:24px 0 24px 0; color:#ffffff; font-size:24px; font-family:Arial,sans-serif; font-weight:bold;">
DNS-BLM Results
</td>
</tr>
<tr>
<td style="padding:24px 24px 24px 24px;">
<table width="100%" cellpadding="8" cellspacing="0" border="1" style="border-collapse:collapse; border-color:#ddd; font-family:Arial,sans-serif; font-size:14px;">
<thead>
<tr bgcolor="#e6f0ff" style="font-weight:bold;">
<th align="left" style="border-color:#ddd;">Domain</th>
<th align="left" style="border-color:#ddd;">Scanner</th>
<th align="left" style="border-color:#ddd;">Details</th>
</tr>
</thead>
<tbody>
{{#each this}}
<tr>
<td style="border-color:#ddd;">{{Domain}}</td>
<td style="border-color:#ddd;">{{ScannerName}}</td>
<td style="border-color:#ddd;">
{{#if ScanResultUrl}}
<a href="{{ScanResultUrl}}" target="_blank" style="color:#004080; text-decoration:underline;">View Report</a>
{{else}}
N/A
{{/if}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" style="padding:16px 0 16px 0; color:#777; font-size:12px; font-family:Arial,sans-serif; border-top:1px solid #ddd;">
Sent by <a href="https://github.com/Hutch79-Dev/DNS-BLM" target="_blank" style="color:#004080; text-decoration:none;">DNS-BLM</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
""";
try
{
var usableTemplate = string.IsNullOrWhiteSpace(template) ? defaultTemplate : template;
var compiledTemplate = _compiledTemplates.GetOrAdd(
(usableTemplate), _ => Handlebars.Compile(usableTemplate));

var result = compiledTemplate(model);
return result;
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to render template", ex);
}
}

/// <summary>
/// Deletes all precompiled Templates.
/// Only needs to be used if a Template is changed
/// </summary>
public void ClearCompiledTemplates()
{
_compiledTemplates.Clear();
}
}
135 changes: 0 additions & 135 deletions test/Tests/Test/MessageServiceTest.cs

This file was deleted.

Loading