diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33a9488 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +example diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..930ff05 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.tcc": "cpp" + } +} \ No newline at end of file diff --git a/lib/ipban.hpp b/lib/ipban.hpp new file mode 100644 index 0000000..46ab509 --- /dev/null +++ b/lib/ipban.hpp @@ -0,0 +1,48 @@ +#ifndef IP_BAN +#define IP_BAN + +#include +#include +#include +#include +#include + +#include +#include + +using namespace std; + +namespace marcelb { + +#define BOT_LOOP_TIME 60 // 1 minutes +#define BOT_SLEEP_LOOP_TIME 1 // 1 second + +struct _ban { + string ip; + time_t _time; +}; + +class ipban { + mutex io; + time_t ban_duration; // u sekundama + vector<_ban> banned; + future unban_bot; + bool run_unban_bot = true; + // interface možda bude trebao za ban + + bool unban(vector<_ban>::iterator ban_itr); + bool ufw_ban(const string& ip); + bool ufw_unban(const string& ip); + + public: + ipban(const uint& _duration); // u minutama? + bool ban(const string& ip); + ~ipban(); + +}; + +static void sleep_if(const uint& _time, const bool& _condition); + +} + +#endif \ No newline at end of file diff --git a/src/ipban.cpp b/src/ipban.cpp new file mode 100644 index 0000000..8214869 --- /dev/null +++ b/src/ipban.cpp @@ -0,0 +1,59 @@ +#include "../lib/ipban.hpp" + +marcelb::ipban::ipban(const uint& _duration) { + ban_duration = _duration*60; + + unban_bot = async(launch::async, [&]() { + while (run_unban_bot) { + sleep_if(BOT_LOOP_TIME, run_unban_bot); + cout << "Sleep" << endl; + io.lock(); + for (uint i=0; i= ban_duration) { + unban(banned.begin() + i); + } + } + io.unlock(); + } + return; + }); + +} + +marcelb::ipban::~ipban() { + run_unban_bot = false; + unban_bot.get(); + for (uint i=0; i::iterator ban_itr) { + ufw_unban(ban_itr->ip); + io.lock(); + banned.erase(ban_itr); + io.unlock(); +} + +bool marcelb::ipban::ufw_ban(const string& ip) { + cout << "UFW ban IP: " << ip << endl; +} + + +bool marcelb::ipban::ufw_unban(const string& ip) { + cout << "UFW unban IP: " << ip << endl; +} + +static void marcelb::sleep_if(const uint& _time, const bool& _condition) { + time_t start_time = time(NULL); + do { + sleep(BOT_SLEEP_LOOP_TIME); + } while (difftime(time(NULL), start_time) < _time && _condition); +} diff --git a/test/test b/test/test new file mode 100755 index 0000000..42cc065 Binary files /dev/null and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..f5e7fed --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,10 @@ +#include "../lib/ipban.hpp" + +using namespace marcelb; + +int main() { + ipban myban(1); + myban.ban("192.168.2.74"); + sleep(80); + return 0; +} \ No newline at end of file