refactoring, documentation pending

This commit is contained in:
Volodymyr Smirnov
2020-10-26 17:06:29 +02:00
parent 29156b7f10
commit 14c9c26979
39 changed files with 121 additions and 457 deletions

View File

@@ -18,15 +18,12 @@ namespace MalwareMultiScan.Backends.Backends.Abstracts
_logger = logger;
}
protected virtual Regex MatchRegex { get; }
protected virtual string BackendPath { get; }
protected abstract Regex MatchRegex { get; }
protected abstract string BackendPath { get; }
protected virtual bool ParseStdErr { get; }
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
protected virtual string GetBackendArguments(string path)
{
throw new NotImplementedException();
}
protected abstract string GetBackendArguments(string path);
public override async Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
{

View File

@@ -3,23 +3,21 @@ using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MalwareMultiScan.Shared.Interfaces;
using MalwareMultiScan.Backends.Interfaces;
using Microsoft.AspNetCore.Http;
namespace MalwareMultiScan.Backends.Backends.Abstracts
{
public abstract class AbstractScanBackend : IScanBackend
{
public virtual string Id => throw new NotImplementedException();
public abstract string Id { get; }
public virtual Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public abstract Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
public async Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken)
{
using var httpClient = new HttpClient();
await using var uriStream = await httpClient.GetStreamAsync(uri);
return await ScanAsync(uriStream, cancellationToken);

View File

@@ -1,5 +1,3 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using MalwareMultiScan.Backends.Backends.Abstracts;
using Microsoft.Extensions.Logging;

View File

@@ -2,7 +2,7 @@ using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MalwareMultiScan.Shared.Interfaces;
using MalwareMultiScan.Backends.Interfaces;
using Microsoft.AspNetCore.Http;
namespace MalwareMultiScan.Backends.Backends.Implementations

View File

@@ -0,0 +1,14 @@
namespace MalwareMultiScan.Backends.Enums
{
public enum BackendType
{
Dummy,
Defender,
Clamav,
DrWeb,
Kes,
Comodo,
Sophos,
McAfee
}
}

View File

@@ -0,0 +1,32 @@
using System;
using MalwareMultiScan.Backends.Backends.Implementations;
using MalwareMultiScan.Backends.Enums;
using MalwareMultiScan.Backends.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace MalwareMultiScan.Backends.Extensions
{
public static class ServiceCollectionExtensions
{
public static void AddScanningBackend(this IServiceCollection services, BackendType type)
{
using var provider = services.BuildServiceProvider();
var logger = provider.GetService<ILogger<IScanBackend>>();
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,18 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace MalwareMultiScan.Backends.Interfaces
{
public interface IScanBackend
{
public string Id { get; }
public Task<string[]> ScanAsync(string path, CancellationToken cancellationToken);
public Task<string[]> ScanAsync(Uri uri, CancellationToken cancellationToken);
public Task<string[]> ScanAsync(IFormFile file, CancellationToken cancellationToken);
public Task<string[]> ScanAsync(Stream stream, CancellationToken cancellationToken);
}
}

View File

@@ -8,4 +8,8 @@
<ProjectReference Include="..\MalwareMultiScan.Shared\MalwareMultiScan.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,11 @@
using System;
namespace MalwareMultiScan.Backends.Messages
{
public class ScanRequestMessage
{
public string Id { get; set; }
public Uri Uri { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace MalwareMultiScan.Backends.Messages
{
public class ScanResultMessage
{
public string Id { get; set; }
public string Backend { get; set; }
public string[] Threats { get; set; }
}
}