This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Protocol.cs
463 lines (343 loc) · 16.3 KB
/
Protocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Aragas.Core.Data;
using Aragas.Core.IO;
using Aragas.Core.Packets;
using Aragas.Core.Wrappers;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using MineLib.Core;
using MineLib.Core.Events;
using MineLib.Core.Exceptions;
using MineLib.Core.IO;
using MineLib.PacketBuilder.Client.Login;
using MineLib.PacketBuilder.Client.Play;
using MineLib.PacketBuilder.Server.Handshaking;
using MineLib.PacketBuilder.Server.Login;
using Newtonsoft.Json;
using PCLStorage;
using ProtocolModern.Enum;
using ProtocolModern.IO;
using ProtocolModern.Packets;
namespace ProtocolModern
{
public sealed partial class Protocol : MineLib.Core.Protocols.Protocol
{
static Protocol()
{
Extensions.PacketExtensions.Init();
}
#region Properties
public override string Name => "Modern";
public override string Version => "1.8.7";
public override ConnectionState State { get; protected set; }
public override string Host => Stream?.Host;
public override ushort Port => Stream?.Port ?? 0;
public override bool Connected => Stream != null && Stream.Connected;
public override bool UseLogin => Minecraft.UseLogin;
private bool IsForge { get; set; }
#endregion Properties
#region Debugging
public override bool SavePackets { get; }
private bool SavePacketsToDisk { get; set; }
private bool SaveEntityPacketsToDisk { get; set; }
private bool SaveRawMapPacketsToDisk { get; set; }
protected override List<ProtobufPacket> PacketsReceived { get; }
protected override List<ProtobufPacket> PacketsSended { get; }
private List<ProtobufPacket> PluginMessage { get; }
protected override List<ProtobufPacket> LastPackets => PacketsReceived?.GetRange(PacketsReceived.Count - 50, 50);
protected override ProtobufPacket LastProtobufPacket => PacketsReceived[PacketsReceived.Count - 1];
private IFolder PacketDumpFolder { get; set; }
private IFolder PacketDumpRawFolder { get; set; }
#endregion Debugging
private IThread ReadThread { get; set; }
private CancellationTokenSource CancellationToken { get; set; }
private MineLibClient Minecraft { get; set; }
private CompressionProtobufStream Stream { get; set; }
private bool CompressionEnabled => Stream != null && Stream.CompressionEnabled;
private long CompressionThreshold => Stream?.CompressionThreshold ?? -1;
public Protocol(MineLibClient client, bool debugPackets = false) : base(client, debugPackets)
{
Minecraft = client;
Stream = new CompressionProtobufStream(TCPClientWrapper.CreateTCPClient());
SavePackets = debugPackets;
SavePacketsToDisk = true;
SaveEntityPacketsToDisk = false;
SaveRawMapPacketsToDisk = false;
PacketsReceived = new List<ProtobufPacket>();
PacketsSended = new List<ProtobufPacket>();
PluginMessage = new List<ProtobufPacket>();
SendingHandlers = new Dictionary<Type, List<Action<SendingEventArgs>>>();
CustomPacketHandlers = new Dictionary<Type, List<Func<ProtobufPacket, Task>>>();
RegisterSupportedSendings();
CancellationToken = new CancellationTokenSource();
var time = DateTime.Now;
PacketDumpFolder = FileSystemWrapper.LogFolder.CreateFolderAsync($"{time:yyyy-MM-dd_hh.mm.ss}", CreationCollisionOption.GenerateUniqueName).Result;
PacketDumpRawFolder = FileSystemWrapper.LogFolder.CreateFolderAsync($"{time:yyyy-MM-dd_hh.mm.ss}-Raw", CreationCollisionOption.GenerateUniqueName).Result;
}
private void ReadCycle()
{
while (!CancellationToken.IsCancellationRequested)
PacketReceiver();
}
private void PacketReceiver()
{
int packetId;
byte[] data;
#region No Compression
if (!CompressionEnabled)
{
var packetLength = Stream.ReadVarInt();
if (packetLength == 0)
throw new ProtocolException("Reading error: Packet Length size is 0");
packetId = Stream.ReadVarInt();
data = Stream.ReadByteArray(packetLength - 1);
}
#endregion
#region Compression
else // (CompressionEnabled)
{
// REWRITE: Blocking thread
var packetLength = Stream.ReadVarInt();
if (packetLength == 0)
{
if (CancellationToken.Token.IsCancellationRequested)
return;
else
throw new ProtocolException("Reading error: Packet Length size is 0");
}
var dataLength = Stream.ReadVarInt();
if (dataLength == 0)
{
if (packetLength > CompressionThreshold)
{
if (CancellationToken.Token.IsCancellationRequested)
return;
else
throw new ProtocolException("Reading error: Received uncompressed message of size " + packetLength + " greater than threshold " + CompressionThreshold);
}
packetId = Stream.ReadVarInt();
//data = _stream.ReadByteArray(packetLength - ModernStream.GetVarIntBytes(packetId).Length);
data = Stream.ReadByteArray(packetLength - 2);
}
else // (dataLength > 0)
{
var dataLengthBytes = new VarInt(dataLength).InByteArray().Length;
var tempBuff = Stream.ReadByteArray(packetLength - dataLengthBytes); // -- Compressed
using (var outputStream = new MemoryStream())
using (var inputStream = new InflaterInputStream(new MemoryStream(tempBuff)))
//using (var reader = new ModernDataReader(new MemoryStream(tempBuff))) // -- For VarInt
{
inputStream.CopyTo(outputStream);
tempBuff = outputStream.ToArray(); // -- Decompressed
packetId = tempBuff[0]; // -- Only 255 packets available. ReadVarInt doesn't work.
var packetIdBytes = new VarInt(packetId).InByteArray().Length;
data = new byte[tempBuff.Length - packetIdBytes];
Buffer.BlockCopy(tempBuff, packetIdBytes, data, 0, data.Length);
}
}
}
#endregion
HandlePacket(packetId, data);
}
/// <summary>
/// Packets are handled here. Compression and encryption are handled here too
/// </summary>
/// <param name="id">Packet ID</param>
/// <param name="data">Packet byte[] data</param>
private void HandlePacket(int id, byte[] data)
{
using (var reader = new ProtobufDataReader(data))
{
var packet = default(ProtobufPacket);
switch (State)
{
#region Status
case ConnectionState.InfoRequest:
if (ClientResponse.StatusPacketResponses.Packets[id] == null)
throw new ProtocolException("Reading error: Wrong Status packet ID.");
packet = ClientResponse.StatusPacketResponses.Packets[id]().ReadPacket(reader);
OnPacketHandled(id, packet, ConnectionState.InfoRequest);
break;
#endregion Status
#region Login
case ConnectionState.Joining:
if (ClientResponse.LoginPacketResponses.Packets[id] == null)
throw new ProtocolException("Reading error: Wrong Login packet ID.");
packet = ClientResponse.LoginPacketResponses.Packets[id]().ReadPacket(reader);
OnPacketHandled(id, packet, ConnectionState.Joining);
break;
#endregion Login
#region Play
case ConnectionState.Joined:
if (ClientResponse.PlayPacketResponses.Packets[id] == null)
{
break;
throw new ProtocolException("Reading error: Wrong Play packet ID.");
}
packet = ClientResponse.PlayPacketResponses.Packets[id]().ReadPacket(reader);
OnPacketHandled(id, packet, ConnectionState.Joined);
break;
#endregion Play
}
if(packet == null)
return;
if(packet is PluginMessagePacket)
PluginMessage.Add(packet);
if (SavePackets)
PacketsReceived.Add(packet);
if (SavePacketsToDisk)
DumpPacketReceived(packet, data);
}
}
private int _rCount;
private readonly Dictionary<short, int> _rCounters = new Dictionary<short, int>();
private void DumpPacketReceived(ProtobufPacket packet, byte[] data)
{
if(!_rCounters.ContainsKey((short) packet.ID))
_rCounters.Add((short) packet.ID, 0);
var mainData = $"R{_rCount}{packet.GetType().Name.Replace("Packet", "")}{_rCounters[(short) packet.ID]}";
if (!SaveEntityPacketsToDisk)
if(packet is EntityHeadLookPacket || packet is EntityLookPacket || packet is EntityRelativeMovePacket || packet is EntityLookAndRelativeMovePacket ||
packet is EntityVelocityPacket || packet is EntityTeleportPacket || packet is SpawnMobPacket || packet is DestroyEntitiesPacket ||
packet is EntityStatusPacket || packet is EntityMetadataPacket || packet is EntityPropertiesPacket || packet is EntityEquipmentPacket || packet is AttachEntityPacket)
{ _rCount++; return; }
var name = $"{mainData}.json";
using (var stream = PacketDumpFolder.CreateFileAsync(name, CreationCollisionOption.OpenIfExists).Result.OpenAsync(FileAccess.ReadAndWrite).Result)
using (var writer = new StreamWriter(stream))
writer.Write(JsonConvert.SerializeObject(packet, Formatting.Indented, new VarIntJsonConverter()));
if (!SaveRawMapPacketsToDisk)
if (packet is MapChunkBulkPacket || packet is ChunkDataPacket)
{ _rCount++; return; }
var rawName = $"{mainData}.bin";
using (var stream = PacketDumpRawFolder.CreateFileAsync(rawName, CreationCollisionOption.OpenIfExists).Result.OpenAsync(FileAccess.ReadAndWrite).Result)
using (var writer = new BinaryWriter(stream))
writer.Write(data);
_rCounters[(short) packet.ID]++;
_rCount++;
}
private int _sCount;
private readonly Dictionary<short, int> _sCounters = new Dictionary<short, int>();
private void DumpPacketSended(ProtobufPacket packet)
{
if (!_sCounters.ContainsKey((short) packet.ID))
_sCounters.Add((short) packet.ID, 0);
var name = $"S{_sCount}{packet.GetType().Name.Replace("Packet", "")}{_sCounters[(short) packet.ID]}.json";
using (var stream = PacketDumpFolder.CreateFileAsync(name, CreationCollisionOption.OpenIfExists).Result.OpenAsync(FileAccess.ReadAndWrite).Result)
using (var writer = new StreamWriter(stream))
writer.Write(JsonConvert.SerializeObject(packet, Formatting.Indented, new VarIntJsonConverter()));
_sCounters[(short) packet.ID]++;
_sCount++;
}
/// <summary>
/// Enabling encryption
/// </summary>
/// <param name="packet">EncryptionRequestPacket</param>
private void ModernEnableEncryption(ProtobufPacket packet)
{
var request = (EncryptionRequestPacket) packet;
var sharedKey = PKCS1Signature.CreateSecretKey();
var hash = GetServerIDHash(request.PublicKey, sharedKey, request.ServerID);
if (!Yggdrasil.JoinSession(AccessToken, SelectedProfile, hash).Result)
throw new ProtocolException("Yggdrasil error: Not authenticated.");
var pkcs = new PKCS1Signature(request.PublicKey);
SendPacket(new EncryptionResponsePacket
{
SharedSecret = pkcs.SignData(sharedKey),
VerifyToken = pkcs.SignData(request.VerifyToken)
});
Stream.InitializeEncryption(sharedKey);
}
private static string GetServerIDHash(byte[] publicKey, byte[] secretKey, string serverID)
{
var hashlist = new List<byte>();
hashlist.AddRange(Encoding.UTF8.GetBytes(serverID));
hashlist.AddRange(secretKey);
hashlist.AddRange(publicKey);
return JavaHelper.JavaHexDigest(hashlist.ToArray());
}
/// <summary>
/// Setting compression
/// </summary>
/// <param name="packet">EncryptionRequestPacket</param>
private void ModernSetCompression(ProtobufPacket packet)
{
if (packet is SetCompressionPacket)
{
var request = (SetCompressionPacket) packet;
Stream.SetCompression(request.Threshold);
}
if (packet is SetCompression2Packet)
{
var request = (SetCompression2Packet)packet;
Stream.SetCompression(request.Threshold);
}
}
#region Network
public override void Connect(string host, ushort port)
{
if (Connected)
throw new ProtocolException("Connection error: Already connected to server.");
// -- Connect to server.
Stream.Connect(host, port);
// -- Begin data reading.
if (ReadThread != null && ReadThread.IsRunning)
throw new ProtocolException("Connection error: Thread already running.");
else
{
ReadThread = ThreadWrapper.CreateThread(ReadCycle);
ReadThread.IsBackground = true;
ReadThread.Name = "PacketReaderThread";
ReadThread.Start();
}
}
public override void Disconnect()
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
CancellationToken.Cancel();
Stream.Disconnect();
}
private void SendPacket(ProtobufPacket packet)
{
if (!Connected)
throw new ProtocolException("Connection error: Not connected to server.");
HandleState(packet);
Stream.SendPacket(ref packet);
if (SavePackets)
PacketsSended.Add(packet);
if (SavePacketsToDisk)
DumpPacketSended(packet);
}
private void HandleState(ProtobufPacket packet)
{
var handshake = packet as HandshakePacket;
if (handshake != null)
{
switch ((NextState)(int)handshake.NextState)
{
case NextState.Status:
State = ConnectionState.InfoRequest;
break;
case NextState.Login:
State = ConnectionState.Joining;
break;
}
}
}
#endregion
public override void Dispose()
{
CancellationToken?.Cancel();
_rCounters?.Clear();
_sCounters?.Clear();
Stream?.Dispose();
PacketsReceived?.Clear();
PacketsSended?.Clear();
//CancellationToken?.Dispose();
}
}
}