-
Notifications
You must be signed in to change notification settings - Fork 2k
/
InitConnectMethod.cpp
505 lines (380 loc) · 12.8 KB
/
InitConnectMethod.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include "StdInc.h"
#include <ClientHttpHandler.h>
#include <ClientRegistry.h>
#include <GameServer.h>
#include <ServerInstanceBase.h>
#include <ResourceManager.h>
#include <ResourceEventComponent.h>
#include <ResourceCallbackComponent.h>
#include <boost/random/random_device.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <botan/base64.h>
#include <botan/sha160.h>
#include <botan/pubkey.h>
#include <botan/rsa.h>
#include <botan/ber_dec.h>
#include <ctime>
#include <ServerIdentityProvider.h>
static std::forward_list<fx::ServerIdentityProviderBase*> g_serverProviders;
static std::map<std::string, fx::ServerIdentityProviderBase*> g_providersByType;
namespace fx
{
void RegisterServerIdentityProvider(ServerIdentityProviderBase* provider)
{
g_serverProviders.push_front(provider);
g_providersByType.insert({ provider->GetIdentifierPrefix(), provider });
}
}
static bool VerifyTicket(const std::string& guid, const std::string& ticket)
{
auto ticketData = Botan::base64_decode(ticket);
// validate ticket length
if (ticketData.size() < 20 + 4 + 128)
{
return false;
}
uint32_t length = *(uint32_t*)&ticketData[0];
if (length != 16)
{
return false;
}
uint64_t ticketGuid = *(uint64_t*)&ticketData[4];
uint64_t ticketExpiry = *(uint64_t*)&ticketData[12];
// check expiration
// get UTC time
std::time_t timeVal;
std::time(&timeVal);
std::tm* tm = std::gmtime(&timeVal);
std::time_t utcTime = std::mktime(tm);
// verify
if (ticketExpiry < utcTime)
{
trace("Connecting player: ticket expired\n");
return false;
}
// check the GUID
uint64_t realGuid = std::stoull(guid);
if (realGuid != ticketGuid)
{
trace("Connecting player: ticket GUID not matching\n");
return false;
}
// check the RSA signature
uint32_t sigLength = *(uint32_t*)&ticketData[length + 4];
if (sigLength != 128)
{
return false;
}
Botan::SHA_160 hashFunction;
auto result = hashFunction.process(&ticketData[4], length);
std::vector<uint8_t> msg(result.size() + 1);
msg[0] = 2;
memcpy(&msg[1], &result[0], result.size());
auto modulus = Botan::base64_decode("1DNT1go22VUAU3BON+jCfXxs7Ow9Zxwng4ARTX/vrv6I65bsSYbdBrcc"
"w/50Fu7AJr8zy8+sXK8wUO4gx00frtA0adaGeZOeBqNq7/K3Gprv98wc"
"ftbxWjUv75pVl9Ush5yxpBPbuYUnGR/Nh2+K3GRrIrKxWYpNSF1JZYzE"
"+5k=");
auto exponent = Botan::base64_decode("AQAB");
Botan::BigInt n(modulus.data(), modulus.size());
Botan::BigInt e(exponent.data(), exponent.size());
auto pk = Botan::RSA_PublicKey(n, e);
auto signer = std::make_unique<Botan::PK_Verifier>(pk, "EMSA_PKCS1(SHA-1)");
bool valid = signer->verify_message(msg.data(), msg.size(), &ticketData[length + 4 + 4], sigLength);
if (!valid)
{
trace("Connecting player: ticket RSA signature not matching\n");
return false;
}
return true;
}
struct TicketData
{
std::optional<std::array<uint8_t, 20>> entitlementHash;
};
static std::optional<TicketData> VerifyTicketEx(const std::string& ticket)
{
auto ticketData = Botan::base64_decode(ticket);
// validate ticket length
if (ticketData.size() < 20 + 4 + 128 + 4)
{
return {};
}
uint32_t length = *(uint32_t*)&ticketData[20 + 4 + 128];
// validate full length
if (ticketData.size() < 20 + 4 + 128 + 4 + length)
{
return {};
}
// copy extra data
std::vector<uint8_t> extraData(length);
if (!extraData.empty())
{
memcpy(&extraData[0], &ticketData[20 + 4 + 128 + 4], length);
}
// check the RSA signature
uint32_t sigLength = *(uint32_t*)&ticketData[20 + 4 + 128 + 4 + length];
if (sigLength != 128)
{
return {};
}
Botan::SHA_160 hashFunction;
auto result = hashFunction.process(&ticketData[4], ticketData.size() - 128 - 4 - 4);
std::vector<uint8_t> msg(result.size() + 1);
msg[0] = 2;
memcpy(&msg[1], &result[0], result.size());
auto modulus = Botan::base64_decode("1DNT1go22VUAU3BON+jCfXxs7Ow9Zxwng4ARTX/vrv6I65bsSYbdBrcc"
"w/50Fu7AJr8zy8+sXK8wUO4gx00frtA0adaGeZOeBqNq7/K3Gprv98wc"
"ftbxWjUv75pVl9Ush5yxpBPbuYUnGR/Nh2+K3GRrIrKxWYpNSF1JZYzE"
"+5k=");
auto exponent = Botan::base64_decode("AQAB");
Botan::BigInt n(modulus.data(), modulus.size());
Botan::BigInt e(exponent.data(), exponent.size());
auto pk = Botan::RSA_PublicKey(n, e);
auto signer = std::make_unique<Botan::PK_Verifier>(pk, "EMSA_PKCS1(SHA-1)");
bool valid = signer->verify_message(msg.data(), msg.size(), &ticketData[length + 4 + 4 + 128 + 20 + 4], sigLength);
if (!valid)
{
trace("Connecting player: ticket RSA signature not matching\n");
return {};
}
TicketData outData;
if (length >= 20)
{
std::array<uint8_t, 20> entitlementHash;
memcpy(entitlementHash.data(), &extraData[0], entitlementHash.size());
outData.entitlementHash = entitlementHash;
}
return outData;
}
static InitFunction initFunction([]()
{
fx::ServerInstanceBase::OnServerCreate.Connect([](fx::ServerInstanceBase* instance)
{
auto minTrustVar = instance->AddVariable<int>("sv_authMinTrust", ConVar_None, 1);
minTrustVar->GetHelper()->SetConstraints(1, 5);
auto maxVarianceVar = instance->AddVariable<int>("sv_authMaxVariance", ConVar_None, 5);
maxVarianceVar->GetHelper()->SetConstraints(1, 5);
auto shVar = instance->AddVariable<bool>("sv_scriptHookAllowed", ConVar_ServerInfo, false);
auto ehVar = instance->AddVariable<bool>("sv_enhancedHostSupport", ConVar_ServerInfo, false);
auto lanVar = instance->AddVariable<bool>("sv_lan", ConVar_ServerInfo, false);
instance->GetComponent<fx::ClientMethodRegistry>()->AddHandler("initConnect", [=](const std::map<std::string, std::string>& postMap, const fwRefContainer<net::HttpRequest>& request, const std::function<void(const json&)>& cb)
{
auto nameIt = postMap.find("name");
auto guidIt = postMap.find("guid");
auto protocolIt = postMap.find("protocol");
if (nameIt == postMap.end() || guidIt == postMap.end() || protocolIt == postMap.end())
{
cb(json::object({ {"error", "fields missing"} }));
return;
}
auto name = nameIt->second;
auto guid = guidIt->second;
auto protocol = atoi(protocolIt->second.c_str());
TicketData ticketData;
if (!lanVar->GetValue())
{
auto ticketIt = postMap.find("cfxTicket");
if (ticketIt == postMap.end())
{
cb(json::object({ { "error", "No FiveM ticket was specified. If this is an offline server, maybe set sv_lan?"} }));
return;
}
if (!VerifyTicket(guid, ticketIt->second))
{
cb(json::object({ { "error", "FiveM ticket authorization failed." } }));
return;
}
auto optionalTicket = VerifyTicketEx(ticketIt->second);
if (!optionalTicket)
{
cb(json::object({ { "error", "FiveM ticket authorization failed. (2)" } }));
return;
}
ticketData = *optionalTicket;
}
std::string token = boost::uuids::to_string(boost::uuids::basic_random_generator<boost::random_device>()());
json data = json::object();
data["protocol"] = 4;
data["sH"] = shVar->GetValue();
data["enhancedHostSupport"] = ehVar->GetValue();
data["token"] = token;
data["netlibVersion"] = 2;
auto clientRegistry = instance->GetComponent<fx::ClientRegistry>();
auto gameServer = instance->GetComponent<fx::GameServer>();
{
auto oldClient = clientRegistry->GetClientByGuid(guid);
if (oldClient)
{
gameServer->DropClient(oldClient, "Reconnecting");
}
}
auto ra = request->GetRemoteAddress();
static std::atomic<uint32_t> g_tempIds;
auto tempId = g_tempIds.fetch_add(1);
auto client = clientRegistry->MakeClient(guid);
client->SetName(name);
client->SetConnectionToken(token);
client->SetTcpEndPoint(ra.substr(0, ra.find_last_of(':')));
client->SetNetId(0x10000 + tempId);
// add the entitlement hash if needed
if (ticketData.entitlementHash)
{
auto& hash = *ticketData.entitlementHash;
client->SetData("entitlementHash", fmt::sprintf("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7], hash[8], hash[9],
hash[10], hash[11], hash[12], hash[13], hash[14], hash[15], hash[16], hash[17], hash[18], hash[19]));
}
client->Touch();
auto it = g_serverProviders.begin();
auto done = [=]()
{
auto allowClient = [=]()
{
client->SetData("passedValidation", true);
client->SetData("canBeDead", false);
};
int maxTrust = INT_MIN;
int minVariance = INT_MAX;
for (const auto& identifier : client->GetIdentifiers())
{
std::string idType = identifier.substr(0, identifier.find_first_of(':'));
auto provider = g_providersByType[idType];
maxTrust = std::max(provider->GetTrustLevel(), maxTrust);
minVariance = std::min(provider->GetVarianceLevel(), minVariance);
}
if (maxTrust < minTrustVar->GetValue() || minVariance > maxVarianceVar->GetValue())
{
clientRegistry->RemoveClient(client);
cb({ {"error", "You can not join this server due to your identifiers being insufficient. Please try starting Steam or another identity provider and try again."} });
return;
}
auto resman = instance->GetComponent<fx::ResourceManager>();
auto eventManager = resman->GetComponent<fx::ResourceEventManagerComponent>();
auto cbComponent = resman->GetComponent<fx::ResourceCallbackComponent>();
// TODO: replace with event stacks once implemented
std::string noReason("Resource prevented connection.");
std::map<std::string, fx::ResourceCallbackComponent::CallbackRef> cbs;
bool isDeferred = false;
using TDeferFn = std::function<void(const json&)>;
using TDeferPtr = std::unique_ptr<TDeferFn>;
json deferData = { { "defer", true },{ "token", token },{ "status", "Deferred connection." } };
auto returnedCb = std::make_shared<bool>(false);
auto deferDoneCb = std::make_shared<TDeferPtr>(std::make_unique<TDeferFn>([&](const json& data)
{
deferData = data;
}));
cbs["defer"] = cbComponent->CreateCallback([&](const msgpack::unpacked& unpacked)
{
isDeferred = true;
});
cbs["update"] = cbComponent->CreateCallback([=](const msgpack::unpacked& unpacked)
{
if (*returnedCb)
{
return;
}
auto obj = unpacked.get().as<std::vector<msgpack::object>>();
if (obj.size() == 1)
{
(**deferDoneCb)({ { "status", obj[0].as<std::string>() }, {"token", token}, {"defer", true} });
}
});
cbs["done"] = cbComponent->CreateCallback([=](const msgpack::unpacked& unpacked)
{
if (*returnedCb)
{
return;
}
auto obj = unpacked.get().as<std::vector<msgpack::object>>();
if (obj.size() == 1)
{
clientRegistry->RemoveClient(client);
(**deferDoneCb)({ { "error", obj[0].as<std::string>() } });
}
else
{
allowClient();
(**deferDoneCb)(data);
}
*returnedCb = true;
});
bool shouldAllow = eventManager->TriggerEvent2("playerConnecting", { fmt::sprintf("net:%d", client->GetNetId()) }, client->GetName(), cbComponent->CreateCallback([&](const msgpack::unpacked& unpacked)
{
auto obj = unpacked.get().as<std::vector<msgpack::object>>();
if (obj.size() == 1)
{
noReason = obj[0].as<std::string>();
}
}), cbs);
if (!shouldAllow)
{
*returnedCb = true;
clientRegistry->RemoveClient(client);
cb({ {"error", noReason} });
return;
}
if (!isDeferred)
{
allowClient();
cb(data);
}
else
{
if (protocol < 5)
{
// set a callback so setting data won't crash
*deferDoneCb = std::make_unique<TDeferFn>([=](const json& data)
{
});
if (!*returnedCb)
{
*returnedCb = true;
clientRegistry->RemoveClient(client);
cb({ {"error", "You need to update your client to join this server."} });
}
return;
}
*deferDoneCb = std::make_unique<TDeferFn>([=](const json& data)
{
client->SetData("deferralState", std::any{ data });
auto& updateCb = client->GetData("deferralCallback");
if (updateCb.has_value())
{
std::any_cast<std::function<void()>>(updateCb)();
}
});
if (!*returnedCb)
{
cb(deferData);
}
}
};
// seriously C++?
auto runOneIdentifier = std::make_shared<std::unique_ptr<std::function<void(decltype(g_serverProviders.begin()))>>>();
*runOneIdentifier = std::make_unique<std::function<void(decltype(g_serverProviders.begin()))>>([=](auto it)
{
if (it == g_serverProviders.end())
{
done();
}
else
{
auto auth = (*it);
auto thisIt = ++it;
auth->RunAuthentication(client, postMap, [=](std::optional<std::string> err)
{
if (err)
{
clientRegistry->RemoveClient(client);
cb(json::object({ {"error", *err} }));
return;
}
(**runOneIdentifier)(thisIt);
});
}
});
(**runOneIdentifier)(it);
});
}, 50);
});