mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-10-10 20:26:16 +00:00
change the communication protocol to RMQ
This commit is contained in:
@@ -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()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
16
MalwareMultiScan.Scanner/MalwareMultiScan.Scanner.csproj
Normal file
16
MalwareMultiScan.Scanner/MalwareMultiScan.Scanner.csproj
Normal 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>
|
31
MalwareMultiScan.Scanner/Program.cs
Normal file
31
MalwareMultiScan.Scanner/Program.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
10
MalwareMultiScan.Scanner/Properties/launchSettings.json
Normal file
10
MalwareMultiScan.Scanner/Properties/launchSettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"profiles": {
|
||||
"MalwareMultiScan.Scanner": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
73
MalwareMultiScan.Scanner/ScanHostedService.cs
Normal file
73
MalwareMultiScan.Scanner/ScanHostedService.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
20
MalwareMultiScan.Scanner/settings.json
Normal file
20
MalwareMultiScan.Scanner/settings.json
Normal 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"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user