aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-12-31 15:55:58 -0600
committerJP Dillingham <jp@dillingham.me>2022-12-31 15:55:58 -0600
commit3c7d16890a247a54c4ebe34682d235341d94f8e4 (patch)
tree5c305c841b44f2af905d5ed95c19e1c565ceaf91
parente38b44f5ae1f685bfcd32989570c7b0e4a457efe (diff)
downloadslskd-3c7d16890a247a54c4ebe34682d235341d94f8e4.tar.gz
tweak broadcast, don't remove download tokens after first use
-rw-r--r--src/slskd/Relay/RelayService.cs33
1 files changed, 26 insertions, 7 deletions
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.
/// </summary>
/// <param name="filename">The filename of the completed file, relative to the downloads directory.</param>
- void BroadcastFileDownloadCompletedNotification(string filename);
+ /// <returns>The operation context.</returns>
+ Task BroadcastFileDownloadCompletedNotificationAsync(string filename);
/// <summary>
/// Generates a random authentication challenge token for the specified <paramref name="connectionId"/>.
@@ -327,8 +328,22 @@ namespace slskd.Relay
/// 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>
- public void BroadcastFileDownloadCompletedNotification(string filename)
+ /// <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
@@ -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));
}
/// <summary>
@@ -738,7 +754,7 @@ namespace slskd.Relay
/// <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));
+ return TryValidateCredential(token.ToString(), agentName, credential, GetDownloadTokenCacheKey(filename, token), suppressRemoval: true);
}
/// <summary>
@@ -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);
+ }
}
}
}