2021-04-24 02:59:39 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
|
|
|
using com.muldersoft.slunkcrypt.gui.utils;
|
|
|
|
|
|
|
|
|
|
namespace com.muldersoft.slunkcrypt.gui.process
|
|
|
|
|
{
|
|
|
|
|
internal class SlunkCryptRunner : ProcessRunner
|
|
|
|
|
{
|
|
|
|
|
public enum Mode { Encrypt, Decrypt }
|
|
|
|
|
|
2024-06-12 17:41:53 +02:00
|
|
|
|
public enum Error { Checksum, Password }
|
|
|
|
|
|
2022-04-03 14:58:59 +02:00
|
|
|
|
public struct SlunkOptions
|
|
|
|
|
{
|
2022-10-15 18:32:54 +02:00
|
|
|
|
public SlunkOptions(bool keepIncompleteFiles, int threadCount, bool enableLegacyCompat)
|
2022-04-03 14:58:59 +02:00
|
|
|
|
{
|
|
|
|
|
this.keepIncompleteFiles = keepIncompleteFiles;
|
|
|
|
|
this.threadCount = threadCount;
|
2022-10-15 18:32:54 +02:00
|
|
|
|
this.enableLegacyCompat = enableLegacyCompat;
|
2022-04-03 14:58:59 +02:00
|
|
|
|
}
|
2022-10-15 18:32:54 +02:00
|
|
|
|
public readonly bool keepIncompleteFiles, enableLegacyCompat;
|
2022-04-03 14:58:59 +02:00
|
|
|
|
public readonly int threadCount;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 02:59:39 +02:00
|
|
|
|
private const string COMMAND_ENCRYPT = "-e";
|
|
|
|
|
private const string COMMAND_DECRYPT = "-d";
|
|
|
|
|
|
2022-10-15 18:32:54 +02:00
|
|
|
|
#if DEBUG
|
|
|
|
|
private const bool ENABLE_DEBUG_LOGGING = true;
|
|
|
|
|
#else
|
|
|
|
|
private const bool ENABLE_DEBUG_LOGGING = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-06-12 17:41:53 +02:00
|
|
|
|
private static readonly Regex RX_PROGRESS = new Regex(@"(\d+)\.(\d)%", RegexOptions.Compiled | RegexOptions.CultureInvariant);
|
|
|
|
|
|
|
|
|
|
private static readonly IReadOnlyDictionary<Error, Regex> RX_ERROR = new Dictionary<Error, Regex>
|
|
|
|
|
{
|
|
|
|
|
{ Error.Checksum, new Regex(@"\bChecksum\s+mismatch\s+detected\b", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant) },
|
|
|
|
|
{ Error.Password, new Regex(@"\bThe\s+given\s+passphrase\s+is\s+forbidden\s+as\s+a\s+precautionary\s+measure\b", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant) }
|
|
|
|
|
}
|
|
|
|
|
.ToReadOnlyDictionary();
|
2021-11-07 21:18:33 +01:00
|
|
|
|
|
2021-04-24 02:59:39 +02:00
|
|
|
|
private readonly FileStream m_executableFile;
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Constructor
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
public SlunkCryptRunner(Dispatcher dispatcher, ProcessPriorityClass? priorityClass = null) : base(dispatcher, priorityClass)
|
|
|
|
|
{
|
2022-02-09 00:44:56 +01:00
|
|
|
|
m_executableFile = ExecutableHelper.GetExecutableFile();
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Public methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
2022-04-03 14:58:59 +02:00
|
|
|
|
public async Task<int> ExecuteAsync(Mode mode, string inputFile, string outputFile, string password, SlunkOptions? options = null)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
2022-04-04 23:55:09 +02:00
|
|
|
|
if ((!EnumHelper.IsDefined(mode)) || string.IsNullOrWhiteSpace(inputFile) || string.IsNullOrWhiteSpace(outputFile) || string.IsNullOrWhiteSpace(password))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Invalid SlunkCrypt parameters!");
|
|
|
|
|
}
|
2024-06-12 17:41:53 +02:00
|
|
|
|
Dictionary<string, string> environmentVariables = new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "SLUNK_PASSPHRASE", password },
|
|
|
|
|
{ "SLUNK_KEEP_INCOMPLETE", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false)) },
|
|
|
|
|
{ "SLUNK_THREADS", Convert.ToString(Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0))) },
|
|
|
|
|
{ "SLUNK_LEGACY_COMPAT", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.enableLegacyCompat : false)) },
|
|
|
|
|
{ "SLUNK_DEBUG_LOGGING", Convert.ToString(Convert.ToInt32(ENABLE_DEBUG_LOGGING)) }
|
|
|
|
|
};
|
|
|
|
|
ErrorState = null;
|
2022-04-04 23:55:09 +02:00
|
|
|
|
return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandString(mode), inputFile, outputFile }, Path.GetDirectoryName(outputFile), environmentVariables);
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
base.Dispose();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_executableFile.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 17:41:53 +02:00
|
|
|
|
public Error? ErrorState { get; private set; } = null;
|
|
|
|
|
|
2021-04-24 02:59:39 +02:00
|
|
|
|
// =============================================================================
|
|
|
|
|
// Internal methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
2022-02-10 22:55:52 +01:00
|
|
|
|
protected override double ParseProgressString(ref string currentLine, bool stream)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
int index = int.MaxValue;
|
2024-06-12 17:41:53 +02:00
|
|
|
|
double temp = double.NaN, result = double.NaN;
|
|
|
|
|
if (!ErrorState.HasValue)
|
|
|
|
|
{
|
|
|
|
|
foreach (KeyValuePair<Error, Regex> errorRegex in RX_ERROR)
|
|
|
|
|
{
|
|
|
|
|
if (errorRegex.Value.IsMatch(currentLine))
|
|
|
|
|
{
|
|
|
|
|
ErrorState = errorRegex.Key;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (Match match = RX_PROGRESS.Match(currentLine); match.Success; match = match.NextMatch())
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
if (TryParseProgressValue(match, out temp))
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
2024-06-12 17:41:53 +02:00
|
|
|
|
result = double.IsNaN(result) ? temp : Math.Max(result, temp);
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
2022-02-10 22:55:52 +01:00
|
|
|
|
index = Math.Min(index, match.Index);
|
|
|
|
|
}
|
|
|
|
|
if (index != int.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
currentLine = (index > 0) ? currentLine.Substring(0, index - 1).TrimEnd() : string.Empty;
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 22:55:52 +01:00
|
|
|
|
private static bool TryParseProgressValue(Match match, out double progress)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
|
|
|
|
uint intPart, fractPart;
|
|
|
|
|
if (uint.TryParse(match.Groups[1].Value, out intPart) && uint.TryParse(match.Groups[2].Value, out fractPart))
|
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
progress = ((intPart * 10) + fractPart) / 1000.0;
|
|
|
|
|
return true;
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
2022-02-10 22:55:52 +01:00
|
|
|
|
progress = double.NaN;
|
|
|
|
|
return false;
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-04 23:55:09 +02:00
|
|
|
|
private static string GetCommandString(Mode mode)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
|
|
|
|
switch(mode)
|
|
|
|
|
{
|
|
|
|
|
case Mode.Encrypt:
|
|
|
|
|
return COMMAND_ENCRYPT;
|
|
|
|
|
case Mode.Decrypt:
|
|
|
|
|
return COMMAND_DECRYPT;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentException("Invalid mode!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|