Skip to content

Free, open-source Windows optimization tool for performance, privacy, and simplicity.

Refactor base64 encoding/decoding in ShellService and extensions

itsfatduck60e4adda9117
diff --git a/optimizerDuck/src/Core/Defaults.cs b/optimizerDuck/src/Core/Defaults.cs
index 1a8a633..b36d8df 100644
--- a/optimizerDuck/src/Core/Defaults.cs
+++ b/optimizerDuck/src/Core/Defaults.cs
@@ -58,7 +58,7 @@ public static class Defaults
public static readonly string ExePath = Environment.ProcessPath!;
public static readonly string ExeDir = Path.GetDirectoryName(ExePath)!;
- public static readonly string ExeName = Path.GetFileName(ExePath)!;
+ public static readonly string ExeName = Path.GetFileName(ExePath);
public static readonly string FileVersion = FileVersionInfo.GetVersionInfo(ExePath).FileVersion!;
public static readonly string RootPath =
diff --git a/optimizerDuck/src/Core/Extensions/TextExtensions.cs b/optimizerDuck/src/Core/Extensions/TextExtensions.cs
index c14c133..6e295fa 100644
--- a/optimizerDuck/src/Core/Extensions/TextExtensions.cs
+++ b/optimizerDuck/src/Core/Extensions/TextExtensions.cs
@@ -94,4 +94,35 @@ public static class TextExtensions
return xml;
}
}
+
+ public static string EncodeBase64(this string value)
+ {
+ if (string.IsNullOrEmpty(value))
+ return string.Empty;
+ try
+ {
+ var bytes = Encoding.Unicode.GetBytes(value);
+ return Convert.ToBase64String(bytes);
+ }
+ catch
+ {
+ return value;
+ }
+ }
+
+ public static string DecodeBase64(this string value)
+ {
+ if (string.IsNullOrEmpty(value))
+ return string.Empty;
+
+ try
+ {
+ var bytes = Convert.FromBase64String(value);
+ return Encoding.Unicode.GetString(bytes);
+ }
+ catch
+ {
+ return value;
+ }
+ }
}
\ No newline at end of file
diff --git a/optimizerDuck/src/Core/Services/ShellService.cs b/optimizerDuck/src/Core/Services/ShellService.cs
index 5226c7c..adacc2b 100644
--- a/optimizerDuck/src/Core/Services/ShellService.cs
+++ b/optimizerDuck/src/Core/Services/ShellService.cs
@@ -19,7 +19,7 @@ public static class ShellService
private static ShellResult Run(string fileName, string arguments, string command, string serviceName)
{
var fullCommand =
- $"{fileName} {arguments.Replace("-EncodedCommand", "-Command")} {DecodeBase64(command)}"; // let user see the real command
+ $"{fileName} {arguments.Replace("-EncodedCommand", "-Command")} {command.DecodeBase64()}"; // let user see the real command
var psi = new ProcessStartInfo
{
@@ -94,31 +94,7 @@ public static class ShellService
command = "$ProgressPreference='SilentlyContinue'; " + command; // to hide progress bar clixml
return Run("powershell.exe",
"-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand",
- EncodePowerShellCommand(command),
+ command.EncodeBase64(),
nameof(PowerShell));
}
-
- /// <summary>
- /// encode command to base64 for better script handling
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- public static string EncodePowerShellCommand(string command)
- {
- var bytes = Encoding.Unicode.GetBytes(command);
- return Convert.ToBase64String(bytes);
- }
-
- private static string DecodeBase64(string value)
- {
- try
- {
- var bytes = Convert.FromBase64String(value);
- return Encoding.Unicode.GetString(bytes);
- }
- catch
- {
- return string.Empty;
- }
- }
}
\ No newline at end of file
diff --git a/optimizerDuck/src/Core/Services/UpdateService.cs b/optimizerDuck/src/Core/Services/UpdateService.cs
index be7411b..a7f700c 100644
--- a/optimizerDuck/src/Core/Services/UpdateService.cs
+++ b/optimizerDuck/src/Core/Services/UpdateService.cs
@@ -10,6 +10,7 @@ using Spectre.Console;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Reflection;
+using optimizerDuck.Core.Extensions;
namespace optimizerDuck.src.Core.Services;
@@ -61,7 +62,7 @@ public class UpdateService
if (latestVersion > currentVersion)
{
- var asset = latestRelease.Assets?
+ var asset = latestRelease.Assets
.FirstOrDefault(a =>
a.Name.StartsWith("optimizerDuck", StringComparison.OrdinalIgnoreCase)
&& a.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase));
@@ -177,7 +178,7 @@ public class UpdateService
StartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
- Arguments = $"-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand {ShellService.EncodePowerShellCommand(script)}",
+ Arguments = $"-NonInteractive -NoLogo -NoProfile -ExecutionPolicy Bypass -EncodedCommand {script.EncodeBase64()}",
UseShellExecute = true, // start as a separate process via the shell so it doesn't inherit debugger/std handles
CreateNoWindow = true
}