mirror of
https://github.com/EpinelPS/EpinelPS.git
synced 2025-12-13 23:44:45 +01:00
refactoring
This commit is contained in:
@@ -10,18 +10,15 @@ namespace nksrv.Utils
|
||||
public class GreatLogger : ILogger
|
||||
{
|
||||
public LogLevel LogLevel => LogLevel.Info;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
static object lockObject = new object();
|
||||
static readonly object lockObject = new();
|
||||
public void Log(LogMessageReceivedEventArgs logEvent)
|
||||
{
|
||||
var msg = logEvent.Message;
|
||||
if (msg.StartsWith("["))
|
||||
|
||||
// strip out request id that embedio prints
|
||||
if (msg.StartsWith('['))
|
||||
{
|
||||
msg = msg.Substring(msg.IndexOf("]") + 2);
|
||||
msg = msg[(msg.IndexOf("]") + 2)..];
|
||||
}
|
||||
|
||||
// ignore telemtry server errors
|
||||
@@ -42,31 +39,27 @@ namespace nksrv.Utils
|
||||
|
||||
}
|
||||
|
||||
private ConsoleColor GetColorForMsg(LogMessageReceivedEventArgs logEvent)
|
||||
private static ConsoleColor GetColorForMsg(LogMessageReceivedEventArgs logEvent)
|
||||
{
|
||||
if (logEvent.Message.Contains("404 Not Found"))
|
||||
return ConsoleColor.Red;
|
||||
else if (logEvent.Message.Contains("200 OK"))
|
||||
return ConsoleColor.DarkGreen;
|
||||
switch (logEvent.MessageType)
|
||||
return logEvent.MessageType switch
|
||||
{
|
||||
case LogLevel.None:
|
||||
return ConsoleColor.White;
|
||||
case LogLevel.Trace:
|
||||
return ConsoleColor.Gray;
|
||||
case LogLevel.Debug:
|
||||
return ConsoleColor.Gray;
|
||||
case LogLevel.Info:
|
||||
return ConsoleColor.Gray;
|
||||
case LogLevel.Warning:
|
||||
return ConsoleColor.Yellow;
|
||||
case LogLevel.Error:
|
||||
return ConsoleColor.Red;
|
||||
case LogLevel.Fatal:
|
||||
return ConsoleColor.Red;
|
||||
default:
|
||||
return ConsoleColor.White;
|
||||
}
|
||||
LogLevel.None => ConsoleColor.White,
|
||||
LogLevel.Trace => ConsoleColor.Gray,
|
||||
LogLevel.Debug => ConsoleColor.Gray,
|
||||
LogLevel.Info => ConsoleColor.Gray,
|
||||
LogLevel.Warning => ConsoleColor.Yellow,
|
||||
LogLevel.Error => ConsoleColor.Red,
|
||||
LogLevel.Fatal => ConsoleColor.Red,
|
||||
_ => ConsoleColor.White,
|
||||
};
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,13 +13,13 @@ namespace nksrv.Utils
|
||||
{
|
||||
public class AccessToken
|
||||
{
|
||||
public string Token;
|
||||
public string Token = "";
|
||||
public long ExpirationTime;
|
||||
public ulong UserID;
|
||||
}
|
||||
public class FieldInfo
|
||||
{
|
||||
public List<NetFieldStageData> CompletedStages = new();
|
||||
public List<NetFieldStageData> CompletedStages = [];
|
||||
}
|
||||
|
||||
public class Character
|
||||
@@ -49,26 +49,26 @@ namespace nksrv.Utils
|
||||
|
||||
|
||||
// Game data
|
||||
public List<string> CompletedScenarios = new();
|
||||
public Dictionary<int, FieldInfo> FieldInfo = new();
|
||||
public Dictionary<string, string> MapJson = new();
|
||||
public Dictionary<CurrencyType, long> Currency = new Dictionary<CurrencyType, long>() {
|
||||
public List<string> CompletedScenarios = [];
|
||||
public Dictionary<int, FieldInfo> FieldInfo = [];
|
||||
public Dictionary<string, string> MapJson = [];
|
||||
public Dictionary<CurrencyType, long> Currency = new() {
|
||||
{ CurrencyType.ContentStamina, 2 },
|
||||
{ CurrencyType.CharPremiumTicket, 23422 }
|
||||
};
|
||||
|
||||
public List<Character> Characters = new();
|
||||
public List<Character> Characters = [];
|
||||
public NetWholeUserTeamData TeamData = new();
|
||||
public List<int> ClearedTutorials = new();
|
||||
public List<int> ClearedTutorials = [];
|
||||
}
|
||||
public class CoreInfo
|
||||
{
|
||||
public List<User> Users = new List<User>();
|
||||
public List<User> Users = [];
|
||||
|
||||
public List<AccessToken> LauncherAccessTokens = new List<AccessToken>();
|
||||
public List<AccessToken> LauncherAccessTokens = [];
|
||||
|
||||
|
||||
public Dictionary<string, GameClientInfo> GameClientTokens = new Dictionary<string, GameClientInfo>();
|
||||
public Dictionary<string, GameClientInfo> GameClientTokens = [];
|
||||
}
|
||||
internal class JsonDb
|
||||
{
|
||||
|
||||
34
nksrv/Utils/LoggingHttpHandler.cs
Normal file
34
nksrv/Utils/LoggingHttpHandler.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace nksrv.Utils
|
||||
{
|
||||
public class LoggingHttpHandler(HttpMessageHandler innerHandler) : DelegatingHandler(innerHandler)
|
||||
{
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine("Request:");
|
||||
Console.WriteLine(request.ToString());
|
||||
if (request.Content != null)
|
||||
{
|
||||
Console.WriteLine(await request.Content.ReadAsStringAsync(cancellationToken));
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
HttpResponseMessage response = await base.SendAsync(request, cancellationToken);
|
||||
|
||||
Console.WriteLine("Response:");
|
||||
Console.WriteLine(response.ToString());
|
||||
if (response.Content != null)
|
||||
{
|
||||
Console.WriteLine(await response.Content.ReadAsStringAsync(cancellationToken));
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,11 +15,11 @@ namespace nksrv.Utils
|
||||
{
|
||||
public class PacketDecryption
|
||||
{
|
||||
public static async Task<PacketDecryptResponse> DecryptOrReturnContentAsync(IHttpContext ctx, bool decompress = false)
|
||||
public static async Task<PacketDecryptResponse> DecryptOrReturnContentAsync(IHttpContext ctx)
|
||||
{
|
||||
byte[] bin = Array.Empty<byte>();
|
||||
byte[] bin = [];
|
||||
|
||||
using MemoryStream buffer = new MemoryStream();
|
||||
using MemoryStream buffer = new();
|
||||
|
||||
var stream = ctx.Request.InputStream;
|
||||
|
||||
@@ -46,20 +46,15 @@ namespace nksrv.Utils
|
||||
var decryptionToken = CBorReadString(stream);
|
||||
var nonce = CBorReadByteString(stream);
|
||||
|
||||
MemoryStream encryptedBytes = new MemoryStream();
|
||||
MemoryStream encryptedBytes = new();
|
||||
stream.CopyTo(encryptedBytes);
|
||||
|
||||
var bytes = encryptedBytes.ToArray();
|
||||
|
||||
var key = LobbyHandler.GetInfo(decryptionToken);
|
||||
if (key == null)
|
||||
{
|
||||
throw HttpException.BadRequest("Invalid decryption token");
|
||||
}
|
||||
|
||||
var key = LobbyHandler.GetInfo(decryptionToken) ?? throw HttpException.BadRequest("Invalid decryption token");
|
||||
var additionalData = GenerateAdditionalData(decryptionToken, false);
|
||||
|
||||
var x = SecretAeadXChaCha20Poly1305.Decrypt(bytes, nonce, key.Keys.ReadSharedSecret, additionalData.ToArray());
|
||||
var x = SecretAeadXChaCha20Poly1305.Decrypt(bytes, nonce, key.Keys.ReadSharedSecret, [.. additionalData]);
|
||||
|
||||
var ms = new MemoryStream(x);
|
||||
// File.WriteAllBytes("fullPkt-decr", ms.ToArray());
|
||||
@@ -78,7 +73,7 @@ namespace nksrv.Utils
|
||||
//File.WriteAllBytes("contentsgzip", contents);
|
||||
// gzip compression is used
|
||||
using Stream csStream = new GZipStream(new MemoryStream(contents), CompressionMode.Decompress);
|
||||
using MemoryStream decoded = new MemoryStream();
|
||||
using MemoryStream decoded = new();
|
||||
csStream.CopyTo(decoded);
|
||||
|
||||
contents = decoded.ToArray();
|
||||
@@ -159,13 +154,8 @@ namespace nksrv.Utils
|
||||
|
||||
public static byte[] EncryptData(byte[] message, string authToken)
|
||||
{
|
||||
var key = LobbyHandler.GetInfo(authToken);
|
||||
if (key == null)
|
||||
{
|
||||
throw HttpException.BadRequest("Invalid decryption token");
|
||||
}
|
||||
|
||||
MemoryStream m = new MemoryStream();
|
||||
var key = LobbyHandler.GetInfo(authToken) ?? throw HttpException.BadRequest("Invalid decryption token");
|
||||
MemoryStream m = new();
|
||||
|
||||
m.WriteByte(89); // cbor ushort
|
||||
|
||||
@@ -205,13 +195,13 @@ namespace nksrv.Utils
|
||||
var additionalData = GenerateAdditionalData(authToken, true);
|
||||
|
||||
// prep payload
|
||||
MemoryStream msm = new MemoryStream();
|
||||
MemoryStream msm = new();
|
||||
msm.WriteByte(88);
|
||||
msm.WriteByte(0);
|
||||
|
||||
msm.Write(message);
|
||||
|
||||
var encryptedBytes = SecretAeadXChaCha20Poly1305.Encrypt(msm.ToArray(), nonce, key.Keys.TransferSharedSecret, additionalData.ToArray());
|
||||
var encryptedBytes = SecretAeadXChaCha20Poly1305.Encrypt(msm.ToArray(), nonce, key.Keys.TransferSharedSecret, [.. additionalData]);
|
||||
|
||||
// write encrypted data
|
||||
m.Write(encryptedBytes);
|
||||
@@ -293,16 +283,18 @@ namespace nksrv.Utils
|
||||
{
|
||||
var b = s.ReadByte();
|
||||
var type = b & 0x1f;
|
||||
var res = new CBorItem();
|
||||
res.MajorType = (b >> 5) & 7;
|
||||
CBorItem res = new()
|
||||
{
|
||||
MajorType = (b >> 5) & 7
|
||||
};
|
||||
switch (type)
|
||||
{
|
||||
case 24:
|
||||
// byte
|
||||
res.ByteValue = new byte[] { (byte)s.ReadByte() };
|
||||
res.ByteValue = [(byte)s.ReadByte()];
|
||||
res.type = CBorItemType.Byte;
|
||||
|
||||
res.FullValue = (int)res.ByteValue[0];
|
||||
res.FullValue = res.ByteValue[0];
|
||||
break;
|
||||
case 25:
|
||||
byte[] arr = new byte[2];
|
||||
@@ -331,9 +323,7 @@ namespace nksrv.Utils
|
||||
while (i != buf.Length)
|
||||
{
|
||||
if (i > buf.Length)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
var pos = buf.Length - i;
|
||||
throw new ArgumentOutOfRangeException(nameof(buf));
|
||||
var read = s.Read(buf, i, buf.Length - i);
|
||||
if (read == 0)
|
||||
break;
|
||||
@@ -349,13 +339,13 @@ namespace nksrv.Utils
|
||||
public class PacketDecryptResponse
|
||||
{
|
||||
public ulong UserId;
|
||||
public string UsedAuthToken;
|
||||
public byte[] Contents;
|
||||
public string UsedAuthToken = "";
|
||||
public byte[] Contents = [];
|
||||
}
|
||||
public class CBorItem
|
||||
{
|
||||
public CBorItemType type;
|
||||
public byte[] ByteValue;
|
||||
public byte[] ByteValue = [];
|
||||
public ushort UShortValue;
|
||||
public int MajorType;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user