From 5211113da3d95863645cc2925be5d2d64c38101b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Oct 2022 12:21:51 -0500 Subject: refactor stream handling --- .../Network/API/Controllers/NetworkController.cs | 77 +++++++++++++++------- src/slskd/Transfers/Uploads/UploadService.cs | 4 +- 2 files changed, 55 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs index 3ae2a6b5..1ee311a7 100644 --- a/src/slskd/Network/API/Controllers/NetworkController.cs +++ b/src/slskd/Network/API/Controllers/NetworkController.cs @@ -25,6 +25,8 @@ namespace slskd.Network using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.WebUtilities; + using Microsoft.Net.Http.Headers; using Serilog; using slskd.Shares; @@ -61,37 +63,64 @@ namespace slskd.Network return BadRequest("Token is not a valid Guid"); } - string credential; - IFormFile file; + // todo: make sure agent is registered and return unauthorized if not - try + if (!HttpContext.Request.HasFormContentType + || !MediaTypeHeaderValue.TryParse(HttpContext.Request.ContentType, out var mediaTypeHeader) + || string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value)) { - credential = Request.Form["credential"].ToString(); - file = Request.Form.Files[0]; + return new UnsupportedMediaTypeResult(); } - catch (Exception ex) + + Log.Debug("Handling file upload for token {Token} from a caller claiming to be agent {Agent}", token, agentName); + + string credential = default; + Stream stream = default; + string filename = default; + + try { - Log.Warning("Failed to handle file upload from agent {Agent}: {Message}", agentName, ex.Message); - return BadRequest(); + try + { + var reader = new MultipartReader(mediaTypeHeader.Boundary.Value, Request.Body); + + // the first section contains the credentail + var section = await reader.ReadNextSectionAsync(); + using var sectionReader = new StreamReader(section.Body); + credential = sectionReader.ReadToEnd(); + + // the second section contains the file + section = await reader.ReadNextSectionAsync(); + var contentDisposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition); + filename = contentDisposition.FileName.Value; + stream = section.Body; + } + catch (Exception ex) + { + Log.Warning("Failed to handle file upload for token {Token} from a caller claiming to be agent {Agent}: {Message}", token, agentName, ex.Message); + return BadRequest(); + } + + if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename, credential)) + { + Log.Warning("Failed to authenticate file upload token {Token} from a caller claiming to be agent {Agent}", agentName); + return Unauthorized(); + } + + Log.Information("File upload of {Filename} ({Token}) from agent {Agent} validated and authenticated. Forwarding file stream.", filename, token, agentName); + + // pass the stream back to the network service, which will in turn pass it to the + // upload service, and use it to feed data into the remote upload. await this call, + // it will complete when the upload is complete. + await Network.HandleGetFileStreamResponse(agentName, filename, id: guid, stream); + + Log.Information("File upload of {Filename} ({Token}) from agent {Agent} complete", filename, token, agentName); + return Ok(); } - - if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename: file.FileName, credential)) + finally { - Log.Warning("Failed to authenticate file upload from caller claiming to be agent {Agent}", agentName); - return Unauthorized(); + stream?.Dispose(); } - - // get the stream from the multipart upload - var stream = Request.Form.Files.First().OpenReadStream(); - - // pass the stream back to the network service, which will in turn pass it to the - // upload service, and use it to feed data into the remote upload. await this call, - // it will complete when the upload is complete. - await Network.HandleGetFileStreamResponse(agentName, filename: file.FileName, id: guid, stream); - - stream.Dispose(); - Console.WriteLine("Upload complete"); - return Ok(); } [HttpPost("shares/{agentName}/{token}")] diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 75184564..a368dc73 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -251,7 +251,7 @@ namespace slskd.Transfers.Uploads slotAwaiter: (tx, ct) => Queue.AwaitStartAsync(tx.Username, tx.Filename), slotReleased: (tx) => Queue.Complete(tx.Username, tx.Filename)); - if (host == "local") + if (host == "locassssl") { var completedTransfer = await Client.UploadAsync( username, @@ -269,7 +269,7 @@ namespace slskd.Transfers.Uploads username, filename, size: localFileLength, - inputStreamFactory: () => Network.GetFileStream(agentName: host, filename), + inputStreamFactory: () => Network.GetFileStream(agentName: "vdesktop", filename), options: topts, cancellationToken: cts.Token); -- cgit v1.2.3