From e4f28439c3590a9ff254d6403abb3d4e597832f8 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Mon, 4 Apr 2022 23:55:09 +0200 Subject: [PATCH] GUI: Small improvement to setting up the PATH and working directory of the child process. --- gui/Process/PasswordGen.cs | 2 +- gui/Process/SlunkCryptRunner.cs | 12 ++++++++---- gui/SlunkCryptGUI.csproj | 1 + gui/Utilities/EnumHelper.cs | 24 ++++++++++++++++++++++++ gui/Utilities/PathUtils.cs | 30 ++++++++++++++++++++++++++++++ gui/Utilities/ProcessRunner.cs | 37 +++++++++++++++++++++++++------------ 6 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 gui/Utilities/EnumHelper.cs diff --git a/gui/Process/PasswordGen.cs b/gui/Process/PasswordGen.cs index cb13037..1b3c1ec 100644 --- a/gui/Process/PasswordGen.cs +++ b/gui/Process/PasswordGen.cs @@ -42,7 +42,7 @@ namespace com.muldersoft.slunkcrypt.gui.process using (FileStream executableFile = ExecutableHelper.GetExecutableFile()) { string[] arguments = new string[] { COMMAND_PASSWRD, string.Format("{0:D}", length) }; - Tuple result = await ProcessRunner.ExecAsnyc(executableFile.Name, arguments, null, ProcessPriorityClass.BelowNormal, TIMEOUT); + Tuple result = await ProcessRunner.ExecAsnyc(executableFile.Name, arguments, null, null, ProcessPriorityClass.BelowNormal, TIMEOUT); if (result.Item1 == 0) { foreach (string password in result.Item2) diff --git a/gui/Process/SlunkCryptRunner.cs b/gui/Process/SlunkCryptRunner.cs index 5229f7f..6992cfa 100644 --- a/gui/Process/SlunkCryptRunner.cs +++ b/gui/Process/SlunkCryptRunner.cs @@ -52,11 +52,15 @@ namespace com.muldersoft.slunkcrypt.gui.process public async Task ExecuteAsync(Mode mode, string inputFile, string outputFile, string password, SlunkOptions? options = null) { + if ((!EnumHelper.IsDefined(mode)) || string.IsNullOrWhiteSpace(inputFile) || string.IsNullOrWhiteSpace(outputFile) || string.IsNullOrWhiteSpace(password)) + { + throw new ArgumentException("Invalid SlunkCrypt parameters!"); + } Dictionary environmentVariables = new Dictionary(); environmentVariables.Add("SLUNK_PASSPHRASE", password); - environmentVariables.Add("SLUNK_KEEP_INCOMPLETE", Convert.ToInt32(options.HasValue ? options.Value.keepIncompleteFiles : false).ToString()); - environmentVariables.Add("SLUNK_THREADS", Math.Max(0, Math.Min(32, options.HasValue ? options.Value.threadCount : 0)).ToString()); - return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandLineModeString(mode), inputFile, outputFile }, environmentVariables); + 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)))); + return await ExecAsnyc(m_executableFile.Name, new string[] { GetCommandString(mode), inputFile, outputFile }, Path.GetDirectoryName(outputFile), environmentVariables); } public override void Dispose() @@ -106,7 +110,7 @@ namespace com.muldersoft.slunkcrypt.gui.process return false; } - private static string GetCommandLineModeString(Mode mode) + private static string GetCommandString(Mode mode) { switch(mode) { diff --git a/gui/SlunkCryptGUI.csproj b/gui/SlunkCryptGUI.csproj index e79c0f6..05035fa 100644 --- a/gui/SlunkCryptGUI.csproj +++ b/gui/SlunkCryptGUI.csproj @@ -81,6 +81,7 @@ PasswordToggleBox.xaml + diff --git a/gui/Utilities/EnumHelper.cs b/gui/Utilities/EnumHelper.cs new file mode 100644 index 0000000..ae399c0 --- /dev/null +++ b/gui/Utilities/EnumHelper.cs @@ -0,0 +1,24 @@ +/******************************************************************************/ +/* SlunkCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +using System; + +namespace com.muldersoft.slunkcrypt.gui.utils +{ + public static class EnumHelper + { + public static bool IsDefined(T value) where T : struct, IConvertible + { + try + { + return Enum.IsDefined(typeof(T), value); + } + catch + { + return false; + } + } + } +} diff --git a/gui/Utilities/PathUtils.cs b/gui/Utilities/PathUtils.cs index 66fb742..9dcc229 100644 --- a/gui/Utilities/PathUtils.cs +++ b/gui/Utilities/PathUtils.cs @@ -104,6 +104,17 @@ namespace com.muldersoft.slunkcrypt.gui.utils catch { } } + public static string CreatePathSpec(params string[] entries) + { + StringBuilder pathBuilder = new StringBuilder(); + foreach (string pathEntry in entries) + { + AppendPathEntry(pathBuilder, pathEntry); + } + AppendPathEntry(pathBuilder, Environment.SystemDirectory); + return pathBuilder.ToString(); + } + // ============================================================================= // Internal methods // ============================================================================= @@ -180,6 +191,25 @@ namespace com.muldersoft.slunkcrypt.gui.utils } } + private static void AppendPathEntry(StringBuilder builder, string pathEntry) + { + if (!string.IsNullOrWhiteSpace(pathEntry)) + { + if (builder.Length > 0) + { + builder.Append(Path.PathSeparator); + } + if (pathEntry.IndexOf(Path.PathSeparator) == -1) + { + builder.Append(pathEntry.Trim()); + } + else + { + builder.Append('"').Append(pathEntry.Trim()).Append('"'); + } + } + } + private static bool HasDrivePrefix(string filePath) { return (filePath.Length > 1) && IsUsEnglishLetter(filePath[0]) && (filePath[1] == Path.VolumeSeparatorChar); diff --git a/gui/Utilities/ProcessRunner.cs b/gui/Utilities/ProcessRunner.cs index 7ffd8e3..d0e89ad 100644 --- a/gui/Utilities/ProcessRunner.cs +++ b/gui/Utilities/ProcessRunner.cs @@ -110,7 +110,7 @@ namespace com.muldersoft.slunkcrypt.gui.utils // Public methods // ============================================================================= - public async Task ExecAsnyc(string executablePath, string[] arguments = null, IReadOnlyDictionary environmentVariables = null) + public async Task ExecAsnyc(string executablePath, string[] arguments = null, string workingDirectory = null, IReadOnlyDictionary environmentVariables = null) { m_dispatcher.VerifyAccess(); if (m_disposed) @@ -124,7 +124,7 @@ namespace com.muldersoft.slunkcrypt.gui.utils m_running = true; try { - return await DoExecAsnyc(executablePath, arguments, environmentVariables); + return await DoExecAsnyc(executablePath, arguments, workingDirectory, environmentVariables); } finally { @@ -133,13 +133,13 @@ namespace com.muldersoft.slunkcrypt.gui.utils } } - public static async Task> ExecAsnyc(string executableFile, string[] arguments = null, IReadOnlyDictionary environmentVariables = null, ProcessPriorityClass? priorityClass = null, TimeSpan? timeout = null) + public static async Task> ExecAsnyc(string executableFile, string[] arguments = null, string workingDirectory = null, IReadOnlyDictionary environmentVariables = null, ProcessPriorityClass? priorityClass = null, TimeSpan? timeout = null) { using (Process process = new Process()) { Task hasExitedTask = InitializeProcess(process); - return await DoExecAsnyc(process, hasExitedTask, executableFile, arguments, environmentVariables, priorityClass, timeout); + return await DoExecAsnyc(process, hasExitedTask, executableFile, arguments, workingDirectory, environmentVariables, priorityClass, timeout); } } @@ -195,11 +195,11 @@ namespace com.muldersoft.slunkcrypt.gui.utils return new ProcessExitHandler(process).Task; } - private async Task DoExecAsnyc(string executablePath, string[] arguments, IReadOnlyDictionary environmentVariables) + private async Task DoExecAsnyc(string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary environmentVariables) { try { - StartProcess(m_process, executablePath, arguments, environmentVariables, m_priorityClass); + StartProcess(m_process, executablePath, arguments, workingDirectory, environmentVariables, m_priorityClass); } catch (Exception err) { @@ -208,11 +208,11 @@ namespace com.muldersoft.slunkcrypt.gui.utils return await WaitForExit(); } - private static async Task> DoExecAsnyc(Process process, Task hasExited, string executablePath, string[] arguments, IReadOnlyDictionary environmentVariables, ProcessPriorityClass? priorityClass, TimeSpan? timeout) + private static async Task> DoExecAsnyc(Process process, Task hasExited, string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary environmentVariables, ProcessPriorityClass? priorityClass, TimeSpan? timeout) { try { - StartProcess(process, executablePath, arguments, environmentVariables, priorityClass); + StartProcess(process, executablePath, arguments, workingDirectory, environmentVariables, priorityClass); } catch (Exception err) { @@ -222,12 +222,12 @@ namespace com.muldersoft.slunkcrypt.gui.utils return Tuple.Create(hasExited.Result, outputLines); } - private static void StartProcess(Process process, string executablePath, string[] arguments, IReadOnlyDictionary environmentVariables, ProcessPriorityClass? priorityClass) + private static void StartProcess(Process process, string executablePath, string[] arguments, string workingDirectory, IReadOnlyDictionary environmentVariables, ProcessPriorityClass? priorityClass) { process.StartInfo.FileName = executablePath; process.StartInfo.Arguments = CreateArgumentList(arguments); + process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? GetWorkingDirectory(executablePath) : workingDirectory; SetupEnvironment(process.StartInfo.EnvironmentVariables, executablePath, environmentVariables); - process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; process.Start(); SetProcessPriority(process, priorityClass); } @@ -390,10 +390,23 @@ namespace com.muldersoft.slunkcrypt.gui.utils return false; } + private static string GetWorkingDirectory(string executablePath) + { + try + { + string directory = Path.GetDirectoryName(executablePath); + if (!string.IsNullOrWhiteSpace(directory)) + { + return directory; + } + } + catch { } + return AppDomain.CurrentDomain.BaseDirectory; + } + private static void SetupEnvironment(StringDictionary dictionary, string executablePath, IReadOnlyDictionary environmentVariables) { - string baseDirectory = Path.GetDirectoryName(executablePath); - dictionary["PATH"] = string.IsNullOrEmpty(baseDirectory) ? Environment.SystemDirectory : baseDirectory; + dictionary["PATH"] = PathUtils.CreatePathSpec(Path.GetDirectoryName(executablePath)); if (!ReferenceEquals(environmentVariables, null)) { foreach (KeyValuePair entry in environmentVariables) -- 2.11.0