mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-24 05:22:22 +00:00
73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using EasyNetQ;
|
|
using MalwareMultiScan.Shared.Data.Messages;
|
|
using MalwareMultiScan.Shared.Interfaces;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace MalwareMultiScan.Scanner
|
|
{
|
|
public class ScanHostedService : IHostedService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<ScanHostedService> _logger;
|
|
private readonly IScanBackend _scanBackend;
|
|
|
|
private IBus _bus;
|
|
|
|
public ScanHostedService(ILogger<ScanHostedService> logger, IConfiguration configuration, IScanBackend scanBackend)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_scanBackend = scanBackend;
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
_bus = RabbitHutch.CreateBus(
|
|
_configuration.GetConnectionString("RabbitMQ"));
|
|
|
|
_bus.Receive<ScanRequestMessage>(
|
|
_scanBackend.Id, Scan);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_bus.Dispose();
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private async Task Scan(ScanRequestMessage message)
|
|
{
|
|
_logger.LogInformation(
|
|
$"Starting scan of {message.Uri} via backend {_scanBackend.Id}");
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource(
|
|
TimeSpan.FromSeconds(_configuration.GetValue<int>("MaxScanningTime")));
|
|
|
|
try
|
|
{
|
|
await _bus.SendAsync(_configuration.GetValue<string>("ResultsSubscriptionId"), new ScanResultMessage
|
|
{
|
|
Id = message.Id,
|
|
Backend = _scanBackend.Id,
|
|
|
|
Threats = await _scanBackend.ScanAsync(
|
|
message.Uri, cancellationTokenSource.Token)
|
|
});
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_logger.LogError(
|
|
exception, "Scanning failed with exception");
|
|
}
|
|
}
|
|
}
|
|
} |