Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve IO ban information #4544

Merged
merged 7 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ bool Ban::acceptConnection(const Connection::Address& clientIP)
return true;
}

bool IOBan::isAccountBanned(uint32_t accountId, BanInfo& banInfo)
std::shared_ptr<BanInfo> IOBan::isAccountBanned(uint32_t accountId)
{
Database& db = Database::getInstance();

DBResult_ptr result = db.storeQuery(fmt::format(
"SELECT `reason`, `expires_at`, `banned_at`, `banned_by`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `account_bans` WHERE `account_id` = {:d}",
accountId));
if (!result) {
return false;
return nullptr;
}

int64_t expiresAt = result->getNumber<int64_t>("expires_at");
Expand All @@ -63,19 +63,25 @@ bool IOBan::isAccountBanned(uint32_t accountId, BanInfo& banInfo)
accountId, db.escapeString(result->getString("reason")), result->getNumber<time_t>("banned_at"), expiresAt,
result->getNumber<uint32_t>("banned_by")));
g_databaseTasks.addTask(fmt::format("DELETE FROM `account_bans` WHERE `account_id` = {:d}", accountId));
return false;
return nullptr;
}

banInfo.expiresAt = expiresAt;
banInfo.reason = result->getString("reason");
banInfo.bannedBy = result->getString("name");
return true;
std::shared_ptr<BanInfo> banInfo = std::make_shared<BanInfo>();
banInfo->expiresAt = expiresAt;

banInfo->reason = result->getString("reason");
if (banInfo->reason.empty()) {
banInfo->reason = "(none)";
}

banInfo->bannedBy = result->getString("name");
return banInfo;
}

bool IOBan::isIpBanned(const Connection::Address& clientIP, BanInfo& banInfo)
std::shared_ptr<BanInfo> IOBan::isIpBanned(const Connection::Address& clientIP)
{
if (clientIP.is_unspecified()) {
return false;
return nullptr;
}

Database& db = Database::getInstance();
Expand All @@ -84,20 +90,26 @@ bool IOBan::isIpBanned(const Connection::Address& clientIP, BanInfo& banInfo)
"SELECT `reason`, `expires_at`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `ip_bans` WHERE `ip` = INET6_ATON('{:s}')",
clientIP.to_string()));
if (!result) {
return false;
return nullptr;
}

int64_t expiresAt = result->getNumber<int64_t>("expires_at");
if (expiresAt != 0 && time(nullptr) > expiresAt) {
g_databaseTasks.addTask(
fmt::format("DELETE FROM `ip_bans` WHERE `ip` = INET6_ATON('{:s}')", clientIP.to_string()));
return false;
return nullptr;
}

banInfo.expiresAt = expiresAt;
banInfo.reason = result->getString("reason");
banInfo.bannedBy = result->getString("name");
return true;
std::shared_ptr<BanInfo> banInfo = std::make_shared<BanInfo>();
banInfo->expiresAt = expiresAt;

banInfo->reason = result->getString("reason");
if (banInfo->reason.empty()) {
banInfo->reason = "(none)";
}

banInfo->bannedBy = result->getString("name");
return banInfo;
}

bool IOBan::isPlayerNamelocked(uint32_t playerId)
Expand Down
4 changes: 2 additions & 2 deletions src/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class Ban
class IOBan
{
public:
static bool isAccountBanned(uint32_t accountId, BanInfo& banInfo);
static bool isIpBanned(const Connection::Address& clientIP, BanInfo& banInfo);
static std::shared_ptr<BanInfo> isAccountBanned(uint32_t accountId);
static std::shared_ptr<BanInfo> isIpBanned(const Connection::Address& clientIP);
static bool isPlayerNamelocked(uint32_t playerId);
};

Expand Down
22 changes: 6 additions & 16 deletions src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,15 @@ void ProtocolGame::login(uint32_t characterId, uint32_t accountId, OperatingSyst
}

if (!player->hasFlag(PlayerFlag_CannotBeBanned)) {
BanInfo banInfo;
if (IOBan::isAccountBanned(accountId, banInfo)) {
if (banInfo.reason.empty()) {
banInfo.reason = "(none)";
}

if (banInfo.expiresAt > 0) {
if (const auto banInfo = IOBan::isAccountBanned(accountId)) {
if (banInfo->expiresAt > 0) {
disconnectClient(
fmt::format("Your account has been banned until {:s} by {:s}.\n\nReason specified:\n{:s}",
formatDateShort(banInfo.expiresAt), banInfo.bannedBy, banInfo.reason));
formatDateShort(banInfo->expiresAt), banInfo->bannedBy, banInfo->reason));
} else {
disconnectClient(
fmt::format("Your account has been permanently banned by {:s}.\n\nReason specified:\n{:s}",
banInfo.bannedBy, banInfo.reason));
banInfo->bannedBy, banInfo->reason));
}
return;
}
Expand Down Expand Up @@ -455,14 +450,9 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage& msg)
return;
}

BanInfo banInfo;
if (IOBan::isIpBanned(getIP(), banInfo)) {
if (banInfo.reason.empty()) {
banInfo.reason = "(none)";
}

if (const auto banInfo = IOBan::isIpBanned(getIP())) {
disconnectClient(fmt::format("Your IP has been banned until {:s} by {:s}.\n\nReason specified:\n{:s}",
formatDateShort(banInfo.expiresAt), banInfo.bannedBy, banInfo.reason));
formatDateShort(banInfo->expiresAt), banInfo->bannedBy, banInfo->reason));
return;
}

Expand Down
9 changes: 2 additions & 7 deletions src/protocollogin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,14 @@ void ProtocolLogin::onRecvFirstMessage(NetworkMessage& msg)
return;
}

BanInfo banInfo;
auto connection = getConnection();
if (!connection) {
return;
}

if (IOBan::isIpBanned(connection->getIP(), banInfo)) {
if (banInfo.reason.empty()) {
banInfo.reason = "(none)";
}

if (const auto banInfo = IOBan::isIpBanned(connection->getIP())) {
disconnectClient(fmt::format("Your IP has been banned until {:s} by {:s}.\n\nReason specified:\n{:s}",
formatDateShort(banInfo.expiresAt), banInfo.bannedBy, banInfo.reason),
formatDateShort(banInfo->expiresAt), banInfo->bannedBy, banInfo->reason),
version);
return;
}
Expand Down
Loading