Skip to content

Commit

Permalink
Replace WindowsService.cs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Apr 27, 2023
1 parent 886d679 commit 0edbf4d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 131 deletions.
58 changes: 42 additions & 16 deletions Agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Remotely.Agent.Services.Windows;
using Microsoft.Extensions.Hosting;
using System.Linq;
using Microsoft.Win32;

namespace Remotely.Agent;

Expand Down Expand Up @@ -50,6 +51,32 @@ public static async Task Main(string[] args)
}
}

private static async Task Init(IServiceProvider services)
{
var logger = services.GetRequiredService<ILogger<IHost>>();

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<IUpdater>().BeginChecking();
await services.GetRequiredService<IAgentHubConnection>().Connect();
}

private static void RegisterServices(IServiceCollection services)
{
services.AddHttpClient();
Expand Down Expand Up @@ -78,12 +105,14 @@ private static void RegisterServices(IServiceCollection services)
services.AddScoped<IAppLauncher, AppLauncherWin>();
services.AddSingleton<IUpdater, UpdaterWin>();
services.AddSingleton<IDeviceInformationService, DeviceInfoGeneratorWin>();
services.AddSingleton<IElevationDetector, ElevationDetectorWin>();
}
else if (OperatingSystem.IsLinux())
{
services.AddScoped<IAppLauncher, AppLauncherLinux>();
services.AddSingleton<IUpdater, UpdaterLinux>();
services.AddSingleton<IDeviceInformationService, DeviceInfoGeneratorLinux>();
services.AddSingleton<IElevationDetector, ElevationDetectorLinux>();
}
else if (OperatingSystem.IsMacOS())
{
Expand All @@ -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<IHost> logger)
{
AppDomain.CurrentDomain.UnhandledException += (sender, ex) =>
try
{
var logger = services.GetRequiredService<ILogger<AppDomain>>();
if (ex.ExceptionObject is Exception exception)
var elevationDetector = services.GetRequiredService<IElevationDetector>();
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<IUpdater>().BeginChecking();

await services.GetRequiredService<IAgentHubConnection>().Connect();
}
catch (Exception ex)
{
logger.LogError(ex, "Error while setting Secure Attention Sequence in the registry.");
}
}

private static void SetWorkingDirectory()
Expand Down
37 changes: 0 additions & 37 deletions Agent/Services/WindowsService.Designer.cs

This file was deleted.

76 changes: 0 additions & 76 deletions Agent/Services/WindowsService.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Server/Services/RcImplementations/HubEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Server/wwwroot/Content/Install-Manjaro-x64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Shared/Services/ElevationDetectorWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0edbf4d

Please sign in to comment.