DTOs for index mostly done, doing DB models
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Text;
|
||||
using MessagePack;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
@@ -8,6 +9,7 @@ using SVSim.Database.Models;
|
||||
using SVSim.EmulatedEntrypoint.Constants;
|
||||
using SVSim.EmulatedEntrypoint.Extensions;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Internal;
|
||||
using SVSim.EmulatedEntrypoint.Security;
|
||||
using SVSim.EmulatedEntrypoint.Services;
|
||||
|
||||
@@ -31,7 +33,7 @@ public class ShadowverseTranslationMiddleware : IMiddleware
|
||||
{
|
||||
bool isUnity = context.Request.Headers.UserAgent.Any(agent => agent?.Contains("UnityPlayer") ?? false);
|
||||
string path = context.Request.Path;
|
||||
var endpointDescriptor =
|
||||
ActionDescriptor? endpointDescriptor =
|
||||
_actionDescriptorCollectionProvider.ActionDescriptors.Items.FirstOrDefault(ad =>
|
||||
$"/{ad.AttributeRouteInfo.Template}".Equals(path, StringComparison.InvariantCultureIgnoreCase));
|
||||
if (!isUnity || endpointDescriptor == null)
|
||||
@@ -41,12 +43,12 @@ public class ShadowverseTranslationMiddleware : IMiddleware
|
||||
}
|
||||
|
||||
// Replace response body stream to re-access it.
|
||||
using var tempResponseBody = new MemoryStream();
|
||||
var originalResponsebody = context.Response.Body;
|
||||
using MemoryStream tempResponseBody = new MemoryStream();
|
||||
Stream originalResponsebody = context.Response.Body;
|
||||
context.Response.Body = tempResponseBody;
|
||||
|
||||
// Pull out the request bytes into a stream
|
||||
using var requestBytesStream = new MemoryStream();
|
||||
using MemoryStream requestBytesStream = new MemoryStream();
|
||||
await context.Request.Body.CopyToAsync(requestBytesStream);
|
||||
byte[] requestBytes = requestBytesStream.ToArray();
|
||||
|
||||
@@ -58,8 +60,8 @@ public class ShadowverseTranslationMiddleware : IMiddleware
|
||||
requestBytes = Encryption.Decrypt(requestBytes, udid);
|
||||
object? data = MessagePackSerializer.Deserialize(endpointDescriptor.Parameters.FirstOrDefault().ParameterType,
|
||||
requestBytes);
|
||||
var json = JsonConvert.SerializeObject(data);
|
||||
var newStream = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
string json = JsonConvert.SerializeObject(data);
|
||||
StringContent newStream = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
context.Request.Body = newStream.ReadAsStream();
|
||||
context.Request.Headers.ContentType = new StringValues("application/json");
|
||||
|
||||
@@ -68,16 +70,16 @@ public class ShadowverseTranslationMiddleware : IMiddleware
|
||||
Viewer? viewer = context.GetViewer();
|
||||
|
||||
// Grab the response object
|
||||
var responseType = ((ControllerActionDescriptor)endpointDescriptor).MethodInfo.ReturnType;
|
||||
Type responseType = ((ControllerActionDescriptor)endpointDescriptor).MethodInfo.ReturnType;
|
||||
if (responseType.IsGenericType && responseType.GetGenericTypeDefinition() == typeof(Task<>))
|
||||
{
|
||||
responseType = responseType.GetGenericArguments()[0];
|
||||
}
|
||||
using var responseBytesStream = new MemoryStream();
|
||||
using MemoryStream responseBytesStream = new MemoryStream();
|
||||
context.Response.Body.Seek(0, SeekOrigin.Begin);
|
||||
await context.Response.Body.CopyToAsync(responseBytesStream);
|
||||
var responseBytes = responseBytesStream.ToArray();
|
||||
var responseData = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(responseBytes), responseType);
|
||||
byte[] responseBytes = responseBytesStream.ToArray();
|
||||
object? responseData = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(responseBytes), responseType);
|
||||
|
||||
// Wrap the response in a datawrapper
|
||||
DataWrapper wrappedResponseData = new DataWrapper
|
||||
@@ -96,7 +98,7 @@ public class ShadowverseTranslationMiddleware : IMiddleware
|
||||
};
|
||||
|
||||
// Convert the response into a messagepack, encrypt it
|
||||
var packedData = MessagePackSerializer.Serialize<DataWrapper>(wrappedResponseData);
|
||||
byte[] packedData = MessagePackSerializer.Serialize<DataWrapper>(wrappedResponseData);
|
||||
packedData = Encryption.Encrypt(packedData, udid);
|
||||
await originalResponsebody.WriteAsync(Encoding.UTF8.GetBytes(Convert.ToBase64String(packedData)));
|
||||
context.Response.Body = originalResponsebody;
|
||||
|
||||
Reference in New Issue
Block a user