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') 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