Server: Secure Master packet handling

This commit is contained in:
David Markowitz 2023-10-06 01:57:18 -07:00
parent b33e9935f5
commit e10351bde6
2 changed files with 87 additions and 104 deletions

View File

@ -110,41 +110,21 @@ Packet* dServer::ReceiveFromMaster() {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) { if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
mLogger->Log("dServer", "Lost our connection to master, shutting DOWN!"); mLogger->Log("dServer", "Lost our connection to master, shutting DOWN!");
mMasterConnectionActive = false; mMasterConnectionActive = false;
mMasterPeer->DeallocatePacket(packet);
packet = nullptr;
//ConnectToMaster(); //We'll just shut down now //ConnectToMaster(); //We'll just shut down now
} } else if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) {
if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) {
mLogger->Log("dServer", "Established connection to master, zone (%i), instance (%i)", this->GetZoneID(), this->GetInstanceID()); mLogger->Log("dServer", "Established connection to master, zone (%i), instance (%i)", this->GetZoneID(), this->GetInstanceID());
mMasterConnectionActive = true; mMasterConnectionActive = true;
mMasterSystemAddress = packet->systemAddress; mMasterSystemAddress = packet->systemAddress;
MasterPackets::SendServerInfo(this, packet); MasterPackets::SendServerInfo(this, packet);
mMasterPeer->DeallocatePacket(packet);
packet = nullptr;
} }
if (packet->data[0] == ID_USER_PACKET_ENUM) {
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::MASTER) {
switch (static_cast<eMasterMessageType>(packet->data[3])) {
case eMasterMessageType::REQUEST_ZONE_TRANSFER_RESPONSE: {
uint64_t requestID = PacketUtils::ReadU64(8, packet);
ZoneInstanceManager::Instance()->HandleRequestZoneTransferResponse(requestID, packet);
break;
} }
case eMasterMessageType::SHUTDOWN:
*mShouldShutdown = true;
break;
//When we handle these packets in World instead dServer, we just return the packet's pointer.
default:
return packet; return packet;
} }
}
}
mMasterPeer->DeallocatePacket(packet);
}
return nullptr;
}
Packet* dServer::Receive() { Packet* dServer::Receive() {
return mPeer->Receive(); return mPeer->Receive();

View File

@ -97,6 +97,7 @@ void WorldShutdownSequence();
void WorldShutdownProcess(uint32_t zoneId); void WorldShutdownProcess(uint32_t zoneId);
void FinalizeShutdown(); void FinalizeShutdown();
void SendShutdownMessageToMaster(); void SendShutdownMessageToMaster();
void HandleMasterPacket(Packet* packet);
dLogger* SetupLogger(uint32_t zoneID, uint32_t instanceID); dLogger* SetupLogger(uint32_t zoneID, uint32_t instanceID);
void HandlePacketChat(Packet* packet); void HandlePacketChat(Packet* packet);
@ -413,7 +414,7 @@ int main(int argc, char** argv) {
//Check for packets here: //Check for packets here:
packet = Game::server->ReceiveFromMaster(); packet = Game::server->ReceiveFromMaster();
if (packet) { //We can get messages not handle-able by the dServer class, so handle them if we returned anything. if (packet) { //We can get messages not handle-able by the dServer class, so handle them if we returned anything.
HandlePacket(packet); HandleMasterPacket(packet);
Game::server->DeallocateMasterPacket(packet); Game::server->DeallocateMasterPacket(packet);
} }
@ -677,64 +678,7 @@ void HandlePacketChat(Packet* packet) {
} }
} }
void HandlePacket(Packet* packet) { void HandleMasterPacket(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
if (!user) return;
auto c = user->GetLastUsedChar();
if (!c) {
UserManager::Instance()->DeleteUser(packet->systemAddress);
return;
}
auto* entity = Game::entityManager->GetEntity(c->GetObjectID());
if (!entity) {
entity = Player::GetPlayer(packet->systemAddress);
}
if (entity) {
auto* skillComponent = entity->GetComponent<SkillComponent>();
if (skillComponent != nullptr) {
skillComponent->Reset();
}
entity->GetCharacter()->SaveXMLToDatabase();
Game::logger->Log("WorldServer", "Deleting player %llu", entity->GetObjectID());
Game::entityManager->DestroyEntity(entity);
}
{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_REMOVED_NOTIFICATION);
bitStream.Write(user->GetLoggedInChar());
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
}
UserManager::Instance()->DeleteUser(packet->systemAddress);
if (PropertyManagementComponent::Instance() != nullptr) {
PropertyManagementComponent::Instance()->Save();
}
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::PLAYER_REMOVED);
bitStream.Write((LWOMAPID)Game::server->GetZoneID());
bitStream.Write((LWOINSTANCEID)instanceID);
Game::server->SendToMaster(&bitStream);
}
if (packet->data[0] != ID_USER_PACKET_ENUM || packet->length < 4) return;
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::SERVER) {
if (static_cast<eServerMessageType>(packet->data[3]) == eServerMessageType::VERSION_CONFIRM) {
AuthPackets::HandleHandshake(Game::server, packet);
}
}
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::MASTER) { if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::MASTER) {
switch (static_cast<eMasterMessageType>(packet->data[3])) { switch (static_cast<eMasterMessageType>(packet->data[3])) {
case eMasterMessageType::REQUEST_PERSISTENT_ID_RESPONSE: { case eMasterMessageType::REQUEST_PERSISTENT_ID_RESPONSE: {
@ -873,6 +817,65 @@ void HandlePacket(Packet* packet) {
return; return;
} }
}
void HandlePacket(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
auto user = UserManager::Instance()->GetUser(packet->systemAddress);
if (!user) return;
auto c = user->GetLastUsedChar();
if (!c) {
UserManager::Instance()->DeleteUser(packet->systemAddress);
return;
}
auto* entity = Game::entityManager->GetEntity(c->GetObjectID());
if (!entity) {
entity = Player::GetPlayer(packet->systemAddress);
}
if (entity) {
auto* skillComponent = entity->GetComponent<SkillComponent>();
if (skillComponent != nullptr) {
skillComponent->Reset();
}
entity->GetCharacter()->SaveXMLToDatabase();
Game::logger->Log("WorldServer", "Deleting player %llu", entity->GetObjectID());
Game::entityManager->DestroyEntity(entity);
}
{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT_INTERNAL, eChatInternalMessageType::PLAYER_REMOVED_NOTIFICATION);
bitStream.Write(user->GetLoggedInChar());
Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
}
UserManager::Instance()->DeleteUser(packet->systemAddress);
if (PropertyManagementComponent::Instance() != nullptr) {
PropertyManagementComponent::Instance()->Save();
}
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::PLAYER_REMOVED);
bitStream.Write((LWOMAPID)Game::server->GetZoneID());
bitStream.Write((LWOINSTANCEID)instanceID);
Game::server->SendToMaster(&bitStream);
}
if (packet->data[0] != ID_USER_PACKET_ENUM || packet->length < 4) return;
if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::SERVER) {
if (static_cast<eServerMessageType>(packet->data[3]) == eServerMessageType::VERSION_CONFIRM) {
AuthPackets::HandleHandshake(Game::server, packet);
}
}
if (static_cast<eConnectionType>(packet->data[1]) != eConnectionType::WORLD) return; if (static_cast<eConnectionType>(packet->data[1]) != eConnectionType::WORLD) return;