aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd')
-rw-r--r--src/slskd/Relay/API/Controllers/RelayController.cs12
-rw-r--r--src/slskd/Relay/API/Hubs/RelayHub.cs8
-rw-r--r--src/slskd/Relay/RelayClient.cs10
-rw-r--r--src/slskd/Relay/RelayService.cs19
4 files changed, 33 insertions, 16 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs
index ddb8d0b0..0be59b13 100644
--- a/src/slskd/Relay/API/Controllers/RelayController.cs
+++ b/src/slskd/Relay/API/Controllers/RelayController.cs
@@ -354,14 +354,16 @@ namespace slskd.Relay
var sw = new Stopwatch();
sw.Start();
- using var outputStream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write);
- using var inputStream = database.OpenReadStream();
-
- await inputStream.CopyToAsync(outputStream);
+ // the streams must be closed (and the output flushed) before the file is handed off to be opened as a database
+ using (var outputStream = new FileStream(temp, FileMode.CreateNew, FileAccess.Write))
+ using (var inputStream = database.OpenReadStream())
+ {
+ await inputStream.CopyToAsync(outputStream);
+ }
sw.Stop();
- Log.Information("Download of shares from {Agent} ({Token}) complete ({Size} in {Duration}ms)", agentName, guid, ((double)inputStream.Length).SizeSuffix(), sw.ElapsedMilliseconds);
+ Log.Information("Download of shares from {Agent} ({Token}) complete ({Size} in {Duration}ms)", agentName, guid, ((double)database.Length).SizeSuffix(), sw.ElapsedMilliseconds);
await Relay.HandleShareUploadAsync(agentName, id: guid, shares, temp);
diff --git a/src/slskd/Relay/API/Hubs/RelayHub.cs b/src/slskd/Relay/API/Hubs/RelayHub.cs
index e839cd1a..4e044bdb 100644
--- a/src/slskd/Relay/API/Hubs/RelayHub.cs
+++ b/src/slskd/Relay/API/Hubs/RelayHub.cs
@@ -223,9 +223,9 @@ namespace slskd.Relay
/// Executed by the agent to notify the controller that the agent was unable to upload the file requested by a call to <see cref="IRelayHub.RequestFileUpload"/>.
/// </summary>
/// <param name="id">The unique identifier of the request.</param>
- /// <param name="exception">The Exception that caused the failure.</param>
+ /// <param name="message">A message describing the failure.</param>
/// <exception cref="UnauthorizedAccessException">Thrown when the agent is not fully authenticated.</exception>
- public void NotifyFileUploadFailed(Guid id, Exception exception)
+ public void NotifyFileUploadFailed(Guid id, string message)
{
if (!Relay.TryGetAgentRegistration(Context.ConnectionId, out var record))
{
@@ -233,9 +233,9 @@ namespace slskd.Relay
throw new UnauthorizedAccessException();
}
- Log.Warning("Agent {Agent} (connection {ConnectionId}) from {IP} reported upload failure for {Id}: {Message}", record.Agent, Context.ConnectionId, GetRemoteIPAddress(), id, exception.Message);
+ Log.Warning("Agent {Agent} (connection {ConnectionId}) from {IP} reported upload failure for {Id}: {Message}", record.Agent.Name, Context.ConnectionId, GetRemoteIPAddress(), id, message);
- Relay.NotifyFileStreamException(record.Agent.Name, id, exception);
+ Relay.NotifyFileStreamException(record.Agent.Name, id, new RelayException(message));
}
/// <summary>
diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs
index 49935b4c..4f1e2924 100644
--- a/src/slskd/Relay/RelayClient.cs
+++ b/src/slskd/Relay/RelayClient.cs
@@ -447,7 +447,9 @@ namespace slskd.Relay
request.Content = content;
Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token);
- var response = await CreateHttpClient().SendAsync(request);
+
+ using var client = CreateHttpClient();
+ using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
@@ -463,7 +465,7 @@ namespace slskd.Relay
Log.Error(ex, "Failed to handle file request: {Message}", ex.Message);
// report the failure to the controller. this avoids a failure due to timeout.
- await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token);
+ await HubConnection.InvokeAsync(nameof(RelayHub.NotifyFileUploadFailed), token, ex.Message);
}
});
}
@@ -658,7 +660,9 @@ namespace slskd.Relay
Log.Information("Beginning upload of shares ({Size})", size);
Log.Debug("Shares: {Shares}", Shares.LocalHost.Shares.ToJson());
- var response = await CreateHttpClient().SendAsync(request, cancellationToken);
+
+ using var client = CreateHttpClient();
+ using var response = await client.SendAsync(request, cancellationToken);
if (!response.IsSuccessStatusCode)
{
diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs
index 26a635a1..c6c1f5d0 100644
--- a/src/slskd/Relay/RelayService.cs
+++ b/src/slskd/Relay/RelayService.cs
@@ -366,8 +366,8 @@ namespace slskd.Relay
{
var token = Cryptography.Random.GetBytes(32).ToBase62();
- // this token is only valid for this connection id for a short time this is important to prevent replay-type attacks.
- MemoryCache.Set(GetAuthTokenCacheKey(connectionId), token, TimeSpan.FromSeconds(10));
+ // this token is only valid for this connection id for a short time; this is important to prevent replay-type attacks.
+ MemoryCache.Set(GetAuthTokenCacheKey(connectionId), token, TimeSpan.FromMinutes(1));
Log.Debug("Cached auth token {Token} for ID {Id}", token, connectionId);
return token;
}
@@ -765,6 +765,9 @@ namespace slskd.Relay
return false;
}
+ // challenge tokens can be used exactly once, pass or fail
+ MemoryCache.Remove(GetAuthTokenCacheKey(connectionId));
+
var agentOptions = OptionsMonitor.CurrentValue.Relay.Agents.Values.SingleOrDefault(a => a.InstanceName == agentName);
if (agentOptions == default)
@@ -777,7 +780,9 @@ namespace slskd.Relay
var tokenBytes = System.Text.Encoding.UTF8.GetBytes((string)challengeToken);
var expectedCredential = Aes.Encrypt(tokenBytes, key).ToBase62();
- if (expectedCredential != credential)
+ if (!System.Security.Cryptography.CryptographicOperations.FixedTimeEquals(
+ left: System.Text.Encoding.UTF8.GetBytes(expectedCredential),
+ right: System.Text.Encoding.UTF8.GetBytes(credential ?? string.Empty)))
{
Log.Debug("Validation failed: Supplied credential {Credential} does not match expected credential {Expected}", credential, expectedCredential);
return false;
@@ -853,6 +858,8 @@ namespace slskd.Relay
else
{
// the controller changed. disconnect and throw away the client and create a new one
+ var oldClient = Client;
+
Client = new RelayClient(
shareService: Shares,
fileService: Files,
@@ -862,6 +869,8 @@ namespace slskd.Relay
Client.StateMonitor.OnChange(clientState
=> State.SetValue(state => state with { Controller = state.Controller with { State = clientState.Current } }));
+ oldClient.TryDispose();
+
State.SetValue(state => state with
{
Mode = mode,
@@ -925,7 +934,9 @@ namespace slskd.Relay
var tokenBytes = System.Text.Encoding.UTF8.GetBytes(token);
var expectedCredential = Aes.Encrypt(tokenBytes, key).ToBase62();
- if (expectedCredential != credential)
+ if (!System.Security.Cryptography.CryptographicOperations.FixedTimeEquals(
+ left: System.Text.Encoding.UTF8.GetBytes(expectedCredential),
+ right: System.Text.Encoding.UTF8.GetBytes(credential ?? string.Empty)))
{
Log.Debug("Validation failed: Supplied credential {Credential} does not match expected credential {Expected}", credential, expectedCredential);
return false;