// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System.Text.Json; using Duplicati.Library.RemoteControl; using Duplicati.Server.Database; using Duplicati.WebserverCore.Abstractions; namespace Duplicati.WebserverCore.Services; /// /// Internal configuration for remote control. /// Persisted in the application settings as JSON. /// public sealed record RemoteControlConfig { /// /// The token used for authentication. /// public required string Token { get; init; } /// /// The URL of the remote control server. /// public required string ServerUrl { get; init; } /// /// The URL for getting new server certificates. /// public required string CertificateUrl { get; init; } /// /// The trusted server certificates. /// public required IEnumerable ServerCertificates { get; init; } } /// /// Service for registering a machine with a remote controller. /// /// The database connection /// The HTTP client factory public class RemoteControllerRegistrationService(Connection connection, IHttpClientFactory httpClientFactory, IRemoteController remoteController) : IRemoteControllerRegistration { /// /// The registration process controller this service is wrapping. /// private RegisterForRemote? _registerForRemote; /// /// Token for cancelling the operation. /// private CancellationTokenSource? _operationCancellation; /// /// The registration completion task. /// private Task? _registrationTask; /// /// A flag indicating if the machine is currently registering /// public bool IsRegistering => _registerForRemote != null; /// /// A flag indicating if the machine is currently registering /// public bool IsClaiming => _registerForRemote != null; /// /// The URL to register the machine with, if registring /// public string? RegistrationUrl { get; private set; } /// public Task RegisterMachine(string registrationUrl) { if (_registerForRemote != null) throw new InvalidOperationException("Already registering"); if (!string.IsNullOrWhiteSpace(connection.ApplicationSettings.RemoteControlConfig)) throw new InvalidOperationException("An existing configuration exists, delete it first"); _operationCancellation = new CancellationTokenSource(); return _registrationTask = Task.Run(async () => { _registerForRemote = new RegisterForRemote(registrationUrl, httpClientFactory.CreateClient(), _operationCancellation.Token); var data = await _registerForRemote.Register(maxRetries: 3, retryInterval: TimeSpan.FromSeconds(5)); // Make the link visible to outside if (data.RegistrationData != null) RegistrationUrl = data.RegistrationData.ClaimLink; // Grab the claim data once it is returned var claimData = await _registerForRemote.Claim(); connection.ApplicationSettings.RemoteControlConfig = JsonSerializer.Serialize(new RemoteControlConfig { Token = claimData.JWT, ServerCertificates = claimData.ServerCertificates, CertificateUrl = claimData.CertificateUrl, ServerUrl = claimData.ServerUrl }); // Automatically connect after we are registered if (remoteController.CanEnable) remoteController.Enable(); }); } /// public Task WaitForRegistration() { if (_registrationTask == null) throw new InvalidOperationException("Not registering"); return _registrationTask; } /// public void CancelRegisterMachine() { _operationCancellation?.Cancel(); _registerForRemote?.Dispose(); _operationCancellation?.Dispose(); _registerForRemote = null; _operationCancellation = null; } }