2020-10-20 16:20:38 +03:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-10-29 16:09:56 +02:00
|
|
|
using MalwareMultiScan.Backends.Services.Interfaces;
|
2020-10-20 16:20:38 +03:00
|
|
|
|
|
|
|
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
|
|
|
{
|
2020-10-29 16:09:56 +02:00
|
|
|
/// <inheritdoc />
|
2020-10-20 16:20:38 +03:00
|
|
|
public abstract class AbstractLocalProcessScanBackend : AbstractScanBackend
|
|
|
|
{
|
2020-10-29 16:09:56 +02:00
|
|
|
private readonly IProcessRunner _processRunner;
|
2020-10-20 17:08:40 +03:00
|
|
|
|
2020-10-29 16:09:56 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected AbstractLocalProcessScanBackend(IProcessRunner processRunner)
|
2020-10-20 17:08:40 +03:00
|
|
|
{
|
2020-10-29 16:09:56 +02:00
|
|
|
_processRunner = processRunner;
|
2020-10-20 17:08:40 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 16:09:56 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Regex to extract names of threats.
|
|
|
|
/// </summary>
|
2020-10-26 17:06:29 +02:00
|
|
|
protected abstract Regex MatchRegex { get; }
|
2020-10-29 16:09:56 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Path to the backend.
|
|
|
|
/// </summary>
|
2020-10-26 17:06:29 +02:00
|
|
|
protected abstract string BackendPath { get; }
|
2020-10-29 16:09:56 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Parse StdErr instead of StdOut.
|
|
|
|
/// </summary>
|
2020-10-26 21:24:40 +02:00
|
|
|
protected virtual bool ParseStdErr { get; } = false;
|
2020-10-29 16:09:56 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Throw on non-zero exit code.
|
|
|
|
/// </summary>
|
2020-10-22 09:28:28 +03:00
|
|
|
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
2020-10-20 16:20:38 +03:00
|
|
|
|
2020-10-29 16:09:56 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Get backend process parameters.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">Path to the temporary file.</param>
|
|
|
|
/// <returns>Formatted string with parameters and path.</returns>
|
2020-10-26 17:06:29 +02:00
|
|
|
protected abstract string GetBackendArguments(string path);
|
2020-10-29 16:09:56 +02:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
2020-10-20 16:20:38 +03:00
|
|
|
{
|
2020-10-29 16:09:56 +02:00
|
|
|
var exitCode = _processRunner.RunTillCompletion(BackendPath, GetBackendArguments(path), cancellationToken,
|
|
|
|
out var standardOutput, out var standardError);
|
2020-10-23 11:11:19 +03:00
|
|
|
|
2020-10-29 16:09:56 +02:00
|
|
|
if (ThrowOnNonZeroExitCode && exitCode != 0)
|
|
|
|
throw new ApplicationException($"Process has terminated with an exit code {exitCode}");
|
2020-10-20 16:20:38 +03:00
|
|
|
|
2020-10-29 16:09:56 +02:00
|
|
|
return Task.FromResult(MatchRegex
|
2020-10-20 16:20:38 +03:00
|
|
|
.Matches(ParseStdErr ? standardError : standardOutput)
|
|
|
|
.Where(x => x.Success)
|
|
|
|
.Select(x => x.Groups["threat"].Value)
|
2020-10-29 16:09:56 +02:00
|
|
|
.ToArray());
|
2020-10-20 16:20:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|