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/slskd/Relay/RelayService.cs') 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 From 3c7d16890a247a54c4ebe34682d235341d94f8e4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 31 Dec 2022 15:55:58 -0600 Subject: tweak broadcast, don't remove download tokens after first use --- src/slskd/Relay/RelayService.cs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src/slskd/Relay/RelayService.cs') diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index e1fa5820..47879b19 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -58,7 +58,8 @@ namespace slskd.Relay /// Notifies connected agents that a file download has completed. /// /// The filename of the completed file, relative to the downloads directory. - void BroadcastFileDownloadCompletedNotification(string filename); + /// The operation context. + Task BroadcastFileDownloadCompletedNotificationAsync(string filename); /// /// Generates a random authentication challenge token for the specified . @@ -327,8 +328,22 @@ namespace slskd.Relay /// 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) + /// The operation context. + public Task BroadcastFileDownloadCompletedNotificationAsync(string filename) { + if (!OptionsMonitor.CurrentValue.Relay.Enabled || + !new[] { RelayMode.Controller, RelayMode.Debug }.Contains(OptionsMonitor.CurrentValue.Relay.Mode.ToEnum())) + { + 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 @@ -352,8 +367,9 @@ namespace slskd.Relay } } - // fire and forget, no waiting - _ = RegisteredAgentDictionary.Values.Select(Notify); + Log.Information("Notifying agents of download completion for {Filename}", filename); + + return Task.WhenAll(RegisteredAgentDictionary.Values.Select(Notify)); } /// @@ -738,7 +754,7 @@ namespace slskd.Relay /// 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)); + return TryValidateCredential(token.ToString(), agentName, credential, GetDownloadTokenCacheKey(filename, token), suppressRemoval: true); } /// @@ -828,7 +844,7 @@ namespace slskd.Relay 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 { @@ -871,7 +887,10 @@ namespace slskd.Relay finally { // tokens can be used exactly once, pass or fail - MemoryCache.Remove(cacheKey); + if (!suppressRemoval) + { + MemoryCache.Remove(cacheKey); + } } } } -- cgit v1.2.3