From ddee3675861863485d26775cbac12dbabce3302d Mon Sep 17 00:00:00 2001 From: Slendy Date: Thu, 20 Apr 2023 11:00:55 -0500 Subject: [PATCH] Add more logging to resources and matching Change uploading to use ReadAllAsync instead of readFromPipeReader --- .../Controllers/Matching/MatchController.cs | 4 +-- .../Resources/ResourcesController.cs | 33 +++++-------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs index e022bddb..6b15d445 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Matching/MatchController.cs @@ -59,7 +59,7 @@ public class MatchController : ControllerBase } catch(Exception e) { - Logger.Error("Exception while parsing matchData: ", LogArea.Match); + Logger.Error($"Exception while parsing matchData: body='{bodyString}'", LogArea.Match); Logger.Error(e.ToDetailedException(), LogArea.Match); return this.BadRequest(); @@ -67,7 +67,7 @@ public class MatchController : ControllerBase if (matchData == null) { - Logger.Error($"Could not parse match data: {nameof(matchData)} is null", LogArea.Match); + Logger.Error($"Could not parse match data: {nameof(matchData)} is null, body='{bodyString}'", LogArea.Match); return this.BadRequest(); } diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Resources/ResourcesController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Resources/ResourcesController.cs index 0ba95849..7973c9a8 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Resources/ResourcesController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Resources/ResourcesController.cs @@ -1,6 +1,5 @@ #nullable enable -using System.Buffers; -using System.IO.Pipelines; +using System.Text; using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Files; using LBPUnion.ProjectLighthouse.Logging; @@ -67,11 +66,18 @@ public class ResourcesController : ControllerBase if (!fullPath.StartsWith(FileHelper.FullResourcePath)) return this.BadRequest(); Logger.Info($"Processing resource upload (hash: {hash})", LogArea.Resources); - LbpFile file = new(await readFromPipeReader(this.Request.BodyReader)); + byte[] data = await this.Request.BodyReader.ReadAllAsync(); + LbpFile file = new(data); if (!FileHelper.IsFileSafe(file)) { Logger.Warn($"File is unsafe (hash: {hash}, type: {file.FileType})", LogArea.Resources); + if (file.FileType == LbpFileType.Unknown) + { + Logger.Warn($"({hash}): File header: '{Convert.ToHexString(data[..4])}', " + + $"ascii='{Encoding.ASCII.GetString(data[..4])}'", + LogArea.Resources); + } return this.Conflict(); } @@ -93,25 +99,4 @@ public class ResourcesController : ControllerBase await IOFile.WriteAllBytesAsync(path, file.Data); return this.Ok(); } - - // Written with reference from - // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/request-response?view=aspnetcore-5.0 - // Surprisingly doesn't take seconds. (67ms for a 100kb file) - private static async Task readFromPipeReader(PipeReader reader) - { - List data = new(); - while (true) - { - ReadResult readResult = await reader.ReadAsync(); - ReadOnlySequence buffer = readResult.Buffer; - - if (readResult.IsCompleted && buffer.Length > 0) data.AddRange(buffer.ToArray()); - - reader.AdvanceTo(buffer.Start, buffer.End); - - if (readResult.IsCompleted) break; - } - - return data.ToArray(); - } } \ No newline at end of file