From 49e910dc98a9ac698b158d599ad9150a57f975b3 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Tue, 16 Jul 2024 11:09:13 -0700 Subject: [PATCH] Merge in abstractions in Desktop projects. --- Desktop.Linux/Program.cs | 7 +-- Desktop.Linux/Services/AppStartup.cs | 1 + .../Startup/IServiceCollectionExtensions.cs | 6 +-- .../Abstractions/IBrandingProvider.cs | 11 ----- Desktop.Shared/Services/BrandingProvider.cs | 36 ++++++++++------ .../Startup/IServiceCollectionExtensions.cs | 9 ++-- .../Startup/RemoteControlClientBuilder.cs | 42 ------------------ Desktop.UI/Services/ViewModelFactory.cs | 1 + Desktop.UI/ViewModels/BrandedViewModelBase.cs | 3 +- Desktop.UI/ViewModels/ChatWindowViewModel.cs | 1 + .../ViewModels/FileTransferWindowViewModel.cs | 1 + .../ViewModels/HostNamePromptViewModel.cs | 1 + Desktop.UI/ViewModels/MainViewViewModel.cs | 2 +- Desktop.UI/ViewModels/MainWindowViewModel.cs | 1 + Desktop.UI/ViewModels/MessageBoxViewModel.cs | 1 + .../PromptForAccessWindowViewModel.cs | 1 + .../SessionIndicatorWindowViewModel.cs | 1 + Desktop.Win/Program.cs | 11 ++--- Desktop.Win/Services/AppStartup.cs | 1 + .../Startup/IServiceCollectionExtensions.cs | 6 +-- Server/API/RemoteControlController.cs | 2 +- Server/Components/Devices/DeviceCard.razor.cs | 2 +- Server/Components/Layout/NavMenu.razor | 2 +- .../IApplicationBuilderExtensions.cs | 9 +--- Server/Models/RemoteControlSession.cs | 2 +- Server/Pages/Viewer.cshtml | 43 ++++++------------- Server/Pages/Viewer.cshtml.cs | 6 +-- Server/Program.cs | 5 ++- Server/wwwroot/manifest-rc.json | 8 ++-- 29 files changed, 79 insertions(+), 143 deletions(-) delete mode 100644 Desktop.Shared/Abstractions/IBrandingProvider.cs delete mode 100644 Desktop.Shared/Startup/RemoteControlClientBuilder.cs diff --git a/Desktop.Linux/Program.cs b/Desktop.Linux/Program.cs index 11d85908..ee0c0287 100644 --- a/Desktop.Linux/Program.cs +++ b/Desktop.Linux/Program.cs @@ -44,12 +44,7 @@ public class Program services.AddSingleton(); services.AddSingleton(); - - services.AddRemoteControlLinux( - config => - { - config.AddBrandingProvider(); - }); + services.AddRemoteControlLinux(); services.AddLogging(builder => { diff --git a/Desktop.Linux/Services/AppStartup.cs b/Desktop.Linux/Services/AppStartup.cs index fd9e5ff2..5695f9f5 100644 --- a/Desktop.Linux/Services/AppStartup.cs +++ b/Desktop.Linux/Services/AppStartup.cs @@ -4,6 +4,7 @@ using Remotely.Desktop.Shared.Services; using Remotely.Desktop.UI.Services; using Remotely.Shared.Models; using Microsoft.Extensions.Logging; +using Desktop.Shared.Services; namespace Remotely.Desktop.Linux.Services; diff --git a/Desktop.Linux/Startup/IServiceCollectionExtensions.cs b/Desktop.Linux/Startup/IServiceCollectionExtensions.cs index b3c29440..da9d6fe0 100644 --- a/Desktop.Linux/Startup/IServiceCollectionExtensions.cs +++ b/Desktop.Linux/Startup/IServiceCollectionExtensions.cs @@ -17,11 +17,9 @@ public static class IServiceCollectionExtensions /// /// /// - public static void AddRemoteControlLinux( - this IServiceCollection services, - Action clientConfig) + public static void AddRemoteControlLinux(this IServiceCollection services) { - services.AddRemoteControlXplat(clientConfig); + services.AddRemoteControlXplat(); services.AddRemoteControlUi(); services.AddSingleton(); diff --git a/Desktop.Shared/Abstractions/IBrandingProvider.cs b/Desktop.Shared/Abstractions/IBrandingProvider.cs deleted file mode 100644 index 9ee292eb..00000000 --- a/Desktop.Shared/Abstractions/IBrandingProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Remotely.Shared.Models; -using Remotely.Shared.Entities; - -namespace Remotely.Desktop.Shared.Abstractions; - -public interface IBrandingProvider -{ - BrandingInfo CurrentBranding { get; } - Task Initialize(); - void SetBrandingInfo(BrandingInfo brandingInfo); -} diff --git a/Desktop.Shared/Services/BrandingProvider.cs b/Desktop.Shared/Services/BrandingProvider.cs index 4be90692..305b4c82 100644 --- a/Desktop.Shared/Services/BrandingProvider.cs +++ b/Desktop.Shared/Services/BrandingProvider.cs @@ -10,6 +10,13 @@ using System.Net.Http.Json; namespace Desktop.Shared.Services; +public interface IBrandingProvider +{ + BrandingInfo CurrentBranding { get; } + Task Initialize(); + void SetBrandingInfo(BrandingInfo brandingInfo); +} + public class BrandingProvider : IBrandingProvider { private readonly IAppState _appState; @@ -56,9 +63,9 @@ public class BrandingProvider : IBrandingProvider }; } - if (_brandingInfo.Icon?.Any() != true) + if (_brandingInfo.Icon is not { Length: > 0 }) { - using var mrs = typeof(BrandingProvider).Assembly.GetManifestResourceStream("Desktop.Shared.Assets.Remotely_Icon.png"); + using var mrs = typeof(BrandingProvider).Assembly.GetManifestResourceStream("Remotely.Desktop.Shared.Assets.Remotely_Icon.png"); using var ms = new MemoryStream(); mrs!.CopyTo(ms); @@ -87,22 +94,25 @@ public class BrandingProvider : IBrandingProvider var result = _embeddedDataSearcher.TryGetEmbeddedData(filePath); - if (!result.IsSuccess) + if (result.IsSuccess) + { + if (!string.IsNullOrWhiteSpace(result.Value.OrganizationId)) + { + _orgIdProvider.OrganizationId = result.Value.OrganizationId; + } + + if (result.Value.ServerUrl is not null) + { + _appState.Host = result.Value.ServerUrl.AbsoluteUri; + } + } + + if (string.IsNullOrWhiteSpace(_appState.Host)) { return result.HadException ? Result.Fail(result.Exception) : Result.Fail(result.Reason); } - - if (!string.IsNullOrWhiteSpace(result.Value.OrganizationId)) - { - _orgIdProvider.OrganizationId = result.Value.OrganizationId; - } - - if (result.Value.ServerUrl is not null) - { - _appState.Host = result.Value.ServerUrl.AbsoluteUri; - } } if (string.IsNullOrWhiteSpace(_appState.Host)) diff --git a/Desktop.Shared/Startup/IServiceCollectionExtensions.cs b/Desktop.Shared/Startup/IServiceCollectionExtensions.cs index 648e294b..7d5f1fe8 100644 --- a/Desktop.Shared/Startup/IServiceCollectionExtensions.cs +++ b/Desktop.Shared/Startup/IServiceCollectionExtensions.cs @@ -5,19 +5,15 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Bitbound.SimpleMessenger; using Desktop.Shared.Services; +using Remotely.Desktop.Shared.Abstractions; namespace Remotely.Desktop.Shared.Startup; public static class IServiceCollectionExtensions { internal static void AddRemoteControlXplat( - this IServiceCollection services, - Action clientConfig) + this IServiceCollection services) { - var builder = new RemoteControlClientBuilder(services); - clientConfig.Invoke(builder); - builder.Validate(); - services.AddLogging(builder => { builder.AddConsole().AddDebug(); @@ -31,6 +27,7 @@ public static class IServiceCollectionExtensions services.AddSingleton(s => WeakReferenceMessenger.Default); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); diff --git a/Desktop.Shared/Startup/RemoteControlClientBuilder.cs b/Desktop.Shared/Startup/RemoteControlClientBuilder.cs deleted file mode 100644 index 364be89e..00000000 --- a/Desktop.Shared/Startup/RemoteControlClientBuilder.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Remotely.Desktop.Shared.Abstractions; -using Microsoft.Extensions.DependencyInjection; - -namespace Remotely.Desktop.Shared.Startup; - -public interface IRemoteControlClientBuilder -{ - void AddBrandingProvider() - where T : class, IBrandingProvider; -} - -internal class RemoteControlClientBuilder : IRemoteControlClientBuilder -{ - private readonly IServiceCollection _services; - - public RemoteControlClientBuilder(IServiceCollection services) - { - _services = services; - } - - public void AddBrandingProvider() - where T : class, IBrandingProvider - { - _services.AddSingleton(); - } - - internal void Validate() - { - var serviceTypes = new[] - { - typeof(IBrandingProvider) - }; - - foreach (var type in serviceTypes) - { - if (!_services.Any(x => x.ServiceType == type)) - { - throw new Exception($"Missing service registration for type {type.Name}."); - } - } - } -} diff --git a/Desktop.UI/Services/ViewModelFactory.cs b/Desktop.UI/Services/ViewModelFactory.cs index e6b1b3b8..be915761 100644 --- a/Desktop.UI/Services/ViewModelFactory.cs +++ b/Desktop.UI/Services/ViewModelFactory.cs @@ -3,6 +3,7 @@ using Remotely.Desktop.Shared.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System.IO; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.Services; diff --git a/Desktop.UI/ViewModels/BrandedViewModelBase.cs b/Desktop.UI/ViewModels/BrandedViewModelBase.cs index 7c11f828..927d4f88 100644 --- a/Desktop.UI/ViewModels/BrandedViewModelBase.cs +++ b/Desktop.UI/ViewModels/BrandedViewModelBase.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Remotely.Shared.Entities; using System.IO; using System.Reflection; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; @@ -66,7 +67,7 @@ public class BrandedViewModelBase : ObservableObject, IBrandedViewModelBase ProductName = _brandingInfo.Product; - if (_brandingInfo.Icon?.Any() == true) + if (_brandingInfo.Icon is { Length: > 0 }) { using var imageStream = new MemoryStream(_brandingInfo.Icon); Icon = new Bitmap(imageStream); diff --git a/Desktop.UI/ViewModels/ChatWindowViewModel.cs b/Desktop.UI/ViewModels/ChatWindowViewModel.cs index ee17c622..04cdf4cb 100644 --- a/Desktop.UI/ViewModels/ChatWindowViewModel.cs +++ b/Desktop.UI/ViewModels/ChatWindowViewModel.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Remotely.Desktop.Shared.Reactive; using Microsoft.Extensions.DependencyInjection; using Remotely.Shared.Models; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/FileTransferWindowViewModel.cs b/Desktop.UI/ViewModels/FileTransferWindowViewModel.cs index 56a6ff57..98b1a455 100644 --- a/Desktop.UI/ViewModels/FileTransferWindowViewModel.cs +++ b/Desktop.UI/ViewModels/FileTransferWindowViewModel.cs @@ -7,6 +7,7 @@ using System.Windows.Input; using Remotely.Desktop.Shared.Abstractions; using Microsoft.Extensions.Logging; using Remotely.Desktop.Shared.Reactive; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/HostNamePromptViewModel.cs b/Desktop.UI/ViewModels/HostNamePromptViewModel.cs index 6685deda..eefdeab9 100644 --- a/Desktop.UI/ViewModels/HostNamePromptViewModel.cs +++ b/Desktop.UI/ViewModels/HostNamePromptViewModel.cs @@ -3,6 +3,7 @@ using System.Windows.Input; using Remotely.Desktop.Shared.Abstractions; using Microsoft.Extensions.Logging; using Remotely.Desktop.Shared.Reactive; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/MainViewViewModel.cs b/Desktop.UI/ViewModels/MainViewViewModel.cs index 7f086292..4e2cbf86 100644 --- a/Desktop.UI/ViewModels/MainViewViewModel.cs +++ b/Desktop.UI/ViewModels/MainViewViewModel.cs @@ -135,7 +135,7 @@ public class MainViewViewModel : BrandedViewModelBase, IMainViewViewModel return; } - await _dispatcher.Clipboard.SetTextAsync($"{Host}/RemoteControl/Viewer?sessionId={StatusMessage.Replace(" ", "")}"); + await _dispatcher.Clipboard.SetTextAsync($"{Host}/Viewer?sessionId={StatusMessage.Replace(" ", "")}"); CopyMessageOpacity = 1; IsCopyMessageVisible = true; diff --git a/Desktop.UI/ViewModels/MainWindowViewModel.cs b/Desktop.UI/ViewModels/MainWindowViewModel.cs index fbf6bb5c..34d69dee 100644 --- a/Desktop.UI/ViewModels/MainWindowViewModel.cs +++ b/Desktop.UI/ViewModels/MainWindowViewModel.cs @@ -1,5 +1,6 @@ using Remotely.Desktop.Shared.Abstractions; using Microsoft.Extensions.Logging; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/MessageBoxViewModel.cs b/Desktop.UI/ViewModels/MessageBoxViewModel.cs index e14e78d5..ad07f06f 100644 --- a/Desktop.UI/ViewModels/MessageBoxViewModel.cs +++ b/Desktop.UI/ViewModels/MessageBoxViewModel.cs @@ -4,6 +4,7 @@ using Remotely.Desktop.Shared.Reactive; using Remotely.Desktop.Shared.Abstractions; using Microsoft.Extensions.Logging; using Remotely.Desktop.UI.Controls.Dialogs; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/PromptForAccessWindowViewModel.cs b/Desktop.UI/ViewModels/PromptForAccessWindowViewModel.cs index 65cf892a..fa70d37a 100644 --- a/Desktop.UI/ViewModels/PromptForAccessWindowViewModel.cs +++ b/Desktop.UI/ViewModels/PromptForAccessWindowViewModel.cs @@ -3,6 +3,7 @@ using Remotely.Desktop.Shared.Abstractions; using Remotely.Desktop.Shared.Reactive; using Microsoft.Extensions.Logging; using System.Windows.Input; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.UI/ViewModels/SessionIndicatorWindowViewModel.cs b/Desktop.UI/ViewModels/SessionIndicatorWindowViewModel.cs index 364d97f7..6ac785ef 100644 --- a/Desktop.UI/ViewModels/SessionIndicatorWindowViewModel.cs +++ b/Desktop.UI/ViewModels/SessionIndicatorWindowViewModel.cs @@ -2,6 +2,7 @@ using Remotely.Desktop.Shared.Abstractions; using Remotely.Desktop.UI.Controls.Dialogs; using Microsoft.Extensions.Logging; +using Desktop.Shared.Services; namespace Remotely.Desktop.UI.ViewModels; diff --git a/Desktop.Win/Program.cs b/Desktop.Win/Program.cs index b8fd9efe..13f48a2e 100644 --- a/Desktop.Win/Program.cs +++ b/Desktop.Win/Program.cs @@ -11,6 +11,9 @@ using Remotely.Shared.Utilities; using System.Diagnostics; using System.Runtime.Versioning; using System.Threading; +using Remotely.Desktop.Shared.Abstractions; +using Remotely.Desktop.UI.Startup; +using Remotely.Desktop.Win.Services; namespace Remotely.Desktop.Win; @@ -45,11 +48,9 @@ public class Program services.AddSingleton(); services.AddSingleton(EmbeddedServerDataProvider.Instance); - services.AddRemoteControlWindows( - config => - { - config.AddBrandingProvider(); - }); + services.AddRemoteControlXplat(); + services.AddRemoteControlUi(); + services.AddRemoteControlWindows(); services.AddLogging(builder => { diff --git a/Desktop.Win/Services/AppStartup.cs b/Desktop.Win/Services/AppStartup.cs index c5d16777..42160ee0 100644 --- a/Desktop.Win/Services/AppStartup.cs +++ b/Desktop.Win/Services/AppStartup.cs @@ -1,3 +1,4 @@ +using Desktop.Shared.Services; using Remotely.Desktop.Shared.Abstractions; using Remotely.Desktop.Shared.Enums; using Remotely.Desktop.Shared.Native.Windows; diff --git a/Desktop.Win/Startup/IServiceCollectionExtensions.cs b/Desktop.Win/Startup/IServiceCollectionExtensions.cs index dbbf4f8c..15440e8b 100644 --- a/Desktop.Win/Startup/IServiceCollectionExtensions.cs +++ b/Desktop.Win/Startup/IServiceCollectionExtensions.cs @@ -17,11 +17,9 @@ public static class IServiceCollectionExtensions /// /// [SupportedOSPlatform("windows")] - public static void AddRemoteControlWindows( - this IServiceCollection services, - Action clientConfig) + public static void AddRemoteControlWindows(this IServiceCollection services) { - services.AddRemoteControlXplat(clientConfig); + services.AddRemoteControlXplat(); services.AddRemoteControlUi(); services.AddSingleton(); diff --git a/Server/API/RemoteControlController.cs b/Server/API/RemoteControlController.cs index 85e3b795..15c58953 100644 --- a/Server/API/RemoteControlController.cs +++ b/Server/API/RemoteControlController.cs @@ -173,6 +173,6 @@ public class RemoteControlController : ControllerBase var otp = _otpProvider.GetOtp(targetDevice.ID); - return Ok($"{HttpContext.Request.Scheme}://{Request.Host}/RemoteControl/Viewer?mode=Unattended&sessionId={sessionId}&accessKey={accessKey}&otp={otp}"); + return Ok($"{HttpContext.Request.Scheme}://{Request.Host}/Viewer?mode=Unattended&sessionId={sessionId}&accessKey={accessKey}&otp={otp}"); } } diff --git a/Server/Components/Devices/DeviceCard.razor.cs b/Server/Components/Devices/DeviceCard.razor.cs index fed232ac..ccd14236 100644 --- a/Server/Components/Devices/DeviceCard.razor.cs +++ b/Server/Components/Devices/DeviceCard.razor.cs @@ -334,7 +334,7 @@ public partial class DeviceCard : AuthComponentBase } JsInterop.OpenWindow( - $"/RemoteControl/Viewer" + + $"/Viewer" + $"?mode=Unattended&sessionId={session.UnattendedSessionId}" + $"&accessKey={session.AccessKey}" + $"&viewonly={viewOnly}", diff --git a/Server/Components/Layout/NavMenu.razor b/Server/Components/Layout/NavMenu.razor index 585ab135..10c1e328 100644 --- a/Server/Components/Layout/NavMenu.razor +++ b/Server/Components/Layout/NavMenu.razor @@ -40,7 +40,7 @@