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 }
|
|
|
|
|
|
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
|
|
|
|
|
|
2021-04-24 02:59:39 +02:00
|
|
|
|
private static readonly Regex RX_PROGRESS = new Regex(@"(\d+)\.(\d)%", RegexOptions.Compiled);
|
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!");
|
|
|
|
|
}
|
2021-04-24 02:59:39 +02:00
|
|
|
|
Dictionary<string, string> environmentVariables = new Dictionary<string, string>();
|
|
|
|
|
environmentVariables.Add("SLUNK_PASSPHRASE", password);
|
2022-04-04 23:55:09 +02:00
|
|
|
|
environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false)));
|
|
|
|
|
environmentVariables.Add("SLUNK_THREADS", Convert.ToString(Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0))));
|
2022-10-15 18:32:54 +02:00
|
|
|
|
environmentVariables.Add("SLUNK_LEGACY_COMPAT", Convert.ToString(Convert.ToInt32(options.HasValue ? options.Value.enableLegacyCompat : false)));
|
|
|
|
|
environmentVariables.Add("SLUNK_DEBUG_LOGGING", Convert.ToString(Convert.ToInt32(ENABLE_DEBUG_LOGGING)));
|
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 { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// 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
|
|
|
|
double temp, result = double.NaN;
|
|
|
|
|
int index = int.MaxValue;
|
|
|
|
|
Match match = RX_PROGRESS.Match(currentLine);
|
|
|
|
|
while (match.Success)
|
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
|
|
|
|
{
|
2022-02-10 22:55:52 +01:00
|
|
|
|
result = temp;
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
2022-02-10 22:55:52 +01:00
|
|
|
|
index = Math.Min(index, match.Index);
|
|
|
|
|
match = RX_PROGRESS.Match(currentLine, match.Index + match.Length);
|
|
|
|
|
}
|
|
|
|
|
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!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|