Add white list feature

dev v0.4
marcelb 11 months ago
parent b149de071d
commit adad9f3b31
  1. 25
      lib/ipban.hpp
  2. 53
      src/ipban.cpp

@ -8,6 +8,7 @@
#include <future> #include <future>
#include <mutex> #include <mutex>
#include <fstream> #include <fstream>
#include <algorithm>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -44,13 +45,14 @@ struct _fail {
* Posjeduje vlastiti DB mehanizam za zaštitu od nepovratnog ban-a * Posjeduje vlastiti DB mehanizam za zaštitu od nepovratnog ban-a
*/ */
class ipban { class ipban {
mutex io, f_io; mutex io, f_io, wl_io;
time_t ban_duration; time_t ban_duration;
uint fail_interval; uint fail_interval;
uint fail_limit; uint fail_limit;
string db_file; string db_file;
vector<_ban> banned; vector<_ban> banned;
map<string, struct _fail> failed; map<string, struct _fail> failed;
vector<string> white_list;
future<void> unban_bot; future<void> unban_bot;
bool run_unban_bot = true; bool run_unban_bot = true;
// interface možda bude trebao za ban // interface možda bude trebao za ban
@ -100,7 +102,7 @@ class ipban {
* ako se prekorači broj dozvoljenih grešaka u intervalu - adresa se banuje * ako se prekorači broj dozvoljenih grešaka u intervalu - adresa se banuje
*/ */
void fail(const string& ip); bool fail(const string& ip);
/** /**
* Uklanja greške za prosljeđenu adresu * Uklanja greške za prosljeđenu adresu
@ -108,6 +110,25 @@ class ipban {
bool unfail(const string& ip); bool unfail(const string& ip);
/**
* Dodaje proslijeđenu adresu u white listu
*/
void add_white_list(const string& ip);
/**
* Dodaje proslijeđene adrese u white listu
*/
void add_white_list(const vector<string>& ips);
/**
* Provjerava da li je prosljeđena adresa u white listi
* Ako je vraća true, ako ne false
*/
bool is_in_white_list(const string& ip);
/** /**
* Destruktor, uklanja sve zabrane. * Destruktor, uklanja sve zabrane.
*/ */

@ -73,11 +73,14 @@ bool marcelb::ipban::update_db() {
} }
bool marcelb::ipban::ban(const string& ip) { bool marcelb::ipban::ban(const string& ip) {
bool status = ufw_ban(ip); bool status = !is_in_white_list(ip);
io.lock(); if (status) {
banned.push_back({ip, time(NULL)}); status = ufw_ban(ip);
status = status && update_db(); io.lock();
io.unlock(); banned.push_back({ip, time(NULL)});
status = status && update_db();
io.unlock();
}
return status; return status;
} }
@ -91,7 +94,7 @@ bool marcelb::ipban::unban(vector<_ban>::iterator ban_itr) {
} }
bool marcelb::ipban::ufw_ban(const string& ip) { bool marcelb::ipban::ufw_ban(const string& ip) {
string ufw_cmd = "sudo ufw deny from " + ip + " to any"; string ufw_cmd = "sudo ufw insert 1 deny from " + ip + " to any";
try { try {
string execute_res = exec(ufw_cmd); string execute_res = exec(ufw_cmd);
if (execute_res == "Rule added\n") { if (execute_res == "Rule added\n") {
@ -118,15 +121,19 @@ bool marcelb::ipban::ufw_unban(const string& ip) {
} }
void marcelb::ipban::fail(const string& ip) { bool marcelb::ipban::fail(const string& ip) {
lock_guard<mutex> _io(f_io); lock_guard<mutex> _io(f_io);
if (failed[ip].n_fails == 0) { bool status = !is_in_white_list(ip);
failed[ip].n_fails = 1; if (status) {
failed[ip].first_fail = time(NULL); if (failed[ip].n_fails == 0) {
} else if (++failed[ip].n_fails >= fail_limit) { failed[ip].n_fails = 1;
ban(ip); failed[ip].first_fail = time(NULL);
failed.erase(ip); } else if (++failed[ip].n_fails >= fail_limit) {
ban(ip);
failed.erase(ip);
}
} }
return status;
} }
@ -136,6 +143,26 @@ bool marcelb::ipban::unfail(const string& ip) {
} }
void marcelb::ipban::add_white_list(const string& ip) {
lock_guard<mutex> _io(wl_io);
if (find(white_list.begin(), white_list.end(), ip) == white_list.end()) {
white_list.push_back(ip);
}
}
void marcelb::ipban::add_white_list(const vector<string>& ips) {
for (auto ip : ips) {
add_white_list(ip);
}
}
bool marcelb::ipban::is_in_white_list(const string& ip) {
return find(white_list.begin(), white_list.end(), ip) != white_list.end();
}
static void marcelb::sleep_if(const uint& _time, const bool& _condition) { static void marcelb::sleep_if(const uint& _time, const bool& _condition) {
time_t start_time = time(NULL); time_t start_time = time(NULL);
do { do {

Loading…
Cancel
Save