mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Handle forge handshake up to mod list sending.
This commit is contained in:
parent
7cc87d8e71
commit
b154639a6b
4 changed files with 234 additions and 83 deletions
|
|
@ -114,6 +114,8 @@
|
|||
<Compile Include="Crypto\Streams\RegularAesStream.cs" />
|
||||
<Compile Include="Crypto\CryptoHandler.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\Compression\CRC32.cs" />
|
||||
<Compile Include="Protocol\Handlers\Compression\Deflate.cs" />
|
||||
|
|
|
|||
23
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeClientState.cs
Executable file
23
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeClientState.cs
Executable 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
|
||||
}
|
||||
}
|
||||
21
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeDiscriminator.cs
Executable file
21
MinecraftClient/Protocol/Handlers/Forge/FMLHandshakeDiscriminator.cs
Executable 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
|
||||
}
|
||||
}
|
||||
|
|
@ -28,6 +28,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
|
||||
// Server forge info -- may be null.
|
||||
private ForgeInfo forgeInfo;
|
||||
private FMLHandshakeClientState fmlHandshakeState = FMLHandshakeClientState.START;
|
||||
|
||||
IMinecraftComHandler handler;
|
||||
Thread netRead;
|
||||
|
|
@ -138,8 +139,56 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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)
|
||||
{
|
||||
case 0x00: //Keep-Alive
|
||||
|
|
@ -209,6 +258,18 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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)
|
||||
{
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case 0x40: //Kick Packet
|
||||
handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(readNextString(ref packetData)));
|
||||
return false;
|
||||
|
|
@ -226,7 +287,6 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
default:
|
||||
return false; //Ignored packet
|
||||
}
|
||||
}
|
||||
return true; //Packet processed
|
||||
}
|
||||
|
||||
|
|
@ -401,6 +461,16 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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>
|
||||
/// Build an integer for sending over the network
|
||||
/// </summary>
|
||||
|
|
@ -436,6 +506,19 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
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>
|
||||
/// Easily append several byte arrays
|
||||
/// </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>
|
||||
/// Send a packet to the server, compression and encryption will be handled automatically
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue