summaryrefslogtreecommitdiffstats
path: root/src/Protocol/ForgeHandshake.cpp
blob: 38162871cc36ad40e5d68640cc20b3a68cdabd2c (plain) (blame)
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

// ForgeHandshake.cpp

// Implements Forge protocol handshaking

#include "Globals.h"
#include "ForgeHandshake.h"
#include "json/json.h"
#include "../Server.h"
#include "../ByteBuffer.h"
#include "../Bindings/PluginManager.h"
#include "../ClientHandle.h"
#include "../Root.h"


/** Discriminator byte values prefixing the FML|HS packets to determine their type. */
namespace Discriminator
{
static const Int8 ServerHello = 0;
static const Int8 ClientHello = 1;
static const Int8 ModList = 2;
static const Int8 RegistryData = 3;
// static const Int8 HandshakeReset = -2;
static const Int8 HandshakeAck = -1;
}  // namespace Discriminator

/** Client handshake state phases. */
namespace ClientPhase
{
static const Int8 WAITINGSERVERDATA = 2;
static const Int8 WAITINGSERVERCOMPLETE = 3;
static const Int8 PENDINGCOMPLETE = 4;
static const Int8 COMPLETE = 5;
}  // namespace ClientPhase

/** Server handshake state phases. */
namespace ServerPhase
{
static const auto WAITINGCACK = std::byte(2);
static const auto COMPLETE = std::byte(3);
}  // namespace ServerPhase





cForgeHandshake::cForgeHandshake() :
	IsForgeClient(false), m_Errored(false)
{
}





void cForgeHandshake::SetError(const AString & message)
{
	LOGD("Forge handshake error: %s", message.c_str());
	m_Errored = true;
}





void cForgeHandshake::AugmentServerListPing(cClientHandle & a_Client, Json::Value & a_ResponseValue)
{
	auto ProtocolVersion = a_Client.GetProtocolVersion();
	auto & Mods = cRoot::Get()->GetServer()->GetRegisteredForgeMods(ProtocolVersion);

	if (Mods.empty())
	{
		return;
	}

	LOGD("Received server ping from version: %d", ProtocolVersion);

	Json::Value Modinfo;
	Modinfo["type"] = "FML";

	Json::Value ModList(Json::arrayValue);
	for (auto & item : Mods)
	{
		Json::Value Mod;
		Mod["modid"] = item.first;
		Mod["version"] = item.second;
		ModList.append(Mod);
	}
	Modinfo["modList"] = ModList;

	a_ResponseValue["modinfo"] = Modinfo;
}





void cForgeHandshake::BeginForgeHandshake(cClientHandle & a_Client)
{
	ASSERT(IsForgeClient);

	static const std::array<std::string_view, 5> Channels {{"FML|HS", "FML", "FML|MP", "FML", "FORGE"}};
	ContiguousByteBuffer ChannelsString;
	for (auto & Channel : Channels)
	{
		ChannelsString.append({reinterpret_cast<const std::byte *>(Channel.data()), Channel.size()});
		ChannelsString.push_back(std::byte(0));
	}

	a_Client.SendPluginMessage("REGISTER", ChannelsString);
	SendServerHello(a_Client);
}





void cForgeHandshake::SendServerHello(cClientHandle & a_Client)
{
	cByteBuffer Buf(6);
	// Discriminator | Byte | Always 0 for ServerHello
	Buf.WriteBEInt8(Discriminator::ServerHello);
	// FML protocol Version | Byte | Determined from NetworkRegistery. Currently 2.
	Buf.WriteBEInt8(2);
	// Dimension TODO
	Buf.WriteBEInt32(0);

	ContiguousByteBuffer Message;
	Buf.ReadAll(Message);

	a_Client.SendPluginMessage("FML|HS", Message);
}





AStringMap cForgeHandshake::ParseModList(const ContiguousByteBufferView a_Data)
{
	AStringMap Mods;

	if (a_Data.size() < 4)
	{
		SetError(fmt::format(FMT_STRING("ParseModList invalid packet, missing length (size = {})"), a_Data.size()));
		return Mods;
	}

	cByteBuffer Buf(a_Data.size());
	Buf.Write(a_Data.data(), a_Data.size());
	UInt32 NumMods;
	if (!Buf.ReadVarInt32(NumMods))
	{
		SetError("ParseModList failed to read mod count");
		return Mods;
	}

	for (UInt32 i = 0; i < NumMods; ++i)
	{
		AString Name, Version;
		if (!Buf.ReadVarUTF8String(Name))
		{
			SetError(fmt::format(FMT_STRING("ParseModList failed to read mod name at i = {}"), i));
			break;
		}
		if (!Buf.ReadVarUTF8String(Version))
		{
			SetError(fmt::format(FMT_STRING("ParseModList failed to read mod version at i = {}"), i));
			break;
		}
		Mods.insert({Name, Version});
	}

	return Mods;
}





void cForgeHandshake::HandleClientHello(cClientHandle & a_Client, const ContiguousByteBufferView a_Data)
{
	if (a_Data.size() == 2)
	{
		const auto FmlProtocolVersion = static_cast<Int8>(a_Data[1]);
		LOGD("Received ClientHello with FML protocol version %d", FmlProtocolVersion);
		if (FmlProtocolVersion != 2)
		{
			SetError(fmt::format(
				FMT_STRING("Unsupported FML client protocol version received in ClientHello: {}"),
				FmlProtocolVersion
			));
		}
	}
	else
	{
		SetError(fmt::format(FMT_STRING("Received unexpected length of ClientHello: {}"), a_Data.size()));
	}
}





void cForgeHandshake::HandleModList(cClientHandle & a_Client, const ContiguousByteBufferView a_Data)
{
	LOGD("Received ModList");

	auto ClientMods = ParseModList(a_Data.substr(1));
	AString ClientModsString;
	for (auto & item : ClientMods)
	{
		ClientModsString.append(fmt::format(FMT_STRING("{}@{}, "), item.first, item.second));
	}

	LOG("Client connected with %zu mods: %s", ClientMods.size(), ClientModsString);

	a_Client.m_ForgeMods = ClientMods;

	// Let the plugins know about this event, they may refuse the player:
	if (cRoot::Get()->GetPluginManager()->CallHookLoginForge(a_Client, ClientMods))
	{
		SetError("Modded client refused by plugin");
		return;
	}

	// Send server ModList

	// Send server-side Forge mods registered by plugins
	const auto & ServerMods = a_Client.GetForgeMods();

	const auto ModCount = ServerMods.size();

	cByteBuffer Buf(256 * ModCount);

	Buf.WriteBEInt8(Discriminator::ModList);
	Buf.WriteVarInt32(static_cast<UInt32>(ModCount));
	for (const auto & item : ServerMods)
	{
		Buf.WriteVarUTF8String(item.first);  // name
		Buf.WriteVarUTF8String(item.second);  // version
	}

	ContiguousByteBuffer ServerModList;
	Buf.ReadAll(ServerModList);

	a_Client.SendPluginMessage("FML|HS", ServerModList);
}





void cForgeHandshake::HandleHandshakeAck(cClientHandle & a_Client, const ContiguousByteBufferView a_Data)
{
	if (a_Data.size() != 2)
	{
		SetError(fmt::format(FMT_STRING("Unexpected HandshakeAck packet length: {}"), a_Data.size()));
		return;
	}

	const auto Phase = static_cast<Int8>(a_Data[1]);
	LOGD("Received client HandshakeAck with phase = %d", Phase);

	switch (Phase)
	{
		case ClientPhase::WAITINGSERVERDATA:
		{
			cByteBuffer Buf(1024);
			Buf.WriteBEInt8(Discriminator::RegistryData);

			// TODO: send real registry data
			bool HasMore = false;
			AString RegistryName = "potions";
			UInt32 NumIDs = 0;
			UInt32 NumSubstitutions = 0;
			UInt32 NumDummies = 0;

			Buf.WriteBool(HasMore);
			Buf.WriteVarUTF8String(RegistryName);
			Buf.WriteVarInt32(NumIDs);
			Buf.WriteVarInt32(NumSubstitutions);
			Buf.WriteVarInt32(NumDummies);

			ContiguousByteBuffer RegistryData;
			Buf.ReadAll(RegistryData);
			a_Client.SendPluginMessage("FML|HS", RegistryData);
			break;
		}

		case ClientPhase::WAITINGSERVERCOMPLETE:
		{
			LOGD("Client finished receiving registry data; acknowledging");

			ContiguousByteBuffer Ack;
			Ack.push_back(std::byte(Discriminator::HandshakeAck));
			Ack.push_back(ServerPhase::WAITINGCACK);
			a_Client.SendPluginMessage("FML|HS", Ack);
			break;
		}

		case ClientPhase::PENDINGCOMPLETE:
		{
			LOGD("Client is pending completion; sending complete ack");

			ContiguousByteBuffer Ack;
			Ack.push_back(std::byte(Discriminator::HandshakeAck));
			Ack.push_back(ServerPhase::COMPLETE);
			a_Client.SendPluginMessage("FML|HS", Ack);

			break;
		}

		case ClientPhase::COMPLETE:
		{
			// Now finish logging in:
			a_Client.FinishAuthenticate();
			break;
		}

		default:
		{
			SetError(fmt::format("Received unknown phase in Forge handshake acknowledgement: {}", Phase));
			break;
		}
	}
}





void cForgeHandshake::DataReceived(cClientHandle & a_Client, const ContiguousByteBufferView a_Data)
{
	if (!IsForgeClient)
	{
		SetError(
			fmt::format(FMT_STRING("Received unexpected Forge data from non-Forge client ({} bytes)"), a_Data.size())
		);
		return;
	}
	if (m_Errored)
	{
		LOGD("Received unexpected Forge data when in errored state, ignored");
		return;
	}

	if (a_Data.size() <= 1)
	{
		SetError(fmt::format(FMT_STRING("Received unexpectedly short Forge data ({} bytes)"), a_Data.size()));
		return;
	}

	const auto Discriminator = static_cast<Int8>(a_Data[0]);
	switch (Discriminator)
	{
		case Discriminator::ClientHello:  HandleClientHello(a_Client, a_Data); break;
		case Discriminator::ModList:      HandleModList(a_Client, a_Data); break;
		case Discriminator::HandshakeAck: HandleHandshakeAck(a_Client, a_Data); break;

		default:
		{
			SetError(fmt::format(FMT_STRING("Unexpected Forge packet {0} (0x{0:x}) received"), Discriminator));
			return;
		}
	}
}