diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-23 12:23:34 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-10-23 12:23:34 -0500 |
| commit | 89900c6646c0e0a17c573f152e6c31c8506df022 (patch) | |
| tree | 10251a5a86bb629cfc02902c07111e6719f0b684 /src/slskd/Network/API/Controllers/NetworkController.cs | |
| parent | a60490c61a0a035ebb67108da4be38b3c7568681 (diff) | |
| parent | 5211113da3d95863645cc2925be5d2d64c38101b (diff) | |
| download | slskd-89900c6646c0e0a17c573f152e6c31c8506df022.tar.gz | |
Merge branch 'signalr-result' of github.com:slskd/slskd into signalr-result
Diffstat (limited to 'src/slskd/Network/API/Controllers/NetworkController.cs')
| -rw-r--r-- | src/slskd/Network/API/Controllers/NetworkController.cs | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/src/slskd/Network/API/Controllers/NetworkController.cs b/src/slskd/Network/API/Controllers/NetworkController.cs index 20c2caa0..5d3f4c77 100644 --- a/src/slskd/Network/API/Controllers/NetworkController.cs +++ b/src/slskd/Network/API/Controllers/NetworkController.cs @@ -66,50 +66,64 @@ namespace slskd.Network return BadRequest("Token is not a valid Guid"); } + // todo: make sure agent is registered and return unauthorized if not + + if (!HttpContext.Request.HasFormContentType + || !MediaTypeHeaderValue.TryParse(HttpContext.Request.ContentType, out var mediaTypeHeader) + || string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value)) + { + return new UnsupportedMediaTypeResult(); + } + + 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 { - var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType)); - var reader = new MultipartReader(boundary, Request.Body); - - var credentialSection = await reader.ReadNextSectionAsync(); - using var sr = new StreamReader(credentialSection.Body); - credential = sr.ReadToEnd(); - - var fileSection = await reader.ReadNextSectionAsync(); - ContentDispositionHeaderValue.TryParse(fileSection.ContentDisposition, out var contentDisposition); - filename = contentDisposition.FileName.Value; - stream = fileSection.Body; + 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(); } - catch (Exception ex) + finally { - Log.Warning("Failed to handle file upload from agent {Agent}: {Message}", agentName, ex.Message); - return BadRequest(); + stream?.Dispose(); } - - if (!Network.TryValidateFileUploadCredential(token: guid, agentName, filename, credential)) - { - Log.Warning("Failed to authenticate file upload from caller claiming to be agent {Agent}", agentName); - return Unauthorized(); - } - - Console.WriteLine($"credential: {credential}, filename: {filename}, stream pos {stream.Position} len {stream.Length}"); - - Console.WriteLine("Opening request stream..."); - - Console.WriteLine("Stream open. calling handler..."); - - // 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); - - stream.Dispose(); - Console.WriteLine("Upload complete"); - return Ok(); } //[DisableFormValueModelBinding] |
