diff --git a/lib/ipban.hpp b/lib/ipban.hpp index 93f97b5..80ccf49 100644 --- a/lib/ipban.hpp +++ b/lib/ipban.hpp @@ -71,6 +71,11 @@ class ipban { */ bool unban(vector<_ban>::iterator ban_itr); + /** + * The method checks if the forwarding ip address is currently banned + */ + bool is_banned(const string& ip); + /** * Method calls exec and adds rules to UFW firewall */ diff --git a/src/ipban.cpp b/src/ipban.cpp index 735aa9d..98ce2a8 100644 --- a/src/ipban.cpp +++ b/src/ipban.cpp @@ -67,6 +67,9 @@ bool marcelb::ipban::update_db() { bool marcelb::ipban::ban(const string& ip) { bool status = !is_in_white_list(ip); if (status) { + if (is_banned(ip)) { + return status; + } status = ufw_ban(ip); io.lock(); banned.push_back({ip, time(NULL)}); @@ -85,6 +88,16 @@ bool marcelb::ipban::unban(vector<_ban>::iterator ban_itr) { return status; } +bool marcelb::ipban::is_banned(const string& ip) { + auto it = std::find_if(banned.begin(), banned.end(), [&](const struct _ban& an_ban){ + return an_ban.ip == ip; + }); + if (it == banned.end()) { + return false; + } + return true; +} + bool marcelb::ipban::ufw_ban(const string& ip) { string ufw_cmd = "sudo ufw insert 1 deny from " + ip + " to any"; try { diff --git a/test/test b/test/test index 17fed07..d36ba6b 100755 Binary files a/test/test and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp index 7dbb3e9..5de67b7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -36,7 +36,7 @@ int main() { // myban.add_white_list("192.168.2.74"); - // cout << myban.ban("192.168.2.74") << endl; + cout << myban.ban("192.168.2.74") << endl; // sleep(2); // cout << myban.ban("192.168.2.75") << endl; // // sleep(200);