-
Notifications
You must be signed in to change notification settings - Fork 13
/
RenX_BanDatabase.h
235 lines (206 loc) · 8.2 KB
/
RenX_BanDatabase.h
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Copyright (C) 2014-2017 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _RENX_BANDATABASE_H_HEADER
#define _RENX_BANDATABASE_H_HEADER
#include <cstdint>
#include <unordered_map>
#include "Jupiter/Database.h"
#include "Jupiter/String.hpp"
#include "Jupiter/ArrayList.h"
#include "RenX.h"
/** DLL Linkage Nagging */
#if defined _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251)
#endif
namespace RenX
{
struct PlayerInfo;
class Server;
/**
* @brief Represents the local ban database.
*/
class RENX_API BanDatabase : public Jupiter::Database
{
public: // Jupiter::Database
/**
* @brief Processes a chunk of data in a database.
*
* @param buffer Buffer to process
* @param file File being processed
* @param pos position that the buffer starts at in the file
*/
void process_data(Jupiter::DataBuffer &buffer, FILE *file, fpos_t pos) override;
/**
* @brief Processes the header for a database.
*
* @param file File being processed
*/
void process_header(FILE *file) override;
/**
* @brief Generates a header for a database.
*
* @param file File being created
*/
void create_header(FILE *file) override;
/**
* @brief Called when process_file() is successfully completed.
*
* @param file File being processed
*/
void process_file_finish(FILE *file) override;
public:
/**
* @brief Represents a Ban entry in the database.
*/
struct RENX_API Entry
{
using VarDataTableType = std::unordered_map<Jupiter::StringS, Jupiter::StringS, Jupiter::default_hash_function>;
fpos_t pos; /** Position of the entry in the database */
uint16_t flags /** Flags affecting this ban entry (See below for flags) */ = 0x00;
std::chrono::system_clock::time_point timestamp /** Time the ban was created */;
std::chrono::seconds length /** Duration of the ban; 0 if permanent */;
uint64_t steamid /** SteamID of the banned player */;
uint32_t ip /** IPv4 address of the banned player */;
uint8_t prefix_length /** Prefix length for the IPv4 address block */;
Jupiter::StringS hwid; /** Hardware ID of the banned player */
Jupiter::StringS rdns /** RDNS of the banned player */;
Jupiter::StringS name /** Name of the banned player */;
Jupiter::StringS banner /** Name of the user who initiated the ban */;
Jupiter::StringS reason /** Reason the player was banned */;
VarDataTableType varData; /** Variable entry data */
static const uint16_t FLAG_ACTIVE = 0x8000U;
static const uint16_t FLAG_USE_RDNS = 0x4000U;
static const uint16_t FLAG_TYPE_GAME = 0x0080U;
static const uint16_t FLAG_TYPE_CHAT = 0x0040U;
static const uint16_t FLAG_TYPE_BOT = 0x0020U;
static const uint16_t FLAG_TYPE_VOTE = 0x0010U;
static const uint16_t FLAG_TYPE_MINE = 0x0008U;
static const uint16_t FLAG_TYPE_LADDER = 0x0004U;
static const uint16_t FLAG_TYPE_ALERT = 0x0002U;
inline bool is_active() { return (flags & FLAG_ACTIVE) != 0; };
inline bool is_rdns_ban() { return (flags & FLAG_USE_RDNS) != 0; };
inline bool is_type_game() { return (flags & FLAG_TYPE_GAME) != 0; };
inline bool is_type_chat() { return (flags & FLAG_TYPE_CHAT) != 0; };
inline bool is_type_bot() { return (flags & FLAG_TYPE_BOT) != 0; };
inline bool is_type_vote() { return (flags & FLAG_TYPE_VOTE) != 0; };
inline bool is_type_mine() { return (flags & FLAG_TYPE_MINE) != 0; };
inline bool is_type_ladder() { return (flags & FLAG_TYPE_LADDER) != 0; };
inline bool is_type_alert() { return (flags & FLAG_TYPE_ALERT) != 0; };
inline void set_active() { flags |= FLAG_ACTIVE; };
inline void set_rdns_ban() { flags |= FLAG_USE_RDNS; }
inline void set_type_game() { flags |= FLAG_TYPE_GAME; };
inline void set_type_chat() { flags |= FLAG_TYPE_CHAT; };
inline void set_type_bot() { flags |= FLAG_TYPE_BOT; };
inline void set_type_vote() { flags |= FLAG_TYPE_VOTE; };
inline void set_type_mine() { flags |= FLAG_TYPE_MINE; };
inline void set_type_ladder() { flags |= FLAG_TYPE_LADDER; };
inline void set_type_alert() { flags |= FLAG_TYPE_ALERT; };
inline void set_type_global() { flags = 0xFFFFU; };
inline void unset_active() { flags &= ~FLAG_ACTIVE; };
inline void unset_rdns_ban() { flags &= ~FLAG_USE_RDNS; }
inline void unset_type_game() { flags &= ~FLAG_TYPE_GAME; };
inline void unset_type_chat() { flags &= ~FLAG_TYPE_CHAT; };
inline void unset_type_bot() { flags &= ~FLAG_TYPE_BOT; };
inline void unset_type_vote() { flags &= ~FLAG_TYPE_VOTE; };
inline void unset_type_mine() { flags &= ~FLAG_TYPE_MINE; };
inline void unset_type_ladder() { flags &= ~FLAG_TYPE_LADDER; };
inline void unset_type_alert() { flags &= ~FLAG_TYPE_ALERT; };
inline void unset_type_global() { flags = 0x0000U; };
};
/**
* @brief Adds a ban entry for a player and immediately writes it to the database.
*
* @param server Server the player is playing in
* @param player Data of the player to be banned
* @param length Duration of the ban
*/
void add(RenX::Server *server, const RenX::PlayerInfo &player, const Jupiter::ReadableString &banner, const Jupiter::ReadableString &reason, std::chrono::seconds length, uint16_t flags = RenX::BanDatabase::Entry::FLAG_TYPE_GAME);
/**
* @brief Adds a ban entry for a set of player information and immediately writes it to the database.
*
* @param name Name of the player to ban
* @param ip IPv4 address of the player to ban
* @param steamid SteamID of the player to ban
* @param hwid Hardware ID of the player to ban
* @param rdns RDNS of the player to ban
* @param banner Person implementing the ban
* @param reason Reason the player is getting banned
* @param length Duration of the ban
*/
void add(const Jupiter::ReadableString &name, uint32_t ip, uint8_t prefix_length, uint64_t steamid, const Jupiter::ReadableString &hwid, const Jupiter::ReadableString &rdns, const Jupiter::ReadableString &banner, Jupiter::ReadableString &reason, std::chrono::seconds length, uint16_t flags = RenX::BanDatabase::Entry::FLAG_TYPE_GAME);
/**
* @brief Upgrades the ban database to the current write_version.
*/
void upgrade_database();
/**
* @brief Writes a ban entry to the database.
*
* @param entry Entry to write to the database.
*/
void write(Entry *entry);
/**
* @brief Writes a ban entry to the database.
*
* @param entry Entry to write to the database.
* @param file FILE stream to write to.
*/
void write(Entry *entry, FILE *file);
/**
* @brief Deactivates a ban entry.
*
* @param index Index of the entry to deactivate.
* @param True if the entry was active and is now inactive, false otherwise.
*/
bool deactivate(size_t index);
/**
* @brief Fetches the version of the database file.
*
* @return Database version
*/
uint8_t getVersion() const;
/**
* @brief Fetches the name of the database file.
*
* @return Database file name
*/
const std::string &getFileName() const;
/**
* @brief Fetches the list of ban entries.
*
* @return List of entries
*/
const Jupiter::ArrayList<RenX::BanDatabase::Entry> &getEntries() const;
virtual bool initialize();
~BanDatabase();
private:
/** Database version */
const uint8_t write_version = 5U;
uint8_t read_version = write_version;
fpos_t eof;
std::string filename;
Jupiter::ArrayList<RenX::BanDatabase::Entry> entries;
};
RENX_API extern RenX::BanDatabase *banDatabase;
RENX_API extern RenX::BanDatabase &defaultBanDatabase;
}
/** Re-enable warnings */
#if defined _MSC_VER
#pragma warning(pop)
#endif
#endif // _RENX_BANDATABASE_H_HEADER