change the communication protocol to RMQ

This commit is contained in:
Volodymyr Smirnov
2020-10-26 16:20:47 +02:00
parent b6c3cb4131
commit 29156b7f10
25 changed files with 439 additions and 173 deletions

View File

@@ -0,0 +1,32 @@
using System;
using MalwareMultiScan.Backends.Backends.Implementations;
using MalwareMultiScan.Shared.Data.Enums;
using MalwareMultiScan.Shared.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace MalwareMultiScan.Scanner.Extensions
{
public static class ServiceCollectionExtensions
{
public static void AddScanningBackend(this IServiceCollection services, BackendType type)
{
using var provider = services.BuildServiceProvider();
var logger = provider.GetService<ILogger>();
services.AddSingleton<IScanBackend>(type switch
{
BackendType.Dummy => new DummyScanBackend(),
BackendType.Defender => new WindowsDefenderScanBackend(logger),
BackendType.Clamav => new ClamavScanBackend(logger),
BackendType.DrWeb => new DrWebScanBackend(logger),
BackendType.Kes => new KesScanBackend(logger),
BackendType.Comodo => new ComodoScanBackend(logger),
BackendType.Sophos => new SophosScanBackend(logger),
BackendType.McAfee => new McAfeeScanBackend(logger),
_ => throw new NotImplementedException()
});
}
}
}

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EasyNetQ" Version="5.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using MalwareMultiScan.Scanner.Extensions;
using MalwareMultiScan.Shared.Data.Enums;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MalwareMultiScan.Scanner
{
public static class Program
{
public static async Task Main(string[] args)
{
await Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(configure =>
{
configure.AddJsonFile("settings.json");
configure.AddEnvironmentVariables();
})
.ConfigureServices((context, services) =>
{
services.AddLogging();
services.AddScanningBackend(
context.Configuration.GetValue<BackendType>("BackendType"));
services.AddHostedService<ScanHostedService>();
}).RunConsoleAsync();
}
}
}

View File

@@ -0,0 +1,10 @@
{
"profiles": {
"MalwareMultiScan.Scanner": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,73 @@
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");
}
}
}
}

View File

@@ -0,0 +1,20 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"BackendType": "Dummy",
"MaxWorkers": 5,
"MaxScanningTime": 60,
"ResultsSubscriptionId": "mms.results",
"ConnectionStrings": {
"RabbitMQ": "host=localhost;prefetchcount=1"
}
}