From 6ff9cfb3cb27f149c473850d02fdb51937ca2056 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 21 Oct 2022 09:36:18 -0500 Subject: handle correct names --- src/slskd/Network/NetworkClient.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Network/NetworkClient.cs') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index a231dad2..1c2515d2 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -318,9 +318,9 @@ namespace slskd.Network HubConnection.Reconnecting += HubConnection_Reconnecting; HubConnection.Closed += HubConnection_Closed; - HubConnection.On(NetworkHubMethods.RequestFile, HandleFileRequest); - HubConnection.On(NetworkHubMethods.RequestFileInfo, HandleFileInfoRequest); - HubConnection.On(NetworkHubMethods.AuthenticationChallenge, HandleAuthenticationChallenge); + HubConnection.On(nameof(INetworkHub.RequestFile), HandleFileRequest); + HubConnection.On(nameof(INetworkHub.RequestFileInfo), HandleFileInfoRequest); + HubConnection.On(nameof(INetworkHub.Challenge), HandleAuthenticationChallenge); LastOptionsHash = optionsHash; -- cgit v1.2.3 From 1d927e49a93ad24ba6dbc91f30f185212775ff30 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 22 Oct 2022 12:13:04 -0500 Subject: update routes, provide credentials in rest calls --- src/slskd/Network/NetworkClient.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/slskd/Network/NetworkClient.cs') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index 1c2515d2..6a2a374d 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,9 +155,19 @@ 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; + + var key = options.Network.Controller.Secret.FromBase62(); + var tokenBytes = token.ToString().FromBase62(); + + return Aes.Encrypt(tokenBytes, key).ToBase62(); + } + + private async Task HandleFileRequest(string filename, Guid token) + { + Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token); try { @@ -171,15 +182,16 @@ namespace slskd.Network } 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 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, id); + Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token); var response = await CreateHttpClient().SendAsync(request); if (!response.IsSuccessStatusCode) @@ -196,7 +208,7 @@ namespace slskd.Network 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); + await HubConnection.InvokeAsync(nameof(NetworkHub.NotifyUploadFailed), token); } } -- cgit v1.2.3 From 92fab4ce25ad8b23fc14f946490d05d8d1da9c4c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Oct 2022 13:55:15 -0500 Subject: run uploads as a separate task --- src/slskd/Network/NetworkClient.cs | 71 ++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 33 deletions(-) (limited to 'src/slskd/Network/NetworkClient.cs') diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs index 6a2a374d..5d8ab100 100644 --- a/src/slskd/Network/NetworkClient.cs +++ b/src/slskd/Network/NetworkClient.cs @@ -165,51 +165,56 @@ namespace slskd.Network return Aes.Encrypt(tokenBytes, key).ToBase62(); } - private async Task HandleFileRequest(string filename, Guid token) + private Task HandleFileRequest(string filename, Guid token) { - Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token); - - try + _ = Task.Run(async () => { - var (_, localFilename) = await Shares.ResolveFileAsync(filename); - - var localFileInfo = new FileInfo(localFilename); + Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token); - if (!localFileInfo.Exists) + try { - Shares.RequestScan(); - throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed."); - } + var (_, localFilename) = await Shares.ResolveFileAsync(filename); - 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 }, - }; + var localFileInfo = new FileInfo(localFilename); - request.Content = content; + if (!localFileInfo.Exists) + { + Shares.RequestScan(); + throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed."); + } - Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token); - var response = await CreateHttpClient().SendAsync(request); + 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 }, + }; - if (!response.IsSuccessStatusCode) - { - Log.Error("Upload of file {Filename} with ID {Id} failed: {StatusCode}", response.StatusCode); + 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), token); - } + return Task.CompletedTask; } private async Task HandleFileInfoRequest(string filename, Guid id) -- cgit v1.2.3