Remotely/Agent/Services/WindowsService.cs
Jared Goodwin 3ef4cdf81a
Extract remote control functionality into separate library. (#539)
* Convert server to new single-file startup model.

* Add remote control implementations.

* Implement IViewerAuthorizer.

* Update hub endpoints.

* Implement HubEventHandler.

* Implement ViewerHubDataProvider.

* Implement page data provider.

* Implement RCL and refactor.

* Update submodule.

* Replace submodule with NuGet.

* Update copy URL.

* Update NuGet.

* Remove deprecated WebRTC.

* Remove deprecated WebRTC.

* Update Immense.RemoteControl

* Building out desktop projects.

* Bring more services into submodule.

* Update submodule.

* Update submodule.

* Refactoring for module.

* Update submodule.

* Update submodule

* Got Windows desktop app running.

* Refactor for submodule changes.

* FIx unattended session start.

* Switch desktop app out of console mode.

* Fix tests.

* Update publishing.

* Remove ClickOnce middleware.

* Remove ClickOnce remnants.

* Update submodule

* Add some logging.

* Update Linux path.

* Update submodule.

* Add cleanup service for unattended sessions that failed to start.

* Update submodule.

* Fix chat.

* Add ValidateExecutableReferencesMatchSelfContained property.

* Add other submodule projects.  Align checkbox.

* Update submodule.  Reduce deserialization in the browser, resulting in faster renders.

* Update submodule.

* Update submodule.

* Update submodule.

* Update submodule.

* Add orgId back for branding.

* Get branding loading in desktop apps.

* Update submodule.

* Create log dir.

* Refactor version check on config page.

* Update submodule.

* Update submodule.

* Change submodule URL.

* Correct namespace.

* Update submodule.

* Checkout submodules recursively.
2022-12-23 06:39:12 -08:00

77 lines
2.6 KiB
C#

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);
}
}
}
}