mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-10-10 20:26:16 +00:00
refactoring, documentation pending
This commit is contained in:
20
MalwareMultiScan.Scanner/Dockerfile
Normal file
20
MalwareMultiScan.Scanner/Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS builder
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY MalwareMultiScan.Worker /src/MalwareMultiScan.Worker
|
||||
COPY MalwareMultiScan.Shared /src/MalwareMultiScan.Shared
|
||||
COPY MalwareMultiScan.Backends /src/MalwareMultiScan.Backends
|
||||
|
||||
RUN dotnet publish -c Release -r linux-x64 -o ./publish MalwareMultiScan.Worker/MalwareMultiScan.Worker.csproj
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
|
||||
|
||||
WORKDIR /worker
|
||||
|
||||
COPY --from=builder /src/publish /worker
|
||||
|
||||
ENV ASPNETCORE_ENVIRONMENT=Production
|
||||
ENV ASPNETCORE_URLS=http://+:9901
|
||||
|
||||
ENTRYPOINT ["/worker/MalwareMultiScan.Worker"]
|
@@ -1,32 +0,0 @@
|
||||
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()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,4 +13,8 @@
|
||||
<ProjectReference Include="..\MalwareMultiScan.Backends\MalwareMultiScan.Backends.csproj" />
|
||||
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<_ContentIncludedByDefault Remove="Properties\launchSettings.json" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System.Threading.Tasks;
|
||||
using MalwareMultiScan.Scanner.Extensions;
|
||||
using MalwareMultiScan.Shared.Data.Enums;
|
||||
using MalwareMultiScan.Backends.Enums;
|
||||
using MalwareMultiScan.Backends.Extensions;
|
||||
using MalwareMultiScan.Scanner.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
@@ -14,7 +15,7 @@ namespace MalwareMultiScan.Scanner
|
||||
await Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(configure =>
|
||||
{
|
||||
configure.AddJsonFile("settings.json");
|
||||
configure.AddJsonFile("appsettings.json");
|
||||
configure.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
|
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"profiles": {
|
||||
"MalwareMultiScan.Scanner": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,27 +2,27 @@ using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using EasyNetQ;
|
||||
using MalwareMultiScan.Shared.Data.Messages;
|
||||
using MalwareMultiScan.Shared.Interfaces;
|
||||
using MalwareMultiScan.Backends.Interfaces;
|
||||
using MalwareMultiScan.Backends.Messages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MalwareMultiScan.Scanner
|
||||
namespace MalwareMultiScan.Scanner.Services
|
||||
{
|
||||
public class ScanHostedService : IHostedService
|
||||
internal class ScanHostedService : IHostedService
|
||||
{
|
||||
private readonly IScanBackend _backend;
|
||||
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)
|
||||
public ScanHostedService(ILogger<ScanHostedService> logger, IConfiguration configuration, IScanBackend backend)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_scanBackend = scanBackend;
|
||||
_backend = backend;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -30,38 +30,48 @@ namespace MalwareMultiScan.Scanner
|
||||
_bus = RabbitHutch.CreateBus(
|
||||
_configuration.GetConnectionString("RabbitMQ"));
|
||||
|
||||
_bus.Receive<ScanRequestMessage>(
|
||||
_scanBackend.Id, Scan);
|
||||
_bus.Receive<ScanRequestMessage>(_backend.Id, Scan);
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Started scan hosting service for the backend {_backend.Id}");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_bus.Dispose();
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Stopped scan hosting service for the backend {_backend.Id}");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task Scan(ScanRequestMessage message)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
$"Starting scan of {message.Uri} via backend {_scanBackend.Id}");
|
||||
$"Starting scan of {message.Uri} via backend {_backend.Id} from {message.Id}");
|
||||
|
||||
var cancellationTokenSource = new CancellationTokenSource(
|
||||
TimeSpan.FromSeconds(_configuration.GetValue<int>("MaxScanningTime")));
|
||||
|
||||
try
|
||||
{
|
||||
await _bus.SendAsync(_configuration.GetValue<string>("ResultsSubscriptionId"), new ScanResultMessage
|
||||
var result = new ScanResultMessage
|
||||
{
|
||||
Id = message.Id,
|
||||
Backend = _scanBackend.Id,
|
||||
Backend = _backend.Id,
|
||||
|
||||
Threats = await _scanBackend.ScanAsync(
|
||||
Threats = await _backend.ScanAsync(
|
||||
message.Uri, cancellationTokenSource.Token)
|
||||
});
|
||||
};
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Backend {_backend.Id} completed a scan of {message.Id} " +
|
||||
$"with result '{string.Join(", ", result.Threats)}'");
|
||||
|
||||
await _bus.SendAsync(_configuration.GetValue<string>("ResultsSubscriptionId"), result);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
@@ -8,10 +8,8 @@
|
||||
},
|
||||
|
||||
"BackendType": "Dummy",
|
||||
|
||||
"MaxWorkers": 5,
|
||||
"MaxScanningTime": 60,
|
||||
|
||||
"MaxScanningTime": 60,
|
||||
"ResultsSubscriptionId": "mms.results",
|
||||
|
||||
"ConnectionStrings": {
|
Reference in New Issue
Block a user