2024-08-13 07:55:18 +00:00
|
|
|
|
using KbinXml.Net;
|
|
|
|
|
using Medusa.Core.Utils;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Medusa.Core.Middlewares
|
|
|
|
|
{
|
|
|
|
|
public class BodyParsingMiddleware(RequestDelegate next)
|
|
|
|
|
{
|
|
|
|
|
static readonly byte[] Key =
|
|
|
|
|
Convert.FromHexString("00000000000069D74627D985EE2187161570D08D93B12455035B6DF0D8205DF5");
|
|
|
|
|
|
|
|
|
|
private readonly RequestDelegate _next = next;
|
|
|
|
|
|
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
if(context.Request.Method == "GET")
|
|
|
|
|
{
|
|
|
|
|
await _next(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var body = await ParseRequest(context);
|
|
|
|
|
|
|
|
|
|
context.Items["Encoding"] = body.Declaration.Encoding;
|
2024-08-13 23:08:00 +00:00
|
|
|
|
var bodyString = body.ToString();
|
|
|
|
|
context.Request.Body = new MemoryStream(Encoding.GetEncoding(932).GetBytes(bodyString));
|
2024-08-13 07:55:18 +00:00
|
|
|
|
|
|
|
|
|
await _next(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<XDocument> ParseRequest(HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
var isCompressed = context.Request.Headers["X-Compress"].ToString().Contains("lz77");
|
|
|
|
|
var info = context.Request.Headers["X-Eamuse-Info"].FirstOrDefault();
|
|
|
|
|
var contentLength = context.Request.Headers.ContentLength ?? 0;
|
|
|
|
|
byte[] data = new byte[(int)contentLength];
|
2024-08-13 23:08:00 +00:00
|
|
|
|
await context.Request.Body.ReadAsync(data.AsMemory(0, (int)contentLength));
|
2024-08-13 07:55:18 +00:00
|
|
|
|
|
|
|
|
|
if(info is not null)
|
|
|
|
|
{
|
|
|
|
|
string[] infoParts = info.Split('-');
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 6; i++)
|
|
|
|
|
{
|
|
|
|
|
Key[i] = Convert.ToByte((infoParts[1] + infoParts[2]).Substring(i << 1, 2), 0x10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte[] rc4Key = MD5.HashData(Key);
|
|
|
|
|
data = RC4.Decrypt(rc4Key, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(isCompressed)
|
|
|
|
|
{
|
|
|
|
|
data = LZ77.Decompress(data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 23:08:00 +00:00
|
|
|
|
var returnData = new XDocument();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
returnData = KbinConverter.ReadXmlLinq(data);
|
|
|
|
|
} catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
}
|
2024-08-13 07:55:18 +00:00
|
|
|
|
//Data is now xml in konami binary form
|
2024-08-13 23:08:00 +00:00
|
|
|
|
return returnData;
|
2024-08-13 07:55:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|