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

@ -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,8 +139,56 @@ 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 (fmlHandshakeState)
{
case FMLHandshakeClientState.START:
if (packetID != 0x3F)
break;
String channel = readNextString(ref packetData);
if (channel != "FML|HS")
break;
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++)
{
ForgeInfo.ForgeMod mod = forgeInfo.Mods[i];
mods[i] = concatBytes(getString(mod.ModID), getString(mod.Version));
}
SendForgeHandshakePacket(FMLHandshakeDiscriminator.ModList, concatBytes(getVarInt(forgeInfo.Mods.Count), concatBytes(mods)));
fmlHandshakeState = FMLHandshakeClientState.WAITINGSERVERDATA;
return true;
}
}
switch (packetID) switch (packetID)
{ {
case 0x00: //Keep-Alive case 0x00: //Keep-Alive
@ -209,6 +258,18 @@ namespace MinecraftClient.Protocol.Handlers
if (tab_list.Length > 0) if (tab_list.Length > 0)
ConsoleIO.WriteLineFormatted("§8" + tab_list, false); ConsoleIO.WriteLineFormatted("§8" + tab_list, false);
break; break;
case 0x3F: //Plugin message.
String channel = readNextString(ref packetData);
if (channel == "FML|HS")
{
FMLHandshakeDiscriminator discriminator = (FMLHandshakeDiscriminator)readNextByte(ref packetData);
if (discriminator == FMLHandshakeDiscriminator.HandshakeReset)
{
}
return true;
}
break;
case 0x40: //Kick Packet case 0x40: //Kick Packet
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData))); handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
return false; return false;
@ -226,7 +287,6 @@ namespace MinecraftClient.Protocol.Handlers
default: default:
return false; //Ignored packet return false; //Ignored packet
} }
}
return true; //Packet processed return true; //Packet processed
} }
@ -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>