mirror-ac/server/Message.cs

120 lines
3.5 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;
using service;
2023-09-07 19:49:36 +02:00
using System.Net;
using System.Net.Sockets;
using server.Types.Reports;
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
{
MESSAGE_TYPE_REPORT = 1,
MESSAGE_TYPE_SEND = 2,
MESSAGE_TYPE_RECEIVE = 3
}
2023-09-07 19:49:36 +02:00
[StructLayout(LayoutKind.Sequential)]
public unsafe 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-07 20:47:48 +02:00
public fixed char motherboard_serial_number[32];
public fixed char device_drive_0_serial[32];
2023-09-07 09:21:00 +02:00
};
2023-09-02 15:47:15 +02:00
struct REPORT_PACKET_HEADER
{
2023-09-07 09:21:00 +02:00
public int reportId;
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 20:47:48 +02:00
char[] string_1 = new char[32];
char[] string_2 = new char[32];
2023-09-07 19:49:36 +02:00
unsafe
{
2023-09-07 20:47:48 +02:00
for (int i = 0; i < 32; i++)
2023-09-07 19:49:36 +02:00
{
string_1[i] = _header.motherboard_serial_number[i];
}
2023-09-07 20:47:48 +02:00
for (int i=0;i<32;i++)
2023-09-07 19:49:36 +02:00
{
string_2[i] = _header.device_drive_0_serial[i];
}
}
2023-09-07 20:47:48 +02:00
string test1 = new string(string_1);
string test2 = new string(string_2);
_logger.Information("SteamID: {0}, MoboSerial: {2}, DriveSerial: {3}, Message type: {1}",
2023-09-07 09:21:00 +02:00
_header.steam64_id,
2023-09-07 19:49:36 +02:00
_header.message_type,
2023-09-07 20:47:48 +02:00
test1,
test2
2023-09-07 09:21:00 +02:00
);
2023-09-02 17:56:46 +02:00
2023-09-07 19:49:36 +02:00
switch (_header.message_type)
2023-09-02 15:47:15 +02:00
{
case (int)MESSAGE_TYPE.MESSAGE_TYPE_REPORT:
2023-09-07 09:21:00 +02:00
int reportId = GetReportType().reportId;
this.HandleReportMessage(reportId);
2023-09-02 15:47:15 +02:00
break;
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-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-07 09:21:00 +02:00
unsafe private REPORT_PACKET_HEADER GetReportType()
2023-09-02 15:47:15 +02:00
{
2023-09-07 20:47:48 +02:00
return Helper.BytesToStructure<REPORT_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 =
Helper.BytesToStructure<Types.Reports.OPEN_HANDLE_FAILURE_REPORT>(ref _buffer, sizeof(PACKET_HEADER));
2023-09-02 17:56:46 +02:00
2023-09-02 18:55:40 +02:00
_logger.Information("Report code: {0}, Process Name: {4} 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
}
}
}