re: replaced raw pointers in favor of unique_ptr in [HandshakeManager] and related classes.

This commit is contained in:
Matthew Toro 2026-04-04 20:49:05 -04:00
parent f238e682b3
commit 7d8163539c
6 changed files with 20 additions and 26 deletions

View file

@ -6,6 +6,7 @@
#include "..\Minecraft.World\HttpClient.h"
#include "..\Minecraft.World\StringHelpers.h"
#include "Common/vendor/nlohmann/json.hpp"
#include <chrono>
#include <fstream>
#include <shellapi.h>
@ -351,7 +352,7 @@ void AuthFlow::microsoftFlowThread()
for (int ms = 0; ms < interval * 1000; ms += 250)
{
if (cancelRequested) return;
Sleep(250);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
auto pollResp = HttpClient::post(

View file

@ -4141,9 +4141,9 @@ ClientConnection::DeferredEntityLinkPacket::DeferredEntityLinkPacket(shared_ptr<
void ClientConnection::beginAuth()
{
handshakeManager = new HandshakeManager(false);
handshakeManager->registerModule(new SessionAuthModule());
handshakeManager->registerModule(new KeypairOfflineAuthModule());
handshakeManager->registerModule(new OfflineAuthModule());
handshakeManager->registerModule(std::make_unique<SessionAuthModule>());
handshakeManager->registerModule(std::make_unique<KeypairOfflineAuthModule>());
handshakeManager->registerModule(std::make_unique<OfflineAuthModule>());
const auto &profiles = AuthProfileManager::getProfiles();
int idx = AuthProfileManager::getSelectedIndex();

View file

@ -413,9 +413,9 @@ void PendingConnection::initAuth()
{
handshakeManager = new HandshakeManager(true);
if (server->authMode == "session")
handshakeManager->registerModule(new SessionAuthModule());
handshakeManager->registerModule(std::make_unique<SessionAuthModule>());
else
handshakeManager->registerModule(new OfflineAuthModule());
handshakeManager->registerModule(std::make_unique<OfflineAuthModule>());
}
void PendingConnection::handleAuth(const shared_ptr<AuthPacket> &packet)

View file

@ -19,15 +19,10 @@ HandshakeManager::HandshakeManager(bool isServer)
{
}
HandshakeManager::~HandshakeManager()
void HandshakeManager::registerModule(unique_ptr<AuthModule> module)
{
for (auto &[name, module] : modules)
delete module;
}
void HandshakeManager::registerModule(AuthModule *module)
{
modules[module->schemeName()] = module;
wstring name = module->schemeName();
modules[std::move(name)] = std::move(module);
}
void HandshakeManager::setCredentials(const wstring &token, const wstring &uid, const wstring &username, const wstring &variation)
@ -70,7 +65,7 @@ shared_ptr<AuthPacket> HandshakeManager::handleServer(const shared_ptr<AuthPacke
if (modules.empty())
return fail();
activeModule = modules.begin()->second;
activeModule = modules.begin()->second.get();
state = HandshakeState::SCHEME_DECLARED;
return makePacket(AuthStage::DECLARE_SCHEME, {
{L"version", PROTOCOL_VERSION},
@ -145,7 +140,7 @@ shared_ptr<AuthPacket> HandshakeManager::handleClient(const shared_ptr<AuthPacke
if (it == modules.end())
return fail();
activeModule = it->second;
activeModule = it->second.get();
auto variations = activeModule->supportedVariations();
if (!preferredVariation.empty() &&

View file

@ -28,7 +28,7 @@ class HandshakeManager
private:
bool isServer;
HandshakeState state;
unordered_map<wstring, AuthModule *> modules;
unordered_map<wstring, unique_ptr<AuthModule>> modules;
AuthModule *activeModule;
wstring activeVariation;
wstring protocolVersion;
@ -45,9 +45,9 @@ public:
wstring finalUsername;
HandshakeManager(bool isServer);
~HandshakeManager();
~HandshakeManager() = default;
void registerModule(AuthModule *module);
void registerModule(unique_ptr<AuthModule> module);
void setCredentials(const wstring &token, const wstring &uid, const wstring &username, const wstring &variation = L"");
shared_ptr<AuthPacket> handlePacket(const shared_ptr<AuthPacket> &packet);
shared_ptr<AuthPacket> createInitialPacket();

View file

@ -2,6 +2,7 @@
#include "UUID.h"
#include "Random.h"
#include <cstring>
#include <vector>
static void sha1_block(uint32_t h[5], const uint8_t block[64])
{
uint32_t w[80];
@ -119,15 +120,12 @@ GameUUID GameUUID::v4(uint64_t high, uint64_t low)
GameUUID GameUUID::v5(const GameUUID& ns, const std::string& name)
{
uint8_t input[256];
ns.toBytes(input);
size_t total = 16 + name.size();
// names over 240 chars would be insane but just in case
if (name.size() <= sizeof(input) - 16)
memcpy(input + 16, name.data(), name.size());
std::vector<uint8_t> input(16 + name.size());
ns.toBytes(input.data());
memcpy(input.data() + 16, name.data(), name.size());
uint8_t hash[20];
sha1(input, total, hash);
sha1(input.data(), input.size(), hash);
GameUUID u = fromBytes(hash);
u.msb = (u.msb & ~0xF000ULL) | 0x5000ULL;
u.lsb = (u.lsb & ~0xC000000000000000ULL) | 0x8000000000000000ULL;