mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-25 14:01:13 +00:00
finished unit tests and docstrings
This commit is contained in:
@@ -1,77 +1,64 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MalwareMultiScan.Backends.Services.Interfaces;
|
||||
|
||||
namespace MalwareMultiScan.Backends.Backends.Abstracts
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public abstract class AbstractLocalProcessScanBackend : AbstractScanBackend
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IProcessRunner _processRunner;
|
||||
|
||||
protected AbstractLocalProcessScanBackend(ILogger logger)
|
||||
/// <inheritdoc />
|
||||
protected AbstractLocalProcessScanBackend(IProcessRunner processRunner)
|
||||
{
|
||||
_logger = logger;
|
||||
_processRunner = processRunner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Regex to extract names of threats.
|
||||
/// </summary>
|
||||
protected abstract Regex MatchRegex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Path to the backend.
|
||||
/// </summary>
|
||||
protected abstract string BackendPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Parse StdErr instead of StdOut.
|
||||
/// </summary>
|
||||
protected virtual bool ParseStdErr { get; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Throw on non-zero exit code.
|
||||
/// </summary>
|
||||
protected virtual bool ThrowOnNonZeroExitCode { get; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Get backend process parameters.
|
||||
/// </summary>
|
||||
/// <param name="path">Path to the temporary file.</param>
|
||||
/// <returns>Formatted string with parameters and path.</returns>
|
||||
protected abstract string GetBackendArguments(string path);
|
||||
|
||||
public override async Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<string[]> ScanAsync(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo(BackendPath, GetBackendArguments(path))
|
||||
{
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
WorkingDirectory = Path.GetDirectoryName(BackendPath) ?? Directory.GetCurrentDirectory()
|
||||
}
|
||||
};
|
||||
var exitCode = _processRunner.RunTillCompletion(BackendPath, GetBackendArguments(path), cancellationToken,
|
||||
out var standardOutput, out var standardError);
|
||||
|
||||
_logger.LogInformation(
|
||||
$"Starting process {process.StartInfo.FileName} " +
|
||||
$"with arguments {process.StartInfo.Arguments} " +
|
||||
$"in working directory {process.StartInfo.WorkingDirectory}");
|
||||
if (ThrowOnNonZeroExitCode && exitCode != 0)
|
||||
throw new ApplicationException($"Process has terminated with an exit code {exitCode}");
|
||||
|
||||
process.Start();
|
||||
|
||||
cancellationToken.Register(() =>
|
||||
{
|
||||
if (process.HasExited)
|
||||
return;
|
||||
|
||||
process.Kill(true);
|
||||
|
||||
throw new TimeoutException("Scanning failed to complete within the timeout");
|
||||
});
|
||||
|
||||
process.WaitForExit();
|
||||
|
||||
_logger.LogInformation($"Process has exited with code {process.ExitCode}");
|
||||
|
||||
var standardOutput = await process.StandardOutput.ReadToEndAsync();
|
||||
var standardError = await process.StandardError.ReadToEndAsync();
|
||||
|
||||
_logger.LogDebug($"Process standard output: {standardOutput}");
|
||||
_logger.LogDebug($"Process standard error: {standardError}");
|
||||
|
||||
if (ThrowOnNonZeroExitCode && process.ExitCode != 0)
|
||||
throw new ApplicationException($"Process has terminated with an exit code {process.ExitCode}");
|
||||
|
||||
return MatchRegex
|
||||
return Task.FromResult(MatchRegex
|
||||
.Matches(ParseStdErr ? standardError : standardOutput)
|
||||
.Where(x => x.Success)
|
||||
.Select(x => x.Groups["threat"].Value)
|
||||
.ToArray();
|
||||
.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user