From 36b20bee334ded439b2d5f4b5d797ba3ca6d0e6c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Dec 2022 17:21:55 -0600 Subject: add file completion broadcast, helpers for auth --- src/slskd/Relay/RelayService.cs | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src') diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index c4fb13a9..e1fa5820 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -54,6 +54,12 @@ namespace slskd.Relay /// IStateMonitor StateMonitor { get; } + /// + /// Notifies connected agents that a file download has completed. + /// + /// The filename of the completed file, relative to the downloads directory. + void BroadcastFileDownloadCompletedNotification(string filename); + /// /// Generates a random authentication challenge token for the specified . /// @@ -225,6 +231,16 @@ namespace slskd.Relay /// A value indicating whether the response is valid. bool TryValidateAuthenticationCredential(string connectionId, string agentName, string credential); + /// + /// Attempts to validate the file download response credential associated with the specified . + /// + /// The token. + /// The name of the responding agent. + /// The name of the file being downloaded. + /// The response credential. + /// A value indicating whether the credential is valid. + bool TryValidateFileDownloadCredential(Guid token, string agentName, string filename, string credential); + /// /// Attempts to validate the file stream response credential associated with the specified . /// @@ -307,6 +323,39 @@ namespace slskd.Relay private string LastOptionsHash { get; set; } private string LastControllerOptionsHash { get; set; } + /// + /// Notifies connected agents that a file download has completed. + /// + /// The filename of the completed file, relative to the downloads directory. + public void BroadcastFileDownloadCompletedNotification(string filename) + { + // 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); + } + } + + // fire and forget, no waiting + _ = RegisteredAgentDictionary.Values.Select(Notify); + } + /// /// Generates a random authentication challenge token for the specified . /// @@ -679,6 +728,19 @@ namespace slskd.Relay return expectedResponse == credential; } + /// + /// Attempts to validate the file download response credential associated with the specified . + /// + /// The token. + /// The name of the responding agent. + /// The name of the file being downloaded. + /// The response credential. + /// A value indicating whether the credential is valid. + public bool TryValidateFileDownloadCredential(Guid token, string agentName, string filename, string credential) + { + return TryValidateCredential(token.ToString(), agentName, credential, GetDownloadTokenCacheKey(filename, token)); + } + /// /// Attempts to validate the file stream response credential associated with the specified . /// @@ -760,6 +822,8 @@ 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"; -- cgit v1.2.3