mirror-ac/server/Message.cs

132 lines
4.3 KiB
C#
Raw Normal View History

2023-09-02 15:47:15 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
2023-09-02 17:56:46 +02:00
using System.Runtime.CompilerServices;
2023-09-02 15:47:15 +02:00
using System.Text;
using System.Threading.Tasks;
using Serilog;
2023-09-02 17:56:46 +02:00
using server;
2023-09-07 19:49:36 +02:00
using System.Net;
using System.Net.Sockets;
2023-09-08 20:41:11 +02:00
using server.Types.ClientReport;
using server.Types.ClientSend;
2023-09-07 19:49:36 +02:00
using System.Runtime.InteropServices;
2023-09-02 15:47:15 +02:00
namespace server
{
public class Message
{
private byte[] _buffer;
private int _bufferSize;
2023-09-02 17:56:46 +02:00
private ILogger _logger;
2023-09-07 09:21:00 +02:00
private PACKET_HEADER _header;
2023-09-07 19:49:36 +02:00
private NetworkStream _networkStream;
2023-09-02 15:47:15 +02:00
private enum MESSAGE_TYPE
{
2023-09-08 20:41:11 +02:00
MESSAGE_TYPE_CLIENT_REPORT = 1,
MESSAGE_TYPE_CLIENT_SEND = 2,
MESSAGE_TYPE_CLIENT_REQUEST = 3
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
private enum CLIENT_SEND_REQUEST_ID
{
SYSTEM_INFORMATION = 10
}
public struct PACKET_HEADER
2023-09-02 15:47:15 +02:00
{
2023-09-07 09:21:00 +02:00
public int message_type;
public Int64 steam64_id;
};
2023-09-02 15:47:15 +02:00
2023-09-08 20:41:11 +02:00
struct PACKET_REQUEST_HEADER
2023-09-02 15:47:15 +02:00
{
2023-09-08 20:41:11 +02:00
public int RequestId;
2023-09-02 15:47:15 +02:00
}
2023-09-07 19:49:36 +02:00
public Message(NetworkStream networkStream, byte[] buffer, int bufferSize, ILogger logger)
2023-09-02 15:47:15 +02:00
{
2023-09-07 19:49:36 +02:00
_networkStream = networkStream;
2023-09-02 15:47:15 +02:00
_buffer = buffer;
_bufferSize = bufferSize;
2023-09-02 17:56:46 +02:00
_logger = logger;
2023-09-07 09:21:00 +02:00
_header = this.GetMessageHeader();
2023-09-02 15:47:15 +02:00
2023-09-07 19:49:36 +02:00
switch (_header.message_type)
2023-09-02 15:47:15 +02:00
{
2023-09-08 20:41:11 +02:00
case (int)MESSAGE_TYPE.MESSAGE_TYPE_CLIENT_REPORT:
int reportId = GetPacketRequestId().RequestId;
2023-09-07 09:21:00 +02:00
this.HandleReportMessage(reportId);
2023-09-02 15:47:15 +02:00
break;
2023-09-08 20:41:11 +02:00
case (int)MESSAGE_TYPE.MESSAGE_TYPE_CLIENT_SEND:
int requestId = GetPacketRequestId().RequestId;
this.HandleClientSendMessage(requestId);
break;
2023-09-02 15:47:15 +02:00
default:
2023-09-02 17:56:46 +02:00
_logger.Information("This message type is not accepted at the moment.");
2023-09-02 15:47:15 +02:00
break;
}
}
2023-09-08 20:41:11 +02:00
2023-09-07 09:21:00 +02:00
private PACKET_HEADER GetMessageHeader()
2023-09-02 15:47:15 +02:00
{
2023-09-07 09:21:00 +02:00
return Helper.BytesToStructure<PACKET_HEADER>(ref _buffer, 0);
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
unsafe private PACKET_REQUEST_HEADER GetPacketRequestId()
{
return Helper.BytesToStructure<PACKET_REQUEST_HEADER>(ref _buffer, sizeof(PACKET_HEADER));
}
unsafe private CLIENT_SEND_PACKET_HEADER GetClientSendPacketHeader()
2023-09-02 15:47:15 +02:00
{
2023-09-08 20:41:11 +02:00
return Helper.BytesToStructure<CLIENT_SEND_PACKET_HEADER>(ref _buffer, sizeof(PACKET_HEADER));
2023-09-02 15:47:15 +02:00
}
2023-09-07 09:21:00 +02:00
unsafe private void HandleReportMessage(int reportId)
2023-09-02 15:47:15 +02:00
{
2023-09-02 17:56:46 +02:00
_logger.Information("Report id: {0}", reportId);
2023-09-07 19:49:36 +02:00
OPEN_HANDLE_FAILURE_REPORT openHandleFailure =
2023-09-08 20:41:11 +02:00
Helper.BytesToStructure<OPEN_HANDLE_FAILURE_REPORT>(ref _buffer, sizeof(PACKET_HEADER));
2023-09-02 17:56:46 +02:00
2023-09-08 09:42:35 +02:00
_logger.Information("Report code: {0}, ProcessID: {1:x}, ThreadId: {2:x}, DesiredAccess{3:x}",
2023-09-02 17:56:46 +02:00
openHandleFailure.ReportCode,
openHandleFailure.ProcessId,
openHandleFailure.ThreadId,
openHandleFailure.DesiredAccess);
2023-09-02 15:47:15 +02:00
}
2023-09-08 20:41:11 +02:00
private void HandleClientSendMessage(int clientSendId)
{
CLIENT_SEND_PACKET_HEADER header = GetClientSendPacketHeader();
_logger.Information("RequestId: {0}, PacketSize: {1}", header.RequestId, header.PacketSize);
switch (header.RequestId)
{
case (int)CLIENT_SEND_REQUEST_ID.SYSTEM_INFORMATION:
this.HandleClientSendHardwareInformation();
break;
}
}
unsafe private void HandleClientSendHardwareInformation()
{
_logger.Information("Handling client send hardware information");
string moboSerial = Helper.FixedUnsafeBufferToSafeString(
ref _buffer, _bufferSize, sizeof(PACKET_HEADER) + sizeof(CLIENT_SEND_PACKET_HEADER), 32);
string driveSerial = Helper.FixedUnsafeBufferToSafeString(
ref _buffer, _bufferSize, sizeof(PACKET_HEADER) + sizeof(CLIENT_SEND_PACKET_HEADER) + 32, 32);
_logger.Information("Mobo Serial: {0}, drive serial: {1}", moboSerial, driveSerial);
}
2023-09-02 15:47:15 +02:00
}
}
2023-09-08 20:41:11 +02:00