mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 05:22:22 +00:00
89 lines
2.8 KiB
C#
89 lines
2.8 KiB
C#
|
using System;
|
||
|
using System.Diagnostics;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
|
using EasyNetQ;
|
||
|
using MalwareMultiScan.Backends.Interfaces;
|
||
|
using MalwareMultiScan.Backends.Messages;
|
||
|
using MalwareMultiScan.Scanner.Services.Interfaces;
|
||
|
using Microsoft.Extensions.Configuration;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace MalwareMultiScan.Scanner.Services.Implementations
|
||
|
{
|
||
|
/// <inheritdoc />
|
||
|
public class ScanBackgroundJob : IScanBackgroundJob
|
||
|
{
|
||
|
private readonly IScanBackend _backend;
|
||
|
private readonly IBus _bus;
|
||
|
private readonly IConfiguration _configuration;
|
||
|
private readonly ILogger<ScanBackgroundJob> _logger;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Initialize scan background job.
|
||
|
/// </summary>
|
||
|
/// <param name="configuration">Configuration.</param>
|
||
|
/// <param name="bus">Bus.</param>
|
||
|
/// <param name="logger">Logger.</param>
|
||
|
/// <param name="backend">Scan backend.</param>
|
||
|
public ScanBackgroundJob(IConfiguration configuration, IBus bus,
|
||
|
ILogger<ScanBackgroundJob> logger, IScanBackend backend)
|
||
|
{
|
||
|
_configuration = configuration;
|
||
|
_bus = bus;
|
||
|
_logger = logger;
|
||
|
_backend = backend;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public async Task Process(ScanRequestMessage message)
|
||
|
{
|
||
|
_logger.LogInformation(
|
||
|
$"Starting scan of {message.Uri} via backend {_backend.Id} from {message.Id}");
|
||
|
|
||
|
var cancellationTokenSource = new CancellationTokenSource(
|
||
|
TimeSpan.FromSeconds(_configuration.GetValue<int>("MaxScanningTime")));
|
||
|
|
||
|
var result = new ScanResultMessage
|
||
|
{
|
||
|
Id = message.Id,
|
||
|
Backend = _backend.Id
|
||
|
};
|
||
|
|
||
|
var stopwatch = new Stopwatch();
|
||
|
|
||
|
stopwatch.Start();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
result.Threats = await _backend.ScanAsync(
|
||
|
message.Uri, cancellationTokenSource.Token);
|
||
|
|
||
|
result.Succeeded = true;
|
||
|
|
||
|
_logger.LogInformation(
|
||
|
$"Backend {_backend.Id} completed a scan of {message.Id} " +
|
||
|
$"with result '{string.Join(", ", result.Threats)}'");
|
||
|
}
|
||
|
catch (Exception exception)
|
||
|
{
|
||
|
result.Succeeded = false;
|
||
|
|
||
|
_logger.LogError(
|
||
|
exception, "Scanning failed with exception");
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
stopwatch.Stop();
|
||
|
}
|
||
|
|
||
|
result.Duration = stopwatch.ElapsedMilliseconds / 1000;
|
||
|
|
||
|
_logger.LogInformation(
|
||
|
$"Sending scan results with status {result.Succeeded}");
|
||
|
|
||
|
await _bus.SendAsync(
|
||
|
_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
||
|
}
|
||
|
}
|
||
|
}
|