diff --git a/Agent/Program.cs b/Agent/Program.cs index c57e10da2..50f3da8dd 100644 --- a/Agent/Program.cs +++ b/Agent/Program.cs @@ -16,6 +16,7 @@ using Remotely.Agent.Services.Windows; using Microsoft.Extensions.Hosting; using System.Linq; +using Microsoft.Win32; namespace Remotely.Agent; @@ -50,6 +51,32 @@ public static async Task Main(string[] args) } } + private static async Task Init(IServiceProvider services) + { + var logger = services.GetRequiredService>(); + + AppDomain.CurrentDomain.UnhandledException += (sender, ex) => + { + if (ex.ExceptionObject is Exception exception) + { + logger.LogError(exception, "Unhandled exception in AppDomain."); + } + else + { + logger.LogError("Unhandled exception in AppDomain."); + } + }; + + SetWorkingDirectory(); + + if (OperatingSystem.IsWindows()) + { + SetSas(services, logger); + } + await services.GetRequiredService().BeginChecking(); + await services.GetRequiredService().Connect(); + } + private static void RegisterServices(IServiceCollection services) { services.AddHttpClient(); @@ -78,12 +105,14 @@ private static void RegisterServices(IServiceCollection services) services.AddScoped(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } else if (OperatingSystem.IsLinux()) { services.AddScoped(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } else if (OperatingSystem.IsMacOS()) { @@ -97,26 +126,23 @@ private static void RegisterServices(IServiceCollection services) } } - private static async Task Init(IServiceProvider services) + [SupportedOSPlatform("windows")] + private static void SetSas(IServiceProvider services, ILogger logger) { - AppDomain.CurrentDomain.UnhandledException += (sender, ex) => + try { - var logger = services.GetRequiredService>(); - if (ex.ExceptionObject is Exception exception) + var elevationDetector = services.GetRequiredService(); + if (elevationDetector.IsElevated()) { - logger.LogError(exception, "Unhandled exception in AppDomain."); - } - else - { - logger.LogError("Unhandled exception in AppDomain."); + // Set Secure Attention Sequence policy to allow app to simulate Ctrl + Alt + Del. + var subkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", true); + subkey?.SetValue("SoftwareSASGeneration", "3", RegistryValueKind.DWord); } - }; - - SetWorkingDirectory(); - - await services.GetRequiredService().BeginChecking(); - - await services.GetRequiredService().Connect(); + } + catch (Exception ex) + { + logger.LogError(ex, "Error while setting Secure Attention Sequence in the registry."); + } } private static void SetWorkingDirectory() diff --git a/Agent/Services/WindowsService.Designer.cs b/Agent/Services/WindowsService.Designer.cs deleted file mode 100644 index b7ff0e7be..000000000 --- a/Agent/Services/WindowsService.Designer.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Remotely.Agent.Services -{ - partial class WindowsService - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Component Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - components = new System.ComponentModel.Container(); - this.ServiceName = "WindowsService"; - } - - #endregion - } -} diff --git a/Agent/Services/WindowsService.cs b/Agent/Services/WindowsService.cs deleted file mode 100644 index 10fc03467..000000000 --- a/Agent/Services/WindowsService.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Remotely.Shared.Utilities; -using System; -using System.Diagnostics; -using System.Runtime.Versioning; -using System.ServiceProcess; - -namespace Remotely.Agent.Services -{ - [SupportedOSPlatform("windows")] - partial class WindowsService : ServiceBase - { - public WindowsService() - { - CanHandleSessionChangeEvent = true; - InitializeComponent(); - } - - protected override void OnSessionChange(SessionChangeDescription changeDescription) - { - try - { - Logger.Write($"Session changed. Reason: {changeDescription.Reason}. Session: {changeDescription.SessionId}"); - if (changeDescription.Reason == SessionChangeReason.ConsoleDisconnect || - changeDescription.Reason == SessionChangeReason.RemoteDisconnect) - { - - foreach (var screenCaster in Process.GetProcessesByName("Remotely_Desktop")) - { - if (screenCaster.SessionId == changeDescription.SessionId) - { - Logger.Write($"Session changed. Kill process ID {screenCaster.Id}."); - screenCaster.Kill(); - } - } - } - base.OnSessionChange(changeDescription); - } - catch (Exception ex) - { - Logger.Write(ex); - } - } - - protected override void OnStart(string[] args) - { - - try - { - // Set Secure Attention Sequence policy to allow app to simulate Ctrl + Alt + Del. - var subkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", true); - subkey.SetValue("SoftwareSASGeneration", "3", Microsoft.Win32.RegistryValueKind.DWord); - } - catch (Exception ex) - { - Logger.Write(ex); - } - } - - protected override void OnStop() - { - try - { - // Remove Secure Attention Sequence policy to allow app to simulate Ctrl + Alt + Del. - var subkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", true); - if (subkey.GetValue("SoftwareSASGeneration") != null) - { - subkey.DeleteValue("SoftwareSASGeneration"); - } - } - catch (Exception ex) - { - Logger.Write(ex); - } - } - } -} diff --git a/Server/Services/RcImplementations/HubEventHandler.cs b/Server/Services/RcImplementations/HubEventHandler.cs index c99141275..e98381bbc 100644 --- a/Server/Services/RcImplementations/HubEventHandler.cs +++ b/Server/Services/RcImplementations/HubEventHandler.cs @@ -36,7 +36,7 @@ public Task ChangeWindowsSession(RemoteControlSession session, string viewerConn { _logger.LogError("Event should have been for RemoteControlSessionEx."); return Task.CompletedTask; - } + } return _serviceHub.Clients .Client(ex.AgentConnectionId) diff --git a/Server/wwwroot/Content/Install-Manjaro-x64.sh b/Server/wwwroot/Content/Install-Manjaro-x64.sh index 0e134ed8a..1de2d4250 100644 --- a/Server/wwwroot/Content/Install-Manjaro-x64.sh +++ b/Server/wwwroot/Content/Install-Manjaro-x64.sh @@ -31,7 +31,7 @@ if [ -z "$ETag" ]; then fi pacman -Sy -pacman -S dotnet-runtime-6.0 --noconfirm +pacman -S dotnet-runtime-7.0 --noconfirm pacman -S libx11 --noconfirm pacman -S unzip --noconfirm pacman -S libc6 --noconfirm diff --git a/Shared/Services/ElevationDetectorWin.cs b/Shared/Services/ElevationDetectorWin.cs index 6a4cba100..31e5ac4ef 100644 --- a/Shared/Services/ElevationDetectorWin.cs +++ b/Shared/Services/ElevationDetectorWin.cs @@ -2,12 +2,14 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Runtime.Versioning; using System.Security.Principal; using System.Text; using System.Threading.Tasks; namespace Remotely.Shared.Services { + [SupportedOSPlatform("windows")] public class ElevationDetectorWin : IElevationDetector { public bool IsElevated()