Skip to content

Commit

Permalink
Remove g_bans global
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Jun 21, 2024
1 parent a490b34 commit 9c36d0c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 61 deletions.
35 changes: 0 additions & 35 deletions src/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@
#include "connection.h"
#include "database.h"
#include "databasetasks.h"
#include "tools.h"

bool Ban::acceptConnection(const Connection::Address& clientIP)
{
std::lock_guard<std::recursive_mutex> lockClass(lock);

uint64_t currentTime = OTSYS_TIME();

auto it = ipConnectMap.find(clientIP);
if (it == ipConnectMap.end()) {
ipConnectMap.emplace(clientIP, ConnectBlock(currentTime, 0, 1));
return true;
}

ConnectBlock& connectBlock = it->second;
if (connectBlock.blockTime > currentTime) {
connectBlock.blockTime += 250;
return false;
}

int64_t timeDiff = currentTime - connectBlock.lastAttempt;
connectBlock.lastAttempt = currentTime;
if (timeDiff <= 5000) {
if (++connectBlock.count > 5) {
connectBlock.count = 0;
if (timeDiff <= 500) {
connectBlock.blockTime = currentTime + 3000;
return false;
}
}
} else {
connectBlock.count = 1;
}
return true;
}

const std::optional<BanInfo> IOBan::getAccountBanInfo(uint32_t accountId)
{
Expand Down
23 changes: 0 additions & 23 deletions src/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,6 @@ struct BanInfo
time_t expiresAt;
};

struct ConnectBlock
{
constexpr ConnectBlock(uint64_t lastAttempt, uint64_t blockTime, uint32_t count) :
lastAttempt(lastAttempt), blockTime(blockTime), count(count)
{}

uint64_t lastAttempt;
uint64_t blockTime;
uint32_t count;
};

using IpConnectMap = std::map<Connection::Address, ConnectBlock>;

class Ban
{
public:
bool acceptConnection(const Connection::Address& clientIP);

private:
IpConnectMap ipConnectMap;
std::recursive_mutex lock;
};

class IOBan
{
public:
Expand Down
48 changes: 45 additions & 3 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,53 @@
#include "ban.h"
#include "configmanager.h"
#include "scheduler.h"

Ban g_bans;
#include "tools.h"

namespace {

struct ConnectBlock
{
uint64_t lastAttempt;
uint64_t blockTime = 0;
uint32_t count = 1;
};

bool acceptConnection(const Connection::Address& clientIP)
{
static std::recursive_mutex mu;
std::lock_guard lock{mu};

uint64_t currentTime = OTSYS_TIME();

static std::map<Connection::Address, ConnectBlock> ipConnectMap;
auto it = ipConnectMap.find(clientIP);
if (it == ipConnectMap.end()) {
ipConnectMap.emplace(clientIP, ConnectBlock{.lastAttempt = currentTime});
return true;
}

ConnectBlock& connectBlock = it->second;
if (connectBlock.blockTime > currentTime) {
connectBlock.blockTime += 250;
return false;
}

int64_t timeDiff = currentTime - connectBlock.lastAttempt;
connectBlock.lastAttempt = currentTime;
if (timeDiff <= 5000) {
if (++connectBlock.count > 5) {
connectBlock.count = 0;
if (timeDiff <= 500) {
connectBlock.blockTime = currentTime + 3000;
return false;
}
}
} else {
connectBlock.count = 1;
}
return true;
}

boost::asio::ip::address getListenAddress()
{
if (getBoolean(ConfigManager::BIND_ONLY_GLOBAL_ADDRESS)) {
Expand Down Expand Up @@ -103,7 +145,7 @@ void ServicePort::onAccept(Connection_ptr connection, const boost::system::error
}

const auto& remote_ip = connection->getIP();
if (g_bans.acceptConnection(remote_ip)) {
if (acceptConnection(remote_ip)) {
Service_ptr service = services.front();
if (service->is_single_socket()) {
connection->accept(service->make_protocol(connection));
Expand Down

0 comments on commit 9c36d0c

Please sign in to comment.