From 75b7244f0565170ccc40a3ebdf277945e5191be8 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 12 Apr 2014 22:15:09 +0200 Subject: Fixed chunkstays not being removed on auto-delete. Fixes #837. --- src/ChunkMap.cpp | 26 +++++++++++++++++--------- src/ChunkStay.cpp | 3 ++- 2 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index e695f0ab2..83eae2665 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -916,19 +916,21 @@ void cChunkMap::SetChunkData( } // Notify relevant ChunkStays: - for (cChunkStays::iterator itr = m_ChunkStays.begin(); itr != m_ChunkStays.end(); ) + cChunkStays ToBeDisabled; + for (cChunkStays::iterator itr = m_ChunkStays.begin(), end = m_ChunkStays.end(); itr != end; ++itr) { if ((*itr)->ChunkAvailable(a_ChunkX, a_ChunkZ)) { - cChunkStays::iterator cur = itr; - ++itr; - m_ChunkStays.erase(cur); - } - else - { - ++itr; + // The chunkstay wants to be disabled, add it to a list of to-be-disabled chunkstays for later processing: + ToBeDisabled.push_back(*itr); } } // for itr - m_ChunkStays[] + + // Disable (and possibly remove) the chunkstays that chose to get disabled: + for (cChunkStays::iterator itr = ToBeDisabled.begin(), end = ToBeDisabled.end(); itr != end; ++itr) + { + (*itr)->Disable(); + } } // Notify plugins of the chunk becoming available @@ -2974,7 +2976,12 @@ void cChunkMap::AddChunkStay(cChunkStay & a_ChunkStay) Chunk->Stay(true); if (Chunk->IsValid()) { - a_ChunkStay.ChunkAvailable(itr->m_ChunkX, itr->m_ChunkZ); + if (a_ChunkStay.ChunkAvailable(itr->m_ChunkX, itr->m_ChunkZ)) + { + // The chunkstay wants to be deactivated, disable it and bail out: + a_ChunkStay.Disable(); + return; + } } } // for itr - WantedChunks[] } @@ -3017,6 +3024,7 @@ void cChunkMap::DelChunkStay(cChunkStay & a_ChunkStay) } Chunk->Stay(false); } // for itr - Chunks[] + a_ChunkStay.OnDisabled(); } diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp index 6b1d5ee34..a35ccf58c 100644 --- a/src/ChunkStay.cpp +++ b/src/ChunkStay.cpp @@ -97,8 +97,9 @@ void cChunkStay::Disable(void) { ASSERT(m_ChunkMap != NULL); - m_ChunkMap->DelChunkStay(*this); + cChunkMap * ChunkMap = m_ChunkMap; m_ChunkMap = NULL; + ChunkMap->DelChunkStay(*this); } -- cgit v1.2.3 From 34cf058b0257587456632536a54db0d4e119dae1 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 12 Apr 2014 22:35:04 +0200 Subject: ChunkStay must be disabled while being deleted. Fixes #758. --- src/ChunkStay.cpp | 5 +---- src/ChunkStay.h | 4 ++++ src/LightingThread.cpp | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp index a35ccf58c..b5002a63d 100644 --- a/src/ChunkStay.cpp +++ b/src/ChunkStay.cpp @@ -31,10 +31,7 @@ cChunkStay::~cChunkStay() void cChunkStay::Clear(void) { - if (m_ChunkMap != NULL) - { - Disable(); - } + ASSERT(m_ChunkMap == NULL); m_Chunks.clear(); } diff --git a/src/ChunkStay.h b/src/ChunkStay.h index 2510cb490..29893befc 100644 --- a/src/ChunkStay.h +++ b/src/ChunkStay.h @@ -36,8 +36,12 @@ class cChunkStay { public: cChunkStay(void); + + /** Deletes the object. Note that this calls Clear(), which means that the ChunkStay needs to be disabled. */ virtual ~cChunkStay(); + /** Clears all the chunks that have been added. + To be used only while the ChunkStay object is not enabled. */ void Clear(void); /** Adds a chunk to be locked from unloading. diff --git a/src/LightingThread.cpp b/src/LightingThread.cpp index 302473d71..5ba2940d2 100644 --- a/src/LightingThread.cpp +++ b/src/LightingThread.cpp @@ -286,6 +286,7 @@ void cLightingThread::LightChunk(cLightingChunkStay & a_Item) { a_Item.m_CallbackAfter->Call(a_Item.m_ChunkX, a_Item.m_ChunkZ); } + a_Item.Disable(); delete &a_Item; } -- cgit v1.2.3 From b506a7407661c0527255466cf8b315824b0003c0 Mon Sep 17 00:00:00 2001 From: daniel0916 Date: Sun, 13 Apr 2014 13:04:56 +0200 Subject: Added Yggdrasil Authentication System Code by Howaner. Fixes/Changes by me. --- src/Authenticator.cpp | 267 --------------------------------- src/Authenticator.h | 93 ------------ src/ClientHandle.cpp | 7 +- src/ClientHandle.h | 6 +- src/Protocol/Authenticator.cpp | 325 +++++++++++++++++++++++++++++++++++++++++ src/Protocol/Authenticator.h | 93 ++++++++++++ src/Root.cpp | 4 +- src/Root.h | 4 +- src/Server.cpp | 4 +- src/Server.h | 2 +- 10 files changed, 435 insertions(+), 370 deletions(-) delete mode 100644 src/Authenticator.cpp delete mode 100644 src/Authenticator.h create mode 100644 src/Protocol/Authenticator.cpp create mode 100644 src/Protocol/Authenticator.h (limited to 'src') diff --git a/src/Authenticator.cpp b/src/Authenticator.cpp deleted file mode 100644 index bd6db1c11..000000000 --- a/src/Authenticator.cpp +++ /dev/null @@ -1,267 +0,0 @@ - -#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules - -#include "Authenticator.h" -#include "OSSupport/BlockingTCPLink.h" -#include "Root.h" -#include "Server.h" - -#include "inifile/iniFile.h" - -#include - - - - - -#define DEFAULT_AUTH_SERVER "session.minecraft.net" -#define DEFAULT_AUTH_ADDRESS "/game/checkserver.jsp?user=%USERNAME%&serverId=%SERVERID%" -#define MAX_REDIRECTS 10 - - - - - -cAuthenticator::cAuthenticator(void) : - super("cAuthenticator"), - m_Server(DEFAULT_AUTH_SERVER), - m_Address(DEFAULT_AUTH_ADDRESS), - m_ShouldAuthenticate(true) -{ -} - - - - - -cAuthenticator::~cAuthenticator() -{ - Stop(); -} - - - - - -void cAuthenticator::ReadINI(cIniFile & IniFile) -{ - m_Server = IniFile.GetValueSet("Authentication", "Server", DEFAULT_AUTH_SERVER); - m_Address = IniFile.GetValueSet("Authentication", "Address", DEFAULT_AUTH_ADDRESS); - m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true); -} - - - - - -void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash) -{ - if (!m_ShouldAuthenticate) - { - cRoot::Get()->AuthenticateUser(a_ClientID); - return; - } - - cCSLock Lock(m_CS); - m_Queue.push_back(cUser(a_ClientID, a_UserName, a_ServerHash)); - m_QueueNonempty.Set(); -} - - - - - -void cAuthenticator::Start(cIniFile & IniFile) -{ - ReadINI(IniFile); - m_ShouldTerminate = false; - super::Start(); -} - - - - - -void cAuthenticator::Stop(void) -{ - m_ShouldTerminate = true; - m_QueueNonempty.Set(); - Wait(); -} - - - - - -void cAuthenticator::Execute(void) -{ - for (;;) - { - cCSLock Lock(m_CS); - while (!m_ShouldTerminate && (m_Queue.size() == 0)) - { - cCSUnlock Unlock(Lock); - m_QueueNonempty.Wait(); - } - if (m_ShouldTerminate) - { - return; - } - ASSERT(!m_Queue.empty()); - - int ClientID = m_Queue.front().m_ClientID; - AString UserName = m_Queue.front().m_Name; - AString ActualAddress = m_Address; - ReplaceString(ActualAddress, "%USERNAME%", UserName); - ReplaceString(ActualAddress, "%SERVERID%", m_Queue.front().m_ServerID); - m_Queue.pop_front(); - Lock.Unlock(); - - if (!AuthFromAddress(m_Server, ActualAddress, UserName)) - { - cRoot::Get()->KickUser(ClientID, "Failed to authenticate account!"); - } - else - { - cRoot::Get()->AuthenticateUser(ClientID); - } - } // for (-ever) -} - - - - - -bool cAuthenticator::AuthFromAddress(const AString & a_Server, const AString & a_Address, const AString & a_UserName, int a_Level /* = 1 */) -{ - // Returns true if the user authenticated okay, false on error; iLevel is the recursion deptht (bails out if too deep) - - cBlockingTCPLink Link; - if (!Link.Connect(a_Server.c_str(), 80)) - { - LOGWARNING("%s: cannot connect to auth server \"%s\", kicking user \"%s\"", - __FUNCTION__, a_Server.c_str(), a_UserName.c_str() - ); - return false; - } - - Link.SendMessage( AString( "GET " + a_Address + " HTTP/1.1\r\n" ).c_str()); - Link.SendMessage( AString( "User-Agent: MCServer\r\n" ).c_str()); - Link.SendMessage( AString( "Host: " + a_Server + "\r\n" ).c_str()); - //Link.SendMessage( AString( "Host: session.minecraft.net\r\n" ).c_str()); - Link.SendMessage( AString( "Accept: */*\r\n" ).c_str()); - Link.SendMessage( AString( "Connection: close\r\n" ).c_str()); //Close so we donīt have to mess with the Content-Length :) - Link.SendMessage( AString( "\r\n" ).c_str()); - AString DataRecvd; - Link.ReceiveData(DataRecvd); - Link.CloseSocket(); - - std::stringstream ss(DataRecvd); - - // Parse the data received: - std::string temp; - ss >> temp; - bool bRedirect = false; - bool bOK = false; - if ((temp.compare("HTTP/1.1") == 0) || (temp.compare("HTTP/1.0") == 0)) - { - int code; - ss >> code; - if (code == 302) - { - // redirect blabla - LOGD("%s: Need to redirect, current level %d!", __FUNCTION__, a_Level); - if (a_Level > MAX_REDIRECTS) - { - LOGERROR("cAuthenticator: received too many levels of redirection from auth server \"%s\" for user \"%s\", bailing out and kicking the user", a_Server.c_str(), a_UserName.c_str()); - return false; - } - bRedirect = true; - } - else if (code == 200) - { - LOGD("cAuthenticator: Received status 200 OK! :D"); - bOK = true; - } - } - else - { - LOGERROR("cAuthenticator: cannot parse auth reply from server \"%s\" for user \"%s\", kicking the user.", a_Server.c_str(), a_UserName.c_str()); - return false; - } - - if( bRedirect ) - { - AString Location; - // Search for "Location:" - bool bFoundLocation = false; - while( !bFoundLocation && ss.good() ) - { - char c = 0; - while( c != '\n' ) - { - ss.get( c ); - } - AString Name; - ss >> Name; - if (Name.compare("Location:") == 0) - { - bFoundLocation = true; - ss >> Location; - } - } - if (!bFoundLocation) - { - LOGERROR("cAuthenticator: received invalid redirection from auth server \"%s\" for user \"%s\", kicking user.", a_Server.c_str(), a_UserName.c_str()); - return false; - } - - Location = Location.substr(strlen("http://"), std::string::npos); // Strip http:// - std::string Server = Location.substr( 0, Location.find( "/" ) ); // Only leave server address - Location = Location.substr( Server.length(), std::string::npos); - return AuthFromAddress(Server, Location, a_UserName, a_Level + 1); - } - - if (!bOK) - { - LOGERROR("cAuthenticator: received an error from auth server \"%s\" for user \"%s\", kicking user.", a_Server.c_str(), a_UserName.c_str()); - return false; - } - - // Header says OK, so receive the rest. - // Go past header, double \n means end of headers - char c = 0; - while (ss.good()) - { - while (c != '\n') - { - ss.get(c); - } - ss.get(c); - if( c == '\n' || c == '\r' || ss.peek() == '\r' || ss.peek() == '\n' ) - break; - } - if (!ss.good()) - { - LOGERROR("cAuthenticator: error while parsing response body from auth server \"%s\" for user \"%s\", kicking user.", a_Server.c_str(), a_UserName.c_str()); - return false; - } - - std::string Result; - ss >> Result; - LOGD("cAuthenticator: Authentication result was %s", Result.c_str()); - - if (Result.compare("YES") == 0) //Works well - { - LOGINFO("Authentication result \"YES\", player authentication success!"); - return true; - } - - - LOGINFO("Authentication result was \"%s\", player authentication failure!", Result.c_str()); - return false; -} - - - - diff --git a/src/Authenticator.h b/src/Authenticator.h deleted file mode 100644 index 02cd6f4c5..000000000 --- a/src/Authenticator.h +++ /dev/null @@ -1,93 +0,0 @@ - -// cAuthenticator.h - -// Interfaces to the cAuthenticator class representing the thread that authenticates users against the official MC server -// Authentication prevents "hackers" from joining with an arbitrary username (possibly impersonating the server admins) -// For more info, see http://wiki.vg/Session#Server_operation -// In MCS, authentication is implemented as a single thread that receives queued auth requests and dispatches them one by one. - - - - - -#pragma once -#ifndef CAUTHENTICATOR_H_INCLUDED -#define CAUTHENTICATOR_H_INCLUDED - -#include "OSSupport/IsThread.h" - - - - - -// fwd: "cRoot.h" -class cRoot; - - - - - -class cAuthenticator : - public cIsThread -{ - typedef cIsThread super; - -public: - cAuthenticator(void); - ~cAuthenticator(); - - /// (Re-)read server and address from INI: - void ReadINI(cIniFile & IniFile); - - /// Queues a request for authenticating a user. If the auth fails, the user is kicked - void Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash); - - /// Starts the authenticator thread. The thread may be started and stopped repeatedly - void Start(cIniFile & IniFile); - - /// Stops the authenticator thread. The thread may be started and stopped repeatedly - void Stop(void); - -private: - - class cUser - { - public: - int m_ClientID; - AString m_Name; - AString m_ServerID; - - cUser(int a_ClientID, const AString & a_Name, const AString & a_ServerID) : - m_ClientID(a_ClientID), - m_Name(a_Name), - m_ServerID(a_ServerID) - { - } - } ; - - typedef std::deque cUserList; - - cCriticalSection m_CS; - cUserList m_Queue; - cEvent m_QueueNonempty; - - AString m_Server; - AString m_Address; - bool m_ShouldAuthenticate; - - // cIsThread override: - virtual void Execute(void) override; - - // Returns true if the user authenticated okay, false on error; iLevel is the recursion deptht (bails out if too deep) - bool AuthFromAddress(const AString & a_Server, const AString & a_Address, const AString & a_UserName, int a_Level = 1); -}; - - - - - -#endif // CAUTHENTICATOR_H_INCLUDED - - - - diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 5876e55c7..615039949 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -24,7 +24,7 @@ #include "Root.h" -#include "Authenticator.h" +#include "Protocol/Authenticator.h" #include "MersenneTwister.h" #include "Protocol/ProtocolRecognizer.h" @@ -188,7 +188,7 @@ void cClientHandle::Kick(const AString & a_Reason) -void cClientHandle::Authenticate(void) +void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID) { if (m_State != csAuthenticating) { @@ -197,6 +197,9 @@ void cClientHandle::Authenticate(void) ASSERT( m_Player == NULL ); + m_Username = a_Name; + m_UUID = a_UUID; + // Spawn player (only serversided, so data is loaded) m_Player = new cPlayer(this, GetUsername()); diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 0c367ec7d..0f07c980b 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -62,8 +62,11 @@ public: cPlayer* GetPlayer() { return m_Player; } // tolua_export + const AString & GetUUID(void) const { return m_UUID; } // tolua_export + void setUUID(const AString & a_UUID) { m_UUID = a_UUID; } + void Kick(const AString & a_Reason); // tolua_export - void Authenticate(void); // Called by cAuthenticator when the user passes authentication + void Authenticate(const AString & a_Name, const AString & a_UUID); // Called by cAuthenticator when the user passes authentication void StreamChunks(void); @@ -326,6 +329,7 @@ private: static int s_ClientCount; int m_UniqueID; + AString m_UUID; /** Set to true when the chunk where the player is is sent to the client. Used for spawning the player */ bool m_HasSentPlayerChunk; diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp new file mode 100644 index 000000000..ba435e9ce --- /dev/null +++ b/src/Protocol/Authenticator.cpp @@ -0,0 +1,325 @@ + +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules + +#include "Authenticator.h" +#include "OSSupport/BlockingTCPLink.h" +#include "Root.h" +#include "Server.h" + +#include "inifile/iniFile.h" +#include "json/json.h" + +#include "polarssl/config.h" +#include "polarssl/net.h" +#include "polarssl/ssl.h" +#include "polarssl/entropy.h" +#include "polarssl/ctr_drbg.h" +#include "polarssl/error.h" +#include "polarssl/certs.h" + +#include +#include + + + + + +#define DEFAULT_AUTH_SERVER "sessionserver.mojang.com" +#define DEFAULT_AUTH_ADDRESS "/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%" +#define MAX_REDIRECTS 10 + + + + + +cAuthenticator::cAuthenticator(void) : +super("cAuthenticator"), +m_Server(DEFAULT_AUTH_SERVER), +m_Address(DEFAULT_AUTH_ADDRESS), +m_ShouldAuthenticate(true) +{ +} + + + + + +cAuthenticator::~cAuthenticator() +{ + Stop(); +} + + + + + +void cAuthenticator::ReadINI(cIniFile & IniFile) +{ + m_Server = IniFile.GetValueSet("Authentication", "Server", DEFAULT_AUTH_SERVER); + m_Address = IniFile.GetValueSet("Authentication", "Address", DEFAULT_AUTH_ADDRESS); + m_ShouldAuthenticate = IniFile.GetValueSetB("Authentication", "Authenticate", true); +} + + + + + +void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, const AString & a_ServerHash) +{ + if (!m_ShouldAuthenticate) + { + cRoot::Get()->AuthenticateUser(a_ClientID, a_UserName, Printf("%d", a_ClientID)); + return; + } + + cCSLock LOCK(m_CS); + m_Queue.push_back(cUser(a_ClientID, a_UserName, a_ServerHash)); + m_QueueNonempty.Set(); +} + + + + + +void cAuthenticator::Start(cIniFile & IniFile) +{ + ReadINI(IniFile); + m_ShouldTerminate = false; + super::Start(); +} + + + + + +void cAuthenticator::Stop(void) +{ + m_ShouldTerminate = true; + m_QueueNonempty.Set(); + Wait(); +} + + + + + +void cAuthenticator::Execute(void) +{ + for (;;) + { + cCSLock Lock(m_CS); + while (!m_ShouldTerminate && (m_Queue.size() == 0)) + { + cCSUnlock Unlock(Lock); + m_QueueNonempty.Wait(); + } + if (m_ShouldTerminate) + { + return; + } + ASSERT(!m_Queue.empty()); + + int ClientID = m_Queue.front().m_ClientID; + AString UserName = m_Queue.front().m_Name; + AString ServerID = m_Queue.front().m_ServerID; + m_Queue.pop_front(); + Lock.Unlock(); + + AString NewUserName = UserName; + AString UUID; + if (AuthWithYggdrasil(NewUserName, ServerID, UUID)) + { + LOGINFO("User %s authenticated with UUID '%s'", NewUserName.c_str(), UUID.c_str()); + cRoot::Get()->AuthenticateUser(ClientID, NewUserName, UUID); + } + else + { + cRoot::Get()->KickUser(ClientID, "Failed to authenticate account!"); + } + } // for (-ever) +} + + + + + +bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_ServerId, AString & a_UUID) +{ + AString REQUEST; + int ret, len, server_fd = -1; + unsigned char buf[1024]; + const char *pers = "cAuthenticator"; + + entropy_context entropy; + ctr_drbg_context ctr_drbg; + ssl_context ssl; + x509_crt cacert; + + /* Initialize the RNG and the session data */ + memset(&ssl, 0, sizeof(ssl_context)); + x509_crt_init(&cacert); + + entropy_init(&entropy); + if ((ret = ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (const unsigned char *)pers, strlen(pers))) != 0) + { + LOGERROR("cAuthenticator: ctr_drbg_init returned %d", ret); + return false; + } + + /* Initialize certificates */ + +#if defined(POLARSSL_CERTS_C) + ret = x509_crt_parse(&cacert, (const unsigned char *)test_ca_list, strlen(test_ca_list)); +#else + ret = 1; + LOGWARNING("cAuthenticator: POLARSSL_CERTS_C is not defined."); +#endif + + if (ret < 0) + { + LOGERROR("cAuthenticator: x509_crt_parse returned -0x%x", -ret); + return false; + } + + /* Connect */ + if ((ret = net_connect(&server_fd, m_Server.c_str(), 443)) != 0) + { + LOGERROR("cAuthenticator: Can't connect to %s: %d", m_Server.c_str(), ret); + return false; + } + + /* Setup */ + if ((ret = ssl_init(&ssl)) != 0) + { + LOGERROR("cAuthenticator: ssl_init returned %d", ret); + return false; + } + ssl_set_endpoint(&ssl, SSL_IS_CLIENT); + ssl_set_authmode(&ssl, SSL_VERIFY_OPTIONAL); + ssl_set_ca_chain(&ssl, &cacert, NULL, "PolarSSL Server 1"); + ssl_set_rng(&ssl, ctr_drbg_random, &ctr_drbg); + ssl_set_bio(&ssl, net_recv, &server_fd, net_send, &server_fd); + + /* Handshake */ + while ((ret = ssl_handshake(&ssl)) != 0) + { + if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) + { + LOGERROR("cAuthenticator: ssl_handshake returned -0x%x", -ret); + return false; + } + } + + /* Write the GET request */ + AString ActualAddress = m_Address; + ReplaceString(ActualAddress, "%USERNAME%", a_UserName); + ReplaceString(ActualAddress, "%SERVERID%", a_ServerId); + + REQUEST += "GET " + ActualAddress + " HTTP/1.1\r\n"; + REQUEST += "Host: " + m_Server + "\r\n"; + REQUEST += "User-Agent: MCServer\r\n"; + REQUEST += "Connection: close\r\n"; + REQUEST += "\r\n"; + + len = sprintf_s((char *)buf, sizeof(buf), REQUEST.c_str()); + + while ((ret = ssl_write(&ssl, buf, len)) <= 0) + { + if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) + { + LOGERROR("cAuthenticator: ssl_write returned %d", ret); + return false; + } + } + + /* Read the HTTP response */ + std::string Builder; + for (;;) + { + len = sizeof(buf)-1; + memset(buf, 0, sizeof(buf)); + ret = ssl_read(&ssl, buf, len); + if (ret > 0) + { + buf[ret] = '\0'; + } + + if (ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE) + continue; + if (ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) + break; + if (ret < 0) + { + LOGERROR("cAuthenticator: ssl_read returned %d", ret); + break; + } + if (ret == 0) + { + LOGERROR("cAuthenticator: EOF"); + break; + } + + std::string str; + str.append(reinterpret_cast(buf)); + Builder += str; + } + + ssl_close_notify(&ssl); + x509_crt_free(&cacert); + net_close(server_fd); + ssl_free(&ssl); + entropy_free(&entropy); + memset(&ssl, 0, sizeof(ssl)); + + std::string prefix("HTTP/1.1 200 OK"); + if (Builder.compare(0, prefix.size(), prefix)) + return false; + + std::stringstream ResponseBuilder; + bool NewLine = false; + bool IsNewLine = false; + for (std::string::const_iterator i = Builder.begin(); i <= Builder.end(); ++i) + { + if (NewLine) + { + ResponseBuilder << *i; + } + else if (!NewLine && *i == '\n') + { + if (IsNewLine) + { + NewLine = true; + } + else + { + IsNewLine = true; + } + } + else if (*i != '\r') + { + IsNewLine = false; + } + } + + AString RESPONSE = ResponseBuilder.str(); + + if (RESPONSE.empty()) + return false; + + Json::Value root; + Json::Reader reader; + if (!reader.parse(RESPONSE, root, false)) + { + LOGWARNING("cAuthenticator: Cannot parse Received Data to json!"); + return false; + } + + a_UserName = root.get("name", "Unknown").asString(); + a_UUID = root.get("id", "").asString(); + return true; +} + + + + + diff --git a/src/Protocol/Authenticator.h b/src/Protocol/Authenticator.h new file mode 100644 index 000000000..211f51394 --- /dev/null +++ b/src/Protocol/Authenticator.h @@ -0,0 +1,93 @@ + +// cAuthenticator.h + +// Interfaces to the cAuthenticator class representing the thread that authenticates users against the official MC server +// Authentication prevents "hackers" from joining with an arbitrary username (possibly impersonating the server admins) +// For more info, see http://wiki.vg/Session#Server_operation +// In MCS, authentication is implemented as a single thread that receives queued auth requests and dispatches them one by one. + + + + + +#pragma once +#ifndef CAUTHENTICATOR_H_INCLUDED +#define CAUTHENTICATOR_H_INCLUDED + +#include "../OSSupport/IsThread.h" + + + + + +// fwd: "cRoot.h" +class cRoot; + + + + + +class cAuthenticator : + public cIsThread +{ + typedef cIsThread super; + +public: + cAuthenticator(void); + ~cAuthenticator(); + + /** (Re-)read server and address from INI: */ + void ReadINI(cIniFile & IniFile); + + /** 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); + + /** Starts the authenticator thread. The thread may be started and stopped repeatedly */ + void Start(cIniFile & IniFile); + + /** Stops the authenticator thread. The thread may be started and stopped repeatedly */ + void Stop(void); + +private: + + class cUser + { + public: + int m_ClientID; + AString m_Name; + AString m_ServerID; + + cUser(int a_ClientID, const AString & a_Name, const AString & a_ServerID) : + m_ClientID(a_ClientID), + m_Name(a_Name), + m_ServerID(a_ServerID) + { + } + }; + + typedef std::deque cUserList; + + cCriticalSection m_CS; + cUserList m_Queue; + cEvent m_QueueNonempty; + + AString m_Server; + AString m_Address; + bool m_ShouldAuthenticate; + + /** cIsThread override: */ + virtual void Execute(void) override; + + /** Returns true if the user authenticated okay, false on error; iLevel is the recursion deptht (bails out if too deep) */ + bool AuthWithYggdrasil(AString & a_UserName, const AString & a_ServerId, AString & a_UUID); +}; + + + + + +#endif // CAUTHENTICATOR_H_INCLUDED + + + + diff --git a/src/Root.cpp b/src/Root.cpp index ba4398b35..5d32bdd87 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -499,9 +499,9 @@ void cRoot::KickUser(int a_ClientID, const AString & a_Reason) -void cRoot::AuthenticateUser(int a_ClientID) +void cRoot::AuthenticateUser(int a_ClientID, const AString & a_Name, const AString & a_UUID) { - m_Server->AuthenticateUser(a_ClientID); + m_Server->AuthenticateUser(a_ClientID, a_Name, a_UUID); } diff --git a/src/Root.h b/src/Root.h index 4bbd7586f..d2a4d1eed 100644 --- a/src/Root.h +++ b/src/Root.h @@ -1,7 +1,7 @@ #pragma once -#include "Authenticator.h" +#include "Protocol/Authenticator.h" #include "HTTPServer/HTTPServer.h" #include "Defines.h" @@ -89,7 +89,7 @@ public: void KickUser(int a_ClientID, const AString & a_Reason); /// Called by cAuthenticator to auth the specified user - void AuthenticateUser(int a_ClientID); + void AuthenticateUser(int a_ClientID, const AString & a_Name, const AString & a_UUID); /// Executes commands queued in the command queue void TickCommands(void); diff --git a/src/Server.cpp b/src/Server.cpp index d1e53bfff..e0feaf0d8 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -615,14 +615,14 @@ void cServer::KickUser(int a_ClientID, const AString & a_Reason) -void cServer::AuthenticateUser(int a_ClientID) +void cServer::AuthenticateUser(int a_ClientID, const AString & a_Name, const AString & a_UUID) { cCSLock Lock(m_CSClients); for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr) { if ((*itr)->GetUniqueID() == a_ClientID) { - (*itr)->Authenticate(); + (*itr)->Authenticate(a_Name, a_UUID); return; } } // for itr - m_Clients[] diff --git a/src/Server.h b/src/Server.h index b5280c59d..b5c384a44 100644 --- a/src/Server.h +++ b/src/Server.h @@ -83,7 +83,7 @@ public: // tolua_export void Shutdown(void); void KickUser(int a_ClientID, const AString & a_Reason); - void AuthenticateUser(int a_ClientID); // Called by cAuthenticator to auth the specified user + void AuthenticateUser(int a_ClientID, const AString & a_Name, const AString & a_UUID); // Called by cAuthenticator to auth the specified user const AString & GetServerID(void) const { return m_ServerID; } // tolua_export -- cgit v1.2.3 From 3733ee2c0ee84c054bb98d714dad49d0384208a0 Mon Sep 17 00:00:00 2001 From: daniel0916 Date: Sun, 13 Apr 2014 15:32:15 +0200 Subject: Code Update --- src/Protocol/Authenticator.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index ba435e9ce..24695b726 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -26,7 +26,6 @@ #define DEFAULT_AUTH_SERVER "sessionserver.mojang.com" #define DEFAULT_AUTH_ADDRESS "/session/minecraft/hasJoined?username=%USERNAME%&serverId=%SERVERID%" -#define MAX_REDIRECTS 10 @@ -146,8 +145,9 @@ void cAuthenticator::Execute(void) bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_ServerId, AString & a_UUID) { AString REQUEST; - int ret, len, server_fd = -1; - unsigned char buf[1024]; + int ret, server_fd = -1; + size_t len = -1; + unsigned char * buf; const char *pers = "cAuthenticator"; entropy_context entropy; @@ -221,7 +221,8 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S REQUEST += "Connection: close\r\n"; REQUEST += "\r\n"; - len = sprintf_s((char *)buf, sizeof(buf), REQUEST.c_str()); + len = REQUEST.size(); + buf = (unsigned char *)REQUEST.c_str(); while ((ret = ssl_write(&ssl, buf, len)) <= 0) { -- cgit v1.2.3 From d258be678a7ea2d027434c2acc51243dd2253beb Mon Sep 17 00:00:00 2001 From: daniel0916 Date: Sun, 13 Apr 2014 16:15:57 +0200 Subject: Fixed Error? --- src/Protocol/Authenticator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index 24695b726..a79f0b7e0 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -147,7 +147,7 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S AString REQUEST; int ret, server_fd = -1; size_t len = -1; - unsigned char * buf; + unsigned char buf[1024]; const char *pers = "cAuthenticator"; entropy_context entropy; @@ -222,7 +222,7 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S REQUEST += "\r\n"; len = REQUEST.size(); - buf = (unsigned char *)REQUEST.c_str(); + strcpy((char *)buf, REQUEST.c_str()); while ((ret = ssl_write(&ssl, buf, len)) <= 0) { -- cgit v1.2.3 From 2618569e0100fdfdbf07f98da8f3e0f83e82b822 Mon Sep 17 00:00:00 2001 From: daniel0916 Date: Sun, 13 Apr 2014 17:09:18 +0200 Subject: Fixed mistake --- src/Protocol/Authenticator.cpp | 2 +- src/Protocol/Protocol17x.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index a79f0b7e0..8943b3c21 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -146,7 +146,7 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S { AString REQUEST; int ret, server_fd = -1; - size_t len = -1; + size_t len = 0; unsigned char buf[1024]; const char *pers = "cAuthenticator"; diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index a4319df37..6c1af61fa 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -1559,7 +1559,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe // Send login success: { cPacketizer Pkt(*this, 0x02); // Login success packet - Pkt.WriteString(Printf("%d", m_Client->GetUniqueID())); // TODO: proper UUID + Pkt.WriteString(m_Client->GetUUID()); Pkt.WriteString(m_Client->GetUsername()); } -- cgit v1.2.3 From 0f55dcf036381f951fb22219158182729453f841 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 14 Apr 2014 18:52:21 +0200 Subject: Initial 1.7.6 protocol support. Doesn't work yet because of missing UUIDs. --- src/Protocol/Protocol17x.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/Protocol/Protocol17x.h | 17 +++++++++++++++++ src/Protocol/ProtocolRecognizer.cpp | 13 +++++++++++++ src/Protocol/ProtocolRecognizer.h | 5 +++-- 4 files changed, 69 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index a4319df37..57cc9118f 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -2755,3 +2755,39 @@ void cProtocol172::cPacketizer::WriteEntityProperties(const cEntity & a_Entity) + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cProtocol176: + +cProtocol176::cProtocol176(cClientHandle * a_Client, const AString &a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : + super(a_Client, a_ServerAddress, a_ServerPort, a_State) +{ +} + + + + + +void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) +{ + // Called to spawn another player for the client + cPacketizer Pkt(*this, 0x0c); // Spawn Player packet + Pkt.WriteVarInt(a_Player.GetUniqueID()); + Pkt.WriteString(Printf("00000000-0000-3000-8000-aaaa%08x", a_Player.GetUniqueID())); // TODO: Proper UUID + Pkt.WriteString(a_Player.GetName()); + Pkt.WriteVarInt(0); // We have no data to send here + Pkt.WriteFPInt(a_Player.GetPosX()); + Pkt.WriteFPInt(a_Player.GetPosY()); + Pkt.WriteFPInt(a_Player.GetPosZ()); + Pkt.WriteByteAngle(a_Player.GetYaw()); + Pkt.WriteByteAngle(a_Player.GetPitch()); + short ItemType = a_Player.GetEquippedItem().IsEmpty() ? 0 : a_Player.GetEquippedItem().m_ItemType; + Pkt.WriteShort(ItemType); + Pkt.WriteByte((3 << 5) | 6); // Metadata: float + index 6 + Pkt.WriteFloat((float)a_Player.GetHealth()); + Pkt.WriteByte(0x7f); // Metadata: end +} + + + + diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 91186b270..7d0196a21 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -306,3 +306,20 @@ protected: + +/** The version 5 lengthed protocol, used by 1.7.6 through 1.7.9. */ +class cProtocol176 : + public cProtocol172 +{ + typedef cProtocol172 super; + +public: + cProtocol176(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); + + // cProtocol172 overrides: + virtual void SendPlayerSpawn(const cPlayer & a_Player) override; +} ; + + + + diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 3f7d7b254..6a1f2fbbd 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -59,6 +59,7 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) case PROTO_VERSION_1_6_3: return "1.6.3"; case PROTO_VERSION_1_6_4: return "1.6.4"; case PROTO_VERSION_1_7_2: return "1.7.2"; + case PROTO_VERSION_1_7_6: return "1.7.6"; } ASSERT(!"Unknown protocol version"); return Printf("Unknown protocol (%d)", a_ProtocolVersion); @@ -965,6 +966,18 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema m_Protocol = new cProtocol172(m_Client, ServerAddress, (UInt16)ServerPort, NextState); return true; } + case PROTO_VERSION_1_7_6: + { + AString ServerAddress; + short ServerPort; + UInt32 NextState; + m_Buffer.ReadVarUTF8String(ServerAddress); + m_Buffer.ReadBEShort(ServerPort); + m_Buffer.ReadVarInt(NextState); + m_Buffer.CommitRead(); + m_Protocol = new cProtocol176(m_Client, ServerAddress, (UInt16)ServerPort, NextState); + return true; + } } LOGINFO("Client \"%s\" uses an unsupported protocol (lengthed, version %u)", m_Client->GetIPString().c_str(), ProtocolVersion diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 072d7c2d2..e53fc9514 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -18,8 +18,8 @@ // Adjust these if a new protocol is added or an old one is removed: -#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.7.2, 1.7.4" -#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78, 4" +#define MCS_CLIENT_VERSIONS "1.2.4, 1.2.5, 1.3.1, 1.3.2, 1.4.2, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.5, 1.5.1, 1.5.2, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.7.2, 1.7.4, 1.7.5, 1.7.6, 1.7.7, 1.7.8, 1.7.9" +#define MCS_PROTOCOL_VERSIONS "29, 39, 47, 49, 51, 60, 61, 73, 74, 77, 78, 4, 5" @@ -50,6 +50,7 @@ public: // These will be kept "under" the next / latest, because the next and latest are only needed for previous protocols PROTO_VERSION_1_7_2 = 4, + PROTO_VERSION_1_7_6 = 5, } ; cProtocolRecognizer(cClientHandle * a_Client); -- cgit v1.2.3 From d505ffc704d85cc1b31ae87647ba979fda84f46f Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 14 Apr 2014 20:21:00 +0200 Subject: A client UUID is generated when the server is in offline mode. 1.7.9 client works with these changes in offline mode. --- src/ClientHandle.cpp | 24 ++++++++++++++++++++++++ src/ClientHandle.h | 7 ++++++- src/Protocol/Protocol17x.cpp | 14 ++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 615039949..7e8c11d67 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -31,6 +31,8 @@ #include "CompositeChat.h" #include "Items/ItemSword.h" +#include "md5/md5.h" + /** Maximum number of explosions to send this tick, server will start dropping if exceeded */ @@ -175,6 +177,28 @@ void cClientHandle::Destroy(void) +void cClientHandle::GenerateOfflineUUID(void) +{ + // Proper format for a version 3 UUID is: + // xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B + + // Generate an md5 checksum, and use it as base for the ID: + MD5 Checksum(m_Username); + m_UUID = Checksum.hexdigest(); + m_UUID[12] = '3'; // Version 3 UUID + m_UUID[16] = '8'; // Variant 1 UUID + + // Now the digest doesn't have the UUID slashes, but the client requires them, so add them into the appropriate positions: + m_UUID.insert(8, "-"); + m_UUID.insert(13, "-"); + m_UUID.insert(18, "-"); + m_UUID.insert(23, "-"); +} + + + + + void cClientHandle::Kick(const AString & a_Reason) { if (m_State >= csAuthenticating) // Don't log pings diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 0f07c980b..90b971e16 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -63,7 +63,12 @@ public: cPlayer* GetPlayer() { return m_Player; } // tolua_export const AString & GetUUID(void) const { return m_UUID; } // tolua_export - void setUUID(const AString & a_UUID) { m_UUID = a_UUID; } + void SetUUID(const AString & a_UUID) { m_UUID = a_UUID; } + + /** Generates an UUID based on the player name provided. + This is used for the offline (non-auth) mode, when there's no UUID source. + Each username generates a unique and constant UUID, so that when the player reconnects with the same name, their UUID is the same. */ + void GenerateOfflineUUID(void); void Kick(const AString & a_Reason); // tolua_export void Authenticate(const AString & a_Name, const AString & a_UUID); // Called by cAuthenticator when the user passes authentication diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 278b29485..b44c9f919 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -88,8 +88,9 @@ cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAdd // Create the comm log file, if so requested: if (g_ShouldLogCommIn || g_ShouldLogCommOut) { + static int sCounter = 0; cFile::CreateFolder("CommLogs"); - AString FileName = Printf("CommLogs/%x__%s.log", (unsigned)time(NULL), a_Client->GetIPString().c_str()); + AString FileName = Printf("CommLogs/%x_%d__%s.log", (unsigned)time(NULL), sCounter++, a_Client->GetIPString().c_str()); m_CommLogFile.Open(FileName, cFile::fmWrite); } } @@ -796,7 +797,7 @@ void cProtocol172::SendPlayerSpawn(const cPlayer & a_Player) // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet Pkt.WriteVarInt(a_Player.GetUniqueID()); - Pkt.WriteString(Printf("%d", a_Player.GetUniqueID())); // TODO: Proper UUID + Pkt.WriteString(a_Player.GetClientHandle()->GetUUID()); Pkt.WriteString(a_Player.GetName()); Pkt.WriteFPInt(a_Player.GetPosX()); Pkt.WriteFPInt(a_Player.GetPosY()); @@ -1556,6 +1557,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe StartEncryption(DecryptedKey); + /* // Send login success: { cPacketizer Pkt(*this, 0x02); // Login success packet @@ -1565,6 +1567,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe m_State = 3; // State = Game m_Client->HandleLogin(4, m_Client->GetUsername()); + */ } @@ -1596,10 +1599,13 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) return; } + // Generate an offline UUID for the player: + m_Client->GenerateOfflineUUID(); + // Send login success: { cPacketizer Pkt(*this, 0x02); // Login success packet - Pkt.WriteString(Printf("%d", m_Client->GetUniqueID())); // TODO: proper UUID + Pkt.WriteString(m_Client->GetUUID()); Pkt.WriteString(Username); } @@ -2773,7 +2779,7 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) // Called to spawn another player for the client cPacketizer Pkt(*this, 0x0c); // Spawn Player packet Pkt.WriteVarInt(a_Player.GetUniqueID()); - Pkt.WriteString(Printf("00000000-0000-3000-8000-aaaa%08x", a_Player.GetUniqueID())); // TODO: Proper UUID + Pkt.WriteString(a_Player.GetClientHandle()->GetUUID()); Pkt.WriteString(a_Player.GetName()); Pkt.WriteVarInt(0); // We have no data to send here Pkt.WriteFPInt(a_Player.GetPosX()); -- cgit v1.2.3 From d12d7b671523f79f760403f3f4c0ef4efb497c79 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 14 Apr 2014 22:52:59 +0200 Subject: Implemented the 1.7.6 protocol and authenticator. Server works both in online and offline modes with 1.7.9. --- src/ClientHandle.cpp | 30 ++++++-- src/ClientHandle.h | 8 +- src/Protocol/Authenticator.cpp | 147 ++++++++++++++++-------------------- src/Protocol/Protocol.h | 1 + src/Protocol/Protocol125.cpp | 42 +++++++++++ src/Protocol/Protocol125.h | 20 +---- src/Protocol/Protocol17x.cpp | 62 +++++++++------ src/Protocol/Protocol17x.h | 5 +- src/Protocol/ProtocolRecognizer.cpp | 10 +++ src/Protocol/ProtocolRecognizer.h | 1 + 10 files changed, 198 insertions(+), 128 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 7e8c11d67..79738ff0b 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -178,21 +178,32 @@ void cClientHandle::Destroy(void) void cClientHandle::GenerateOfflineUUID(void) +{ + m_UUID = GenerateOfflineUUID(m_Username); +} + + + + + +AString cClientHandle::GenerateOfflineUUID(const AString & a_Username) { // Proper format for a version 3 UUID is: // xxxxxxxx-xxxx-3xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B // Generate an md5 checksum, and use it as base for the ID: - MD5 Checksum(m_Username); - m_UUID = Checksum.hexdigest(); - m_UUID[12] = '3'; // Version 3 UUID - m_UUID[16] = '8'; // Variant 1 UUID + MD5 Checksum(a_Username); + AString UUID = Checksum.hexdigest(); + UUID[12] = '3'; // Version 3 UUID + UUID[16] = '8'; // Variant 1 UUID // Now the digest doesn't have the UUID slashes, but the client requires them, so add them into the appropriate positions: - m_UUID.insert(8, "-"); - m_UUID.insert(13, "-"); - m_UUID.insert(18, "-"); - m_UUID.insert(23, "-"); + UUID.insert(8, "-"); + UUID.insert(13, "-"); + UUID.insert(18, "-"); + UUID.insert(23, "-"); + + return UUID; } @@ -223,6 +234,9 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID) m_Username = a_Name; m_UUID = a_UUID; + + // Send login success (if the protocol supports it): + m_Protocol->SendLoginSuccess(); // Spawn player (only serversided, so data is loaded) m_Player = new cPlayer(this, GetUsername()); diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 90b971e16..12e0256de 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -65,10 +65,16 @@ public: const AString & GetUUID(void) const { return m_UUID; } // tolua_export void SetUUID(const AString & a_UUID) { m_UUID = a_UUID; } + /** Generates an UUID based on the username stored for this client, and stores it in the m_UUID member. + This is used for the offline (non-auth) mode, when there's no UUID source. + Each username generates a unique and constant UUID, so that when the player reconnects with the same name, their UUID is the same. + Internally calls the GenerateOfflineUUID static function. */ + void GenerateOfflineUUID(void); + /** Generates an UUID based on the player name provided. This is used for the offline (non-auth) mode, when there's no UUID source. Each username generates a unique and constant UUID, so that when the player reconnects with the same name, their UUID is the same. */ - void GenerateOfflineUUID(void); + static AString GenerateOfflineUUID(const AString & a_Username); // tolua_export void Kick(const AString & a_Reason); // tolua_export void Authenticate(const AString & a_Name, const AString & a_UUID); // Called by cAuthenticator when the user passes authentication diff --git a/src/Protocol/Authenticator.cpp b/src/Protocol/Authenticator.cpp index 8943b3c21..a30131b36 100644 --- a/src/Protocol/Authenticator.cpp +++ b/src/Protocol/Authenticator.cpp @@ -2,9 +2,10 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Authenticator.h" -#include "OSSupport/BlockingTCPLink.h" -#include "Root.h" -#include "Server.h" +#include "../OSSupport/BlockingTCPLink.h" +#include "../Root.h" +#include "../Server.h" +#include "../ClientHandle.h" #include "inifile/iniFile.h" #include "json/json.h" @@ -32,10 +33,10 @@ cAuthenticator::cAuthenticator(void) : -super("cAuthenticator"), -m_Server(DEFAULT_AUTH_SERVER), -m_Address(DEFAULT_AUTH_ADDRESS), -m_ShouldAuthenticate(true) + super("cAuthenticator"), + m_Server(DEFAULT_AUTH_SERVER), + m_Address(DEFAULT_AUTH_ADDRESS), + m_ShouldAuthenticate(true) { } @@ -67,7 +68,7 @@ void cAuthenticator::Authenticate(int a_ClientID, const AString & a_UserName, co { if (!m_ShouldAuthenticate) { - cRoot::Get()->AuthenticateUser(a_ClientID, a_UserName, Printf("%d", a_ClientID)); + cRoot::Get()->AuthenticateUser(a_ClientID, a_UserName, cClientHandle::GenerateOfflineUUID(a_UserName)); return; } @@ -144,9 +145,9 @@ void cAuthenticator::Execute(void) bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_ServerId, AString & a_UUID) { - AString REQUEST; + LOGD("Trying to auth user %s", a_UserName.c_str()); + int ret, server_fd = -1; - size_t len = 0; unsigned char buf[1024]; const char *pers = "cAuthenticator"; @@ -162,36 +163,31 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S entropy_init(&entropy); if ((ret = ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (const unsigned char *)pers, strlen(pers))) != 0) { - LOGERROR("cAuthenticator: ctr_drbg_init returned %d", ret); + LOGWARNING("cAuthenticator: ctr_drbg_init returned %d", ret); return false; } /* Initialize certificates */ - -#if defined(POLARSSL_CERTS_C) + // TODO: Grab the sessionserver's root CA and any intermediates and hard-code them here, instead of test_ca_list ret = x509_crt_parse(&cacert, (const unsigned char *)test_ca_list, strlen(test_ca_list)); -#else - ret = 1; - LOGWARNING("cAuthenticator: POLARSSL_CERTS_C is not defined."); -#endif if (ret < 0) { - LOGERROR("cAuthenticator: x509_crt_parse returned -0x%x", -ret); + LOGWARNING("cAuthenticator: x509_crt_parse returned -0x%x", -ret); return false; } /* Connect */ if ((ret = net_connect(&server_fd, m_Server.c_str(), 443)) != 0) { - LOGERROR("cAuthenticator: Can't connect to %s: %d", m_Server.c_str(), ret); + LOGWARNING("cAuthenticator: Can't connect to %s: %d", m_Server.c_str(), ret); return false; } /* Setup */ if ((ret = ssl_init(&ssl)) != 0) { - LOGERROR("cAuthenticator: ssl_init returned %d", ret); + LOGWARNING("cAuthenticator: ssl_init returned %d", ret); return false; } ssl_set_endpoint(&ssl, SSL_IS_CLIENT); @@ -203,9 +199,9 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S /* Handshake */ while ((ret = ssl_handshake(&ssl)) != 0) { - if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) + if ((ret != POLARSSL_ERR_NET_WANT_READ) && (ret != POLARSSL_ERR_NET_WANT_WRITE)) { - LOGERROR("cAuthenticator: ssl_handshake returned -0x%x", -ret); + LOGWARNING("cAuthenticator: ssl_handshake returned -0x%x", -ret); return false; } } @@ -215,54 +211,47 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S ReplaceString(ActualAddress, "%USERNAME%", a_UserName); ReplaceString(ActualAddress, "%SERVERID%", a_ServerId); - REQUEST += "GET " + ActualAddress + " HTTP/1.1\r\n"; - REQUEST += "Host: " + m_Server + "\r\n"; - REQUEST += "User-Agent: MCServer\r\n"; - REQUEST += "Connection: close\r\n"; - REQUEST += "\r\n"; - - len = REQUEST.size(); - strcpy((char *)buf, REQUEST.c_str()); + AString Request; + Request += "GET " + ActualAddress + " HTTP/1.1\r\n"; + Request += "Host: " + m_Server + "\r\n"; + Request += "User-Agent: MCServer\r\n"; + Request += "Connection: close\r\n"; + Request += "\r\n"; - while ((ret = ssl_write(&ssl, buf, len)) <= 0) + ret = ssl_write(&ssl, (const unsigned char *)Request.c_str(), Request.size()); + if (ret <= 0) { - if (ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE) - { - LOGERROR("cAuthenticator: ssl_write returned %d", ret); - return false; - } + LOGWARNING("cAuthenticator: ssl_write returned %d", ret); + return false; } /* Read the HTTP response */ - std::string Builder; + std::string Response; for (;;) { - len = sizeof(buf)-1; memset(buf, 0, sizeof(buf)); - ret = ssl_read(&ssl, buf, len); - if (ret > 0) - { - buf[ret] = '\0'; - } + ret = ssl_read(&ssl, buf, sizeof(buf)); - if (ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE) + if ((ret == POLARSSL_ERR_NET_WANT_READ) || (ret == POLARSSL_ERR_NET_WANT_WRITE)) + { continue; + } if (ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) + { break; + } if (ret < 0) { - LOGERROR("cAuthenticator: ssl_read returned %d", ret); + LOGWARNING("cAuthenticator: ssl_read returned %d", ret); break; } if (ret == 0) { - LOGERROR("cAuthenticator: EOF"); + LOGWARNING("cAuthenticator: EOF"); break; } - std::string str; - str.append(reinterpret_cast(buf)); - Builder += str; + Response.append((const char *)buf, ret); } ssl_close_notify(&ssl); @@ -272,51 +261,49 @@ bool cAuthenticator::AuthWithYggdrasil(AString & a_UserName, const AString & a_S entropy_free(&entropy); memset(&ssl, 0, sizeof(ssl)); - std::string prefix("HTTP/1.1 200 OK"); - if (Builder.compare(0, prefix.size(), prefix)) + // Check the HTTP status line: + AString prefix("HTTP/1.1 200 OK"); + AString HexDump; + if (Response.compare(0, prefix.size(), prefix)) + { + LOGINFO("User \"%s\" failed to auth, bad http status line received", a_UserName.c_str()); + LOG("Response: \n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str()); return false; + } - std::stringstream ResponseBuilder; - bool NewLine = false; - bool IsNewLine = false; - for (std::string::const_iterator i = Builder.begin(); i <= Builder.end(); ++i) + // Erase the HTTP headers from the response: + size_t idxHeadersEnd = Response.find("\r\n\r\n"); + if (idxHeadersEnd == AString::npos) { - if (NewLine) - { - ResponseBuilder << *i; - } - else if (!NewLine && *i == '\n') - { - if (IsNewLine) - { - NewLine = true; - } - else - { - IsNewLine = true; - } - } - else if (*i != '\r') - { - IsNewLine = false; - } + LOGINFO("User \"%s\" failed to authenticate, bad http response header received", a_UserName.c_str()); + LOG("Response: \n%s", CreateHexDump(HexDump, Response.data(), Response.size(), 16).c_str()); + return false; } + Response.erase(0, idxHeadersEnd + 4); - AString RESPONSE = ResponseBuilder.str(); - - if (RESPONSE.empty()) + // Parse the Json response: + if (Response.empty()) + { return false; - + } Json::Value root; Json::Reader reader; - if (!reader.parse(RESPONSE, root, false)) + if (!reader.parse(Response, root, false)) { LOGWARNING("cAuthenticator: Cannot parse Received Data to json!"); return false; } - a_UserName = root.get("name", "Unknown").asString(); a_UUID = root.get("id", "").asString(); + + // If the UUID doesn't contain the hashes, insert them at the proper places: + if (a_UUID.size() == 32) + { + a_UUID.insert(8, "-"); + a_UUID.insert(13, "-"); + a_UUID.insert(18, "-"); + a_UUID.insert(23, "-"); + } return true; } diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 939170f0e..2fbeef0fa 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -83,6 +83,7 @@ public: virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) = 0; virtual void SendKeepAlive (int a_PingID) = 0; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) = 0; + virtual void SendLoginSuccess (void) = 0; virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) = 0; virtual void SendMapDecorators (int a_ID, const cMapDecoratorList & a_Decorators) = 0; virtual void SendMapInfo (int a_ID, unsigned int a_Scale) = 0; diff --git a/src/Protocol/Protocol125.cpp b/src/Protocol/Protocol125.cpp index bf946ef19..af1b1c604 100644 --- a/src/Protocol/Protocol125.cpp +++ b/src/Protocol/Protocol125.cpp @@ -594,6 +594,15 @@ void cProtocol125::SendLogin(const cPlayer & a_Player, const cWorld & a_World) +void cProtocol125::SendLoginSuccess(void) +{ + // Not supported in this protocol version +} + + + + + void cProtocol125::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) { cCSLock Lock(m_CSPacket); @@ -642,6 +651,17 @@ void cProtocol125::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decor +void cProtocol125::SendMapInfo(int a_ID, unsigned int a_Scale) +{ + // This protocol doesn't support such message + UNUSED(a_ID); + UNUSED(a_Scale); +} + + + + + void cProtocol125::SendPickupSpawn(const cPickup & a_Pickup) { cCSLock Lock(m_CSPacket); @@ -683,6 +703,16 @@ void cProtocol125::SendParticleEffect(const AString & a_ParticleName, float a_Sr +void cProtocol125::SendPaintingSpawn(const cPainting & a_Painting) +{ + // Not implemented in this protocol version + UNUSED(a_Painting); +} + + + + + void cProtocol125::SendPlayerListItem(const cPlayer & a_Player, bool a_IsOnline) { cCSLock Lock(m_CSPacket); @@ -842,6 +872,18 @@ void cProtocol125::SendExperienceOrb(const cExpOrb & a_ExpOrb) +void cProtocol125::SendScoreboardObjective(const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) +{ + // This protocol version doesn't support such message + UNUSED(a_Name); + UNUSED(a_DisplayName); + UNUSED(a_Mode); +} + + + + + void cProtocol125::SendSoundEffect(const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) { // Not needed in this protocol version diff --git a/src/Protocol/Protocol125.h b/src/Protocol/Protocol125.h index 08d3ebbe9..16f31bd0e 100644 --- a/src/Protocol/Protocol125.h +++ b/src/Protocol/Protocol125.h @@ -56,19 +56,12 @@ public: virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; virtual void SendKeepAlive (int a_PingID) override; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; + virtual void SendLoginSuccess (void) override; virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override; virtual void SendMapDecorators (int a_ID, const cMapDecoratorList & a_Decorators) override; - virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override - { - // This protocol doesn't support such message - UNUSED(a_ID); - UNUSED(a_Scale); - } + virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override; virtual void SendParticleEffect (const AString & a_ParticleName, float a_SrcX, float a_SrcY, float a_SrcZ, float a_OffsetX, float a_OffsetY, float a_OffsetZ, float a_ParticleData, int a_ParticleAmmount) override; - virtual void SendPaintingSpawn (const cPainting & a_Painting) override - { - UNUSED(a_Painting); - }; + virtual void SendPaintingSpawn (const cPainting & a_Painting) override; virtual void SendPickupSpawn (const cPickup & a_Pickup) override; virtual void SendPlayerAbilities (void) override {} // This protocol doesn't support such message virtual void SendEntityAnimation (const cEntity & a_Entity, char a_Animation) override; @@ -82,12 +75,7 @@ public: virtual void SendRespawn (void) override; virtual void SendExperience (void) override; virtual void SendExperienceOrb (const cExpOrb & a_ExpOrb) override; - virtual void SendScoreboardObjective (const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) override - { - UNUSED(a_Name); - UNUSED(a_DisplayName); - UNUSED(a_Mode); - } // This protocol doesn't support such message + virtual void SendScoreboardObjective (const AString & a_Name, const AString & a_DisplayName, Byte a_Mode) override; virtual void SendScoreUpdate (const AString & a_Objective, const AString & a_Player, cObjective::Score a_Score, Byte a_Mode) override {} // This protocol doesn't support such message virtual void SendDisplayObjective (const AString & a_Objective, cScoreboard::eDisplaySlot a_Display) override {} // This protocol doesn't support such message virtual void SendSoundEffect (const AString & a_SoundName, int a_SrcX, int a_SrcY, int a_SrcZ, float a_Volume, float a_Pitch) override; // a_Src coords are Block * 8 diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index b44c9f919..c8105c03a 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -574,6 +574,21 @@ void cProtocol172::SendLogin(const cPlayer & a_Player, const cWorld & a_World) +void cProtocol172::SendLoginSuccess(void) +{ + ASSERT(m_State == 2); // State: login? + + cPacketizer Pkt(*this, 0x02); // Login success packet + Pkt.WriteString(m_Client->GetUUID()); + Pkt.WriteString(m_Client->GetUsername()); + + m_State = 3; // State = Game +} + + + + + void cProtocol172::SendPaintingSpawn(const cPainting & a_Painting) { cPacketizer Pkt(*this, 0x10); // Spawn Painting packet @@ -1556,18 +1571,7 @@ void cProtocol172::HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffe } StartEncryption(DecryptedKey); - - /* - // Send login success: - { - cPacketizer Pkt(*this, 0x02); // Login success packet - Pkt.WriteString(m_Client->GetUUID()); - Pkt.WriteString(m_Client->GetUsername()); - } - - m_State = 3; // State = Game m_Client->HandleLogin(4, m_Client->GetUsername()); - */ } @@ -1599,17 +1603,6 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) return; } - // Generate an offline UUID for the player: - m_Client->GenerateOfflineUUID(); - - // Send login success: - { - cPacketizer Pkt(*this, 0x02); // Login success packet - Pkt.WriteString(m_Client->GetUUID()); - Pkt.WriteString(Username); - } - - m_State = 3; // State = Game m_Client->HandleLogin(4, Username); } @@ -2797,3 +2790,28 @@ void cProtocol176::SendPlayerSpawn(const cPlayer & a_Player) + +void cProtocol176::HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) +{ + // Send the response: + AString Response = "{\"version\":{\"name\":\"1.7.6\",\"protocol\":5},\"players\":{"; + AppendPrintf(Response, "\"max\":%u,\"online\":%u,\"sample\":[]},", + cRoot::Get()->GetServer()->GetMaxPlayers(), + cRoot::Get()->GetServer()->GetNumPlayers() + ); + AppendPrintf(Response, "\"description\":{\"text\":\"%s\"},", + cRoot::Get()->GetServer()->GetDescription().c_str() + ); + AppendPrintf(Response, "\"favicon\":\"data:image/png;base64,%s\"", + cRoot::Get()->GetServer()->GetFaviconData().c_str() + ); + Response.append("}"); + + cPacketizer Pkt(*this, 0x00); // Response packet + Pkt.WriteString(Response); +} + + + + + diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 7d0196a21..5cafc4722 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -87,6 +87,7 @@ public: virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; virtual void SendKeepAlive (int a_PingID) override; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; + virtual void SendLoginSuccess (void) override; virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override; virtual void SendMapDecorators (int a_ID, const cMapDecoratorList & a_Decorators) override; virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override; @@ -252,7 +253,7 @@ protected: // Packet handlers while in the Status state (m_State == 1): void HandlePacketStatusPing (cByteBuffer & a_ByteBuffer); - void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer); + virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer); // Packet handlers while in the Login state (m_State == 2): void HandlePacketLoginEncryptionResponse(cByteBuffer & a_ByteBuffer); @@ -318,6 +319,8 @@ public: // cProtocol172 overrides: virtual void SendPlayerSpawn(const cPlayer & a_Player) override; + virtual void HandlePacketStatusRequest(cByteBuffer & a_ByteBuffer) override; + } ; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 6a1f2fbbd..8a97b9713 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -397,6 +397,16 @@ void cProtocolRecognizer::SendLogin(const cPlayer & a_Player, const cWorld & a_W +void cProtocolRecognizer::SendLoginSuccess(void) +{ + ASSERT(m_Protocol != NULL); + m_Protocol->SendLoginSuccess(); +} + + + + + void cProtocolRecognizer::SendMapColumn(int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) { ASSERT(m_Protocol != NULL); diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index e53fc9514..408109ef4 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -91,6 +91,7 @@ public: virtual void SendInventorySlot (char a_WindowID, short a_SlotNum, const cItem & a_Item) override; virtual void SendKeepAlive (int a_PingID) override; virtual void SendLogin (const cPlayer & a_Player, const cWorld & a_World) override; + virtual void SendLoginSuccess (void) override; virtual void SendMapColumn (int a_ID, int a_X, int a_Y, const Byte * a_Colors, unsigned int a_Length) override; virtual void SendMapDecorators (int a_ID, const cMapDecoratorList & a_Decorators) override; virtual void SendMapInfo (int a_ID, unsigned int a_Scale) override; -- cgit v1.2.3 From 146824cb68193fd20a1f1dd441f8ceb7803e363a Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 13 Apr 2014 17:26:15 +0200 Subject: NetherFortPrefabs: Fixed a connector in MidStaircase. --- src/Generating/Prefabs/NetherFortPrefabs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Generating/Prefabs/NetherFortPrefabs.cpp b/src/Generating/Prefabs/NetherFortPrefabs.cpp index 29b35a5a4..164bd9557 100644 --- a/src/Generating/Prefabs/NetherFortPrefabs.cpp +++ b/src/Generating/Prefabs/NetherFortPrefabs.cpp @@ -3367,7 +3367,7 @@ const cPrefab::sDef g_NetherFortPrefabs[] = // Connectors: "1: 12, 1, 6: 5\n" /* Type 1, direction X+ */ - "1: -1, 1, 6: 4\n" /* Type 1, direction X- */, + "1: 0, 1, 6: 4\n" /* Type 1, direction X- */, // AllowedRotations: 7, /* 1, 2, 3 CCW rotations */ -- cgit v1.2.3 From b5ea5fbf905c0917594188635b11d577d4e27f5c Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 13 Apr 2014 17:27:36 +0200 Subject: Added support for per-piece per-placement weights in cPieceGenerator. Ref.: #753. --- src/Generating/PieceGenerator.cpp | 56 ++++++++++++++++++++------------------- src/Generating/PieceGenerator.h | 20 +++++++++++++- 2 files changed, 48 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/Generating/PieceGenerator.cpp b/src/Generating/PieceGenerator.cpp index 8999a5ff7..8e9a48be6 100644 --- a/src/Generating/PieceGenerator.cpp +++ b/src/Generating/PieceGenerator.cpp @@ -392,14 +392,17 @@ bool cPieceGenerator::TryPlacePieceAtConnector( Connections.reserve(AvailablePieces.size()); Vector3i ConnPos = a_Connector.m_Pos; // The position at which the new connector should be placed - 1 block away from the connector AddFaceDirection(ConnPos.x, ConnPos.y, ConnPos.z, a_Connector.m_Direction); - - /* - // DEBUG: - printf("Placing piece at connector pos {%d, %d, %d}, direction %s\n", ConnPos.x, ConnPos.y, ConnPos.z, BlockFaceToString(a_Connector.m_Direction).c_str()); - //*/ - + int WeightTotal = 0; for (cPieces::iterator itrP = AvailablePieces.begin(), endP = AvailablePieces.end(); itrP != endP; ++itrP) { + // Get the relative chance of this piece being generated in this path: + int Weight = m_PiecePool.GetPieceWeight(a_ParentPiece, a_Connector, **itrP); + if (Weight <= 0) + { + continue; + } + + // Try fitting each of the piece's connector: cPiece::cConnectors Connectors = (*itrP)->GetConnectors(); for (cPiece::cConnectors::iterator itrC = Connectors.begin(), endC = Connectors.end(); itrC != endC; ++itrC) { @@ -419,7 +422,9 @@ bool cPieceGenerator::TryPlacePieceAtConnector( // Doesn't fit in this rotation continue; } - Connections.push_back(cConnection(**itrP, *itrC, NumCCWRotations)); + // Fits, add it to list of possibile connections: + Connections.push_back(cConnection(**itrP, *itrC, NumCCWRotations, Weight)); + WeightTotal += Weight; } // for itrC - Connectors[] } // for itrP - AvailablePieces[] if (Connections.empty()) @@ -427,21 +432,23 @@ bool cPieceGenerator::TryPlacePieceAtConnector( // No available connections, bail out return false; } + ASSERT(WeightTotal > 0); - // Choose a random connection from the list: - int rnd = m_Noise.IntNoise3DInt(a_Connector.m_Pos.x, a_Connector.m_Pos.y, a_Connector.m_Pos.z) / 7; - cConnection & Conn = Connections[rnd % Connections.size()]; + // Choose a random connection from the list, based on the weights: + int rnd = (m_Noise.IntNoise3DInt(a_Connector.m_Pos.x, a_Connector.m_Pos.y, a_Connector.m_Pos.z) / 7) % WeightTotal; + size_t ChosenIndex = 0; + for (cConnections::const_iterator itr = Connections.begin(), end = Connections.end(); itr != end; ++itr, ++ChosenIndex) + { + rnd -= itr->m_Weight; + if (rnd <= 0) + { + // This is the piece to choose + break; + } + } + cConnection & Conn = Connections[ChosenIndex]; // Place the piece: - /* - // DEBUG - printf("Chosen connector at {%d, %d, %d}, direction %s, needs %d rotations\n", - Conn.m_Connector.m_Pos.x, Conn.m_Connector.m_Pos.y, Conn.m_Connector.m_Pos.z, - BlockFaceToString(Conn.m_Connector.m_Direction).c_str(), - Conn.m_NumCCWRotations - ); - //*/ - Vector3i NewPos = Conn.m_Piece->RotatePos(Conn.m_Connector.m_Pos, Conn.m_NumCCWRotations); ConnPos -= NewPos; cPlacedPiece * PlacedPiece = new cPlacedPiece(&a_ParentPiece, *(Conn.m_Piece), ConnPos, Conn.m_NumCCWRotations); @@ -449,12 +456,6 @@ bool cPieceGenerator::TryPlacePieceAtConnector( // Add the new piece's connectors to the list of free connectors: cPiece::cConnectors Connectors = Conn.m_Piece->GetConnectors(); - - /* - // DEBUG: - printf("Adding %u connectors to the pool\n", Connectors.size() - 1); - //*/ - for (cPiece::cConnectors::const_iterator itr = Connectors.begin(), end = Connectors.end(); itr != end; ++itr) { if (itr->m_Pos.Equals(Conn.m_Connector.m_Pos)) @@ -524,10 +525,11 @@ void cPieceGenerator::DebugConnectorPool(const cPieceGenerator::cFreeConnectors /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cPieceGenerator::cConnection: -cPieceGenerator::cConnection::cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations) : +cPieceGenerator::cConnection::cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations, int a_Weight) : m_Piece(&a_Piece), m_Connector(a_Connector), - m_NumCCWRotations(a_NumCCWRotations) + m_NumCCWRotations(a_NumCCWRotations), + m_Weight(a_Weight) { } diff --git a/src/Generating/PieceGenerator.h b/src/Generating/PieceGenerator.h index bef9d3463..f4433b947 100644 --- a/src/Generating/PieceGenerator.h +++ b/src/Generating/PieceGenerator.h @@ -85,6 +85,13 @@ typedef std::vector cPieces; +// fwd: +class cPlacedPiece; + + + + + /** This class is an interface that provides pieces for the generator. It can keep track of what pieces were placed and adjust the returned piece vectors. */ class cPiecePool @@ -101,6 +108,16 @@ public: Multiple starting points are supported, one of the returned piece will be chosen. */ virtual cPieces GetStartingPieces(void) = 0; + /** Returns the relative weight with which the a_NewPiece is to be selected for placing under a_PlacedPiece through a_ExistingConnector. + This allows the pool to tweak the piece's chances, based on the previous pieces in the tree and the connector used. + The higher the number returned, the higher the chance the piece will be chosen. 0 means the piece will never be chosen. + */ + virtual int GetPieceWeight( + const cPlacedPiece & a_PlacedPiece, + const cPiece::cConnector & a_ExistingConnector, + const cPiece & a_NewPiece + ) { return 1; } + /** Called after a piece is placed, to notify the pool that it has been used. The pool may adjust the pieces it will return the next time. */ virtual void PiecePlaced(const cPiece & a_Piece) = 0; @@ -157,8 +174,9 @@ protected: cPiece * m_Piece; // The piece being connected cPiece::cConnector m_Connector; // The piece's connector being used (relative non-rotated coords) int m_NumCCWRotations; // Number of rotations necessary to match the two connectors + int m_Weight; // Relative chance that this connection will be chosen - cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations); + cConnection(cPiece & a_Piece, cPiece::cConnector & a_Connector, int a_NumCCWRotations, int a_Weight); }; typedef std::vector cConnections; -- cgit v1.2.3 From 99e4225269776d4e79b7d0d14dc8bbe11a2ad32d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 15 Apr 2014 23:40:06 +0200 Subject: Attempted fix for the client crash with the new protocols. --- src/ClientHandle.cpp | 15 +++++++++------ src/Protocol/Protocol17x.cpp | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 79738ff0b..9ab32d6ec 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -1718,13 +1718,16 @@ void cClientHandle::Tick(float a_Dt) } // Send a ping packet: - cTimer t1; - if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime())) + if (m_State == csPlaying) { - m_PingID++; - m_PingStartTime = t1.GetNowTime(); - m_Protocol->SendKeepAlive(m_PingID); - m_LastPingTime = m_PingStartTime; + cTimer t1; + if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime())) + { + m_PingID++; + m_PingStartTime = t1.GetNowTime(); + m_Protocol->SendKeepAlive(m_PingID); + m_LastPingTime = m_PingStartTime; + } } // Handle block break animation: diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index c8105c03a..aae87f994 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -538,6 +538,13 @@ void cProtocol172::SendInventorySlot(char a_WindowID, short a_SlotNum, const cIt void cProtocol172::SendKeepAlive(int a_PingID) { + // Drop the packet if the protocol is not in the Game state yet (caused a client crash): + if (m_State != 3) + { + LOGWARNING("Trying to send a KeepAlive packet to a player who's not yet fully logged in (%d). The protocol class prevented the packet.", m_State); + return; + } + cPacketizer Pkt(*this, 0x00); // Keep Alive packet Pkt.WriteInt(a_PingID); } -- cgit v1.2.3