mirror of
https://github.com/smartcmd/MinecraftConsoles.git
synced 2026-08-20 09:57:09 +00:00
734 lines
22 KiB
C++
734 lines
22 KiB
C++
#include "stdafx.h"
|
||
#include "PendingConnection.h"
|
||
#include "PlayerConnection.h"
|
||
#include "ServerConnection.h"
|
||
#include "ServerPlayer.h"
|
||
#include "ServerPlayerGameMode.h"
|
||
#include "ServerLevel.h"
|
||
#include "PlayerList.h"
|
||
#include "MinecraftServer.h"
|
||
#include "..\Minecraft.World\net.minecraft.network.h"
|
||
#include "..\Minecraft.World\pos.h"
|
||
#include "..\Minecraft.World\net.minecraft.world.level.dimension.h"
|
||
#include "..\Minecraft.World\net.minecraft.world.level.storage.h"
|
||
#include "..\Minecraft.World\net.minecraft.world.item.h"
|
||
#include "..\Minecraft.World\SharedConstants.h"
|
||
#include "..\Minecraft.World\GameUUID.h"
|
||
#include "..\Minecraft.World\AuthSchemePacket.h"
|
||
#include "..\Minecraft.World\AuthResponsePacket.h"
|
||
#include "..\Minecraft.World\AuthResultPacket.h"
|
||
#include "Settings.h"
|
||
#include <thread>
|
||
#ifdef _WINDOWS64
|
||
#include "..\newauth\include\newauth.h"
|
||
#endif
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
#include "..\Minecraft.Server\ServerLogManager.h"
|
||
#include "..\Minecraft.Server\Access\Access.h"
|
||
#include "..\Minecraft.World\Socket.h"
|
||
#endif
|
||
// #ifdef __PS3__
|
||
// #include "PS3\Network\NetworkPlayerSony.h"
|
||
// #endif
|
||
|
||
Random *PendingConnection::random = new Random();
|
||
|
||
#ifdef _WINDOWS64
|
||
bool g_bRejectDuplicateNames = true;
|
||
#endif
|
||
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
namespace
|
||
{
|
||
static unsigned char GetPendingConnectionSmallId(Connection *connection)
|
||
{
|
||
if (connection != nullptr)
|
||
{
|
||
Socket *socket = connection->getSocket();
|
||
if (socket != nullptr)
|
||
{
|
||
return socket->getSmallId();
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
}
|
||
#endif
|
||
|
||
PendingConnection::PendingConnection(MinecraftServer *server, Socket *socket, const wstring& id)
|
||
{
|
||
// 4J - added initialisers
|
||
done = false;
|
||
_tick = 0;
|
||
name = L"";
|
||
acceptedLogin = nullptr;
|
||
loginKey = L"";
|
||
|
||
this->server = server;
|
||
connection = new Connection(socket, id, this);
|
||
connection->fakeLag = FAKE_LAG;
|
||
}
|
||
|
||
PendingConnection::~PendingConnection()
|
||
{
|
||
if (m_authVerifyResult)
|
||
m_authVerifyResult->cancelled.store(true, std::memory_order_release);
|
||
delete connection;
|
||
}
|
||
|
||
void PendingConnection::tick()
|
||
{
|
||
if (acceptedLogin != nullptr)
|
||
{
|
||
this->handleAcceptedLogin(acceptedLogin);
|
||
acceptedLogin = nullptr;
|
||
}
|
||
|
||
if (m_authState == eAuth_Verifying && m_authVerifyResult &&
|
||
m_authVerifyResult->ready.load(std::memory_order_acquire))
|
||
{
|
||
auto verifyResult = m_authVerifyResult;
|
||
bool authSuccess = false;
|
||
std::string uuid;
|
||
std::string uname;
|
||
std::vector<uint8_t> skinData;
|
||
std::string skinUuid;
|
||
std::string errMsg;
|
||
|
||
{
|
||
std::lock_guard<std::mutex> lock(verifyResult->mutex);
|
||
authSuccess = verifyResult->success;
|
||
if (authSuccess)
|
||
{
|
||
uuid = verifyResult->uuid;
|
||
uname = verifyResult->username;
|
||
skinData = std::move(verifyResult->skinData);
|
||
skinUuid = verifyResult->skinUuid;
|
||
}
|
||
else
|
||
{
|
||
errMsg = verifyResult->errorMessage;
|
||
}
|
||
}
|
||
|
||
m_authVerifyResult.reset();
|
||
|
||
if (authSuccess)
|
||
{
|
||
if (m_authAssignedUuid.empty())
|
||
{
|
||
m_authAssignedUuid = std::wstring(uuid.begin(), uuid.end());
|
||
m_authAssignedUsername = std::wstring(uname.begin(), uname.end());
|
||
}
|
||
|
||
std::string skinKeyUuid = skinUuid.empty() ? uuid : skinUuid;
|
||
std::string skinKey;
|
||
#ifdef _WINDOWS64
|
||
if (server->authProvider == "elyby")
|
||
skinKey = newauth::MakeElybySkinKey(skinKeyUuid);
|
||
else
|
||
skinKey = newauth::MakeSkinKey(skinKeyUuid);
|
||
#endif
|
||
m_authSkinKey = std::wstring(skinKey.begin(), skinKey.end());
|
||
m_authSkinData = std::move(skinData);
|
||
|
||
name = m_authAssignedUsername;
|
||
|
||
send(std::make_shared<AuthResultPacket>(true, m_authAssignedUuid,
|
||
m_authAssignedUsername, L"", m_authSkinKey, m_authSkinData));
|
||
m_authState = eAuth_WaitingAck;
|
||
app.DebugPrintf("Auth: Verification succeeded for %ls\n", m_authAssignedUsername.c_str());
|
||
}
|
||
else
|
||
{
|
||
std::wstring wErrMsg(errMsg.begin(), errMsg.end());
|
||
app.DebugPrintf("Auth: Verification failed: %s\n", errMsg.c_str());
|
||
send(std::make_shared<AuthResultPacket>(false, L"", L"", wErrMsg));
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
}
|
||
}
|
||
|
||
if (_tick++ == MAX_TICKS_BEFORE_LOGIN)
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_LoginTooLong);
|
||
}
|
||
else
|
||
{
|
||
connection->tick();
|
||
}
|
||
}
|
||
|
||
void PendingConnection::disconnect(DisconnectPacket::eDisconnectReason reason)
|
||
{
|
||
// try { // 4J - removed try/catch
|
||
// logger.info("Disconnecting " + getName() + ": " + reason);
|
||
app.DebugPrintf("Pending connection disconnect: %d\n", reason );
|
||
connection->send(std::make_shared<DisconnectPacket>(reason));
|
||
connection->sendAndQuit();
|
||
done = true;
|
||
// } catch (Exception e) {
|
||
// e.printStackTrace();
|
||
// }
|
||
}
|
||
|
||
void PendingConnection::handlePreLogin(shared_ptr<PreLoginPacket> packet)
|
||
{
|
||
if (packet->m_netcodeVersion != MINECRAFT_NET_VERSION)
|
||
{
|
||
app.DebugPrintf("Netcode version is %d not equal to %d\n", packet->m_netcodeVersion, MINECRAFT_NET_VERSION);
|
||
if (packet->m_netcodeVersion > MINECRAFT_NET_VERSION)
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_OutdatedServer);
|
||
}
|
||
else
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_OutdatedClient);
|
||
}
|
||
return;
|
||
}
|
||
// printf("Server: handlePreLogin\n");
|
||
name = packet->loginKey; // 4J Stu - Change from the login packet as we know better on client end during the pre-login packet
|
||
sendPreLoginResponse();
|
||
}
|
||
|
||
void PendingConnection::sendPreLoginResponse()
|
||
{
|
||
// 4J Stu - Calculate the players with UGC privileges set
|
||
PlayerUID *ugcXuids = new PlayerUID[MINECRAFT_NET_MAX_PLAYERS];
|
||
DWORD ugcXuidCount = 0;
|
||
DWORD hostIndex = 0;
|
||
BYTE ugcFriendsOnlyBits = 0;
|
||
char szUniqueMapName[14];
|
||
|
||
StorageManager.GetSaveUniqueFilename(szUniqueMapName);
|
||
|
||
PlayerList *playerList = MinecraftServer::getInstance()->getPlayers();
|
||
for(auto& player : playerList->players)
|
||
{
|
||
// If the offline Xuid is invalid but the online one is not then that's guest which we should ignore
|
||
// If the online Xuid is invalid but the offline one is not then we are definitely an offline game so dont care about UGC
|
||
|
||
// PADDY - this is failing when a local player with chat restrictions joins an online game
|
||
|
||
if( player != nullptr && player->connection->m_offlineXUID != INVALID_XUID && player->connection->m_onlineXUID != INVALID_XUID )
|
||
{
|
||
if( player->connection->m_friendsOnlyUGC )
|
||
{
|
||
ugcFriendsOnlyBits |= (1<<ugcXuidCount);
|
||
}
|
||
// Need to use the online XUID otherwise friend checks will fail on the client
|
||
ugcXuids[ugcXuidCount] = player->connection->m_onlineXUID;
|
||
|
||
if( player->connection->getNetworkPlayer() != nullptr && player->connection->getNetworkPlayer()->IsHost() ) hostIndex = ugcXuidCount;
|
||
|
||
++ugcXuidCount;
|
||
}
|
||
}
|
||
|
||
#if 0
|
||
if (false) // 4J - removed
|
||
{
|
||
loginKey = L"TOIMPLEMENT"; // 4J - todo Long.toHexString(random.nextLong());
|
||
connection->send( shared_ptr<PreLoginPacket>( new PreLoginPacket(loginKey, ugcXuids, ugcXuidCount, ugcFriendsOnlyBits, server->m_ugcPlayersVersion, szUniqueMapName,app.GetGameHostOption(eGameHostOption_All),hostIndex) ) );
|
||
}
|
||
else
|
||
#endif
|
||
{
|
||
DWORD cappedCount = (ugcXuidCount > 255u) ? 255u : ugcXuidCount;
|
||
BYTE cappedHostIndex = (hostIndex >= 255u) ? 254 : static_cast<BYTE>(hostIndex);
|
||
connection->send(std::make_shared<PreLoginPacket>(L"-", ugcXuids, cappedCount, ugcFriendsOnlyBits, server->m_ugcPlayersVersion, szUniqueMapName, app.GetGameHostOption(eGameHostOption_All), cappedHostIndex, server->m_texturePackId));
|
||
}
|
||
|
||
#ifdef _WINDOWS64
|
||
sendAuthScheme();
|
||
#endif
|
||
}
|
||
|
||
void PendingConnection::sendAuthScheme()
|
||
{
|
||
std::string hexId = GameUUID::generateV4().toUndashed();
|
||
if (hexId.size() > 20) hexId.resize(20);
|
||
m_authServerId = hexId;
|
||
|
||
vector<wstring> schemes;
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
if (server->authProvider == "elyby")
|
||
{
|
||
schemes.push_back(L"elyby");
|
||
}
|
||
else if (server->authProvider == "offline")
|
||
{
|
||
schemes.push_back(L"offline");
|
||
}
|
||
else
|
||
{
|
||
schemes.push_back(L"mojang");
|
||
}
|
||
#else
|
||
{
|
||
schemes.push_back(L"mojang");
|
||
schemes.push_back(L"elyby");
|
||
schemes.push_back(L"offline");
|
||
}
|
||
#endif
|
||
|
||
wstring wServerId(m_authServerId.begin(), m_authServerId.end());
|
||
send(std::make_shared<AuthSchemePacket>(schemes, wServerId));
|
||
m_authState = eAuth_WaitingResponse;
|
||
|
||
app.DebugPrintf("Auth: Sent AuthSchemePacket with %d scheme(s), serverId=%s\n",
|
||
(int)schemes.size(), m_authServerId.c_str());
|
||
}
|
||
|
||
void PendingConnection::handleAuthResponse(shared_ptr<AuthResponsePacket> packet)
|
||
{
|
||
if (m_authState != eAuth_WaitingResponse)
|
||
{
|
||
app.DebugPrintf("Auth: Received AuthResponsePacket in wrong state %d\n", m_authState);
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
return;
|
||
}
|
||
|
||
std::string chosenScheme(packet->chosenScheme.begin(), packet->chosenScheme.end());
|
||
std::string clientUsername(packet->username.begin(), packet->username.end());
|
||
std::string clientUuid(packet->mojangUuid.begin(), packet->mojangUuid.end());
|
||
|
||
app.DebugPrintf("Auth: Client chose scheme '%s', username='%s'\n",
|
||
chosenScheme.c_str(), clientUsername.c_str());
|
||
|
||
if (chosenScheme == "offline")
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
bool offlineAllowed = (server->authProvider == "offline");
|
||
#else
|
||
bool offlineAllowed = true;
|
||
#endif
|
||
if (!offlineAllowed)
|
||
{
|
||
send(std::make_shared<AuthResultPacket>(false, L"", L"",
|
||
L"Server does not allow offline authentication"));
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
return;
|
||
}
|
||
|
||
#if !defined(MINECRAFT_SERVER_BUILD)
|
||
// assumed that offline/singleplayer or lan worlds will auto-trust uuids.
|
||
std::string uuid = clientUuid;
|
||
if (uuid.empty())
|
||
{
|
||
GameUUID offlineUuid = GameUUID::generateOffline(clientUsername);
|
||
uuid = offlineUuid.toDashed();
|
||
}
|
||
|
||
m_authAssignedUuid = std::wstring(uuid.begin(), uuid.end());
|
||
m_authAssignedUsername = std::wstring(clientUsername.begin(), clientUsername.end());
|
||
name = m_authAssignedUsername;
|
||
|
||
send(std::make_shared<AuthResultPacket>(true, m_authAssignedUuid,
|
||
m_authAssignedUsername, L""));
|
||
m_authState = eAuth_WaitingAck;
|
||
app.DebugPrintf("Auth: Offline auth accepted (single-player), uuid=%s\n", uuid.c_str());
|
||
return;
|
||
#else
|
||
// for ded server to regenerate uuid serverside to prevent uuid forgery
|
||
GameUUID offlineUuid = GameUUID::generateOffline(clientUsername);
|
||
std::string dashedUuid = offlineUuid.toDashed();
|
||
|
||
m_authAssignedUuid = std::wstring(dashedUuid.begin(), dashedUuid.end());
|
||
m_authAssignedUsername = std::wstring(clientUsername.begin(), clientUsername.end());
|
||
name = m_authAssignedUsername;
|
||
if (!clientUuid.empty())
|
||
{
|
||
m_authState = eAuth_Verifying;
|
||
m_authVerifyResult = std::make_shared<AuthVerifyResult>();
|
||
m_authVerifyResult->success = true;
|
||
m_authVerifyResult->username = clientUsername;
|
||
m_authVerifyResult->uuid = dashedUuid;
|
||
m_authVerifyResult->skinUuid = clientUuid; // just use the user's uuid if auth'd with mojang or ely.by. ez.
|
||
|
||
std::shared_ptr<AuthVerifyResult> result = m_authVerifyResult;
|
||
std::string fetchUuid = clientUuid;
|
||
|
||
std::thread([result, fetchUuid]() {
|
||
try {
|
||
if (result->cancelled.load(std::memory_order_acquire))
|
||
return;
|
||
std::string fetchErr;
|
||
std::string skinUrl = newauth::FetchProfileSkinUrl(fetchUuid, fetchErr);
|
||
if (!skinUrl.empty())
|
||
{
|
||
auto skinPng = newauth::FetchSkinPng(skinUrl, fetchErr);
|
||
if (!skinPng.empty() && newauth::ValidateSkinPng(skinPng.data(), skinPng.size()))
|
||
{
|
||
std::lock_guard<std::mutex> lock(result->mutex);
|
||
result->skinUrl = skinUrl;
|
||
result->skinData = std::move(skinPng);
|
||
}
|
||
}
|
||
result->ready.store(true, std::memory_order_release);
|
||
} catch (...) {
|
||
result->ready.store(true, std::memory_order_release);
|
||
}
|
||
}).detach();
|
||
return;
|
||
}
|
||
|
||
send(std::make_shared<AuthResultPacket>(true, m_authAssignedUuid,
|
||
m_authAssignedUsername, L""));
|
||
m_authState = eAuth_WaitingAck;
|
||
app.DebugPrintf("Auth: Offline auth success, uuid=%s\n", dashedUuid.c_str());
|
||
return;
|
||
#endif
|
||
}
|
||
|
||
if (chosenScheme == "mojang" || chosenScheme == "elyby")
|
||
{
|
||
bool isElyby = (chosenScheme == "elyby");
|
||
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
if (server->authProvider == "offline")
|
||
{
|
||
send(std::make_shared<AuthResultPacket>(false, L"", L"",
|
||
L"Server does not allow online authentication"));
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
return;
|
||
}
|
||
|
||
bool wantElyby = (server->authProvider == "elyby");
|
||
if (isElyby != wantElyby)
|
||
{
|
||
app.DebugPrintf("Auth: Client chose '%s' but server expects '%s'\n",
|
||
chosenScheme.c_str(), server->authProvider.c_str());
|
||
send(std::make_shared<AuthResultPacket>(false, L"", L"",
|
||
L"Auth scheme mismatch"));
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
return;
|
||
}
|
||
#endif
|
||
|
||
m_authState = eAuth_Verifying;
|
||
m_authVerifyResult = std::make_shared<AuthVerifyResult>();
|
||
|
||
std::string serverId = m_authServerId;
|
||
std::string username = clientUsername;
|
||
std::shared_ptr<AuthVerifyResult> result = m_authVerifyResult;
|
||
|
||
#ifdef _WINDOWS64
|
||
std::thread([result, username, serverId, isElyby]() {
|
||
try {
|
||
if (result->cancelled.load(std::memory_order_acquire))
|
||
return;
|
||
|
||
std::string error;
|
||
newauth::HasJoinedResult hjResult;
|
||
|
||
if (isElyby)
|
||
hjResult = newauth::ElybyHasJoined(username, serverId, error);
|
||
else
|
||
hjResult = newauth::HasJoined(username, serverId, error);
|
||
|
||
if (result->cancelled.load(std::memory_order_acquire))
|
||
return;
|
||
|
||
std::vector<uint8_t> skin;
|
||
if (hjResult.success && !hjResult.skinUrl.empty())
|
||
{
|
||
std::string skinErr;
|
||
skin = newauth::FetchSkinPng(hjResult.skinUrl, skinErr);
|
||
if (!skin.empty() && !newauth::ValidateSkinPng(skin.data(), skin.size()))
|
||
skin.clear();
|
||
}
|
||
|
||
{
|
||
std::lock_guard<std::mutex> lock(result->mutex);
|
||
if (hjResult.success)
|
||
{
|
||
result->success = true;
|
||
result->username = hjResult.username;
|
||
result->uuid = hjResult.uuid;
|
||
result->skinUrl = hjResult.skinUrl;
|
||
if (!skin.empty())
|
||
result->skinData = std::move(skin);
|
||
}
|
||
else
|
||
{
|
||
result->success = false;
|
||
result->errorMessage = error.empty() ? "Authentication failed" : error;
|
||
}
|
||
}
|
||
result->ready.store(true, std::memory_order_release);
|
||
} catch (...) {
|
||
{
|
||
std::lock_guard<std::mutex> lock(result->mutex);
|
||
result->success = false;
|
||
result->errorMessage = "Internal auth error";
|
||
}
|
||
result->ready.store(true, std::memory_order_release);
|
||
}
|
||
}).detach();
|
||
#endif
|
||
return;
|
||
}
|
||
|
||
// only really a problem if the client is modified. You Know Who You
|
||
app.DebugPrintf("Auth: Unknown scheme '%s'\n", chosenScheme.c_str());
|
||
send(std::make_shared<AuthResultPacket>(false, L"", L"", L"Unknown auth scheme"));
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
}
|
||
|
||
void PendingConnection::handleLogin(shared_ptr<LoginPacket> packet)
|
||
{
|
||
#ifdef _WINDOWS64
|
||
if (m_authState != eAuth_WaitingAck && m_authState != eAuth_Done)
|
||
{
|
||
app.DebugPrintf("Auth: LoginPacket received in state %d, rejecting\n", m_authState);
|
||
disconnect(DisconnectPacket::eDisconnect_AuthFailed);
|
||
return;
|
||
}
|
||
if (m_authState == eAuth_WaitingAck)
|
||
{
|
||
m_authState = eAuth_Done;
|
||
}
|
||
#endif
|
||
|
||
// printf("Server: handleLogin\n");
|
||
//name = packet->userName;
|
||
if (packet->clientVersion != SharedConstants::NETWORK_PROTOCOL_VERSION)
|
||
{
|
||
app.DebugPrintf("Client version is %d not equal to %d\n", packet->clientVersion, SharedConstants::NETWORK_PROTOCOL_VERSION);
|
||
if (packet->clientVersion > SharedConstants::NETWORK_PROTOCOL_VERSION)
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_OutdatedServer);
|
||
}
|
||
else
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_OutdatedClient);
|
||
}
|
||
return;
|
||
}
|
||
|
||
bool sentDisconnect = false;
|
||
|
||
PlayerUID loginXuid = packet->m_offlineXuid;
|
||
if (loginXuid == INVALID_XUID) loginXuid = packet->m_onlineXuid;
|
||
|
||
bool duplicateXuid = false;
|
||
if (loginXuid != INVALID_XUID && server->getPlayers()->getPlayer(loginXuid) != nullptr)
|
||
{
|
||
duplicateXuid = true;
|
||
}
|
||
else if (packet->m_onlineXuid != INVALID_XUID &&
|
||
packet->m_onlineXuid != loginXuid &&
|
||
server->getPlayers()->getPlayer(packet->m_onlineXuid) != nullptr)
|
||
{
|
||
duplicateXuid = true;
|
||
}
|
||
|
||
bool bannedXuid = false;
|
||
if (loginXuid != INVALID_XUID)
|
||
{
|
||
bannedXuid = server->getPlayers()->isXuidBanned(loginXuid);
|
||
}
|
||
if (!bannedXuid && packet->m_onlineXuid != INVALID_XUID && packet->m_onlineXuid != loginXuid)
|
||
{
|
||
bannedXuid = server->getPlayers()->isXuidBanned(packet->m_onlineXuid);
|
||
}
|
||
|
||
bool whitelistSatisfied = true;
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
if (ServerRuntime::Access::IsWhitelistEnabled())
|
||
{
|
||
whitelistSatisfied = false;
|
||
if (loginXuid != INVALID_XUID)
|
||
{
|
||
whitelistSatisfied = ServerRuntime::Access::IsPlayerWhitelisted(loginXuid);
|
||
}
|
||
if (!whitelistSatisfied && packet->m_onlineXuid != INVALID_XUID && packet->m_onlineXuid != loginXuid)
|
||
{
|
||
whitelistSatisfied = ServerRuntime::Access::IsPlayerWhitelisted(packet->m_onlineXuid);
|
||
}
|
||
}
|
||
#endif
|
||
|
||
if( sentDisconnect )
|
||
{
|
||
// Do nothing
|
||
}
|
||
else if (bannedXuid)
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
ServerRuntime::ServerLogManager::OnRejectedPlayerLogin(GetPendingConnectionSmallId(connection), name, ServerRuntime::ServerLogManager::eLoginRejectReason_BannedXuid);
|
||
#endif
|
||
disconnect(DisconnectPacket::eDisconnect_Banned);
|
||
}
|
||
else if (!whitelistSatisfied)
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
ServerRuntime::ServerLogManager::OnRejectedPlayerLogin(GetPendingConnectionSmallId(connection), name, ServerRuntime::ServerLogManager::eLoginRejectReason_NotWhitelisted);
|
||
#endif
|
||
disconnect(DisconnectPacket::eDisconnect_Banned);
|
||
}
|
||
else if (duplicateXuid)
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
ServerRuntime::ServerLogManager::OnRejectedPlayerLogin(GetPendingConnectionSmallId(connection), name, ServerRuntime::ServerLogManager::eLoginRejectReason_DuplicateXuid);
|
||
#endif
|
||
// Reject the incoming connection — a player with this UID is already
|
||
// on the server. Allowing duplicates causes invisible players and
|
||
// other undefined behaviour.
|
||
app.DebugPrintf("LOGIN: Rejecting duplicate xuid for name: %ls\n", name.c_str());
|
||
disconnect(DisconnectPacket::eDisconnect_Banned);
|
||
}
|
||
#ifdef _WINDOWS64
|
||
else if (g_bRejectDuplicateNames)
|
||
{
|
||
bool nameTaken = false;
|
||
vector<shared_ptr<ServerPlayer> >& pl = server->getPlayers()->players;
|
||
for (const auto& i : pl)
|
||
{
|
||
if (i != nullptr && i->name == name)
|
||
{
|
||
nameTaken = true;
|
||
break;
|
||
}
|
||
}
|
||
if (nameTaken)
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
ServerRuntime::ServerLogManager::OnRejectedPlayerLogin(GetPendingConnectionSmallId(connection), name, ServerRuntime::ServerLogManager::eLoginRejectReason_DuplicateName);
|
||
#endif
|
||
app.DebugPrintf("Rejecting duplicate name: %ls\n", name.c_str());
|
||
disconnect(DisconnectPacket::eDisconnect_Banned);
|
||
}
|
||
else
|
||
{
|
||
handleAcceptedLogin(packet);
|
||
}
|
||
}
|
||
#endif
|
||
else
|
||
{
|
||
handleAcceptedLogin(packet);
|
||
}
|
||
//else
|
||
{
|
||
//4J - removed
|
||
#if 0
|
||
new Thread() {
|
||
public void run() {
|
||
try {
|
||
String key = loginKey;
|
||
URL url = new URL("http://www.minecraft.net/game/checkserver.jsp?user=" + URLEncoder.encode(packet.userName, "UTF-8") + "&serverId=" + URLEncoder.encode(key, "UTF-8"));
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
|
||
String msg = br.readLine();
|
||
br.close();
|
||
if (msg.equals("YES")) {
|
||
acceptedLogin = packet;
|
||
} else {
|
||
disconnect("Failed to verify username!");
|
||
}
|
||
} catch (Exception e) {
|
||
disconnect("Failed to verify username! [internal error " + e + "]");
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}.start();
|
||
#endif
|
||
}
|
||
|
||
}
|
||
|
||
void PendingConnection::handleAcceptedLogin(shared_ptr<LoginPacket> packet)
|
||
{
|
||
if(packet->m_ugcPlayersVersion != server->m_ugcPlayersVersion)
|
||
{
|
||
// Send the pre-login packet again with the new list of players
|
||
sendPreLoginResponse();
|
||
return;
|
||
}
|
||
|
||
// Guests use the online xuid, everyone else uses the offline one
|
||
PlayerUID playerXuid = packet->m_offlineXuid;
|
||
if(playerXuid == INVALID_XUID) playerXuid = packet->m_onlineXuid;
|
||
|
||
shared_ptr<ServerPlayer> playerEntity = server->getPlayers()->getPlayerForLogin(this, name, playerXuid,packet->m_onlineXuid);
|
||
if (playerEntity != nullptr)
|
||
{
|
||
#if defined(_WINDOWS64) && defined(MINECRAFT_SERVER_BUILD)
|
||
ServerRuntime::ServerLogManager::OnAcceptedPlayerLogin(GetPendingConnectionSmallId(connection), name);
|
||
#endif
|
||
|
||
#ifdef _WINDOWS64
|
||
// Apply auth-verified skin to the player so that placeNewPlayer can
|
||
// distribute it to all clients via the existing texture mechanism.
|
||
if (!m_authSkinKey.empty() && !m_authSkinData.empty())
|
||
{
|
||
DWORD skinBytes = (DWORD)m_authSkinData.size();
|
||
PBYTE skinCopy = new BYTE[skinBytes];
|
||
memcpy(skinCopy, m_authSkinData.data(), skinBytes);
|
||
app.AddMemoryTextureFile(m_authSkinKey, skinCopy, skinBytes);
|
||
playerEntity->customTextureUrl = m_authSkinKey;
|
||
app.DebugPrintf("Auth: Registered server-side skin '%ls' (%d bytes) for %ls\n",
|
||
m_authSkinKey.c_str(), (int)skinBytes, name.c_str());
|
||
}
|
||
#endif
|
||
|
||
server->getPlayers()->placeNewPlayer(connection, playerEntity, packet);
|
||
connection = nullptr; // We've moved responsibility for this over to the new PlayerConnection, nullptr so we don't delete our reference to it here in our dtor
|
||
}
|
||
done = true;
|
||
|
||
}
|
||
|
||
void PendingConnection::onDisconnect(DisconnectPacket::eDisconnectReason reason, void *reasonObjects)
|
||
{
|
||
// logger.info(getName() + " lost connection");
|
||
done = true;
|
||
}
|
||
|
||
void PendingConnection::handleGetInfo(shared_ptr<GetInfoPacket> packet)
|
||
{
|
||
//try {
|
||
//String message = server->motd + "<22>" + server->players->getPlayerCount() + "<22>" + server->players->getMaxPlayers();
|
||
//connection->send(new DisconnectPacket(message));
|
||
connection->send(std::make_shared<DisconnectPacket>(DisconnectPacket::eDisconnect_ServerFull));
|
||
connection->sendAndQuit();
|
||
server->connection->removeSpamProtection(connection->getSocket());
|
||
done = true;
|
||
//} catch (Exception e) {
|
||
// e.printStackTrace();
|
||
//}
|
||
}
|
||
|
||
void PendingConnection::handleKeepAlive(shared_ptr<KeepAlivePacket> packet)
|
||
{
|
||
// Ignore
|
||
}
|
||
|
||
void PendingConnection::onUnhandledPacket(shared_ptr<Packet> packet)
|
||
{
|
||
disconnect(DisconnectPacket::eDisconnect_UnexpectedPacket);
|
||
}
|
||
|
||
void PendingConnection::send(shared_ptr<Packet> packet)
|
||
{
|
||
connection->send(packet);
|
||
}
|
||
|
||
wstring PendingConnection::getName()
|
||
{
|
||
return L"Unimplemented";
|
||
// if (name != null) return name + " [" + connection.getRemoteAddress().toString() + "]";
|
||
// return connection.getRemoteAddress().toString();
|
||
}
|
||
|
||
bool PendingConnection::isServerPacketListener()
|
||
{
|
||
return true;
|
||
}
|
||
|
||
bool PendingConnection::isDisconnected()
|
||
{
|
||
return done;
|
||
}
|