Skip to content

Commit

Permalink
refactor: Apply the async logic for Credentials and Updates registry …
Browse files Browse the repository at this point in the history
…settings, remove obsolete code

- Moved async registry loading logic to OptRegistryCredentialsPage and OptRegistryUpdatesPage to improve structure and maintainability.
- Removed obsolete logic and redundant code that is no longer needed due to the new asynchronous architecture.
- Enhanced settings management to take advantage of the just-in-time loading mechanism.
- Improved performance by ensuring registry settings are loaded asynchronously right before they are needed by the UI or application logic, minimizing unnecessary loads.
- Update documents
  • Loading branch information
xRushG committed Oct 1, 2024
1 parent 9b28b70 commit 5e3bce9
Show file tree
Hide file tree
Showing 13 changed files with 754 additions and 622 deletions.
55 changes: 55 additions & 0 deletions mRemoteNG/App/Info/WindowsRegistryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,72 @@ public static class WindowsRegistryInfo

#region Key Locations

// StartupExit
// Registry subkey for general application startup and exit settings
// Registry subkey for startup and exit options page settings
public const string StartupExit = RootKey + "\\StartupExit";
public const string StartupExitOptions = StartupExit + "\\" + OptionsSubKey;

// Appearance
// Registry subkey for general application appearance settings
// Registry subkey for appearance options page settings
public const string Appearance = RootKey + "\\Appearance";
public const string AppearanceOptions = Appearance + "\\" + OptionsSubKey;

// Connections
// Registry subkey for general application connection settings
// Registry subkey for connections options page settings
public const string Connection = RootKey + "\\Connections";
public const string ConnectionOptions = Connection + "\\" + OptionsSubKey;

// Tabs & Panels
// Registry subkey for general application tabs and panels settings
// Registry subkey for tabs and panels options page settings
public const string TabsAndPanels = RootKey + "\\TabsAndPanels";
public const string TabsAndPanelsOptions = TabsAndPanels + "\\" + OptionsSubKey;

// Notifications
// Registry subkey for general application notifications settings
// Registry subkey for notifications options page settings
public const string Notification = RootKey + "\\Notifications";
public const string NotificationOptions = Notification + "\\" + OptionsSubKey;

// Credential
// Registry subkey for general application credentials settings
// Registry subkey for credentials options page settings
public const string Credential = RootKey + "\\Credentials";
public const string CredentialOptions = Credential + "\\" + OptionsSubKey;

// SQL Server
// Registry subkey for general application SQL server settings
// Registry subkey for SQL server options page settings
public const string SQLServer = RootKey + "\\SQLServer";
public const string SQLServerOptions = SQLServer + "\\" + OptionsSubKey;

// Updates
// Registry subkey for general application update settings
// Registry subkey for updates options page settings
public const string Update = RootKey + "\\Updates";
public const string UpdateOptions = Update + "\\" + OptionsSubKey;

// Security
// Registry subkey for general application security settings
// Registry subkey for security options page settings
public const string Security = RootKey + "\\Security";
public const string SecurityOptions = Security + "\\" + OptionsSubKey;

// Advanced
// Registry subkey for general application advanced settings
// Registry subkey for advanced options page settings
public const string Advanced = RootKey + "\\Advanced";
public const string AdvancedOptions = Advanced + "\\" + OptionsSubKey;

// Backup
// Registry subkey for general application backup settings
// Registry subkey for backup options page settings
public const string Backup = RootKey + "\\Backup";
public const string BackupOptions = Backup + "\\" + OptionsSubKey;

#endregion
}
}
15 changes: 4 additions & 11 deletions mRemoteNG/Config/Settings/Registry/CommonRegistrySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
namespace mRemoteNG.Config.Settings.Registry
{
[SupportedOSPlatform("windows")]
/// Static utility class that provides access to and management of registry settings on the local machine.
/// It abstracts complex registry operations and centralizes the handling of various registry keys.
/// Benefits: Simplified code, enhances maintainability, and ensures consistency. #ReadOnly
public static class CommonRegistrySettings
{
#region general update registry settings
Expand Down Expand Up @@ -37,10 +34,6 @@ public static class CommonRegistrySettings
/// </remarks>
public static bool AllowCheckForUpdatesManual { get; }

/// <summary>
/// Specifies whether a question about checking for updates is displayed at startup.
/// </summary>
public static bool AllowPromptForUpdatesPreference { get; }

#endregion

Expand Down Expand Up @@ -70,21 +63,21 @@ public static class CommonRegistrySettings

static CommonRegistrySettings()
{
IRegistry regValueUtility = new WinRegistry();
IRegistryRead regValueUtility = new WinRegistry();
RegistryHive hive = WindowsRegistryInfo.Hive;

#region update registry settings setup
#region update registry settings

string updateSubkey = WindowsRegistryInfo.Update;

AllowCheckForUpdates = regValueUtility.GetBoolValue(hive, updateSubkey, nameof(AllowCheckForUpdates), true);
AllowCheckForUpdatesAutomatical = regValueUtility.GetBoolValue(hive, updateSubkey, nameof(AllowCheckForUpdatesAutomatical), AllowCheckForUpdates);
AllowCheckForUpdatesManual = regValueUtility.GetBoolValue(hive, updateSubkey, nameof(AllowCheckForUpdatesManual), AllowCheckForUpdates);
AllowPromptForUpdatesPreference = regValueUtility.GetBoolValue(hive, updateSubkey, nameof(AllowPromptForUpdatesPreference), AllowCheckForUpdates);


#endregion

#region credential registry settings setup
#region credential registry settings

string credentialSubkey = WindowsRegistryInfo.Credential;

Expand Down
138 changes: 117 additions & 21 deletions mRemoteNG/Config/Settings/Registry/OptRegistryCredentialsPage.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,178 @@
using System.Runtime.Versioning;
using Microsoft.Win32;
using mRemoteNG.App;
using mRemoteNG.App.Info;
using mRemoteNG.Security.SymmetricEncryption;
using mRemoteNG.Tools.WindowsRegistry;

namespace mRemoteNG.Config.Settings.Registry
{
[SupportedOSPlatform("windows")]
/// Static utility class that provides access to and management of registry settings on the local machine.
/// It abstracts complex registry operations and centralizes the handling of various registry keys.
/// Benefits: Simplified code, enhances maintainability, and ensures consistency. #ReadOnly
public sealed partial class OptRegistryCredentialsPage
{
#region option page credential registry settings

/// <summary>
/// Indicates whether modifying credential page settings is enabled.
/// </summary>
public bool CredentialPageEnabled { get; }

/// <summary>
/// Specifies the radio button is set to none, windows or custom on the credentials page.
/// </summary>
/// <remarks>
/// When set to noinfo or windows, WindowsCredentials and CustomCredentials are not evaluated and disabled.
/// </remarks>
public WinRegistryEntry<string> UseCredentials { get; }
public WinRegistryEntry<string> UseCredentials { get; private set; }

/// <summary>
/// Specifies the user set via API as the default username.
/// </summary>
/// <remarks>
/// Only avaiable if UseCredentials is set to custom!
/// </remarks>
public WinRegistryEntry<string> UserViaAPIDefault { get; }
public WinRegistryEntry<string> UserViaAPIDefault { get; private set; }

/// <summary>
/// Specifies the default username.
/// </summary>
/// <remarks>
/// Only avaiable if UseCredentials is set to custom!
/// </remarks>
public WinRegistryEntry<string> DefaultUsername { get; }
public WinRegistryEntry<string> DefaultUsername { get; private set; }

/// <summary>
/// Specifies the default password.
/// </summary>
/// <remarks>
/// Only avaiable if UseCredentials is set to custom!
/// </remarks>
public WinRegistryEntry<string> DefaultPassword { get; }
public WinRegistryEntry<string> DefaultPassword { get; private set; }

/// <summary>
/// Specifies the default domain.
/// </summary>
/// <remarks>
/// Only avaiable if UseCredentials is set to custom!
/// </remarks>
public WinRegistryEntry<string> DefaultDomain { get; }
public WinRegistryEntry<string> DefaultDomain { get; private set; }

/// <summary>
/// Specifies that entering the custom default username field is enabled.
/// </summary>
public bool DefaultUsernameEnabled { get; private set; }

/// <summary>
/// Specifies that entering the custom default password field is enabled.
/// </summary>
public bool DefaultPasswordEnabled { get; private set; }

/// <summary>
/// Specifies that entering the custom default api user field is enabled.
/// </summary>
public bool DefaultUserViaAPIEnabled { get; private set; }

#endregion

public OptRegistryCredentialsPage()
{
IRegistry regValueUtility = new WinRegistry();
IRegistryRead regValueUtility = new WinRegistry();
RegistryHive hive = WindowsRegistryInfo.Hive;
string subKey = WindowsRegistryInfo.CredentialOptions;

CredentialPageEnabled = regValueUtility.GetBoolValue(hive, subKey, nameof(CredentialPageEnabled), true);
UseCredentials = new WinRegistryEntry<string>(hive, subKey, nameof(UseCredentials)).SetValidation(
new string[] {
"noinfo",
"windows",
"custom"
}).Read();
UseCredentials = new WinRegistryEntry<string>(hive, subKey, nameof(UseCredentials)).Read();
UserViaAPIDefault = new WinRegistryEntry<string>(hive, subKey, nameof(UserViaAPIDefault)).Read();
DefaultUsername = new WinRegistryEntry<string>(hive, subKey, nameof(DefaultUsername)).Read();
DefaultPassword = new WinRegistryEntry<string>(hive, subKey, nameof(DefaultPassword)).Read();
DefaultDomain = new WinRegistryEntry<string>(hive, subKey, nameof(DefaultDomain)).Read();

DefaultUsernameEnabled = regValueUtility.GetBoolValue(hive, subKey, nameof(DefaultUsernameEnabled), true);
DefaultPasswordEnabled = regValueUtility.GetBoolValue(hive, subKey, nameof(DefaultPasswordEnabled), true);
DefaultUserViaAPIEnabled = regValueUtility.GetBoolValue(hive, subKey, nameof(DefaultUserViaAPIEnabled), true);

SetupValidation();
Apply();
}

/// <summary>
/// Configures validation settings for various parameters
/// </summary>
private void SetupValidation()
{
UseCredentials.SetValidation(
new string[] {
"noinfo",
"windows",
"custom"
});
}

/// <summary>
/// Applies registry settings and overrides various properties.
/// </summary>
private void Apply()
{
// UseCredentials musst be present in registry.
if (! UseCredentials.IsSet)
return;
ApplyUseCredentials();

// UseCredentials musst be set to custom.
if (UseCredentials.Value != "custom")
return;

ApplyDefaultUsername();
ApplyDefaultPassword();
ApplyDefaultDomain();
ApplyUserViaAPIDefault();
}

private void ApplyUseCredentials()
{
if (UseCredentials.IsValid)
Properties.OptionsCredentialsPage.Default.EmptyCredentials = UseCredentials.Value;
}

private void ApplyDefaultUsername()
{
if (DefaultUsername.IsSet && DefaultUsernameEnabled)
Properties.OptionsCredentialsPage.Default.DefaultUsername = DefaultUsername.Value;
else if (!DefaultUsernameEnabled)
Properties.OptionsCredentialsPage.Default.DefaultUsername = "";
}

private void ApplyDefaultPassword()
{
if (DefaultPassword.IsSet && DefaultPasswordEnabled)
{
try
{
LegacyRijndaelCryptographyProvider cryptographyProvider = new();
string decryptedPassword;
string defaultPassword = DefaultPassword.Value;

decryptedPassword = cryptographyProvider.Decrypt(defaultPassword, Runtime.EncryptionKey);
Properties.OptionsCredentialsPage.Default.DefaultPassword = defaultPassword;
}
catch
{
// Fire-and-forget: The DefaultPassword in the registry is not encrypted.
DefaultPassword.Clear();
}
}
else if (!DefaultPasswordEnabled)
{
Properties.OptionsCredentialsPage.Default.DefaultPassword = "";
}
}

private void ApplyDefaultDomain()
{
if (DefaultDomain.IsSet)
Properties.OptionsCredentialsPage.Default.DefaultDomain = DefaultDomain.Value;
}

private void ApplyUserViaAPIDefault()
{
if (UserViaAPIDefault.IsSet && DefaultUserViaAPIEnabled)
Properties.OptionsCredentialsPage.Default.UserViaAPIDefault = UserViaAPIDefault.Value;
else if (!DefaultUserViaAPIEnabled)
Properties.OptionsCredentialsPage.Default.UserViaAPIDefault = "";
}
}
}
Loading

0 comments on commit 5e3bce9

Please sign in to comment.