diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-12-31 16:50:27 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-31 16:50:27 -0600 |
| commit | ce67db39e0fa02a764a13546043e71d25d338219 (patch) | |
| tree | 84c6818063270d78f6655342450012385b8a205f /src/slskd/Relay/RelayClient.cs | |
| parent | 49bc255026aa5ef2c4df0727134b7627808270fc (diff) | |
| parent | d259789fcb605aa9d87857ba2fba8c35604297c2 (diff) | |
| download | slskd-ce67db39e0fa02a764a13546043e71d25d338219.tar.gz | |
Merge pull request #815 from slskd/relay-down
Add the ability to configure Relay Agents to download files downloaded by the controller
Diffstat (limited to 'src/slskd/Relay/RelayClient.cs')
| -rw-r--r-- | src/slskd/Relay/RelayClient.cs | 66 |
1 files changed, 62 insertions, 4 deletions
diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index f1a1c51d..d6bde1d7 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -244,15 +244,17 @@ namespace slskd.Relay var stream = new FileStream(temp, FileMode.Open, FileAccess.Read); using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/relay/shares/{token}"); + + request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey); + request.Headers.Add("X-Relay-Agent", OptionsMonitor.CurrentValue.InstanceName); + request.Headers.Add("X-Relay-Credential", ComputeCredential(token)); + using var content = new MultipartFormDataContent { - { new StringContent(OptionsMonitor.CurrentValue.InstanceName), "name" }, - { new StringContent(ComputeCredential(token)), "credential" }, { new StringContent(Shares.LocalHost.Shares.ToJson()), "shares" }, { new StreamContent(stream), "database", "shares" }, }; - request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey); request.Content = content; var size = ((double)stream.Length).SizeSuffix(); @@ -312,6 +314,11 @@ namespace slskd.Relay stream.Seek(startOffset, SeekOrigin.Begin); using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/relay/files/{token}"); + + request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey); + request.Headers.Add("X-Relay-Agent", OptionsMonitor.CurrentValue.InstanceName); + request.Headers.Add("X-Relay-Credential", ComputeCredential(token)); + using var content = new MultipartFormDataContent { { new StringContent(OptionsMonitor.CurrentValue.InstanceName), "name" }, @@ -319,7 +326,6 @@ namespace slskd.Relay { new StreamContent(stream), "file", filename }, }; - request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey); request.Content = content; Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token); @@ -392,6 +398,57 @@ namespace slskd.Relay } } + private Task HandleNotifyFileDownloadCompleted(string filename, Guid token) + { + if (!OptionsMonitor.CurrentValue.Relay.Controller.Downloads) + { + return Task.CompletedTask; + } + + Log.Information("Relay controller sent a download notification for {Filename} ({Token})", filename, token); + + _ = Task.Run(async () => + { + var destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename); + + if (OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<RelayMode>() == RelayMode.Debug) + { + // if we're debugging, we're referencing the same file for both the controller and agent + // which will lead to an access violation. prefix the destination file to avoid this. + destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, $"{filename}.relayed"); + } + + await Retry.Do(task: async () => + { + using var request = new HttpRequestMessage(HttpMethod.Get, $"api/v0/relay/downloads/{token}"); + + request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey); + request.Headers.Add("X-Relay-Agent", OptionsMonitor.CurrentValue.InstanceName); + request.Headers.Add("X-Relay-Credential", ComputeCredential(token)); + request.Headers.Add("X-Relay-Filename", filename); + + using var client = CreateHttpClient(); + using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); + + response.EnsureSuccessStatusCode(); + + using var remoteStream = await response.Content.ReadAsStreamAsync(); + + Directory.CreateDirectory(Path.GetDirectoryName(destinationFile)); + using var localStream = new FileStream(destinationFile, FileMode.Create); + await remoteStream.CopyToAsync(localStream); + }, + isRetryable: (_, _) => true, + onFailure: (_, ex) => Log.Error(ex, "Failed to handle file download notification for {Filename} ({Token})", filename, token), + maxAttempts: 3, + maxDelayInMilliseconds: 60000); + + Log.Information("File {Filename} successfully downloaded to {Destination}", filename, destinationFile); + }); + + return Task.CompletedTask; + } + private Task HubConnection_Closed(Exception arg) { Log.Warning("Relay controller connection closed: {Message}", arg.Message); @@ -491,6 +548,7 @@ namespace slskd.Relay HubConnection.On<string, long, Guid>(nameof(IRelayHub.RequestFileUpload), HandleFileUploadRequest); HubConnection.On<string, Guid>(nameof(IRelayHub.RequestFileInfo), HandleFileInfoRequest); HubConnection.On<string>(nameof(IRelayHub.Challenge), HandleAuthenticationChallenge); + HubConnection.On<string, Guid>(nameof(IRelayHub.NotifyFileDownloadCompleted), HandleNotifyFileDownloadCompleted); LastOptionsHash = optionsHash; |
