aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Relay/RelayService.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-12-31 16:50:27 -0600
committerGitHub <noreply@github.com>2022-12-31 16:50:27 -0600
commitce67db39e0fa02a764a13546043e71d25d338219 (patch)
tree84c6818063270d78f6655342450012385b8a205f /src/slskd/Relay/RelayService.cs
parent49bc255026aa5ef2c4df0727134b7627808270fc (diff)
parentd259789fcb605aa9d87857ba2fba8c35604297c2 (diff)
downloadslskd-ce67db39e0fa02a764a13546043e71d25d338219.tar.gz
Merge pull request #815 from slskd/relay-down
Add the ability to configure Relay Agents to download files downloaded by the controller
Diffstat (limited to 'src/slskd/Relay/RelayService.cs')
-rw-r--r--src/slskd/Relay/RelayService.cs87
1 files changed, 85 insertions, 2 deletions
diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs
index c4fb13a9..47879b19 100644
--- a/src/slskd/Relay/RelayService.cs
+++ b/src/slskd/Relay/RelayService.cs
@@ -55,6 +55,13 @@ namespace slskd.Relay
IStateMonitor<RelayState> StateMonitor { get; }
/// <summary>
+ /// Notifies connected agents that a file download has completed.
+ /// </summary>
+ /// <param name="filename">The filename of the completed file, relative to the downloads directory.</param>
+ /// <returns>The operation context.</returns>
+ Task BroadcastFileDownloadCompletedNotificationAsync(string filename);
+
+ /// <summary>
/// Generates a random authentication challenge token for the specified <paramref name="connectionId"/>.
/// </summary>
/// <remarks>The token is cached internally, and is only valid while it remains in the cache.</remarks>
@@ -226,6 +233,16 @@ namespace slskd.Relay
bool TryValidateAuthenticationCredential(string connectionId, string agentName, string credential);
/// <summary>
+ /// Attempts to validate the file download response credential associated with the specified <paramref name="token"/>.
+ /// </summary>
+ /// <param name="token">The token.</param>
+ /// <param name="agentName">The name of the responding agent.</param>
+ /// <param name="filename">The name of the file being downloaded.</param>
+ /// <param name="credential">The response credential.</param>
+ /// <returns>A value indicating whether the credential is valid.</returns>
+ bool TryValidateFileDownloadCredential(Guid token, string agentName, string filename, string credential);
+
+ /// <summary>
/// Attempts to validate the file stream response credential associated with the specified <paramref name="token"/>.
/// </summary>
/// <param name="token">The token.</param>
@@ -308,6 +325,54 @@ namespace slskd.Relay
private string LastControllerOptionsHash { get; set; }
/// <summary>
+ /// Notifies connected agents that a file download has completed.
+ /// </summary>
+ /// <param name="filename">The filename of the completed file, relative to the downloads directory.</param>
+ /// <returns>The operation context.</returns>
+ public Task BroadcastFileDownloadCompletedNotificationAsync(string filename)
+ {
+ if (!OptionsMonitor.CurrentValue.Relay.Enabled ||
+ !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<RelayMode>()))
+ {
+ return Task.CompletedTask;
+ }
+
+ var agents = RegisteredAgentDictionary.Values;
+
+ if (!agents.Any())
+ {
+ return Task.CompletedTask;
+ }
+
+ // ensure filename is relative to the local download directory
+ // and make sure it doesn't start with a slash
+ filename = filename
+ .ReplaceFirst(OptionsMonitor.CurrentValue.Directories.Downloads, string.Empty)
+ .TrimStart(new[] { '/', '\\' });
+
+ async Task Notify((string ConnectionId, Agent Agent) record)
+ {
+ try
+ {
+ var id = Guid.NewGuid();
+
+ MemoryCache.Set(GetDownloadTokenCacheKey(filename, id), record.Agent.Name, TimeSpan.FromMinutes(10));
+ Log.Debug("Cached file download token {Token} for agent {Agent}", id, record.Agent.Name);
+
+ await RelayHub.Clients.Client(record.ConnectionId).NotifyFileDownloadCompleted(filename, id);
+ }
+ catch
+ {
+ Log.Warning("Failed to notify agent {Agent} of download completion for {Filename}", record.Agent.Name, filename);
+ }
+ }
+
+ Log.Information("Notifying agents of download completion for {Filename}", filename);
+
+ return Task.WhenAll(RegisteredAgentDictionary.Values.Select(Notify));
+ }
+
+ /// <summary>
/// Generates a random authentication challenge token for the specified <paramref name="connectionId"/>.
/// </summary>
/// <remarks>The token is cached internally, and is only valid while it remains in the cache.</remarks>
@@ -680,6 +745,19 @@ namespace slskd.Relay
}
/// <summary>
+ /// Attempts to validate the file download response credential associated with the specified <paramref name="token"/>.
+ /// </summary>
+ /// <param name="token">The token.</param>
+ /// <param name="agentName">The name of the responding agent.</param>
+ /// <param name="filename">The name of the file being downloaded.</param>
+ /// <param name="credential">The response credential.</param>
+ /// <returns>A value indicating whether the credential is valid.</returns>
+ public bool TryValidateFileDownloadCredential(Guid token, string agentName, string filename, string credential)
+ {
+ return TryValidateCredential(token.ToString(), agentName, credential, GetDownloadTokenCacheKey(filename, token), suppressRemoval: true);
+ }
+
+ /// <summary>
/// Attempts to validate the file stream response credential associated with the specified <paramref name="token"/>.
/// </summary>
/// <param name="token">The token.</param>
@@ -760,11 +838,13 @@ namespace slskd.Relay
private string GetAuthTokenCacheKey(string connectionId) => $"{connectionId}.auth";
+ private string GetDownloadTokenCacheKey(string filename, Guid token) => $"{filename}.{token}.download";
+
private string GetFileTokenCacheKey(string filename, Guid token) => $"{filename}.{token}.file";
private string GetShareTokenCacheKey(Guid token) => $"{token}.share";
- private bool TryValidateCredential(string token, string agentName, string credential, string cacheKey)
+ private bool TryValidateCredential(string token, string agentName, string credential, string cacheKey, bool suppressRemoval = false)
{
try
{
@@ -807,7 +887,10 @@ namespace slskd.Relay
finally
{
// tokens can be used exactly once, pass or fail
- MemoryCache.Remove(cacheKey);
+ if (!suppressRemoval)
+ {
+ MemoryCache.Remove(cacheKey);
+ }
}
}
}