Remotely/Agent/Program.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

138 lines
4.5 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Agent.Services;
using Remotely.Shared.Enums;
using Remotely.Shared.Utilities;
using Remotely.Shared.Services;
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.Versioning;
namespace Remotely.Agent
{
public class Program
{
public static IServiceProvider Services { get; set; }
public static async Task Main(string[] args)
{
try
{
BuildServices();
await Init();
_ = Task.Run(Services.GetRequiredService<AgentSocket>().HandleConnection);
await Task.Delay(-1);
}
catch (Exception ex)
{
Logger.Write(ex);
throw;
}
}
private static void BuildServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
serviceCollection.AddLogging(builder =>
{
builder.AddConsole().AddDebug();
});
serviceCollection.AddSingleton<AgentSocket>();
serviceCollection.AddScoped<ChatClientService>();
serviceCollection.AddTransient<PSCore>();
serviceCollection.AddTransient<ExternalScriptingShell>();
serviceCollection.AddScoped<ConfigService>();
serviceCollection.AddScoped<Uninstaller>();
serviceCollection.AddScoped<ScriptExecutor>();
serviceCollection.AddScoped<IProcessInvoker, ProcessInvoker>();
serviceCollection.AddScoped<IUpdateDownloader, UpdateDownloader>();
if (EnvironmentHelper.IsWindows)
{
serviceCollection.AddScoped<IAppLauncher, AppLauncherWin>();
serviceCollection.AddSingleton<IUpdater, UpdaterWin>();
serviceCollection.AddSingleton<IDeviceInformationService, DeviceInformationServiceWin>();
}
else if (EnvironmentHelper.IsLinux)
{
serviceCollection.AddScoped<IAppLauncher, AppLauncherLinux>();
serviceCollection.AddSingleton<IUpdater, UpdaterLinux>();
serviceCollection.AddSingleton<IDeviceInformationService, DeviceInformationServiceLinux>();
}
else if (EnvironmentHelper.IsMac)
{
serviceCollection.AddScoped<IAppLauncher, AppLauncherMac>();
serviceCollection.AddSingleton<IUpdater, UpdaterMac>();
serviceCollection.AddSingleton<IDeviceInformationService, DeviceInformationServiceMac>();
}
else
{
throw new NotSupportedException("Operating system not supported.");
}
Services = serviceCollection.BuildServiceProvider();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Logger.Write(e.ExceptionObject as Exception);
}
private static async Task Init()
{
try
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
SetWorkingDirectory();
if (OperatingSystem.IsWindows() &&
Process.GetCurrentProcess().SessionId == 0)
{
_ = Task.Run(StartService);
}
await Services.GetRequiredService<IUpdater>().BeginChecking();
await Services.GetRequiredService<AgentSocket>().Connect();
}
catch (Exception ex)
{
Logger.Write(ex);
}
}
private static void SetWorkingDirectory()
{
var assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyDir = Path.GetDirectoryName(assemblyPath);
Directory.SetCurrentDirectory(assemblyDir);
}
[SupportedOSPlatform("windows")]
private static void StartService()
{
try
{
ServiceBase.Run(new WindowsService());
}
catch (Exception ex)
{
Logger.Write(ex, "Failed to start service.", EventType.Warning);
}
}
}
}