Skip to content

Commit

Permalink
Feature: Detect executable in AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
BornToBeRoot committed Dec 22, 2024
1 parent 378512a commit 3d2833e
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Source/NETworkManager.Models/ApplicationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static Canvas GetIcon(ApplicationName name)
canvas.Children.Add(new PackIconFontAwesome { Kind = PackIconFontAwesomeKind.TerminalSolid });
break;
case ApplicationName.AWSSessionManager:
canvas.Children.Add(new PackIconMaterial { Kind = PackIconMaterialKind.Aws });
canvas.Children.Add(new PackIconFontAwesome { Kind = PackIconFontAwesomeKind.AwsBrands });
break;
case ApplicationName.TigerVNC:
canvas.Children.Add(new PackIconMaterial { Kind = PackIconMaterialKind.EyeOutline });
Expand Down
27 changes: 13 additions & 14 deletions Source/NETworkManager.Models/PowerShell/PowerShell.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using log4net;
using Microsoft.Win32;

namespace NETworkManager.Models.PowerShell;
Expand All @@ -11,20 +11,18 @@ namespace NETworkManager.Models.PowerShell;
/// </summary>
public static class PowerShell
{
private static readonly ILog Log = LogManager.GetLogger(typeof(PowerShell));

/// <summary>
/// Default installation paths for PowerShell.
/// Windows PowerShell file name.
/// </summary>
public const string WindowsPowerShellFileName = "powershell.exe";

/// <summary>
/// PowerShell Core file name.
/// </summary>
public const string PwshFileName = "pwsh.exe";

public static readonly List<string> GetDefaultInstallationPaths =
[
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "PowerShell", "7", "pwsh.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "PowerShell", "7",
"pwsh.exe"),

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),
@"System32\WindowsPowerShell\v1.0\powershell.exe")
];

/// <summary>
/// Default SZ registry keys for the global PowerShell profile.
/// </summary>
Expand Down Expand Up @@ -92,12 +90,13 @@ public static void WriteDefaultProfileToRegistry(string theme, string powerShell

// Windows PowerShell --> HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
if (powerShellPath.StartsWith(systemRoot))
registryPath += "%SystemRoot%" + powerShellPath
.Substring(systemRoot.Length, powerShellPath.Length - systemRoot.Length).Replace(@"\", "_");
registryPath += "%SystemRoot%" + powerShellPath.Substring(systemRoot.Length, powerShellPath.Length - systemRoot.Length).Replace(@"\", "_");
// PWSH --> HKCU:\Console\C:_Program Files_PowerShell_7_pwsh.exe
else
registryPath += powerShellPath.Replace(@"\", "_");

Log.Info($"Registry path for PowerShell profile: \"{registryPath}\"");

var registryKey = Registry.CurrentUser.OpenSubKey(registryPath, true);

registryKey ??= Registry.CurrentUser.CreateSubKey(registryPath);
Expand Down
4 changes: 2 additions & 2 deletions Source/NETworkManager.Models/PuTTY/PuTTY.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace NETworkManager.Models.PuTTY;
/// <summary>
/// Class control PuTTY.
/// </summary>
public class PuTTY
public static class PuTTY
{
/// <summary>
/// PuTTY file name.
/// </summary>
public static readonly string FileName = "putty.exe";
public const string FileName = "putty.exe";

/// <summary>
/// Default SZ registry keys for PuTTY profile NETworkManager.
Expand Down
55 changes: 19 additions & 36 deletions Source/NETworkManager.Settings/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,6 @@ public static void Upgrade(Version fromVersion, Version toVersion)
{
Log.Info($"Start settings upgrade from {fromVersion} to {toVersion}...");

// 2022.12.20.0
if (fromVersion < new Version(2022, 12, 20, 0))
UpgradeTo_2022_12_20_0();

// 2023.3.7.0
if (fromVersion < new Version(2023, 3, 7, 0))
UpgradeTo_2023_3_7_0();
Expand All @@ -193,6 +189,11 @@ public static void Upgrade(Version fromVersion, Version toVersion)
if (fromVersion < new Version(2023, 11, 28, 0))
UpgradeTo_2023_11_28_0();


// 2024.11.11.0
if (fromVersion < new Version(2024, 11, 11, 0))
UpgradeTo_2024_11_11_0();

// Latest
if (fromVersion < toVersion)
UpgradeToLatest(toVersion);
Expand All @@ -204,35 +205,7 @@ public static void Upgrade(Version fromVersion, Version toVersion)
Log.Info("Settings upgrade finished!");
}

/// <summary>
/// Method to apply changes for version 2022.12.20.0.
/// </summary>
private static void UpgradeTo_2022_12_20_0()
{
Log.Info("Apply update to 2022.12.20.0...");

// Add AWS Session Manager application
Log.Info("Add new app \"AWSSessionManager\"...");
Current.General_ApplicationList.Add(ApplicationManager.GetDefaultList()
.First(x => x.Name == ApplicationName.AWSSessionManager));

var powerShellPath = "";
foreach (var file in PowerShell.GetDefaultInstallationPaths.Where(File.Exists))
{
powerShellPath = file;
break;
}

Log.Info($"Set \"AWSSessionManager_ApplicationFilePath\" to \"{powerShellPath}\"...");
Current.AWSSessionManager_ApplicationFilePath = powerShellPath;

// Add Bit Calculator application
Log.Info("Add new app \"BitCalculator\"...");
Current.General_ApplicationList.Add(ApplicationManager.GetDefaultList()
.First(x => x.Name == ApplicationName.BitCalculator));
}

/// <summary>
/// <summary>
/// Method to apply changes for version 2023.3.7.0.
/// </summary>
private static void UpgradeTo_2023_3_7_0()
Expand Down Expand Up @@ -326,6 +299,18 @@ private static void UpgradeTo_2023_11_28_0()
Current.DNSLookup_DNSServers =
new ObservableCollection<DNSServerConnectionInfoProfile>(DNSServer.GetDefaultList());
}

/// <summary>
/// Method to apply changes for version 2024.11.11.0.
/// </summary>
private static void UpgradeTo_2024_11_11_0()
{
Log.Info("Apply upgrade to 2024.11.11.0...");

Log.Info("Reset ApplicationList to default...");
Current.General_ApplicationList =
new ObservableSetCollection<ApplicationInfo>(ApplicationManager.GetDefaultList());
}

/// <summary>
/// Method to apply changes for the latest version.
Expand All @@ -335,9 +320,7 @@ private static void UpgradeToLatest(Version version)
{
Log.Info($"Apply upgrade to {version}...");

Log.Info("Reset ApplicationList to default...");
Current.General_ApplicationList =
new ObservableSetCollection<ApplicationInfo>(ApplicationManager.GetDefaultList());

}

#endregion
Expand Down
50 changes: 2 additions & 48 deletions Source/NETworkManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,6 @@ private void SettingsManager_PropertyChanged(object sender, PropertyChangedEvent
case nameof(SettingsInfo.Network_CustomDNSServer):
ConfigureDNSServer();

break;

// Update PowerShell profile if changed in the settings
case nameof(SettingsInfo.Appearance_PowerShellModifyGlobalProfile):
case nameof(SettingsInfo.Appearance_Theme):
case nameof(SettingsInfo.PowerShell_ApplicationFilePath):
case nameof(SettingsInfo.AWSSessionManager_ApplicationFilePath):
// Skip on welcome dialog
if (SettingsManager.Current.WelcomeDialog_Show)
return;

WriteDefaultPowerShellProfileToRegistry();

break;
}
}
Expand Down Expand Up @@ -538,15 +525,6 @@ await this.ShowMessageAsync(Strings.SettingsHaveBeenReset,
SettingsManager.Current.SNTPLookup_SNTPServers =
new ObservableCollection<ServerConnectionInfoProfile>(SNTPServer.GetDefaultList());

// Check if PowerShell is installed
foreach (var file in PowerShell.GetDefaultInstallationPaths.Where(File.Exists))
{
SettingsManager.Current.PowerShell_ApplicationFilePath = file;
SettingsManager.Current.AWSSessionManager_ApplicationFilePath = file;

break;
}

SettingsManager.Current.WelcomeDialog_Show = false;

// Save it to create a settings file
Expand Down Expand Up @@ -593,9 +571,6 @@ private void Load()
NetworkChange.NetworkAvailabilityChanged += (_, _) => OnNetworkHasChanged();
NetworkChange.NetworkAddressChanged += (_, _) => OnNetworkHasChanged();

// Set PowerShell global profile
WriteDefaultPowerShellProfileToRegistry();

// Search for updates...
if (SettingsManager.Current.Update_CheckForUpdatesAtStartup)
CheckForUpdates();
Expand Down Expand Up @@ -1895,28 +1870,7 @@ private void ConfigureDNSServer()

DNSClient.GetInstance().Configure(dnsSettings);
}

private void WriteDefaultPowerShellProfileToRegistry()
{
if (!SettingsManager.Current.Appearance_PowerShellModifyGlobalProfile)
return;

HashSet<string> paths = [];

// PowerShell
if (!string.IsNullOrEmpty(SettingsManager.Current.PowerShell_ApplicationFilePath) &&
File.Exists(SettingsManager.Current.PowerShell_ApplicationFilePath))
paths.Add(SettingsManager.Current.PowerShell_ApplicationFilePath);

// AWS Session Manager
if (!string.IsNullOrEmpty(SettingsManager.Current.AWSSessionManager_ApplicationFilePath) &&
File.Exists(SettingsManager.Current.AWSSessionManager_ApplicationFilePath))
paths.Add(SettingsManager.Current.AWSSessionManager_ApplicationFilePath);

foreach (var path in paths)
PowerShell.WriteDefaultProfileToRegistry(SettingsManager.Current.Appearance_Theme, path);
}


#endregion

#region Status window
Expand Down Expand Up @@ -2003,4 +1957,4 @@ private async void FocusEmbeddedWindow()
}

#endregion
}
}
Loading

0 comments on commit 3d2833e

Please sign in to comment.