Files
duplicati/Duplicati/WebserverCore/Services/RemoteControllerService.cs
T
Kenneth Skovhede 179d1ceb47 Make controller Async
This PR has a large blast radius because it takes the final step and bumps up the Controller to be fully async.

We have historically done a piece-by-piece update, so all operations were already async but the controller interface was kept synchronous.

With this update, the controller is now fully async and all tests are updated.

Most places where the new C# compiler warns about function names not ending in `Async` were also adressed, giving a massive refactor change.

Functionally, no changes are done.
2026-05-13 15:04:13 +02:00

136 lines
5.4 KiB
C#

// Copyright (C) 2026, 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 Duplicati.Library.RemoteControl;
using Duplicati.Server;
using Duplicati.Server.Database;
using Duplicati.WebserverCore.Abstractions;
using Newtonsoft.Json;
namespace Duplicati.WebserverCore.Services;
/// <summary>
/// Controller for toggling remote control.
/// </summary>
/// <param name="connection">The connection to the database</param>
/// <param name="controllerHandler">The remote controller handler</param>
/// <param name="eventPollNotify">The event poll notifier</param>
public class RemoteControllerService(Connection connection, IRemoteControllerHandler controllerHandler, EventPollNotify eventPollNotify) : IRemoteController
{
/// <summary>
/// Gets a value indicating whether remote control is enabled.
/// </summary>
public bool IsEnabled => connection.ApplicationSettings.RemoteControlEnabled;
/// <summary>
/// Gets a value indicating whether remote control can be enabled.
/// </summary>
public bool CanEnable => !string.IsNullOrWhiteSpace(connection.ApplicationSettings.RemoteControlConfig);
/// <summary>
/// Gets a value indicating whether the remote control is connected.
/// </summary>
public bool Connected
{
get
{
if (!IsEnabled || _keepRemoteConnection == null)
return false;
return _keepRemoteConnection?.State == KeepRemoteConnection.ConnectionState.Authenticated;
}
}
/// <summary>
/// The remote control connection handler that this service is wrapping.
/// </summary>
private KeepRemoteConnection? _keepRemoteConnection;
/// </inheritdoc>
public void Enable(bool forceConnect)
{
if (_keepRemoteConnection != null)
return;
if (!CanEnable)
throw new InvalidOperationException("Remote control is not configured");
var config = JsonConvert.DeserializeObject<RemoteControlConfig>(connection.ApplicationSettings.RemoteControlConfig ?? string.Empty)
?? throw new InvalidOperationException("Invalid remote control configuration");
_keepRemoteConnection = KeepRemoteConnection.CreateRemoteListener(
config.ServerUrl,
config.Token,
config.CertificateUrl,
config.ServerCertificates,
config.RefreshSettingsBy,
forceConnect,
CancellationToken.None,
controllerHandler.OnConnectAsync,
controllerHandler.ReKeyAsync,
controllerHandler.OnControlAsync,
controllerHandler.OnMessageAsync
);
_keepRemoteConnection.StateChanged += (_, _) => eventPollNotify.SignalRemoteControlUpdate();
connection.ApplicationSettings.RemoteControlEnabled = true;
eventPollNotify.SignalRemoteControlUpdate();
}
/// </inheritdoc>
public void Disable()
{
_keepRemoteConnection?.Dispose();
_keepRemoteConnection = null;
connection.ApplicationSettings.RemoteControlEnabled = false;
eventPollNotify.SignalRemoteControlUpdate();
}
/// </inheritdoc>
public void DeleteRegistration()
{
Disable();
// Clear all remote control related settings
connection.ApplicationSettings.RemoteControlConfig = string.Empty;
connection.ApplicationSettings.AdditionalReportUrl = string.Empty;
connection.ApplicationSettings.RemoteControlDashboardUrl = string.Empty;
connection.ApplicationSettings.RemoteControlStorageApiId = string.Empty;
connection.ApplicationSettings.RemoteControlStorageApiKey = string.Empty;
connection.ApplicationSettings.RemoteControlStorageEndpointUrl = string.Empty;
connection.ApplicationSettings.ClientLicenseKey = string.Empty;
Duplicati.Proprietary.LicenseChecker.LicenseHelper.SetRemoteClientLicenseKey(null);
// Remove remotely configured backups
var remoteBackupIds = connection.Backups.Where(x => !string.IsNullOrWhiteSpace(x.ExternalID) && x.ExternalID.StartsWith(ControlRequestMessage.BackupConfigKeyPrefix, StringComparison.OrdinalIgnoreCase))
.Select(x => x.ID)
.Where(x => long.TryParse(x, out _))
.Select(x => long.Parse(x))
.ToList();
foreach (var id in remoteBackupIds)
connection.DeleteBackup(id);
eventPollNotify.SignalRemoteControlUpdate();
}
}