diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-27 09:44:17 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-27 09:44:17 -0500 |
| commit | 028af934a264a91c831eac3561c5e24833b6ee38 (patch) | |
| tree | 125b0dc693c72cf97370f9ee9a9f7e51fbdc9210 /src/slskd/Network/NetworkClient.cs | |
| parent | a85c5cf8d414b71ac66455e9d97cac62b29c95ad (diff) | |
| parent | 218f5d8b2376fbee798c9a4575e5068a602aec04 (diff) | |
| download | slskd-028af934a264a91c831eac3561c5e24833b6ee38.tar.gz | |
Merge pull request #711 from slskd/signalr-result
Various improvements to the Network mode functionality
Diffstat (limited to 'src/slskd/Network/NetworkClient.cs')
| -rw-r--r-- | src/slskd/Network/NetworkClient.cs | 87 |
1 files changed, 52 insertions, 35 deletions
diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index a231dad2..5d8ab100 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -134,9 +134,10 @@ namespace slskd.Network var stream = new FileStream(temp, FileMode.Open, FileAccess.Read); - using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/shares/{token}"); + using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/shares/{OptionsMonitor.CurrentValue.InstanceName}/{token}"); using var content = new MultipartFormDataContent { + { new StringContent(ComputeCredential(token)), "credential" }, { new StringContent(Shares.LocalHost.Shares.ToJson()), "shares" }, { new StreamContent(stream), "database", "shares" }, }; @@ -154,50 +155,66 @@ namespace slskd.Network Log.Information("Upload shares succeeded"); } - private async Task HandleFileRequest(string filename, Guid id) + private string ComputeCredential(Guid token) { - Log.Information("Network controller requested file {Filename} with ID {Id}", filename, id); + var options = OptionsMonitor.CurrentValue; - try - { - var (_, localFilename) = await Shares.ResolveFileAsync(filename); + var key = options.Network.Controller.Secret.FromBase62(); + var tokenBytes = token.ToString().FromBase62(); - var localFileInfo = new FileInfo(localFilename); + return Aes.Encrypt(tokenBytes, key).ToBase62(); + } - if (!localFileInfo.Exists) - { - Shares.RequestScan(); - throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed."); - } + private Task HandleFileRequest(string filename, Guid token) + { + _ = Task.Run(async () => + { + Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token); - using var stream = new FileStream(localFileInfo.FullName, FileMode.Open, FileAccess.Read); - using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/files/{id}"); - using var content = new MultipartFormDataContent + try { - { new StreamContent(stream), "file", filename }, - }; + var (_, localFilename) = await Shares.ResolveFileAsync(filename); - request.Content = content; + var localFileInfo = new FileInfo(localFilename); - Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, id); - var response = await CreateHttpClient().SendAsync(request); + if (!localFileInfo.Exists) + { + Shares.RequestScan(); + throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed."); + } - if (!response.IsSuccessStatusCode) - { - Log.Error("Upload of file {Filename} with ID {Id} failed: {StatusCode}", response.StatusCode); + using var stream = new FileStream(localFileInfo.FullName, FileMode.Open, FileAccess.Read); + using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/files/{OptionsMonitor.CurrentValue.InstanceName}/{token}"); + using var content = new MultipartFormDataContent + { + { new StringContent(ComputeCredential(token)), "credential" }, + { new StreamContent(stream), "file", filename }, + }; + + request.Content = content; + + Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token); + var response = await CreateHttpClient().SendAsync(request); + + if (!response.IsSuccessStatusCode) + { + Log.Error("Upload of file {Filename} with ID {Id} failed: {StatusCode}", response.StatusCode); + } + else + { + Log.Information("Upload of file {Filename} with ID {Id} succeeded.", filename); + } } - else + catch (Exception ex) { - Log.Information("Upload of file {Filename} with ID {Id} succeeded.", filename); + 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(NetworkHub.NotifyUploadFailed), token); } - } - catch (Exception ex) - { - 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(NetworkHub.NotifyUploadFailed), id); - } + return Task.CompletedTask; } private async Task HandleFileInfoRequest(string filename, Guid id) @@ -318,9 +335,9 @@ namespace slskd.Network HubConnection.Reconnecting += HubConnection_Reconnecting; HubConnection.Closed += HubConnection_Closed; - HubConnection.On<string, Guid>(NetworkHubMethods.RequestFile, HandleFileRequest); - HubConnection.On<string, Guid>(NetworkHubMethods.RequestFileInfo, HandleFileInfoRequest); - HubConnection.On<string>(NetworkHubMethods.AuthenticationChallenge, HandleAuthenticationChallenge); + HubConnection.On<string, Guid>(nameof(INetworkHub.RequestFile), HandleFileRequest); + HubConnection.On<string, Guid>(nameof(INetworkHub.RequestFileInfo), HandleFileInfoRequest); + HubConnection.On<string>(nameof(INetworkHub.Challenge), HandleAuthenticationChallenge); LastOptionsHash = optionsHash; |
