forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ban.cpp
127 lines (107 loc) · 3.99 KB
/
ban.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2016 Mark Samman <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "ban.h"
#include "database.h"
#include "databasetasks.h"
#include "tools.h"
bool Ban::acceptConnection(uint32_t 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;
}
bool IOBan::isAccountBanned(uint32_t accountId, BanInfo& banInfo)
{
Database* db = Database::getInstance();
std::ostringstream query;
query << "SELECT `reason`, `expires_at`, `banned_at`, `banned_by`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `account_bans` WHERE `account_id` = " << accountId;
DBResult_ptr result = db->storeQuery(query.str());
if (!result) {
return false;
}
int64_t expiresAt = result->getNumber<int64_t>("expires_at");
if (expiresAt != 0 && time(nullptr) > expiresAt) {
// Move the ban to history if it has expired
query.str(std::string());
query << "INSERT INTO `account_ban_history` (`account_id`, `reason`, `banned_at`, `expired_at`, `banned_by`) VALUES (" << accountId << ',' << db->escapeString(result->getString("reason")) << ',' << result->getNumber<time_t>("banned_at") << ',' << expiresAt << ',' << result->getNumber<uint32_t>("banned_by") << ')';
g_databaseTasks.addTask(query.str());
query.str(std::string());
query << "DELETE FROM `account_bans` WHERE `account_id` = " << accountId;
g_databaseTasks.addTask(query.str());
return false;
}
banInfo.expiresAt = expiresAt;
banInfo.reason = result->getString("reason");
banInfo.bannedBy = result->getString("name");
return true;
}
bool IOBan::isIpBanned(uint32_t clientip, BanInfo& banInfo)
{
if (clientip == 0) {
return false;
}
Database* db = Database::getInstance();
std::ostringstream query;
query << "SELECT `reason`, `expires_at`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `ip_bans` WHERE `ip` = " << clientip;
DBResult_ptr result = db->storeQuery(query.str());
if (!result) {
return false;
}
int64_t expiresAt = result->getNumber<int64_t>("expires_at");
if (expiresAt != 0 && time(nullptr) > expiresAt) {
query.str(std::string());
query << "DELETE FROM `ip_bans` WHERE `ip` = " << clientip;
g_databaseTasks.addTask(query.str());
return false;
}
banInfo.expiresAt = expiresAt;
banInfo.reason = result->getString("reason");
banInfo.bannedBy = result->getString("name");
return true;
}
bool IOBan::isPlayerNamelocked(uint32_t playerId)
{
std::ostringstream query;
query << "SELECT 1 FROM `player_namelocks` WHERE `player_id` = " << playerId;
return Database::getInstance()->storeQuery(query.str()).get() != nullptr;
}