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
|
#include "pch.h"
#include "bansystem.h"
#include "serverauthentication.h"
#include "concommand.h"
#include "miscserverscript.h"
#include <filesystem>
#include "configurables.h"
const char* BANLIST_PATH_SUFFIX = "/banlist.txt";
const char BANLIST_COMMENT_CHAR = '#';
ServerBanSystem* g_ServerBanSystem;
void ServerBanSystem::OpenBanlist()
{
std::ifstream enabledModsStream(GetNorthstarPrefix() + "/banlist.txt");
std::stringstream enabledModsStringStream;
if (!enabledModsStream.fail())
{
std::string line;
while (std::getline(enabledModsStream, line))
{
// ignore line if first char is # or line is empty
if (line == "" || line.front() == BANLIST_COMMENT_CHAR)
continue;
// remove tabs which shouldnt be there but maybe someone did the funny
line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
// remove spaces to allow for spaces before uids
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
// check if line is empty to allow for newlines in the file
if (line == "")
continue;
// for inline comments like: 123123123 #banned for unfunny
std::string uid = line.substr(0, line.find(BANLIST_COMMENT_CHAR));
m_vBannedUids.push_back(strtoull(uid.c_str(), nullptr, 10));
}
enabledModsStream.close();
}
// open write stream for banlist
m_sBanlistStream.open(GetNorthstarPrefix() + "/banlist.txt", std::ofstream::out | std::ofstream::binary | std::ofstream::app);
}
void ServerBanSystem::ClearBanlist()
{
m_vBannedUids.clear();
// reopen the file, don't provide std::ofstream::app so it clears on open
m_sBanlistStream.close();
m_sBanlistStream.open(GetNorthstarPrefix() + "/banlist.txt", std::ofstream::out | std::ofstream::binary);
}
void ServerBanSystem::BanUID(uint64_t uid)
{
// checking if last char is \n to make sure uids arent getting fucked
std::ifstream fsBanlist(GetNorthstarPrefix() + "/banlist.txt");
std::string content((std::istreambuf_iterator<char>(fsBanlist)), (std::istreambuf_iterator<char>()));
fsBanlist.close();
if (content.back() != '\n')
m_sBanlistStream << std::endl;
m_vBannedUids.push_back(uid);
m_sBanlistStream << std::to_string(uid) << std::endl;
spdlog::info("{} was banned", uid);
}
void ServerBanSystem::UnbanUID(uint64_t uid)
{
auto findResult = std::find(m_vBannedUids.begin(), m_vBannedUids.end(), uid);
if (findResult == m_vBannedUids.end())
return;
m_vBannedUids.erase(findResult);
spdlog::info("{} was unbanned", uid);
// todo: this needs to erase from the banlist file
// atm unsure how to do this aside from just clearing and fully rewriting the file
}
bool ServerBanSystem::IsUIDAllowed(uint64_t uid)
{
return std::find(m_vBannedUids.begin(), m_vBannedUids.end(), uid) == m_vBannedUids.end();
}
void BanPlayerCommand(const CCommand& args)
{
if (args.ArgC() < 2)
return;
// assuming maxplayers 32
for (int i = 0; i < 32; i++)
{
void* player = GetPlayerByIndex(i);
if (!strcmp((char*)player + 0x16, args.Arg(1)) || !strcmp((char*)player + 0xF500, args.Arg(1)))
{
g_ServerBanSystem->BanUID(strtoull((char*)player + 0xF500, nullptr, 10));
CBaseClient__Disconnect(player, 1, "Banned from server");
break;
}
}
}
void UnbanPlayerCommand(const CCommand& args)
{
if (args.ArgC() < 2)
return;
// assumedly the player being unbanned here wasn't already connected, so don't need to iterate over players or anything
g_ServerBanSystem->UnbanUID(strtoull(args.Arg(1), nullptr, 10));
}
void ClearBanlistCommand(const CCommand& args)
{
g_ServerBanSystem->ClearBanlist();
}
void InitialiseBanSystem(HMODULE baseAddress)
{
g_ServerBanSystem = new ServerBanSystem;
g_ServerBanSystem->OpenBanlist();
RegisterConCommand("ban", BanPlayerCommand, "bans a given player by uid or name", FCVAR_GAMEDLL);
RegisterConCommand("unban", UnbanPlayerCommand, "unbans a given player by uid", FCVAR_NONE);
RegisterConCommand("clearbanlist", ClearBanlistCommand, "clears all uids on the banlist", FCVAR_NONE);
}
|