-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrules.cpp
425 lines (417 loc) · 15 KB
/
rules.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "rules.hpp"
// class RulesList
template<class T>
RulesList<T>::RulesList(boost::program_options::options_description opt)
: parse_opt_(opt), last_update_(std::chrono::high_resolution_clock::now())
{}
template<class T>
bool RulesList<T>::operator==(const RulesList& other) const
{
boost::lock(m_, other.m_);
boost::lock_guard<boost::shared_mutex> g(m_, boost::adopt_lock);
boost::lock_guard<boost::shared_mutex> g_other(other.m_, boost::adopt_lock);
return (rules_ == other.rules_);
}
template<class T>
RulesList<T>& RulesList<T>::operator=(const RulesList& other)
{
if (this != &other)
{
boost::lock(m_, other.m_);
boost::lock_guard<boost::shared_mutex> g(m_, boost::adopt_lock);
boost::lock_guard<boost::shared_mutex> g_other(other.m_, boost::adopt_lock);
rules_ = other.rules_;
last_update_ = other.last_update_;
}
return *this;
}
template<class T>
RulesList<T>& RulesList<T>::operator+=(RulesList& other)
{
if (this != &other)
{
last_update_ = std::chrono::high_resolution_clock::now(); // current time point
boost::lock(m_, other.m_);
boost::lock_guard<boost::shared_mutex> g(m_, boost::adopt_lock);
boost::lock_guard<boost::shared_mutex> g_other(other.m_, boost::adopt_lock);
if(rules_.size() == other.rules_.size())
{
for(unsigned int i=0; i < rules_.size(); i++)
{
// прибавляем счетчики правил из other, затем обнуляем счетчики в other
rules_[i] += other.rules_[i];
}
}
else
{
throw RuleException("Current rules list size() != new rules list size()");
}
}
return *this;
}
template<class T>
void RulesList<T>::calc_delta(const RulesList& rules_old)
{
if(this != &rules_old)
{
boost::lock(m_, rules_old.m_);
boost::lock_guard<boost::shared_mutex> g(m_, boost::adopt_lock);
boost::lock_guard<boost::shared_mutex> g_old(rules_old.m_, boost::adopt_lock);
if(rules_.size() == rules_old.rules_.size())
{
// сколько прошло миллисекунд между итерациями
double delta_time = std::chrono::duration<double, std::milli>(
last_update_ - rules_old.last_update_).count();
uint64_t delta_c = 0;
for(unsigned int i=0; i < rules_.size(); i++)
{
// считаем дельту пакет/сек
delta_c = rules_[i].count_packets - rules_old.rules_[i].count_packets;
rules_[i].pps = round((delta_c / delta_time) * 1000);
// считаем дельту байт/сек
delta_c = rules_[i].count_bytes - rules_old.rules_[i].count_bytes;
rules_[i].bps = round((delta_c / delta_time) * 1000);
}
}
}
}
template<class T>
void RulesList<T>::check_triggers(ts_queue<action::TriggerJob>& task_list,
InfluxClient& influx)
{
boost::lock_guard<boost::shared_mutex> guard(m_);
for(auto& r: rules_)
{
if(r.is_triggered()) // если триггер сработал
{
// добавляем задание триггера в очередь обработчика заданий
task_list.push(action::TriggerJob(r.act, r.get_job_info()));
// отправляем event в базу
influx.insert(r.get_trigger_influx());
}
// очищаем проверочные счетчики, чтобы не забивать память
r.dst_top.clear();
}
}
template<class T>
void RulesList<T>::add_rule(T rule)
{
rule.parse(parse_opt_);
boost::lock_guard<boost::shared_mutex> guard(m_);
rules_.push_back(rule);
}
template<class T>
void RulesList<T>::clear()
{
boost::lock_guard<boost::shared_mutex> guard(m_);
rules_.clear();
}
template<class T>
void RulesList<T>::del_rule(const unsigned int num)
{
boost::lock_guard<boost::shared_mutex> guard(m_);
if(rules_.empty())
throw RuleException("rules list is empty");
if(num > (rules_.size()-1))
throw RuleException("not found " + to_string(num) + " rule");
rules_.erase(rules_.begin() + num);
}
template<class T>
void RulesList<T>::insert_rule(const unsigned int num, T rule)
{
rule.parse(parse_opt_);
boost::lock_guard<boost::shared_mutex> guard(m_);
if(rules_.empty())
throw RuleException("rules list is empty");
if(num > (rules_.size()-1))
throw RuleException("incorrect number rule '" + to_string(num)
+ "', it should be: 0 < num < " + to_string(rules_.size()));
// сдвигаем все элементы списка начиная с num-того
// вперед и добавляем новый элемент
std::vector<T> temp;
temp.reserve(rules_.size() + 1);
temp.insert(temp.end(), rules_.begin(), rules_.begin()+num);
temp.push_back(rule);
temp.insert(temp.end(), rules_.begin()+num, rules_.end());
rules_ = temp;
}
template<class T>
std::string RulesList<T>::get_rules()
{
std::string res = "";
uint64_t all_count_packets = 0;
uint64_t all_count_bytes = 0;
uint64_t all_pps = 0;
uint64_t all_bps = 0;
unsigned int max_text_size = 0;
boost::format num_f("%5s"); // форматируем по ширине вывод номеров правил
boost::shared_lock<boost::shared_mutex> guard(m_);
for (unsigned int i=0; i<rules_.size(); i++) // /находим самую длинную строку
{
if(rules_[i].text_rule.length() > max_text_size)
{
max_text_size = rules_[i].text_rule.length();
}
}
for (unsigned int i=0; i<rules_.size(); i++)
{
res += boost::str(num_f % to_string(i))
+ ": "
// форматируем ширину всех строк по самой длинной строке
+ format_len(rules_[i].text_rule, max_text_size)
+ " : "
+ parser::to_short_size(rules_[i].pps, false) + " ("
+ parser::to_short_size(rules_[i].bps, true) + "), "
+ to_string(rules_[i].count_packets) + " packets, "
+ to_string(rules_[i].count_bytes) + " bytes\n";
all_count_packets += rules_[i].count_packets;
all_count_bytes += rules_[i].count_bytes;
all_pps += rules_[i].pps;
all_bps += rules_[i].bps;
}
res += "Total: "
+ parser::to_short_size(all_pps, false) + " ("
+ parser::to_short_size(all_bps, true) + "), "
+ to_string(all_count_packets) + " packets, "
+ to_string(all_count_bytes) + " bytes\n\n";
return res;
}
template<class T>
std::string RulesList<T>::get_influx_querys()
{
std::string res = "";
std::string t = "none";
uint64_t all_count_packets = 0;
uint64_t all_count_bytes = 0;
uint64_t all_pps = 0;
uint64_t all_bps = 0;
boost::shared_lock<boost::shared_mutex> guard(m_);
for (unsigned int i=0; i<rules_.size(); i++)
{
res += rules_[i].rule_type
+ ",rule=" + std::to_string(i)
+ " pps=" + to_string(rules_[i].pps)
+ ",bps=" + to_string(rules_[i].bps * 8)
// + ",pk=" + to_string(rules_[i].count_packets)
// + ",bt=" + to_string(rules_[i].count_bytes)
+ " {timestamp}\n";
all_count_packets += rules_[i].count_packets;
all_count_bytes += rules_[i].count_bytes;
all_pps += rules_[i].pps;
all_bps += rules_[i].bps;
t = rules_[i].rule_type;
}
res += "total,type=" + t
+ " pps=" + to_string(all_pps)
+ ",bps=" + to_string(all_bps * 8)
// + ",pk=" + to_string(all_count_packets)
// + ",bt=" + to_string(all_count_bytes)
+ " {timestamp}\n";
return res;
}
template<class T>
boost::program_options::options_description RulesList<T>::get_params() const
{
return parse_opt_;
}
// struct RulesCollection
RulesCollection::RulesCollection(
boost::program_options::options_description& help_opt,
boost::program_options::options_description& tcp_opt,
boost::program_options::options_description& udp_opt,
boost::program_options::options_description& icmp_opt)
: types_({"TCP", "UDP", "ICMP"}), help_(help_opt),
tcp(tcp_opt), udp(udp_opt), icmp(icmp_opt),
last_change(std::chrono::high_resolution_clock::now()) {}
RulesCollection::RulesCollection(const RulesCollection& parent, bool clear)
: types_({"TCP", "UDP", "ICMP"}), tcp(parent.tcp.get_params()),
udp(parent.udp.get_params()), icmp(parent.icmp.get_params()),
last_change(std::chrono::high_resolution_clock::now())
{
tcp = parent.tcp;
udp = parent.udp;
icmp = parent.icmp;
if(clear)
{ // очищаем правила в списках правил
tcp.clear();
udp.clear();
icmp.clear();
}
last_change = std::chrono::high_resolution_clock::now();
}
bool RulesCollection::operator!=(const RulesCollection& other) const
{
return !(tcp == other.tcp && udp == other.udp && icmp == other.icmp);
}
RulesCollection& RulesCollection::operator=(const RulesCollection& other)
{
if (this != &other)
{
types_ = other.types_;
tcp = other.tcp;
udp = other.udp;
icmp = other.icmp;
last_change = std::chrono::high_resolution_clock::now();
}
return *this;
}
RulesCollection& RulesCollection::operator+=(RulesCollection& other)
{
if (this != &other)
{
tcp += other.tcp;
udp += other.udp;
icmp += other.icmp;
}
return *this;
}
std::string RulesCollection::get_help() const
{
std::ostringstream stream;
stream << help_;
return stream.str();
}
std::string RulesCollection::get_rules()
{
std::string cnt;
cnt += "TCP rules (num, rule, counter):\n";
cnt += tcp.get_rules();
cnt += "UDP rules (num, rule, counter):\n";
cnt += udp.get_rules();
cnt += "ICMP rules (num, rule, counter):\n";
cnt += icmp.get_rules();
return cnt;
}
std::string RulesCollection::get_influx_querys()
{
std::string querys;
querys += tcp.get_influx_querys();
querys += udp.get_influx_querys();
querys += icmp.get_influx_querys();
std::string cur_time = std::to_string(std::time(0)) + "000000000";
boost::replace_all(querys, "{timestamp}", cur_time);
return querys;
}
bool RulesCollection::is_type(const std::string& type) const
{
if (std::find(types_.begin(), types_.end(), type) != types_.end())
{
return true;
}
return false;
}
void RulesCollection::calc_delta(const RulesCollection& old)
{
if (this != &old)
{
tcp.calc_delta(old.tcp);
udp.calc_delta(old.udp);
icmp.calc_delta(old.icmp);
}
}
void RulesCollection::check_triggers(ts_queue<action::TriggerJob>& task_list,
InfluxClient& influx)
{
tcp.check_triggers(task_list, influx);
udp.check_triggers(task_list, influx);
icmp.check_triggers(task_list, influx);
}
// class RulesFileLoader
RulesFileLoader::RulesFileLoader(boost::asio::io_service& service,
const std::string& file, std::shared_ptr<RulesCollection>& c)
: sig_set_(service, SIGHUP), rules_config_file_(file), collect_(c) {}
void RulesFileLoader::reload_config()
{
if(is_file_exist(rules_config_file_))
{
std::ifstream r_file(rules_config_file_);
std::string line;
// Создаем копию текущей коллекции правил и очищаем правила в листах
RulesCollection buff_collect(*collect_, true);
while(std::getline(r_file, line))
{
// разбиваем строку в вектор по пробелам
std::vector<std::string> t_cmd = tokenize(line);
if(t_cmd.size() > 1)
{
try
{
if(t_cmd[0].at(0) == '#')
{
continue;
}
if(t_cmd[0] == "TCP") // добавляем TCP правило
{
buff_collect.tcp.add_rule(
TcpRule(std::vector<std::string>(
t_cmd.begin() + 1, t_cmd.end()
)
)
);
}
else if(t_cmd[0] == "UDP") // добавлем UDP правило
{
buff_collect.udp.add_rule(
UdpRule(std::vector<std::string>(
t_cmd.begin() + 1, t_cmd.end()
)
)
);
}
else if(t_cmd[0] == "ICMP") // добавляем ICMP правило
{
buff_collect.icmp.add_rule(
IcmpRule(std::vector<std::string>(
t_cmd.begin() + 1, t_cmd.end()
)
)
);
}
else // если правило неопределенного типа
{
logger << log4cpp::Priority::ERROR
<< "Not found rule type '" + t_cmd[0] + "'";
}
}
catch(const std::exception& e)
{
logger << log4cpp::Priority::ERROR
<< "Load rule failed: " << e.what();
}
}
}
*collect_ = buff_collect;
logger << log4cpp::Priority::INFO << "Rules from file "
<< rules_config_file_
<< " loaded";
}
else
{
logger << log4cpp::Priority::ERROR << "File "
<< rules_config_file_
<< " not found";
}
}
void RulesFileLoader::sig_hook(boost::asio::signal_set& this_set_,
boost::system::error_code error, int signal_number)
{
if (!error)
{
// загружаем правила из файла
reload_config();
// добавляем новое асинхронное задание для сигнала
sig_set_.async_wait(boost::bind(&RulesFileLoader::sig_hook,
this, boost::ref(sig_set_), _1, _2));
}
}
void RulesFileLoader::start()
{
// загружаем правила из файла
reload_config();
// добавляем новое асинхронное задание для сигнала
sig_set_.async_wait(boost::bind(&RulesFileLoader::sig_hook,
this, boost::ref(sig_set_), _1, _2));
}
template class RulesList<TcpRule>;
template class RulesList<UdpRule>;
template class RulesList<IcmpRule>;