From 1a0d9450eaa0f3c2ff475175f5d45932fd4dd7fa Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 2 Oct 2021 21:27:32 +0100 Subject: Authentication flow: move construction, slim down classes (#5312) - Remove extra members in ForgeHandshake --- src/Protocol/Authenticator.cpp | 25 +++++++++---------- src/Protocol/Authenticator.h | 2 +- src/Protocol/ForgeHandshake.cpp | 55 +++++++++++++++++++---------------------- src/Protocol/ForgeHandshake.h | 32 +++++++++++------------- src/Protocol/Protocol_1_8.cpp | 3 ++- 5 files changed, 55 insertions(+), 62 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index 6233ddb32..15a246f6e 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -57,17 +57,17 @@ void cAuthenticator::ReadSettings(cSettingsRepositoryInterface & a_Settings) -void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash) +void cAuthenticator::Authenticate(int a_ClientID, AString && a_Username, const AString & a_ServerHash) { if (!m_ShouldAuthenticate) { - Json::Value Value; - cRoot::Get()->AuthenticateUser(a_ClientID, a_UserName, cClientHandle::GenerateOfflineUUID(a_UserName), Value); + const auto UUID = cClientHandle::GenerateOfflineUUID(a_Username); + cRoot::Get()->GetServer()->AuthenticateUser(a_ClientID, std::move(a_Username), UUID, Json::Value{}); return; } - cCSLock LOCK(m_CS); - m_Queue.emplace_back(a_ClientID, a_UserName, a_ServerHash); + cCSLock Lock(m_CS); + m_Queue.emplace_back(a_ClientID, std::move(a_Username), a_ServerHash); m_QueueNonempty.Set(); } @@ -112,20 +112,19 @@ void cAuthenticator::Execute(void) } ASSERT(!m_Queue.empty()); - cAuthenticator::cUser & User = m_Queue.front(); - int ClientID = User.m_ClientID; - AString UserName = User.m_Name; - AString ServerID = User.m_ServerID; + cAuthenticator::cUser User = std::move(m_Queue.front()); + int & ClientID = User.m_ClientID; + AString & UserName = User.m_Name; + AString & ServerID = User.m_ServerID; m_Queue.pop_front(); Lock.Unlock(); - AString NewUserName = UserName; cUUID UUID; Json::Value Properties; - if (AuthWithYggdrasil(NewUserName, ServerID, UUID, Properties)) + if (AuthWithYggdrasil(UserName, ServerID, UUID, Properties)) { - LOGINFO("User %s authenticated with UUID %s", NewUserName.c_str(), UUID.ToShortString().c_str()); - cRoot::Get()->AuthenticateUser(ClientID, NewUserName, UUID, Properties); + LOGINFO("User %s authenticated with UUID %s", UserName.c_str(), UUID.ToShortString().c_str()); + cRoot::Get()->GetServer()->AuthenticateUser(ClientID, std::move(UserName), UUID, std::move(Properties)); } else { diff --git a/src/Protocol/Authenticator.h b/src/Protocol/Authenticator.h index 5fd8e5434..39a50ac47 100644 --- a/src/Protocol/Authenticator.h +++ b/src/Protocol/Authenticator.h @@ -41,7 +41,7 @@ public: void ReadSettings(cSettingsRepositoryInterface & a_Settings); /** Queues a request for authenticating a user. If the auth fails, the user will be kicked */ - void Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash); + void Authenticate(int a_ClientID, AString && a_Username, const AString & a_ServerHash); /** Starts the authenticator thread. The thread may be started and stopped repeatedly */ void Start(cSettingsRepositoryInterface & a_Settings); diff --git a/src/Protocol/ForgeHandshake.cpp b/src/Protocol/ForgeHandshake.cpp index 99b585894..34e044fbc 100644 --- a/src/Protocol/ForgeHandshake.cpp +++ b/src/Protocol/ForgeHandshake.cpp @@ -44,10 +44,9 @@ namespace ServerPhase -cForgeHandshake::cForgeHandshake(cClientHandle *a_Client) : - m_IsForgeClient(false), - m_Errored(false), - m_Client(a_Client) +cForgeHandshake::cForgeHandshake() : + IsForgeClient(false), + m_Errored(false) { } @@ -65,9 +64,9 @@ void cForgeHandshake::SetError(const AString & message) -void cForgeHandshake::AugmentServerListPing(Json::Value & a_ResponseValue) +void cForgeHandshake::AugmentServerListPing(cClientHandle & a_Client, Json::Value & a_ResponseValue) { - auto ProtocolVersion = m_Client->GetProtocolVersion(); + auto ProtocolVersion = a_Client.GetProtocolVersion(); auto & Mods = cRoot::Get()->GetServer()->GetRegisteredForgeMods(ProtocolVersion); if (Mods.empty()) @@ -97,13 +96,9 @@ void cForgeHandshake::AugmentServerListPing(Json::Value & a_ResponseValue) -void cForgeHandshake::BeginForgeHandshake(const AString & a_Name, const cUUID & a_UUID, const Json::Value & a_Properties) +void cForgeHandshake::BeginForgeHandshake(cClientHandle & a_Client) { - ASSERT(m_IsForgeClient); - - m_Name = a_Name; - m_UUID = a_UUID; - m_Properties = a_Properties; + ASSERT(IsForgeClient); static const std::array Channels{{ "FML|HS", "FML", "FML|MP", "FML", "FORGE" }}; ContiguousByteBuffer ChannelsString; @@ -113,15 +108,15 @@ void cForgeHandshake::BeginForgeHandshake(const AString & a_Name, const cUUID & ChannelsString.push_back(std::byte(0)); } - m_Client->SendPluginMessage("REGISTER", ChannelsString); - SendServerHello(); + a_Client.SendPluginMessage("REGISTER", ChannelsString); + SendServerHello(a_Client); } -void cForgeHandshake::SendServerHello() +void cForgeHandshake::SendServerHello(cClientHandle & a_Client) { cByteBuffer Buf(6); // Discriminator | Byte | Always 0 for ServerHello @@ -134,7 +129,7 @@ void cForgeHandshake::SendServerHello() ContiguousByteBuffer Message; Buf.ReadAll(Message); - m_Client->SendPluginMessage("FML|HS", Message); + a_Client.SendPluginMessage("FML|HS", Message); } @@ -183,7 +178,7 @@ AStringMap cForgeHandshake::ParseModList(const ContiguousByteBufferView a_Data) -void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) +void cForgeHandshake::HandleClientHello(cClientHandle & a_Client, const ContiguousByteBufferView a_Data) { if (a_Data.size() == 2) { @@ -204,7 +199,7 @@ void cForgeHandshake::HandleClientHello(cClientHandle * a_Client, const Contiguo -void cForgeHandshake::HandleModList(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) +void cForgeHandshake::HandleModList(cClientHandle & a_Client, const ContiguousByteBufferView a_Data) { LOGD("Received ModList"); @@ -217,10 +212,10 @@ void cForgeHandshake::HandleModList(cClientHandle * a_Client, const ContiguousBy LOG("Client connected with %zu mods: %s", ClientMods.size(), ClientModsString.c_str()); - m_Client->m_ForgeMods = ClientMods; + 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)) + if (cRoot::Get()->GetPluginManager()->CallHookLoginForge(a_Client, ClientMods)) { SetError("Modded client refused by plugin"); return; @@ -229,7 +224,7 @@ void cForgeHandshake::HandleModList(cClientHandle * a_Client, const ContiguousBy // Send server ModList // Send server-side Forge mods registered by plugins - const auto & ServerMods = m_Client->GetForgeMods(); + const auto & ServerMods = a_Client.GetForgeMods(); const auto ModCount = ServerMods.size(); @@ -246,14 +241,14 @@ void cForgeHandshake::HandleModList(cClientHandle * a_Client, const ContiguousBy ContiguousByteBuffer ServerModList; Buf.ReadAll(ServerModList); - m_Client->SendPluginMessage("FML|HS", ServerModList); + a_Client.SendPluginMessage("FML|HS", ServerModList); } -void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) +void cForgeHandshake::HandleHandshakeAck(cClientHandle & a_Client, const ContiguousByteBufferView a_Data) { if (a_Data.size() != 2) { @@ -286,7 +281,7 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const Contigu ContiguousByteBuffer RegistryData; Buf.ReadAll(RegistryData); - m_Client->SendPluginMessage("FML|HS", RegistryData); + a_Client.SendPluginMessage("FML|HS", RegistryData); break; } @@ -297,7 +292,7 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const Contigu ContiguousByteBuffer Ack; Ack.push_back(std::byte(Discriminator::HandshakeAck)); Ack.push_back(ServerPhase::WAITINGCACK); - m_Client->SendPluginMessage("FML|HS", Ack); + a_Client.SendPluginMessage("FML|HS", Ack); break; } @@ -308,15 +303,15 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const Contigu ContiguousByteBuffer Ack; Ack.push_back(std::byte(Discriminator::HandshakeAck)); Ack.push_back(ServerPhase::COMPLETE); - m_Client->SendPluginMessage("FML|HS", Ack); + a_Client.SendPluginMessage("FML|HS", Ack); break; } case ClientPhase::COMPLETE: { - // Now finish logging in - m_Client->FinishAuthenticate(m_Name, m_UUID, m_Properties); + // Now finish logging in: + a_Client.FinishAuthenticate(); break; } @@ -332,9 +327,9 @@ void cForgeHandshake::HandleHandshakeAck(cClientHandle * a_Client, const Contigu -void cForgeHandshake::DataReceived(cClientHandle * a_Client, const ContiguousByteBufferView a_Data) +void cForgeHandshake::DataReceived(cClientHandle & a_Client, const ContiguousByteBufferView a_Data) { - if (!m_IsForgeClient) + if (!IsForgeClient) { SetError(Printf("Received unexpected Forge data from non-Forge client (%zu bytes)", a_Data.size())); return; diff --git a/src/Protocol/ForgeHandshake.h b/src/Protocol/ForgeHandshake.h index 061369c15..7c3969b96 100644 --- a/src/Protocol/ForgeHandshake.h +++ b/src/Protocol/ForgeHandshake.h @@ -9,6 +9,10 @@ #include "../UUID.h" #include "json/json.h" + + + + // fwd: class cClientHandle; @@ -19,38 +23,32 @@ class cClientHandle; class cForgeHandshake { public: + /** True if the client advertised itself as a Forge client. */ - bool m_IsForgeClient; + bool IsForgeClient; - cForgeHandshake(cClientHandle * client); + cForgeHandshake(); /** Add the registered Forge mods to the server ping list packet. */ - void AugmentServerListPing(Json::Value & ResponseValue); + void AugmentServerListPing(cClientHandle & a_Client, Json::Value & ResponseValue); /** Begin the Forge Modloader Handshake (FML|HS) sequence. */ - void BeginForgeHandshake(const AString & a_Name, const cUUID & a_UUID, const Json::Value & a_Properties); + void BeginForgeHandshake(cClientHandle & a_Client); /** Send the ServerHello packet in the Forge handshake. */ - void SendServerHello(); + void SendServerHello(cClientHandle & a_Client); /** Process received data from the client advancing the Forge handshake. */ - void DataReceived(cClientHandle * a_Client, ContiguousByteBufferView a_Data); + void DataReceived(cClientHandle & a_Client, ContiguousByteBufferView a_Data); private: + /** True if the Forge handshake is in an errored state. */ bool m_Errored; - /** The client handle undergoing this Forge handshake. */ - cClientHandle * m_Client; - - /** Values saved from BeginForgeHandshake() for continuing the normal handshake after Forge completes. */ - AString m_Name; - cUUID m_UUID; - Json::Value m_Properties; - - void HandleClientHello(cClientHandle * a_Client, ContiguousByteBufferView a_Data); - void HandleModList(cClientHandle * a_Client, ContiguousByteBufferView a_Data); - void HandleHandshakeAck(cClientHandle * a_Client, ContiguousByteBufferView a_Data); + void HandleClientHello(cClientHandle & a_Client, ContiguousByteBufferView a_Data); + void HandleModList(cClientHandle & a_Client, ContiguousByteBufferView a_Data); + void HandleHandshakeAck(cClientHandle & a_Client, ContiguousByteBufferView a_Data); /** Set errored state to prevent further handshake message processing. */ void SetError(const AString & message); diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index c34e95117..d17b65e2d 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -2161,6 +2161,7 @@ void cProtocol_1_8_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) auto NumPlayers = static_cast(Server->GetNumPlayers()); auto MaxPlayers = static_cast(Server->GetMaxPlayers()); AString Favicon = Server->GetFaviconData(); + cRoot::Get()->GetPluginManager()->CallHookServerPing(*m_Client, ServerDescription, NumPlayers, MaxPlayers, Favicon); // Version: @@ -2177,7 +2178,7 @@ void cProtocol_1_8_0::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) // Description: Json::Value Description; - Description["text"] = ServerDescription.c_str(); + Description["text"] = std::move(ServerDescription); // Create the response: Json::Value ResponseValue; -- cgit v1.2.3