2023-11-21 20:37:50 +00:00
|
|
|
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
|
2023-11-20 18:58:30 +00:00
|
|
|
|
2023-08-12 21:02:59 +00:00
|
|
|
using System.Security.Cryptography;
|
2023-08-10 06:45:51 +00:00
|
|
|
using System.Text;
|
|
|
|
using MassTransit.Mediator;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NetCoreServer;
|
|
|
|
|
2023-11-19 16:07:28 +00:00
|
|
|
namespace Server;
|
|
|
|
|
2023-08-10 06:45:51 +00:00
|
|
|
public class ChannelSession : TcpSession
|
|
|
|
{
|
2023-10-27 17:47:17 +00:00
|
|
|
private static readonly byte[] _key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 }
|
2023-08-10 06:45:51 +00:00
|
|
|
.Reverse().ToArray();
|
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
private static readonly byte[] _iv = new byte[]
|
2023-08-11 09:31:30 +00:00
|
|
|
{
|
|
|
|
0xfe, 220, 0xba, 0x98, 0x76, 0x54, 50, 0x10, 15, 30, 0x2d, 60, 0x4b, 90, 0x69, 120
|
|
|
|
}.Reverse().ToArray();
|
2023-08-10 06:45:51 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly ICryptoTransform _decryptor;
|
2023-08-12 21:02:59 +00:00
|
|
|
|
2023-10-27 17:47:17 +00:00
|
|
|
private readonly ICryptoTransform _encryptor;
|
|
|
|
private readonly ILogger<ChannelSession> _logger;
|
|
|
|
private readonly IMediator _mediator;
|
2023-08-10 06:45:51 +00:00
|
|
|
|
|
|
|
public ChannelSession(TcpServer server, IMediator mediator, ILogger<ChannelSession> logger) : base(server)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_mediator = mediator;
|
|
|
|
_logger = logger;
|
2023-08-10 06:45:51 +00:00
|
|
|
var aes = Aes.Create();
|
2023-10-27 17:47:17 +00:00
|
|
|
aes.Key = _key;
|
|
|
|
aes.IV = _iv;
|
2023-08-10 06:45:51 +00:00
|
|
|
aes.Padding = PaddingMode.None;
|
2023-10-12 07:15:34 +00:00
|
|
|
#pragma warning disable SEC0026
|
2023-08-10 06:45:51 +00:00
|
|
|
aes.Mode = CipherMode.ECB;
|
2023-10-12 07:15:34 +00:00
|
|
|
#pragma warning restore SEC0026
|
2023-08-10 06:45:51 +00:00
|
|
|
|
2023-11-19 16:07:28 +00:00
|
|
|
_decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
|
|
|
|
_encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
|
2023-08-10 06:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnReceived(byte[] buffer, long offset, long size)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-08-12 21:02:59 +00:00
|
|
|
using (var ms = new MemoryStream(Decrypt(buffer)))
|
2023-11-19 16:07:28 +00:00
|
|
|
using (var cs = new CryptoStream(ms, _decryptor, CryptoStreamMode.Read))
|
2023-08-10 06:45:51 +00:00
|
|
|
{
|
2023-10-27 15:40:58 +00:00
|
|
|
var amountOfReadBytes = cs.Read(buffer);
|
|
|
|
if (amountOfReadBytes != buffer.Length)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogError("Amount of read bytes is not equal to buffer length.");
|
2023-10-27 15:40:58 +00:00
|
|
|
}
|
2023-08-10 06:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base.OnReceived(buffer, offset, size);
|
|
|
|
}
|
|
|
|
catch (CryptographicException ex)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
_logger.LogError("An error has occured while decrypting: {ErrorMessage}", ex.Message);
|
|
|
|
_logger.LogError("Default buffer message: {Message}", Encoding.ASCII.GetString(buffer));
|
2023-08-10 06:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 21:02:59 +00:00
|
|
|
private static byte[] Decrypt(byte[] buffer)
|
2023-08-10 06:45:51 +00:00
|
|
|
{
|
2023-08-11 09:31:30 +00:00
|
|
|
for (var i = 0; i < buffer.Length; ++i)
|
2023-08-10 06:45:51 +00:00
|
|
|
{
|
|
|
|
buffer[i] = (byte)(buffer[i] ^ i ^ (3 * (0xFE - i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2023-08-11 09:31:30 +00:00
|
|
|
}
|