103 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EasyNetQ;
2020-10-29 12:17:09 +02:00
using MalwareMultiScan.Api.Services.Interfaces;
2020-10-26 17:06:29 +02:00
using MalwareMultiScan.Backends.Messages;
using Microsoft.Extensions.Configuration;
2020-10-26 17:06:29 +02:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
2020-10-29 12:17:09 +02:00
namespace MalwareMultiScan.Api.Services.Implementations
{
2020-10-29 16:09:56 +02:00
/// <inheritdoc />
2020-10-29 12:17:09 +02:00
public class ReceiverHostedService : IReceiverHostedService
{
private readonly IBus _bus;
private readonly IConfiguration _configuration;
private readonly IHttpClientFactory _httpClientFactory;
2020-10-26 17:06:29 +02:00
private readonly ILogger<ReceiverHostedService> _logger;
2020-10-29 12:17:09 +02:00
private readonly IScanResultService _scanResultService;
2020-10-29 16:09:56 +02:00
/// <summary>
/// Initialize receiver hosted service.
/// </summary>
/// <param name="bus">EasyNetQ bus.</param>
/// <param name="configuration">Configuration.</param>
/// <param name="scanResultService">Scan result service.</param>
/// <param name="logger">Logger.</param>
/// <param name="httpClientFactory">HTTP client factory.</param>
2020-10-29 12:17:09 +02:00
public ReceiverHostedService(IBus bus, IConfiguration configuration, IScanResultService scanResultService,
ILogger<ReceiverHostedService> logger, IHttpClientFactory httpClientFactory)
{
_bus = bus;
_configuration = configuration;
_scanResultService = scanResultService;
2020-10-26 17:06:29 +02:00
_logger = logger;
_httpClientFactory = httpClientFactory;
}
2020-10-29 16:09:56 +02:00
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
_bus.Receive<ScanResultMessage>(
_configuration.GetValue<string>("ResultsSubscriptionId"), StoreScanResult);
2020-10-26 17:06:29 +02:00
_logger.LogInformation(
"Started hosted service for receiving scan results");
return Task.CompletedTask;
}
2020-10-29 16:09:56 +02:00
/// <inheritdoc />
public Task StopAsync(CancellationToken cancellationToken)
{
2020-10-29 12:17:09 +02:00
_bus?.Dispose();
2020-10-26 17:06:29 +02:00
_logger.LogInformation(
"Stopped hosted service for receiving scan results");
return Task.CompletedTask;
}
private async Task StoreScanResult(ScanResultMessage message)
{
message.Threats ??= new string[] { };
_logger.LogInformation(
$"Received a result from {message.Backend} for {message.Id} " +
$"with threats {string.Join(",", message.Threats)}");
await _scanResultService.UpdateScanResultForBackend(
message.Id, message.Backend, message.Duration, true,
message.Succeeded, message.Threats);
var result = await _scanResultService.GetScanResult(message.Id);
if (result?.CallbackUrl == null)
return;
var cancellationTokenSource = new CancellationTokenSource(
TimeSpan.FromSeconds(3));
using var httpClient = _httpClientFactory.CreateClient();
try
{
var response = await httpClient.PostAsync(
result.CallbackUrl,
new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json"),
cancellationTokenSource.Token);
response.EnsureSuccessStatusCode();
}
catch (Exception exception)
{
_logger.LogError(exception, $"Failed to POST to callback URL {result.CallbackUrl}");
}
}
}
}