2023-10-27 17:47:17 +00:00
|
|
|
using Wonderking.Packets;
|
|
|
|
|
2023-10-12 07:05:42 +00:00
|
|
|
namespace Server;
|
2023-08-12 21:02:59 +00:00
|
|
|
|
|
|
|
using System.Reflection;
|
2023-08-09 14:23:41 +00:00
|
|
|
using MassTransit.Mediator;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NetCoreServer;
|
2023-08-12 21:02:59 +00:00
|
|
|
using Packets;
|
2023-08-09 14:23:41 +00:00
|
|
|
|
2023-08-10 08:47:35 +00:00
|
|
|
public class AuthSession : TcpSession
|
2023-08-09 14:23:41 +00:00
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly ILogger<AuthSession> _logger;
|
|
|
|
private readonly IMediator _mediator;
|
2023-08-09 14:23:41 +00:00
|
|
|
|
2023-08-10 08:47:35 +00:00
|
|
|
public AuthSession(TcpServer
|
|
|
|
server, IMediator mediator, ILogger<AuthSession> logger) : base(server)
|
2023-08-09 14:23:41 +00:00
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
this._mediator = mediator;
|
|
|
|
this._logger = logger;
|
2023-08-09 14:23:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 20:54:22 +00:00
|
|
|
public Guid AccountId { get; set; }
|
|
|
|
|
2023-08-10 23:18:11 +00:00
|
|
|
public override long Send(byte[] buffer)
|
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogInformation("Data being sent is: {Data}", BitConverter.ToString(buffer));
|
2023-08-10 23:18:11 +00:00
|
|
|
return base.Send(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Send(IPacket packet)
|
|
|
|
{
|
|
|
|
var type = packet.GetType();
|
2023-11-16 11:23:22 +00:00
|
|
|
this._logger.LogInformation("Packet of type {Type} is being serialized", type.Name);
|
2023-08-12 21:02:59 +00:00
|
|
|
var packetIdAttribute = type.GetCustomAttribute<PacketIdAttribute>();
|
2023-08-11 09:31:30 +00:00
|
|
|
if (packetIdAttribute == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 23:18:11 +00:00
|
|
|
var opcode = packetIdAttribute.Code;
|
|
|
|
|
|
|
|
Span<byte> packetData = packet.Serialize();
|
2023-08-11 09:31:30 +00:00
|
|
|
var length = (ushort)(packetData.Length + 8);
|
2023-08-10 23:18:11 +00:00
|
|
|
|
|
|
|
Span<byte> buffer = stackalloc byte[length];
|
|
|
|
buffer.Clear();
|
2023-10-12 07:05:42 +00:00
|
|
|
packetData.CopyTo(buffer[8..length]);
|
2023-08-10 23:18:11 +00:00
|
|
|
|
|
|
|
var bytesOfLength = BitConverter.GetBytes(length);
|
|
|
|
var bytesOfOpcode = BitConverter.GetBytes((ushort)opcode);
|
2023-08-11 09:31:30 +00:00
|
|
|
for (var i = 0; i < bytesOfLength.Length || i < 2; i++)
|
2023-08-10 23:18:11 +00:00
|
|
|
{
|
|
|
|
buffer[i] = bytesOfLength[i];
|
|
|
|
}
|
|
|
|
|
2023-08-11 09:31:30 +00:00
|
|
|
for (var i = 0; i < bytesOfOpcode.Length || i < 2; i++)
|
2023-08-10 23:18:11 +00:00
|
|
|
{
|
|
|
|
buffer[2 + i] = bytesOfOpcode[i];
|
|
|
|
}
|
|
|
|
|
2023-11-16 11:23:22 +00:00
|
|
|
this._logger.LogInformation("Packet data being parsed is: {Data}", BitConverter.ToString(packetData.ToArray()));
|
|
|
|
this._logger.LogInformation("Packet being parsed is: {Data}", BitConverter.ToString(buffer.ToArray()));
|
2023-08-10 23:18:11 +00:00
|
|
|
|
2023-08-11 09:31:30 +00:00
|
|
|
this.Send(buffer);
|
2023-08-10 23:18:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 14:23:41 +00:00
|
|
|
protected override void OnReceived(byte[] buffer, long offset, long size)
|
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogDebug("Length: {Size} & offset: {Offset}", size, offset);
|
2023-08-10 06:45:51 +00:00
|
|
|
Span<byte> decryptedBuffer = new byte[size];
|
2023-08-09 14:23:41 +00:00
|
|
|
|
2023-08-10 06:45:51 +00:00
|
|
|
// xor every value after the first 8 bytes
|
2023-08-12 21:02:59 +00:00
|
|
|
var dataBuffer = Decrypt(new ArraySegment<byte>(buffer, 8, (int)size - 8).ToArray());
|
2023-08-10 06:45:51 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogDebug("Length {Length}", BitConverter.ToUInt16(buffer, 0));
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-08-10 06:45:51 +00:00
|
|
|
var opCode = BitConverter.ToUInt16(buffer.ToArray(), 2);
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogDebug("Packet Op Code: {OpCode}", opCode);
|
|
|
|
this._logger.LogDebug("Some Value: {RandomValue}", buffer[4]);
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-08-10 06:45:51 +00:00
|
|
|
var clientAliveTime = BitConverter.ToUInt16(buffer.ToArray(), 5);
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogDebug("Client Alive time: {ClientAliveTime}", clientAliveTime);
|
|
|
|
this._logger.LogDebug("Might be a flag: {Flag}", buffer[7]);
|
2023-08-10 06:45:51 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogDebug("Full buffer: {Buffer}", BitConverter.ToString(dataBuffer.ToArray()));
|
2023-08-10 06:45:51 +00:00
|
|
|
|
2023-08-11 09:53:14 +00:00
|
|
|
var rawPacket = new RawPacket((OperationCode)opCode, dataBuffer, clientAliveTime, buffer[7],
|
|
|
|
buffer[4], this.Id, this);
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
_ = this._mediator.Send(rawPacket);
|
2023-10-12 07:05:42 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
this._logger.LogInformation("Connection from: {@RemoteEndpoint}", this.Socket.RemoteEndPoint?.ToString());
|
2023-08-10 06:45:51 +00:00
|
|
|
base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length);
|
2023-08-09 14:23:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 21:02:59 +00:00
|
|
|
private static byte[] Decrypt(byte[] buffer)
|
2023-08-09 14:23:41 +00:00
|
|
|
{
|
2023-08-11 09:31:30 +00:00
|
|
|
for (var i = 0; i < buffer.Length; ++i)
|
2023-08-09 14:23:41 +00:00
|
|
|
{
|
|
|
|
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2023-08-11 09:31:30 +00:00
|
|
|
}
|