aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-10-23 12:21:51 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-10-23 12:21:51 -0500
commit5211113da3d95863645cc2925be5d2d64c38101b (patch)
tree4fbe45cbff50a4dcc1574320006170984750d5a9 /src
parent913821a6c012b287603230b2dc93191b9f7cfdc9 (diff)
downloadslskd-5211113da3d95863645cc2925be5d2d64c38101b.tar.gz
refactor stream handling
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Network/API/Controllers/NetworkController.cs77
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs4
2 files changed, 55 insertions, 26 deletions
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);