Handle forge handshake up to mod list sending.

This commit is contained in:
Pokechu22 2015-10-24 13:31:43 -07:00
parent 7cc87d8e71
commit b154639a6b
4 changed files with 234 additions and 83 deletions

View file

@ -114,6 +114,8 @@
<Compile Include="Crypto\Streams\RegularAesStream.cs" /> <Compile Include="Crypto\Streams\RegularAesStream.cs" />
<Compile Include="Crypto\CryptoHandler.cs" /> <Compile Include="Crypto\CryptoHandler.cs" />
<Compile Include="CSharpRunner.cs" /> <Compile Include="CSharpRunner.cs" />
<Compile Include="Protocol\Handlers\Forge\FMLHandshakeClientState.cs" />
<Compile Include="Protocol\Handlers\Forge\FMLHandshakeDiscriminator.cs" />
<Compile Include="Protocol\Handlers\Forge\ForgeInfo.cs" /> <Compile Include="Protocol\Handlers\Forge\ForgeInfo.cs" />
<Compile Include="Protocol\Handlers\Compression\CRC32.cs" /> <Compile Include="Protocol\Handlers\Compression\CRC32.cs" />
<Compile Include="Protocol\Handlers\Compression\Deflate.cs" /> <Compile Include="Protocol\Handlers\Compression\Deflate.cs" />

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol.Handlers.Forge
{
/// <summary>
/// Copy of the forge enum for client states.
/// https://github.com/MinecraftForge/MinecraftForge/blob/ebe9b6d4cbc4a5281c386994f1fbda04df5d2e1f/src/main/java/net/minecraftforge/fml/common/network/handshake/FMLHandshakeClientState.java
/// </summary>
enum FMLHandshakeClientState : byte
{
START,
HELLO,
WAITINGSERVERDATA,
WAITINGSERVERCOMPLETE,
PENDINGCOMPLETE,
COMPLETE,
DONE,
ERROR
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Protocol.Handlers.Forge
{
/// <summary>
/// Different "discriminator byte" values for the forge handshake.
/// https://github.com/MinecraftForge/MinecraftForge/blob/ebe9b6d4cbc4a5281c386994f1fbda04df5d2e1f/src/main/java/net/minecraftforge/fml/common/network/handshake/FMLHandshakeCodec.java
/// </summary>
enum FMLHandshakeDiscriminator : byte
{
ServerHello = 0,
ClientHello = 1,
ModList = 2,
RegistryData = 3,
HandshakeAck = 255, //-1
HandshakeReset = 254, //-2
}
}

View file

@ -18,7 +18,7 @@ namespace MinecraftClient.Protocol.Handlers
class Protocol18Handler : IMinecraftCom class Protocol18Handler : IMinecraftCom
{ {
private const int MC18Version = 47; private const int MC18Version = 47;
private int compression_treshold = 0; private int compression_treshold = 0;
private bool autocomplete_received = false; private bool autocomplete_received = false;
private string autocomplete_result = ""; private string autocomplete_result = "";
@ -28,6 +28,7 @@ namespace MinecraftClient.Protocol.Handlers
// Server forge info -- may be null. // Server forge info -- may be null.
private ForgeInfo forgeInfo; private ForgeInfo forgeInfo;
private FMLHandshakeClientState fmlHandshakeState = FMLHandshakeClientState.START;
IMinecraftComHandler handler; IMinecraftComHandler handler;
Thread netRead; Thread netRead;
@ -138,94 +139,153 @@ namespace MinecraftClient.Protocol.Handlers
return false; //Ignored packet return false; //Ignored packet
} }
} }
else //Regular in-game packets // Regular in-game packets
if (forgeInfo != null && fmlHandshakeState != FMLHandshakeClientState.DONE) //Check forge login
{ {
switch (packetID) switch (fmlHandshakeState)
{ {
case 0x00: //Keep-Alive case FMLHandshakeClientState.START:
SendPacket(0x00, packetData); if (packetID != 0x3F)
break; break;
case 0x01: //Join game
handler.OnGameJoined(); String channel = readNextString(ref packetData);
break;
case 0x02: //Chat message if (channel != "FML|HS")
string message = readNextString(ref packetData); break;
try
FMLHandshakeDiscriminator discriminator = (FMLHandshakeDiscriminator)readNextByte(ref packetData);
if (discriminator != FMLHandshakeDiscriminator.ServerHello)
return false;
// Send the plugin channel registration.
// REGISTER is somewhat special in that it doesn't actually include length information,
// and is also \0-separated.
// Also, yes, "FML" is there twice. Don't ask me why, but that's the way forge does it.
string[] channels = { "FML|HS", "FML", "FML|MP", "FML", "FORGE" };
SendPluginChannelPacket("REGISTER", Encoding.UTF8.GetBytes(string.Join("\0", channels)));
byte fmlProtocolVersion = readNextByte(ref packetData);
// There's another value afterwards for the dimension, but we don't need it.
ConsoleIO.WriteLineFormatted("§8Forge protocol version : " + fmlProtocolVersion);
// Tell the server we're running the same version.
SendForgeHandshakePacket(FMLHandshakeDiscriminator.ClientHello, new byte[] { fmlProtocolVersion });
// Then tell the server that we're running the same mods.
ConsoleIO.WriteLineFormatted("§8Sending falsified mod list to server...");
byte[][] mods = new byte[forgeInfo.Mods.Count][];
for (int i = 0; i < forgeInfo.Mods.Count; i++)
{ {
//Hide system messages or xp bar messages? ForgeInfo.ForgeMod mod = forgeInfo.Mods[i];
byte messageType = readData(1, ref packetData)[0]; mods[i] = concatBytes(getString(mod.ModID), getString(mod.Version));
if ((messageType == 1 && !Settings.DisplaySystemMessages)
|| (messageType == 2 && !Settings.DisplayXPBarMessages))
break;
} }
catch (IndexOutOfRangeException) { /* No message type */ } SendForgeHandshakePacket(FMLHandshakeDiscriminator.ModList, concatBytes(getVarInt(forgeInfo.Mods.Count), concatBytes(mods)));
handler.OnTextReceived(ChatParser.ParseText(message));
break; fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERDATA;
case 0x38: //Player List update
if (protocolversion >= MC18Version) return true;
}
}
switch (packetID)
{
case 0x00: //Keep-Alive
SendPacket(0x00, packetData);
break;
case 0x01: //Join game
handler.OnGameJoined();
break;
case 0x02: //Chat message
string message = readNextString(ref packetData);
try
{
//Hide system messages or xp bar messages?
byte messageType = readData(1, ref packetData)[0];
if ((messageType == 1 && !Settings.DisplaySystemMessages)
|| (messageType == 2 && !Settings.DisplayXPBarMessages))
break;
}
catch (IndexOutOfRangeException) { /* No message type */ }
handler.OnTextReceived(ChatParser.ParseText(message));
break;
case 0x38: //Player List update
if (protocolversion >= MC18Version)
{
int action = readNextVarInt(ref packetData);
int numActions = readNextVarInt(ref packetData);
for (int i = 0; i < numActions; i++)
{ {
int action = readNextVarInt(ref packetData); Guid uuid = readNextUUID(ref packetData);
int numActions = readNextVarInt(ref packetData); switch (action)
for (int i = 0; i < numActions; i++)
{ {
Guid uuid = readNextUUID(ref packetData); case 0x00: //Player Join
switch (action) string name = readNextString(ref packetData);
{ handler.OnPlayerJoin(uuid, name);
case 0x00: //Player Join break;
string name = readNextString(ref packetData); case 0x04: //Player Leave
handler.OnPlayerJoin(uuid, name); handler.OnPlayerLeave(uuid);
break; break;
case 0x04: //Player Leave default:
handler.OnPlayerLeave(uuid); //Unknown player list item type
break; break;
default:
//Unknown player list item type
break;
}
} }
} }
else //MC 1.7.X does not provide UUID in tab-list updates }
else //MC 1.7.X does not provide UUID in tab-list updates
{
string name = readNextString(ref packetData);
bool online = readNextBool(ref packetData);
short ping = readNextShort(ref packetData);
Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
if (online)
handler.OnPlayerJoin(FakeUUID, name);
else handler.OnPlayerLeave(FakeUUID);
}
break;
case 0x3A: //Tab-Complete Result
int autocomplete_count = readNextVarInt(ref packetData);
string tab_list = "";
for (int i = 0; i < autocomplete_count; i++)
{
autocomplete_result = readNextString(ref packetData);
if (autocomplete_result != "")
tab_list = tab_list + autocomplete_result + " ";
}
autocomplete_received = true;
tab_list = tab_list.Trim();
if (tab_list.Length > 0)
ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
break;
case 0x3F: //Plugin message.
String channel = readNextString(ref packetData);
if (channel == "FML|HS")
{
FMLHandshakeDiscriminator discriminator = (FMLHandshakeDiscriminator)readNextByte(ref packetData);
if (discriminator == FMLHandshakeDiscriminator.HandshakeReset)
{ {
string name = readNextString(ref packetData);
bool online = readNextBool(ref packetData);
short ping = readNextShort(ref packetData);
Guid FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
if (online)
handler.OnPlayerJoin(FakeUUID, name);
else handler.OnPlayerLeave(FakeUUID);
} }
break; return true;
case 0x3A: //Tab-Complete Result }
int autocomplete_count = readNextVarInt(ref packetData); break;
string tab_list = ""; case 0x40: //Kick Packet
for (int i = 0; i < autocomplete_count; i++) handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
{ return false;
autocomplete_result = readNextString(ref packetData); case 0x46: //Network Compression Treshold Info
if (autocomplete_result != "") if (protocolversion >= MC18Version)
tab_list = tab_list + autocomplete_result + " "; compression_treshold = readNextVarInt(ref packetData);
} break;
autocomplete_received = true; case 0x48: //Resource Pack Send
tab_list = tab_list.Trim(); string url = readNextString(ref packetData);
if (tab_list.Length > 0) string hash = readNextString(ref packetData);
ConsoleIO.WriteLineFormatted("§8" + tab_list, false); //Send back "accepted" and "successfully loaded" responses for plugins making use of resource pack mandatory
break; SendPacket(0x19, concatBytes(getVarInt(hash.Length), Encoding.UTF8.GetBytes(hash), getVarInt(3)));
case 0x40: //Kick Packet SendPacket(0x19, concatBytes(getVarInt(hash.Length), Encoding.UTF8.GetBytes(hash), getVarInt(0)));
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData))); break;
return false; default:
case 0x46: //Network Compression Treshold Info return false; //Ignored packet
if (protocolversion >= MC18Version)
compression_treshold = readNextVarInt(ref packetData);
break;
case 0x48: //Resource Pack Send
string url = readNextString(ref packetData);
string hash = readNextString(ref packetData);
//Send back "accepted" and "successfully loaded" responses for plugins making use of resource pack mandatory
SendPacket(0x19, concatBytes(getVarInt(hash.Length), Encoding.UTF8.GetBytes(hash), getVarInt(3)));
SendPacket(0x19, concatBytes(getVarInt(hash.Length), Encoding.UTF8.GetBytes(hash), getVarInt(0)));
break;
default:
return false; //Ignored packet
}
} }
return true; //Packet processed return true; //Packet processed
} }
@ -263,7 +323,7 @@ namespace MinecraftClient.Protocol.Handlers
/// </summary> /// </summary>
/// <param name="offset">Amount of bytes to read</param> /// <param name="offset">Amount of bytes to read</param>
/// <returns>The data read from the network as an array</returns> /// <returns>The data read from the network as an array</returns>
private byte[] readDataRAW(int offset) private byte[] readDataRAW(int offset)
{ {
if (offset > 0) if (offset > 0)
@ -278,7 +338,7 @@ namespace MinecraftClient.Protocol.Handlers
} }
return new byte[] { }; return new byte[] { };
} }
/// <summary> /// <summary>
/// Read some data from a cache of bytes and remove it from the cache /// Read some data from a cache of bytes and remove it from the cache
/// </summary> /// </summary>
@ -377,7 +437,7 @@ namespace MinecraftClient.Protocol.Handlers
} }
return i; return i;
} }
/// <summary> /// <summary>
/// Read an integer from a cache of bytes and remove it from the cache /// Read an integer from a cache of bytes and remove it from the cache
/// </summary> /// </summary>
@ -401,6 +461,16 @@ namespace MinecraftClient.Protocol.Handlers
return i; return i;
} }
/// <summary>
/// Read a single byte from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The byte that was read</returns>
private static byte readNextByte(ref byte[] cache)
{
return readData(1, ref cache)[0];
}
/// <summary> /// <summary>
/// Build an integer for sending over the network /// Build an integer for sending over the network
/// </summary> /// </summary>
@ -436,6 +506,19 @@ namespace MinecraftClient.Protocol.Handlers
else return concatBytes(getVarInt(array.Length), array); else return concatBytes(getVarInt(array.Length), array);
} }
/// <summary>
/// Get a byte array from the given string for sending over the network, with length information prepended.
/// </summary>
/// <param name="array">String to process</param>
/// <returns>Array ready to send</returns>
private byte[] getString(string text)
{
byte[] bytes = Encoding.UTF8.GetBytes(text);
return concatBytes(getVarInt(bytes.Length), bytes);
}
/// <summary> /// <summary>
/// Easily append several byte arrays /// Easily append several byte arrays
/// </summary> /// </summary>
@ -478,6 +561,28 @@ namespace MinecraftClient.Protocol.Handlers
} }
} }
/// <summary>
/// Send a forge plugin channel packet ("FML|HS"). Compression and encryption will be handled automatically
/// </summary>
/// <param name="discriminator">Discriminator to use.</param>
/// <param name="data">packet Data</param>
private void SendForgeHandshakePacket(FMLHandshakeDiscriminator discriminator, byte[] data)
{
SendPluginChannelPacket("FML|HS", concatBytes(new byte[] { (byte)discriminator }, data));
}
/// <summary>
/// Send a plugin channel packet (0x3F) to the server, compression and encryption will be handled automatically
/// </summary>
/// <param name="channel">Channel to send packet on</param>
/// <param name="data">packet Data</param>
private void SendPluginChannelPacket(string channel, byte[] data)
{
SendPacket(0x17, concatBytes(getString(channel), data));
}
/// <summary> /// <summary>
/// Send a packet to the server, compression and encryption will be handled automatically /// Send a packet to the server, compression and encryption will be handled automatically
/// </summary> /// </summary>
@ -505,7 +610,7 @@ namespace MinecraftClient.Protocol.Handlers
} }
} }
SendRAW(concatBytes(getVarInt(the_packet.Length), the_packet)); SendRAW(concatBytes(getVarInt(the_packet.Length), the_packet));
} }
/// <summary> /// <summary>