diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-11-16 08:46:50 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-16 08:46:50 -0600 |
| commit | fe41700f5466c434ad984ca0e273e08c66d3597a (patch) | |
| tree | 9880a9f83cd6c809f2b7cdd954661055780643fd /src | |
| parent | 0bfe5062ea6cc3d3dadef7c88497c2a15a573ece (diff) | |
| parent | 1d092bf0b18ba44c0547838715a721900cb4a569 (diff) | |
| download | slskd-fe41700f5466c434ad984ca0e273e08c66d3597a.tar.gz | |
Merge pull request #1928 from slskd/relay
Minor Relay improvements
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Relay/API/Hubs/RelayHub.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Relay/RelayClient.cs | 10 | ||||
| -rw-r--r-- | src/slskd/Relay/RelayService.cs | 19 | ||||
| -rw-r--r-- | src/web/src/lib/relay.js | 4 |
5 files changed, 35 insertions, 18 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; diff --git a/src/web/src/lib/relay.js b/src/web/src/lib/relay.js index 35149f02..956a8073 100644 --- a/src/web/src/lib/relay.js +++ b/src/web/src/lib/relay.js @@ -1,9 +1,9 @@ import api from './api'; export const connect = () => { - return api.put('/relay'); + return api.put('/relay/agent'); }; export const disconnect = () => { - return api.delete('/relay'); + return api.delete('/relay/agent'); }; |
