mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-10-02 02:43:44 +00:00
feat: web dashboard and playground work
The NexusDashboard-parity dashboard (dDashboardServer) and everything built on it on the experimental branch: accounts, characters, properties and moderation tools, permissions shared with in-game slash commands, economy reports, World 3D and property 3D views with client scenery, scheduled events (features, vanity changes, live events, announcements, restarts), vanity files and events, the CDClient browser, the message inspector with saved captures, chat filter tools, community challenges, live ops, the AI moderator helper, and the server-side changes they need. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include "AuthPackets.h"
|
||||
|
||||
#include <ctime>
|
||||
#include "BitStreamUtils.h"
|
||||
|
||||
#include "dNetCommon.h"
|
||||
@@ -156,6 +158,15 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The password first: someone who doesn't know it learns nothing about the account (ban details, lock, play key),
|
||||
// and a failed attempt changes nothing (an expired ban is only lifted for the real owner)
|
||||
if (::bcrypt_checkpw(password.GetAsString().c_str(), accountInfo->bcryptPassword.c_str()) != 0) {
|
||||
stamps.emplace_back(eStamps::PASSPORT_AUTH_ERROR, 1);
|
||||
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::WRONG_PASS, "", "", 2001, username, stamps);
|
||||
LOG("Wrong password used");
|
||||
return;
|
||||
}
|
||||
|
||||
//If we aren't running in live mode, then only GMs are allowed to enter:
|
||||
if (Game::config->GetValue<bool>("closed_to_non_devs", false) && accountInfo->maxGmLevel == eGameMasterLevel::CIVILIAN) {
|
||||
stamps.emplace_back(eStamps::GM_REQUIRED, 1);
|
||||
@@ -191,9 +202,25 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
|
||||
stamps.emplace_back(eStamps::PASSPORT_AUTH_BYPASS, 1);
|
||||
}
|
||||
|
||||
// A temporary ban that has run out is lifted as the player logs in
|
||||
if (accountInfo->banned && accountInfo->banExpires > 0 && accountInfo->banExpires <= static_cast<int64_t>(std::time(nullptr))) {
|
||||
Database::Get()->SetAccountBan(accountInfo->id, false, 0, "");
|
||||
Database::Get()->InsertAccountNote({ 0, accountInfo->id, "unban", "Temporary ban ended", "[server]", static_cast<int64_t>(std::time(nullptr)) });
|
||||
accountInfo->banned = false;
|
||||
LOG("Temporary ban of %s ended", username.c_str());
|
||||
}
|
||||
|
||||
if (accountInfo->banned) {
|
||||
stamps.emplace_back(eStamps::PASSPORT_AUTH_ERROR, 1);
|
||||
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::BANNED, "", "", 2001, username, stamps);
|
||||
std::string message;
|
||||
if (accountInfo->banExpires > 0) {
|
||||
char until[32];
|
||||
const std::time_t expires = accountInfo->banExpires;
|
||||
std::strftime(until, sizeof(until), "%Y-%m-%d %H:%M UTC", std::gmtime(&expires));
|
||||
message = std::string("You are banned until ") + until + ".";
|
||||
}
|
||||
if (!accountInfo->banReason.empty()) message += (message.empty() ? "" : " ") + std::string("Reason: ") + accountInfo->banReason;
|
||||
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::BANNED, message, "", 2001, username, stamps);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -203,15 +230,14 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool loginSuccess = ::bcrypt_checkpw(password.GetAsString().c_str(), accountInfo->bcryptPassword.c_str()) == 0;
|
||||
|
||||
if (!loginSuccess) {
|
||||
stamps.emplace_back(eStamps::PASSPORT_AUTH_ERROR, 1);
|
||||
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::WRONG_PASS, "", "", 2001, username, stamps);
|
||||
LOG("Wrong password used");
|
||||
} else {
|
||||
{
|
||||
SystemAddress system = packet->systemAddress; //Copy the sysAddr before the Packet gets destroyed from main
|
||||
|
||||
// Where accounts log in from, so staff can see accounts that share a connection (log_login_addresses, on by default)
|
||||
if (Game::config->GetValue("log_login_addresses") != "0") {
|
||||
Database::Get()->RecordLoginAddress(accountInfo->id, system.ToString(false), static_cast<int64_t>(std::time(nullptr)));
|
||||
}
|
||||
|
||||
if (!server->GetIsConnectedToMaster()) {
|
||||
stamps.emplace_back(eStamps::PASSPORT_AUTH_WORLD_DISCONNECT, 1);
|
||||
AuthPackets::SendLoginResponse(server, system, eLoginResponse::GENERAL_FAILED, "", "", 0, username, stamps);
|
||||
|
||||
105
dNet/DashboardMessages.h
Normal file
105
dNet/DashboardMessages.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#ifndef __DASHBOARDMESSAGES__H__
|
||||
#define __DASHBOARDMESSAGES__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
namespace DashboardMessages {
|
||||
// Length-prefixed string, capped so a bad packet can't ask for huge allocations
|
||||
inline void WriteText(RakNet::BitStream& stream, const std::string& text, uint16_t max) {
|
||||
const auto length = static_cast<uint16_t>(std::min<size_t>(text.size(), max));
|
||||
stream.Write(length);
|
||||
stream.Write(text.data(), length);
|
||||
}
|
||||
|
||||
inline bool ReadText(RakNet::BitStream& stream, std::string& text, uint16_t max) {
|
||||
uint16_t length{};
|
||||
if (!stream.Read(length) || length > max) return false;
|
||||
text.resize(length);
|
||||
return length == 0 || stream.Read(text.data(), length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PLAYER_POSITIONS (world -> master -> dashboard): where each player in a world instance is, sent every
|
||||
* second while anyone is online and once more when the world empties.
|
||||
*/
|
||||
struct PlayerPositions {
|
||||
struct Player {
|
||||
LWOOBJID characterId{};
|
||||
float x{}, y{}, z{};
|
||||
};
|
||||
|
||||
static constexpr uint16_t MAX_PLAYERS = 1000;
|
||||
|
||||
uint32_t zoneId{};
|
||||
uint32_t instanceId{};
|
||||
uint32_t cloneId{};
|
||||
std::vector<Player> players;
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
stream.Write(zoneId);
|
||||
stream.Write(instanceId);
|
||||
stream.Write(cloneId);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(players.size(), MAX_PLAYERS));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
stream.Write(players[i].characterId);
|
||||
stream.Write(players[i].x);
|
||||
stream.Write(players[i].y);
|
||||
stream.Write(players[i].z);
|
||||
}
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(zoneId) || !stream.Read(instanceId) || !stream.Read(cloneId) || !stream.Read(count) || count > MAX_PLAYERS) return false;
|
||||
players.resize(count);
|
||||
for (auto& player : players) {
|
||||
if (!stream.Read(player.characterId) || !stream.Read(player.x) || !stream.Read(player.y) || !stream.Read(player.z)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* ANNOUNCE (dashboard -> master -> worlds): a message shown to every player online, or only to those in the listed
|
||||
* zones (master only forwards it to worlds of those zones).
|
||||
*/
|
||||
struct Announcement {
|
||||
static constexpr uint16_t MAX_TITLE = 100;
|
||||
static constexpr uint16_t MAX_MESSAGE = 1000;
|
||||
static constexpr uint16_t MAX_ZONES = 200;
|
||||
|
||||
std::string title;
|
||||
std::string message;
|
||||
std::vector<uint32_t> zones; // empty: every world
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
DashboardMessages::WriteText(stream, title, MAX_TITLE);
|
||||
DashboardMessages::WriteText(stream, message, MAX_MESSAGE);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(zones.size(), MAX_ZONES));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) stream.Write(zones[i]);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
if (!DashboardMessages::ReadText(stream, title, MAX_TITLE) || !DashboardMessages::ReadText(stream, message, MAX_MESSAGE)) return false;
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_ZONES) return false;
|
||||
zones.resize(count);
|
||||
for (auto& zone : zones) if (!stream.Read(zone)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ShownIn(uint32_t zoneId) const {
|
||||
return zones.empty() || std::find(zones.begin(), zones.end(), zoneId) != zones.end();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__DASHBOARDMESSAGES__H__
|
||||
57
dNet/DataChanged.h
Normal file
57
dNet/DataChanged.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#ifndef __DATACHANGED__H__
|
||||
#define __DATACHANGED__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* DATA_CHANGED payload: the tables and rows a game server just wrote. The dashboard turns each entry into a
|
||||
* table_changed event so open pages refresh without waiting for its periodic database check.
|
||||
*/
|
||||
struct DataChanged {
|
||||
struct Entry {
|
||||
std::string table; // dashboard table name, e.g. "characters", "mail", "economy"
|
||||
LWOOBJID id{}; // row the change is about, 0 for the table as a whole
|
||||
};
|
||||
|
||||
static constexpr size_t MAX_ENTRIES = 1024;
|
||||
static constexpr size_t MAX_TABLE_NAME = 32;
|
||||
|
||||
std::vector<Entry> entries;
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
const auto count = static_cast<uint16_t>(std::min(entries.size(), MAX_ENTRIES));
|
||||
stream.Write(count);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
const auto& entry = entries[i];
|
||||
const auto length = static_cast<uint8_t>(std::min(entry.table.size(), MAX_TABLE_NAME));
|
||||
stream.Write(length);
|
||||
stream.Write(entry.table.data(), length);
|
||||
stream.Write(entry.id);
|
||||
}
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_ENTRIES) return false;
|
||||
entries.clear();
|
||||
entries.reserve(count);
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
uint8_t length{};
|
||||
if (!stream.Read(length) || length > MAX_TABLE_NAME) return false;
|
||||
Entry entry;
|
||||
entry.table.resize(length);
|
||||
if (length > 0 && !stream.Read(entry.table.data(), length)) return false;
|
||||
if (!stream.Read(entry.id)) return false;
|
||||
entries.push_back(std::move(entry));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__DATACHANGED__H__
|
||||
@@ -20,6 +20,8 @@ struct MailInfo {
|
||||
uint64_t timeSent{};
|
||||
bool wasRead{};
|
||||
uint16_t languageCode{};
|
||||
// Attachment item config (LDF "key=type:value" lines), kept so items keep their data through the mail
|
||||
std::string itemConfig;
|
||||
struct {
|
||||
LWOOBJID itemID{};
|
||||
int16_t itemCount{};
|
||||
|
||||
@@ -94,6 +94,7 @@ void MasterPackets::HandleServerInfo(Packet* packet) {
|
||||
}
|
||||
|
||||
void MasterPackets::SendServerInfo(dServer* server, Packet* packet) {
|
||||
LOG("SendServerInfo called for server type %i", static_cast<int>(server->GetServerType()));
|
||||
RakNet::BitStream bitStream;
|
||||
BitStreamUtils::WriteHeader(bitStream, ServiceType::MASTER, MessageType::Master::SERVER_INFO);
|
||||
|
||||
|
||||
263
dNet/MessageCapture.h
Normal file
263
dNet/MessageCapture.h
Normal file
@@ -0,0 +1,263 @@
|
||||
#ifndef __MESSAGECAPTURE__H__
|
||||
#define __MESSAGECAPTURE__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* The dashboard's game message inspector: staff capture the game messages one online player sends and receives.
|
||||
*
|
||||
* MESSAGE_CAPTURE_CONTROL (dashboard -> master -> every world) starts or stops a capture; only the world holding the
|
||||
* character acts on it. MESSAGE_CAPTURE_DATA (world -> master -> dashboard) reports that the capture started, carries
|
||||
* batches of captured messages, and says when and why it ended there. Worlds stop a capture on their own at its time
|
||||
* limit, so one can never be left running.
|
||||
*/
|
||||
enum class eMessageDirection : uint8_t {
|
||||
TO_SERVER, // sent by the player's client
|
||||
TO_CLIENT, // sent to the player's client (including broadcasts they receive)
|
||||
};
|
||||
|
||||
enum class eMessageCaptureControl : uint8_t {
|
||||
START,
|
||||
STOP,
|
||||
};
|
||||
|
||||
enum class eMessageCaptureStatus : uint8_t {
|
||||
STARTED, // the world holding the character started capturing
|
||||
ENTRIES, // captured messages (also sent empty every few seconds while capturing)
|
||||
ENDED, // the world stopped capturing; `reason` says why
|
||||
};
|
||||
|
||||
enum class eMessageCaptureEnd : uint8_t {
|
||||
STOPPED, // staff stopped it
|
||||
TIME_LIMIT, // its time ran out
|
||||
PLAYER_LEFT, // the player left this world (logged out or changed zones)
|
||||
};
|
||||
|
||||
namespace MessageCapture {
|
||||
// A capture runs at most this long
|
||||
constexpr uint32_t MAX_SECONDS = 15 * 60;
|
||||
// Captured bytes kept per message; larger messages are cut (the entry keeps their full size)
|
||||
constexpr uint16_t MAX_PAYLOAD = 2048;
|
||||
// Decoded fields (JSON text) kept per message
|
||||
constexpr uint16_t MAX_DECODED = 8192;
|
||||
// Message IDs in a filter list
|
||||
constexpr uint16_t MAX_FILTER = 256;
|
||||
|
||||
// Lowercase hex, two digits per byte
|
||||
inline std::string ToHex(std::string_view bytes) {
|
||||
static constexpr char DIGITS[] = "0123456789abcdef";
|
||||
std::string hex;
|
||||
hex.reserve(bytes.size() * 2);
|
||||
for (const unsigned char byte : bytes) {
|
||||
hex += DIGITS[byte >> 4];
|
||||
hex += DIGITS[byte & 0x0f];
|
||||
}
|
||||
return hex;
|
||||
}
|
||||
|
||||
inline void WriteString(RakNet::BitStream& stream, const std::string& text, uint16_t max) {
|
||||
const auto length = static_cast<uint16_t>(std::min<size_t>(text.size(), max));
|
||||
stream.Write(length);
|
||||
if (length) stream.Write(text.data(), length);
|
||||
}
|
||||
|
||||
inline bool ReadString(RakNet::BitStream& stream, std::string& text, uint16_t max) {
|
||||
uint16_t length{};
|
||||
if (!stream.Read(length) || length > max) return false;
|
||||
text.resize(length);
|
||||
return length == 0 || stream.Read(text.data(), length);
|
||||
}
|
||||
|
||||
inline void WriteIds(RakNet::BitStream& stream, const std::vector<uint16_t>& ids) {
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(ids.size(), MAX_FILTER));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) stream.Write(ids[i]);
|
||||
}
|
||||
|
||||
inline bool ReadIds(RakNet::BitStream& stream, std::vector<uint16_t>& ids) {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(count) || count > MAX_FILTER) return false;
|
||||
ids.resize(count);
|
||||
for (auto& id : ids) if (!stream.Read(id)) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// MESSAGE_CAPTURE_CONTROL payload
|
||||
struct MessageCaptureControl {
|
||||
uint32_t captureId{};
|
||||
eMessageCaptureControl action{};
|
||||
LWOOBJID characterId{};
|
||||
uint32_t seconds{}; // START: how long to capture (capped at MessageCapture::MAX_SECONDS)
|
||||
bool toServer{ true }; // START: capture what the client sends
|
||||
bool toClient{ true }; // START: capture what the client receives
|
||||
std::vector<uint16_t> only; // START: capture only these message IDs (empty: all)
|
||||
std::vector<uint16_t> skip; // START: never capture these message IDs
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
stream.Write(captureId);
|
||||
stream.Write(action);
|
||||
stream.Write(characterId);
|
||||
stream.Write(std::min(seconds, MessageCapture::MAX_SECONDS));
|
||||
stream.Write<uint8_t>(toServer);
|
||||
stream.Write<uint8_t>(toClient);
|
||||
MessageCapture::WriteIds(stream, only);
|
||||
MessageCapture::WriteIds(stream, skip);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
uint8_t server{}, client{};
|
||||
if (!stream.Read(captureId) || !stream.Read(action) || !stream.Read(characterId) || !stream.Read(seconds) ||
|
||||
!stream.Read(server) || !stream.Read(client)) return false;
|
||||
seconds = std::min(seconds, MessageCapture::MAX_SECONDS);
|
||||
toServer = server != 0;
|
||||
toClient = client != 0;
|
||||
return MessageCapture::ReadIds(stream, only) && MessageCapture::ReadIds(stream, skip);
|
||||
}
|
||||
|
||||
// Whether a message passes this capture's filters
|
||||
bool Wants(eMessageDirection direction, uint16_t messageId) const {
|
||||
if (!(direction == eMessageDirection::TO_SERVER ? toServer : toClient)) return false;
|
||||
if (std::ranges::find(skip, messageId) != skip.end()) return false;
|
||||
return only.empty() || std::ranges::find(only, messageId) != only.end();
|
||||
}
|
||||
};
|
||||
|
||||
// One captured game message
|
||||
struct MessageCaptureEntry {
|
||||
uint32_t sequence{}; // per capture, from 1; gaps mean messages were dropped
|
||||
int64_t timeMs{}; // Unix time in milliseconds
|
||||
eMessageDirection direction{};
|
||||
uint16_t messageId{}; // MessageType::Game
|
||||
LWOOBJID objectId{}; // the object the message is addressed to
|
||||
uint32_t bits{}; // full size of the message's fields, in bits
|
||||
std::string payload; // the fields' bytes (at most MAX_PAYLOAD)
|
||||
std::string decoded; // the fields as JSON when the server has a typed struct for the message, else empty
|
||||
|
||||
// Roughly how many bytes this takes in a packet, for batching
|
||||
size_t WireSize() const { return 32 + payload.size() + decoded.size(); }
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
stream.Write(sequence);
|
||||
stream.Write(timeMs);
|
||||
stream.Write(direction);
|
||||
stream.Write(messageId);
|
||||
stream.Write(objectId);
|
||||
stream.Write(bits);
|
||||
MessageCapture::WriteString(stream, payload, MessageCapture::MAX_PAYLOAD);
|
||||
MessageCapture::WriteString(stream, decoded, MessageCapture::MAX_DECODED);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
return stream.Read(sequence) && stream.Read(timeMs) && stream.Read(direction) && stream.Read(messageId) &&
|
||||
stream.Read(objectId) && stream.Read(bits) &&
|
||||
MessageCapture::ReadString(stream, payload, MessageCapture::MAX_PAYLOAD) &&
|
||||
MessageCapture::ReadString(stream, decoded, MessageCapture::MAX_DECODED);
|
||||
}
|
||||
};
|
||||
|
||||
// MESSAGE_CAPTURE_DATA payload
|
||||
struct MessageCaptureData {
|
||||
static constexpr uint16_t MAX_ENTRIES = 500;
|
||||
|
||||
uint32_t captureId{};
|
||||
eMessageCaptureStatus status{};
|
||||
LWOOBJID characterId{};
|
||||
uint32_t zoneId{};
|
||||
uint32_t instanceId{};
|
||||
eMessageCaptureEnd reason{}; // ENDED
|
||||
uint32_t dropped{}; // messages left out since the last batch (over the rate or buffer limit)
|
||||
std::vector<MessageCaptureEntry> entries;
|
||||
uint32_t cloneId{}; // the world's clone (a property's owner), 0 elsewhere
|
||||
|
||||
void Serialize(RakNet::BitStream& stream) const {
|
||||
stream.Write(captureId);
|
||||
stream.Write(status);
|
||||
stream.Write(characterId);
|
||||
stream.Write(zoneId);
|
||||
stream.Write(instanceId);
|
||||
stream.Write(reason);
|
||||
stream.Write(dropped);
|
||||
const auto count = static_cast<uint16_t>(std::min<size_t>(entries.size(), MAX_ENTRIES));
|
||||
stream.Write(count);
|
||||
for (uint16_t i = 0; i < count; i++) entries[i].Serialize(stream);
|
||||
stream.Write(cloneId);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& stream) {
|
||||
uint16_t count{};
|
||||
if (!stream.Read(captureId) || !stream.Read(status) || !stream.Read(characterId) || !stream.Read(zoneId) ||
|
||||
!stream.Read(instanceId) || !stream.Read(reason) || !stream.Read(dropped) || !stream.Read(count) || count > MAX_ENTRIES) return false;
|
||||
entries.resize(count);
|
||||
for (auto& entry : entries) if (!entry.Deserialize(stream)) return false;
|
||||
return stream.Read(cloneId);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Captured messages waiting to be sent, bounded by count and by rate so a busy player can't flood master and the
|
||||
* dashboard. What goes over either limit is counted as dropped instead.
|
||||
*/
|
||||
class MessageCaptureQueue {
|
||||
public:
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
MessageCaptureQueue(size_t maxPending, uint32_t maxPerSecond) : m_MaxPending(maxPending), m_MaxPerSecond(maxPerSecond) {}
|
||||
|
||||
// False if the entry was dropped
|
||||
bool Push(MessageCaptureEntry entry, Clock::time_point now) {
|
||||
if (now - m_WindowStart >= std::chrono::seconds(1)) {
|
||||
m_WindowStart = now;
|
||||
m_InWindow = 0;
|
||||
}
|
||||
if (m_InWindow >= m_MaxPerSecond || m_Pending.size() >= m_MaxPending) {
|
||||
m_Dropped++;
|
||||
return false;
|
||||
}
|
||||
m_InWindow++;
|
||||
m_Pending.push_back(std::move(entry));
|
||||
return true;
|
||||
}
|
||||
|
||||
// The oldest entries, up to maxEntries and about maxBytes (always at least one when any are waiting)
|
||||
std::vector<MessageCaptureEntry> Take(size_t maxEntries, size_t maxBytes) {
|
||||
std::vector<MessageCaptureEntry> batch;
|
||||
size_t bytes = 0;
|
||||
while (!m_Pending.empty() && batch.size() < maxEntries) {
|
||||
const auto size = m_Pending.front().WireSize();
|
||||
if (!batch.empty() && bytes + size > maxBytes) break;
|
||||
bytes += size;
|
||||
batch.push_back(std::move(m_Pending.front()));
|
||||
m_Pending.pop_front();
|
||||
}
|
||||
return batch;
|
||||
}
|
||||
|
||||
// Entries dropped since the last call
|
||||
uint32_t TakeDropped() {
|
||||
const auto dropped = m_Dropped;
|
||||
m_Dropped = 0;
|
||||
return dropped;
|
||||
}
|
||||
|
||||
size_t Pending() const { return m_Pending.size(); }
|
||||
|
||||
private:
|
||||
size_t m_MaxPending;
|
||||
uint32_t m_MaxPerSecond;
|
||||
std::deque<MessageCaptureEntry> m_Pending;
|
||||
Clock::time_point m_WindowStart{};
|
||||
uint32_t m_InWindow{};
|
||||
uint32_t m_Dropped{};
|
||||
};
|
||||
|
||||
#endif //!__MESSAGECAPTURE__H__
|
||||
122
dNet/PlayerAction.h
Normal file
122
dNet/PlayerAction.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#ifndef __PLAYERACTION__H__
|
||||
#define __PLAYERACTION__H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
/**
|
||||
* Actions the dashboard asks world servers to apply to players who are currently online.
|
||||
* The dashboard sends MessageType::Master::PLAYER_ACTION to master, which forwards it to every world.
|
||||
* Each world answers PLAYER_ACTION_RESULT, and master sends the dashboard a single aggregated result
|
||||
* once every world has answered or the request timed out.
|
||||
*/
|
||||
enum class ePlayerAction : uint8_t {
|
||||
KICK_ACCOUNT, // Disconnect every session of accountId
|
||||
REFRESH_ACCOUNT, // Reload account data (GM level) for online sessions of accountId
|
||||
REFRESH_CHARACTER, // Reload character data (restrictions) for characterId if loaded
|
||||
RESCUE_CHARACTER, // Transfer characterId to zoneId if it is loaded in a world, landing on spawn point `text` (empty: the zone's default)
|
||||
// Tell a player a moderator decided on something they asked for (approved = yes/no, text = name or reason)
|
||||
NAME_MODERATED, // characterId's requested name; text is the name
|
||||
PET_NAME_MODERATED, // pet targetId's name; text is the name
|
||||
PROPERTY_MODERATED, // property targetId owned by characterId; text is the rejection reason
|
||||
TELEPORT_TO_PLAYER, // move characterId to where character targetId stands (only when both are in the same world)
|
||||
WARN_ACCOUNT, // show accountId's online character a moderator's warning (text)
|
||||
RELOAD_VANITY, // respawn the vanity NPCs from the vanity/*.xml files (every world; answers 1 per world reloaded)
|
||||
CHAT_MESSAGE, // show `text` in chat from `name` (e.g. "[Discord] Bob") in every world, or only zoneId; answers players reached
|
||||
RELOAD_CHAT_FILTER, // load the chat filter's words added on the dashboard again (every world, answering 1 each; master also passes it to the chat server)
|
||||
// Change mission targetId of characterId if it is loaded in a world, as the GM commands do
|
||||
MISSION_COMPLETE, // as /completemission; approved = give its rewards
|
||||
MISSION_RESET, // as /resetmission
|
||||
MISSION_ACCEPT, // as /addmission (prerequisites are skipped)
|
||||
RELOAD_LIVE_OPS, // load the running live events and open challenges again (every world, answering 1 each; see LiveEvents.h)
|
||||
};
|
||||
|
||||
struct PlayerActionRequest {
|
||||
uint32_t requestId{};
|
||||
ePlayerAction action{};
|
||||
uint32_t accountId{};
|
||||
LWOOBJID characterId{};
|
||||
LWOMAPID zoneId{};
|
||||
// eServerDisconnectIdentifiers shown to the client for KICK_ACCOUNT
|
||||
uint32_t disconnectReason{};
|
||||
// For the *_MODERATED actions
|
||||
LWOOBJID targetId{};
|
||||
bool approved{};
|
||||
std::string text;
|
||||
std::string name; // CHAT_MESSAGE: who it's from
|
||||
int32_t instanceId{ -1 }; // CHAT_MESSAGE: only this instance of zoneId (-1: every instance)
|
||||
|
||||
static constexpr uint16_t MAX_TEXT = 300;
|
||||
|
||||
// How many bytes of UTF-8 text fit in maxBytes without cutting a character in half
|
||||
static size_t Utf8Length(const std::string& value, size_t maxBytes) {
|
||||
if (value.size() <= maxBytes) return value.size();
|
||||
size_t length = maxBytes;
|
||||
// Back up over continuation bytes (10xxxxxx) to the start of the character that doesn't fit
|
||||
while (length > 0 && (static_cast<unsigned char>(value[length]) & 0xC0) == 0x80) length--;
|
||||
return length;
|
||||
}
|
||||
|
||||
void Serialize(RakNet::BitStream& bitStream) const {
|
||||
bitStream.Write(requestId);
|
||||
bitStream.Write(action);
|
||||
bitStream.Write(accountId);
|
||||
bitStream.Write(characterId);
|
||||
bitStream.Write(zoneId);
|
||||
bitStream.Write(disconnectReason);
|
||||
bitStream.Write(targetId);
|
||||
bitStream.Write<uint8_t>(approved);
|
||||
const auto length = static_cast<uint16_t>(Utf8Length(text, MAX_TEXT));
|
||||
bitStream.Write(length);
|
||||
bitStream.Write(text.data(), length);
|
||||
const auto nameLength = static_cast<uint16_t>(Utf8Length(name, MAX_TEXT));
|
||||
bitStream.Write(nameLength);
|
||||
bitStream.Write(name.data(), nameLength);
|
||||
bitStream.Write(instanceId);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& bitStream) {
|
||||
uint8_t approvedByte{};
|
||||
uint16_t length{};
|
||||
if (!(bitStream.Read(requestId) && bitStream.Read(action) && bitStream.Read(accountId) && bitStream.Read(characterId) &&
|
||||
bitStream.Read(zoneId) && bitStream.Read(disconnectReason) && bitStream.Read(targetId) && bitStream.Read(approvedByte) &&
|
||||
bitStream.Read(length)) || length > MAX_TEXT) return false;
|
||||
approved = approvedByte != 0;
|
||||
text.resize(length);
|
||||
if (length != 0 && !bitStream.Read(text.data(), length)) return false;
|
||||
uint16_t nameLength{};
|
||||
if (!bitStream.Read(nameLength) || nameLength > MAX_TEXT) return false;
|
||||
name.resize(nameLength);
|
||||
if (nameLength != 0 && !bitStream.Read(name.data(), nameLength)) return false;
|
||||
return bitStream.Read(instanceId);
|
||||
}
|
||||
};
|
||||
|
||||
struct PlayerActionResult {
|
||||
uint32_t requestId{};
|
||||
ePlayerAction action{};
|
||||
// Number of sessions/characters the action applied to. Zero means the player was not online.
|
||||
uint32_t affected{};
|
||||
// Set by master when not every world answered before the timeout
|
||||
bool timedOut{};
|
||||
|
||||
void Serialize(RakNet::BitStream& bitStream) const {
|
||||
bitStream.Write(requestId);
|
||||
bitStream.Write(action);
|
||||
bitStream.Write(affected);
|
||||
bitStream.Write<uint8_t>(timedOut);
|
||||
}
|
||||
|
||||
bool Deserialize(RakNet::BitStream& bitStream) {
|
||||
uint8_t timedOutByte{};
|
||||
const bool ok = bitStream.Read(requestId) && bitStream.Read(action) && bitStream.Read(affected) && bitStream.Read(timedOutByte);
|
||||
timedOut = timedOutByte != 0;
|
||||
return ok;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //!__PLAYERACTION__H__
|
||||
@@ -144,7 +144,7 @@ Packet* dServer::ReceiveFromMaster() {
|
||||
break;
|
||||
}
|
||||
case ID_CONNECTION_REQUEST_ACCEPTED: {
|
||||
LOG("Established connection to master, zone (%i), instance (%i)", this->GetZoneID(), this->GetInstanceID());
|
||||
LOG("Established connection to master: ServiceType (%s), Zone (%i), Instance (%i)", StringifiedEnum::ToString(this->GetServerType()).data(), this->GetZoneID(), this->GetInstanceID());
|
||||
mMasterConnectionActive = true;
|
||||
mMasterSystemAddress = packet->systemAddress;
|
||||
MasterPackets::SendServerInfo(this, packet);
|
||||
@@ -161,6 +161,11 @@ Packet* dServer::ReceiveFromMaster() {
|
||||
*mShouldShutdown = -2;
|
||||
break;
|
||||
|
||||
case MessageType::Master::CONFIG_RELOAD:
|
||||
LOG("Reloading settings (changed on the dashboard)");
|
||||
if (mConfig) mConfig->ReloadConfig();
|
||||
break;
|
||||
|
||||
// When we handle these packets in World instead dServer, we just return the packet's pointer.
|
||||
default:
|
||||
return packet;
|
||||
@@ -189,6 +194,7 @@ void dServer::DeallocateMasterPacket(Packet* packet) {
|
||||
}
|
||||
|
||||
void dServer::Send(RakNet::BitStream& bitStream, const SystemAddress& sysAddr, bool broadcast) {
|
||||
if (mSendObserver) mSendObserver(bitStream, sysAddr, broadcast);
|
||||
mPeer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE_ORDERED, 0, sysAddr, broadcast);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <functional>
|
||||
#include "RakPeerInterface.h"
|
||||
#include "ReplicaManager.h"
|
||||
#include "NetworkIDManager.h"
|
||||
@@ -43,6 +44,11 @@ public:
|
||||
virtual void Send(RakNet::BitStream& bitStream, const SystemAddress& sysAddr, bool broadcast);
|
||||
void SendToMaster(RakNet::BitStream& bitStream);
|
||||
|
||||
// Sees every packet Send sends, before it goes out (the dashboard's message inspector sets it only while it
|
||||
// captures, so sending costs nothing extra otherwise). Pass nullptr to remove it.
|
||||
using SendObserver = std::function<void(const RakNet::BitStream& bitStream, const SystemAddress& sysAddr, bool broadcast)>;
|
||||
void SetSendObserver(SendObserver observer) { mSendObserver = std::move(observer); }
|
||||
|
||||
void Disconnect(const SystemAddress& sysAddr, eServerDisconnectIdentifiers disconNotifyID);
|
||||
|
||||
bool IsConnected(const SystemAddress& sysAddr);
|
||||
@@ -109,4 +115,5 @@ protected:
|
||||
int mMasterPort;
|
||||
std::chrono::steady_clock::time_point mStartTime = std::chrono::steady_clock::now();
|
||||
std::string mMasterPassword;
|
||||
SendObserver mSendObserver;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user