-
Notifications
You must be signed in to change notification settings - Fork 8
/
WhiteBlackList.cpp
169 lines (148 loc) · 4.64 KB
/
WhiteBlackList.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
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
#include "config.h"
#include "json/CJsonObject.h"
#include "WhiteBlackList.h"
#include <algorithm>
#include <mutex>
static std::list<std::string> WhiteList,BlackList;
static std::mutex WhiteBlackListMutex;
static bool WhiteListStatus = false;
/*±¾º¯Êý²»×ö¶àÏ̱߳£»¤*/
static void SaveWhiteBlackList()
{
neb::CJsonObject config;
config.AddEmptySubArray("WhiteList");
config.AddEmptySubArray("BlackList");
for (auto& uuid : WhiteList)
{
config["WhiteList"].Add(uuid);
}
for (auto& uuid : BlackList)
{
config["BlackList"].Add(uuid);
}
RbsLib::Storage::StorageFile file(Config::get_config<std::string>("WhiteBlcakListPath"));
file.Open(RbsLib::Storage::FileIO::OpenMode::Write| RbsLib::Storage::FileIO::OpenMode::Replace, RbsLib::Storage::FileIO::SeekBase::begin, 0L).Write(RbsLib::Buffer(config.ToFormattedString()));
}
void WhiteBlackList::WhiteListOn()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
WhiteListStatus = true;
}
void WhiteBlackList::WhiteListOff()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
WhiteListStatus = false;
}
void WhiteBlackList::Init()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
neb::CJsonObject config;
RbsLib::Storage::StorageFile file(Config::get_config<std::string>("WhiteBlcakListPath"));
if (!file.IsExist())
{
file.Open(RbsLib::Storage::FileIO::OpenMode::Write, RbsLib::Storage::FileIO::SeekBase::begin, 0L).Write(RbsLib::Buffer("{\"WhiteList\":[],\"BlackList\":[]}"));
}
auto buffer = file.Open(RbsLib::Storage::FileIO::OpenMode::Read,RbsLib::Storage::FileIO::SeekBase::begin,0L).Read(file.GetFileSize());
if (!config.Parse(buffer.ToString()))
{
throw WhiteBlackListException("WhiteBlackList config file parse failed");
}
for (int i=0;i<config["WhiteList"].GetArraySize();i++)
{
WhiteList.push_back(config["WhiteList"](i));
}
for (int i = 0; i < config["BlackList"].GetArraySize(); i++)
{
BlackList.push_back(config["BlackList"](i));
}
lock.unlock();
Config::get_config<bool>("DefaultEnableWhitelist") ? WhiteListOn() : WhiteListOff();
}
void WhiteBlackList::AddWhiteList(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
if (std::find(WhiteList.begin(), WhiteList.end(), uuid) != WhiteList.end())
{
throw WhiteBlackListException("User already in WhiteList");
}
WhiteList.push_back(uuid);
SaveWhiteBlackList();
}
void WhiteBlackList::AddBlackList(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
if (std::find(BlackList.begin(), BlackList.end(), uuid) != BlackList.end())
{
throw WhiteBlackListException("User already in BlackList");
}
BlackList.push_back(uuid);
SaveWhiteBlackList();
}
void WhiteBlackList::RemoveWhiteList(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
auto iterator = std::find(WhiteList.begin(), WhiteList.end(), uuid);
if (iterator == WhiteList.end())
{
throw WhiteBlackListException("User not in WhiteList");
}
WhiteList.erase(iterator);
SaveWhiteBlackList();
}
void WhiteBlackList::RemoveBlackList(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
auto iterator = std::find(BlackList.begin(), BlackList.end(), uuid);
if (iterator == BlackList.end())
{
throw WhiteBlackListException("uuid not in BlackList");
}
BlackList.erase(iterator);
SaveWhiteBlackList();
}
void WhiteBlackList::ClearWhiteList()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
WhiteList.clear();
SaveWhiteBlackList();
}
void WhiteBlackList::ClearBlackList()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
BlackList.clear();
SaveWhiteBlackList();
}
bool WhiteBlackList::IsInWhite(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
if (WhiteListStatus == false) return true;
return std::find(WhiteList.begin(), WhiteList.end(), uuid) != WhiteList.end();
}
bool WhiteBlackList::IsInBlack(const std::string& uuid)
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
return std::find(BlackList.begin(), BlackList.end(), uuid) != BlackList.end();
}
auto WhiteBlackList::GetWhiteList() -> std::list<std::string>
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
return WhiteList;
}
auto WhiteBlackList::GetBlackList() -> std::list<std::string>
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
return BlackList;
}
bool WhiteBlackList::IsWhiteListOn()
{
std::unique_lock<std::mutex> lock(WhiteBlackListMutex);
return WhiteListStatus;
}
WhiteBlackList::WhiteBlackListException::WhiteBlackListException(const std::string& message) noexcept
: message(message)
{
}
const char* WhiteBlackList::WhiteBlackListException::what() const noexcept
{
return message.c_str();
}