Skip to content

Commit

Permalink
Add DotNetty network implementation with SpanNetty
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaioru committed Aug 2, 2024
1 parent eae39ad commit c3a6c43
Show file tree
Hide file tree
Showing 29 changed files with 1,936 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Edelstein.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "protocol", "protocol", "{82
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Protocol.Network", "src\protocol\Edelstein.Protocol.Network\Edelstein.Protocol.Network.csproj", "{AF5AC908-62BB-48CE-99E8-83388738CCB1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Common.Network.DotNetty", "src\common\Edelstein.Common.Network.DotNetty\Edelstein.Common.Network.DotNetty.csproj", "{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Edelstein.Common.Crypto", "src\common\Edelstein.Common.Crypto\Edelstein.Common.Crypto.csproj", "{875EE192-E044-4C8D-941F-86C24C3FABF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -40,11 +44,21 @@ Global
{E50DFCDF-39D5-4D0D-A46E-94D11D795087} = {0C8602E7-AED6-43A4-BA0F-165EAB963D07}
{82D7864B-19AD-484C-BD2E-897F05B5852C} = {0C8602E7-AED6-43A4-BA0F-165EAB963D07}
{AF5AC908-62BB-48CE-99E8-83388738CCB1} = {82D7864B-19AD-484C-BD2E-897F05B5852C}
{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01} = {E50DFCDF-39D5-4D0D-A46E-94D11D795087}
{875EE192-E044-4C8D-941F-86C24C3FABF7} = {E50DFCDF-39D5-4D0D-A46E-94D11D795087}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AF5AC908-62BB-48CE-99E8-83388738CCB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF5AC908-62BB-48CE-99E8-83388738CCB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF5AC908-62BB-48CE-99E8-83388738CCB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF5AC908-62BB-48CE-99E8-83388738CCB1}.Release|Any CPU.Build.0 = Release|Any CPU
{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0ED46D09-2617-4FBE-BFB4-D8B49BCE5C01}.Release|Any CPU.Build.0 = Release|Any CPU
{875EE192-E044-4C8D-941F-86C24C3FABF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{875EE192-E044-4C8D-941F-86C24C3FABF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{875EE192-E044-4C8D-941F-86C24C3FABF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{875EE192-E044-4C8D-941F-86C24C3FABF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<Project>
<PropertyGroup>
<DotNettyVersion>0.7.6</DotNettyVersion>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="BinarySerializer" Version="8.6.4.1" />
<PackageVersion Include="CommunityToolkit.HighPerformance" Version="8.2.2" />
<PackageVersion Include="SpanNetty.Buffers" Version="$(DotNettyVersion)" />
<PackageVersion Include="SpanNetty.Codecs" Version="$(DotNettyVersion)" />
<PackageVersion Include="SpanNetty.Common" Version="$(DotNettyVersion)" />
<PackageVersion Include="SpanNetty.Handlers" Version="$(DotNettyVersion)" />
<PackageVersion Include="SpanNetty.Transport" Version="$(DotNettyVersion)" />
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
<PackageVersion Include="MinVer" Version="5.0.0" />
</ItemGroup>
Expand Down
69 changes: 69 additions & 0 deletions src/common/Edelstein.Common.Crypto/CipherAES.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Buffers;
using System.Security.Cryptography;

namespace Edelstein.Common.Crypto;

public class CipherAES
{
private readonly ICryptoTransform _transformer;

public CipherAES() : this(new byte[] { 0x13, 0x08, 0x06, 0xb4, 0x1b, 0x0f, 0x33, 0x52 })
{
}

public CipherAES(ReadOnlySpan<byte> userKey)
{
var expandedKey = new byte[userKey.Length * 4];
var cipher = Aes.Create();

for (var i = 0; i < userKey.Length; i++)
expandedKey[i * 4] = userKey[i];

cipher.KeySize = 256;
cipher.Key = expandedKey;
cipher.Mode = CipherMode.ECB;
_transformer = cipher.CreateEncryptor();
}

public void Transform(Span<byte> input, int remaining, uint src)
{
var length = 0x5B0;
var start = 0;

const int srcExpL = sizeof(int) * 4;
var srcExp = ArrayPool<byte>.Shared.Rent(srcExpL);
var srcBytes = BitConverter.GetBytes(src);

while (remaining > 0)
{
for (var i = 0; i < srcExpL; ++i)
srcExp[i] = srcBytes[i % 4];

if (remaining < length)
length = remaining;

for (var i = start; i < start + length; ++i)
{
var sub = i - start;

if (sub % srcExpL == 0)
_transformer.TransformBlock(
srcExp,
0,
srcExpL,
srcExp,
0
);

input[i] ^= srcExp[sub % srcExpL];
}

start += length;
remaining -= length;
length = 0x5B4;
}

ArrayPool<byte>.Shared.Return(srcExp);
}
}
63 changes: 63 additions & 0 deletions src/common/Edelstein.Common.Crypto/CipherIG.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace Edelstein.Common.Crypto;

public class CipherIG
{
private readonly byte[] _shuffle;
private readonly uint _key;

public CipherIG() : this(new byte[]
{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0x73, 0xE6, 0x0A, 0xDE, 0x2B, 0x99, 0x4A, 0x47, 0x9C, 0xDF,
0x09, 0x76, 0x9E, 0x30, 0x0E, 0xE4, 0xB2, 0x94, 0xA0, 0x3B, 0x34, 0x1D, 0x28, 0x0F, 0x36, 0xE3,
0x23, 0xB4, 0x03, 0xD8, 0x90, 0xC8, 0x3C, 0xFE, 0x5E, 0x32, 0x24, 0x50, 0x1F, 0x3A, 0x43, 0x8A,
0x96, 0x41, 0x74, 0xAC, 0x52, 0x33, 0xF0, 0xD9, 0x29, 0x80, 0xB1, 0x16, 0xD3, 0xAB, 0x91, 0xB9,
0x84, 0x7F, 0x61, 0x1E, 0xCF, 0xC5, 0xD1, 0x56, 0x3D, 0xCA, 0xF4, 0x05, 0xC6, 0xE5, 0x08, 0x49
})
{
}

public CipherIG(byte[] shuffle, uint key = 0xC65053F2)
{
_shuffle = shuffle;
_key = key;
}

public unsafe uint Hash(uint pSrc, int nLen, uint dwKey)
{
if (dwKey == 0) dwKey = _key;
if (nLen <= 0) return _key;

var ptrDwKey = &dwKey;

for (var i = 0; i < nLen; i++)
{
var ptrPSrc = &pSrc;

fixed (byte* pShuffle = _shuffle)
{
*((byte*)ptrDwKey + 0) +=
(byte)(*(pShuffle + *((byte*)ptrDwKey + 1)) - *((byte*)ptrPSrc + i));
*((byte*)ptrDwKey + 1) -=
(byte)(*((byte*)ptrDwKey + 2) ^ *(pShuffle + *((byte*)ptrPSrc + i)));
*((byte*)ptrDwKey + 2) ^=
(byte)(*((byte*)ptrPSrc + i) + *(pShuffle + *((byte*)ptrDwKey + 3)));
*((byte*)ptrDwKey + 3) =
(byte)(*((byte*)ptrDwKey + 3) - *(byte*)ptrDwKey + *(pShuffle + *((byte*)ptrPSrc + i)));
*ptrDwKey = *ptrDwKey << 3 | *ptrDwKey >> 29;
}
}

return dwKey;
}
}
89 changes: 89 additions & 0 deletions src/common/Edelstein.Common.Crypto/CipherShanda.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;

namespace Edelstein.Common.Crypto;

public static class CipherShanda
{
public static void EncryptTransform(Span<byte> input, int size)
{
for (var i = 0; i < 3; i++)
{
byte a = 0;
byte c;
for (var j = size; j > 0; j--)
{
c = input[size - j];
c = RollLeft(c, 3);
c = (byte)(c + j);
c ^= a;
a = c;
c = RollRight(a, j);
c ^= 0xFF;
c += 0x48;
input[size - j] = c;
}

a = 0;
for (var j = size; j > 0; j--)
{
c = input[j - 1];
c = RollLeft(c, 4);
c = (byte)(c + j);
c ^= a;
a = c;
c ^= 0x13;
c = RollRight(c, 3);
input[j - 1] = c;
}
}
}

public static void DecryptTransform(Span<byte> input, int size)
{
for (var i = 0; i < 3; i++)
{
byte a;
byte b = 0;
byte c;
for (var j = size; j > 0; j--)
{
c = input[j - 1];
c = RollLeft(c, 3);
c ^= 0x13;
a = c;
c ^= b;
c = (byte)(c - j);
c = RollRight(c, 4);
b = a;
input[j - 1] = c;
}

b = 0;
for (var j = size; j > 0; j--)
{
c = input[size - j];
c -= 0x48;
c ^= 0xFF;
c = RollLeft(c, j);
a = c;
c ^= b;
c = (byte)(c - j);
c = RollRight(c, 3);
b = a;
input[size - j] = c;
}
}
}

private static byte RollLeft(byte value, int shift)
{
var num = (uint)(value << shift % 8);
return (byte)(num & 0xff | num >> 8);
}

private static byte RollRight(byte value, int shift)
{
var num = (uint)(value << 8 >> shift % 8);
return (byte)(num & 0xff | num >> 8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
13 changes: 13 additions & 0 deletions src/common/Edelstein.Common.Crypto/packages.lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 2,
"dependencies": {
"net9.0": {
"MinVer": {
"type": "Direct",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "ybkgpQMtt0Fo91l5rYtE3TZtD+Nmy5Ko091xvfXXOosQdMi30XO2EZ2+ShZt89gdu7RMmJqZaJ+e1q6d+6+KNw=="
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using CommunityToolkit.HighPerformance.Buffers;
using DotNetty.Buffers;
using DotNetty.Codecs;
using DotNetty.Transport.Channels;
using Edelstein.Common.Crypto;
using Edelstein.Protocol.Network.Packets;
using Edelstein.Protocol.Network.Transports;

namespace Edelstein.Common.Network.DotNetty.Codecs;

public class NettyPacketDecoder(
TransportVersion transportVersion,
CipherAES cipherAES,
CipherIG cipherIG
) : ReplayingDecoder<NettyPacketState>(NettyPacketState.DecodingHeader)
{
private short _length;
private short _sequence;

protected override void Decode(
IChannelHandlerContext context,
IByteBuffer input,
List<object> output
)
{
var socket = context.Channel.GetAttribute(NettyAttributes.SocketKey).Get();

switch (State)
{
case NettyPacketState.DecodingHeader:
if (socket != null)
{
if (input.ReadableBytes < 4)
{
RequestReplay();
return;
}

var sequence = input.ReadShortLE();
var length = input.ReadShortLE();

if (socket.IsDataEncrypted) length ^= sequence;

_sequence = sequence;
_length = length;
}
else
{
if (input.ReadableBytes < 2)
{
RequestReplay();
return;
}

_length = input.ReadShortLE();
}

Checkpoint(NettyPacketState.DecodingPayload);
return;
case NettyPacketState.DecodingPayload:
if (input.ReadableBytes < _length)
{
RequestReplay();
return;
}

var owner = MemoryOwner<byte>.Allocate(_length);
var buffer = owner.Span;

input.ReadBytes(buffer);
Checkpoint(NettyPacketState.DecodingHeader);

if (_length < 0x2) return;

if (socket != null)
{
var seqRecv = socket.SeqRecv;
var version = (short)(seqRecv >> 16) ^ _sequence;

if (!(version == -(transportVersion.Major + 1) ||
version == transportVersion.Major)) return;

if (socket.IsDataEncrypted)
{
cipherAES.Transform(buffer, _length, seqRecv);
CipherShanda.DecryptTransform(buffer, _length);
}

socket.SeqRecv = cipherIG.Hash(seqRecv, 4, 0);
}

output.Add(new RawPacket(owner));
return;
default:
throw new ArgumentOutOfRangeException(nameof(State));
}
}
}
Loading

0 comments on commit c3a6c43

Please sign in to comment.