continuity/Continuity.AuthServer/AuthSession.cs
Timothy Schenk 3a24dabdf2
chore: formatting and slnx
Signed-off-by: Timothy Schenk <admin@rainote.dev>
2025-01-16 14:30:40 +01:00

100 lines
3.7 KiB
C#

// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
using System.Net.Sockets;
using Continuity.AuthServer.Packets;
using MassTransit.Mediator;
using Microsoft.Extensions.Logging;
using NetCoreServer;
using RaiNote.PacketMediator;
using Wonderking.Packets;
namespace Continuity.AuthServer;
public class AuthSession : TcpSession {
private readonly PacketDistributorService<OperationCode, AuthSession> _distributorService;
private readonly ILogger<AuthSession> _logger;
private readonly IMediator _mediator;
public AuthSession(TcpServer
server, IMediator mediator, ILogger<AuthSession> logger,
PacketDistributorService<OperationCode, AuthSession> distributorService) : base(server) {
_mediator = mediator;
_logger = logger;
_distributorService = distributorService;
}
public Guid AccountId { get; set; }
public override long Send(byte[] buffer) {
_logger.LogInformation("Data being sent is: {Data}", BitConverter.ToString(buffer));
return base.Send(buffer);
}
public Task SendAsync(IOutgoingPacket packet) {
var opcode = _distributorService.GetOperationCodeByPacketType(packet);
Span<byte> packetData = packet.Serialize();
var length = (ushort)(packetData.Length + 8);
Span<byte> buffer = stackalloc byte[length];
buffer.Clear();
packetData.CopyTo(buffer[8..length]);
var bytesOfLength = BitConverter.GetBytes(length);
var bytesOfOpcode = BitConverter.GetBytes((ushort)opcode);
for (var i = 0; i < bytesOfLength.Length || i < 2; i++) {
buffer[i] = bytesOfLength[i];
}
for (var i = 0; i < bytesOfOpcode.Length || i < 2; i++) {
buffer[2 + i] = bytesOfOpcode[i];
}
_logger.LogInformation("Packet data being parsed is: {Data}", BitConverter.ToString(packetData.ToArray()));
_logger.LogInformation("Packet being parsed is: {Data}", BitConverter.ToString(buffer.ToArray()));
SendAsync(buffer);
return Task.CompletedTask;
}
protected override void OnReceived(byte[] buffer, long offset, long size) {
_logger.LogDebug("Length: {Size} & offset: {Offset}", size, offset);
Span<byte> decryptedBuffer = stackalloc byte[(int)size];
// xor every value after the first 8 bytes
var dataBuffer = Decrypt(buffer.AsSpan(8, (int)size - 8));
_logger.LogDebug("Length {Length}", BitConverter.ToUInt16(buffer, 0));
var opCode = BitConverter.ToUInt16(buffer.ToArray(), 2);
_logger.LogDebug("Packet Op Code: {OpCode}", opCode);
_logger.LogDebug("Some Value: {RandomValue}", buffer[4]);
var clientAliveTime = BitConverter.ToUInt16(buffer.ToArray(), 5);
_logger.LogDebug("Client Alive time: {ClientAliveTime}", clientAliveTime);
_logger.LogDebug("Might be a flag: {Flag}", buffer[7]);
_logger.LogDebug("Full buffer: {Buffer}", BitConverter.ToString(dataBuffer.ToArray()));
var rawPacket = new RawPacket((OperationCode)opCode, dataBuffer, clientAliveTime, buffer[7],
buffer[4], Id, this);
_ = _mediator.Send(rawPacket);
_logger.LogInformation("Connection from: {@RemoteEndpoint}", Socket.RemoteEndPoint?.ToString());
base.OnReceived(decryptedBuffer.ToArray(), offset, decryptedBuffer.Length);
}
private static Span<byte> Decrypt(Span<byte> buffer) {
for (var i = 0; i < buffer.Length; ++i) {
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
}
return buffer;
}
protected override void OnError(SocketError error) {
_logger.LogWarning("An error has occured: {Error}", error);
}
}