cleanup Firewall
This commit is contained in:
parent
e25cca86f1
commit
1e87f21334
2 changed files with 137 additions and 23 deletions
121
ESPFirewall/lib/Firewall/src/Firewall.cpp
Normal file
121
ESPFirewall/lib/Firewall/src/Firewall.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
#include "Firewall.hpp"
|
||||
|
||||
namespace fw
|
||||
{
|
||||
Firewall::Firewall()
|
||||
{
|
||||
this->amount_of_rules = retrieve_settings_value("amount_of_rules");
|
||||
for (uint8_t i = 1; i <= this->amount_of_rules; i++)
|
||||
{
|
||||
firewall_rule_t *rule_ptr = retrieve_firewall_rule(i);
|
||||
this->add_rule_to_firewall(rule_ptr, false);
|
||||
}
|
||||
}
|
||||
|
||||
Firewall::~Firewall()
|
||||
{
|
||||
}
|
||||
|
||||
firewall_rule_t *Firewall::get_rule_head()
|
||||
{
|
||||
return this->rule_head;
|
||||
}
|
||||
|
||||
void Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr, const bool save_in_eeprom)
|
||||
{
|
||||
store_settings_value("amount_of_rules", this->amount_of_rules);
|
||||
if (save_in_eeprom)
|
||||
Storage::store_firewall_rule(rule_ptr);
|
||||
if (this->rule_head == NULL)
|
||||
{
|
||||
this->rule_head = rule_ptr;
|
||||
rule_ptr->next = NULL;
|
||||
return;
|
||||
}
|
||||
firewall_rule_t *current_rule;
|
||||
current_rule = this->rule_head;
|
||||
while (current_rule->next != NULL)
|
||||
{
|
||||
current_rule = current_rule->next;
|
||||
}
|
||||
current_rule->next = rule_ptr;
|
||||
rule_ptr->next = NULL;
|
||||
}
|
||||
|
||||
firewall_rule_t *Firewall::add_rule_to_firewall(String source, String destination, String port_from, String port_to, String protocol, String target)
|
||||
{
|
||||
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||
rule_ptr->key = ++this->amount_of_rules;
|
||||
|
||||
strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source));
|
||||
strncpy(rule_ptr->destination, destination.c_str(), sizeof(rule_ptr->destination));
|
||||
rule_ptr->port_from = port_from.toInt();
|
||||
rule_ptr->port_to = port_to.toInt();
|
||||
rule_ptr->protocol = string_to_protocol(protocol);
|
||||
rule_ptr->target = string_to_target(target);
|
||||
|
||||
add_rule_to_firewall(rule_ptr);
|
||||
return rule_ptr;
|
||||
}
|
||||
|
||||
firewall_rule_t *Firewall::get_rule_from_firewall(const uint8_t key)
|
||||
{
|
||||
firewall_rule_t *rule_ptr = this->rule_head;
|
||||
if (this->rule_head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
while (rule_ptr->key != key)
|
||||
{
|
||||
if (rule_ptr->next == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
rule_ptr = rule_ptr->next;
|
||||
}
|
||||
}
|
||||
return rule_ptr;
|
||||
}
|
||||
|
||||
ok_t Firewall::delete_rule_from_firewall(const uint8_t key)
|
||||
{
|
||||
if (this->rule_head == NULL)
|
||||
return NO_ACTION;
|
||||
firewall_rule_t *current_rule = this->rule_head;
|
||||
firewall_rule_t *previous_rule = NULL;
|
||||
firewall_rule_t *temp = NULL;
|
||||
while (current_rule->key != key)
|
||||
{
|
||||
if (current_rule->next == NULL)
|
||||
return NO_ACTION;
|
||||
else
|
||||
{
|
||||
previous_rule = current_rule;
|
||||
current_rule = current_rule->next;
|
||||
}
|
||||
}
|
||||
if (current_rule == this->rule_head)
|
||||
{
|
||||
this->rule_head = rule_head->next;
|
||||
temp = this->rule_head;
|
||||
}
|
||||
else
|
||||
{
|
||||
previous_rule->next = current_rule->next;
|
||||
temp = previous_rule->next;
|
||||
}
|
||||
while (temp != NULL)
|
||||
{
|
||||
temp->key--;
|
||||
temp = temp->next;
|
||||
}
|
||||
free(current_rule);
|
||||
this->amount_of_rules--;
|
||||
Storage::store_settings_value("amount_of_rules", this->amount_of_rules);
|
||||
if (this->amount_of_rules != 0)
|
||||
Storage::store_all_firewall_rules(rule_head);
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
|
@ -1,34 +1,27 @@
|
|||
#ifndef FIREWALL_HPP
|
||||
#define FIREWALL_HPP
|
||||
#ifndef ESP32_FIREWALL_HPP
|
||||
#define ESP32_FIREWALL_HPP
|
||||
|
||||
#include "API.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "Storage.hpp"
|
||||
|
||||
namespace fw
|
||||
{
|
||||
class Firewall : public API
|
||||
class Firewall : public Storage
|
||||
{
|
||||
private:
|
||||
public:
|
||||
Firewall(const char *, const char *, const char *, const char *, const String ip, const uint16_t = 8080);
|
||||
Firewall();
|
||||
~Firewall();
|
||||
void handle_api_client();
|
||||
bool check_client(WiFiClient);
|
||||
};
|
||||
|
||||
Firewall::Firewall(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port)
|
||||
: API(cert, key, username, password, ip, port) {}
|
||||
Firewall::~Firewall() {}
|
||||
void Firewall::handle_api_client()
|
||||
{
|
||||
handle_client();
|
||||
}
|
||||
bool Firewall::check_client(WiFiClient client)
|
||||
{
|
||||
if (client.remoteIP())
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
firewall_rule_t *get_rule_head();
|
||||
void add_rule_to_firewall(firewall_rule_t *rule_ptr, const bool save_in_eeprom = true);
|
||||
firewall_rule_t *add_rule_to_firewall(String source, String destination, String port_from, String port_to, String protocol, String target);
|
||||
firewall_rule_t *get_rule_from_firewall(const uint8_t key);
|
||||
ok_t delete_rule_from_firewall(const uint8_t key);
|
||||
|
||||
protected:
|
||||
uint8_t amount_of_rules = 0;
|
||||
firewall_rule_t *rule_head = NULL;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Reference in a new issue