Fix ban and unban functions on bad ufw states, and fix response on ufw_ban

dev v0.8
mbandic 9 months ago
parent 4d212938a5
commit 8d1b58d51a
  1. 28
      src/ipban.cpp
  2. BIN
      test/test
  3. 6
      test/test.cpp

@ -66,25 +66,33 @@ bool marcelb::ipban::update_db() {
bool marcelb::ipban::ban(const string& ip) {
bool status = !is_in_white_list(ip);
if (!status) {
return status;
}
if (is_banned(ip)) {
return status;
}
status = ufw_ban(ip);
if (status) {
if (is_banned(ip)) {
return status;
}
status = ufw_ban(ip);
io.lock();
banned.push_back({ip, time(NULL)});
status &= update_db();
status = update_db();
io.unlock();
}
return status;
}
bool marcelb::ipban::unban(vector<_ban>::iterator ban_itr) {
bool status = ufw_unban(ban_itr->ip);
io.lock();
banned.erase(ban_itr);
status &= update_db();
io.unlock();
if (status) {
io.lock();
banned.erase(ban_itr);
status = update_db();
io.unlock();
}
return status;
}
@ -102,7 +110,7 @@ bool marcelb::ipban::ufw_ban(const string& ip) {
string ufw_cmd = "sudo ufw insert 1 deny from " + ip + " to any";
try {
string execute_res = exec(ufw_cmd);
if (execute_res == "Rule added\n") {
if (execute_res == "Rule added\n" || execute_res == "Rule inserted\n") {
return true;
}
} catch (const string except) {

Binary file not shown.

@ -37,9 +37,9 @@ int main() {
cout << myban.ban("192.168.2.74") << endl;
// sleep(2);
// cout << myban.ban("192.168.2.75") << endl;
// // sleep(200);
sleep(5);
cout << myban.ban("192.168.2.74") << endl;
sleep(200);
// myban.fail("192.168.2.74");
// sleep(5);

Loading…
Cancel
Save