eeprom successfully implemented for firewall rules
This commit is contained in:
parent
66dffbc2fc
commit
4a34439fd2
2 changed files with 46 additions and 11 deletions
|
@ -3,7 +3,6 @@
|
||||||
ESPFirewall::ESPFirewall(int port)
|
ESPFirewall::ESPFirewall(int port)
|
||||||
{
|
{
|
||||||
this->setup_eeprom();
|
this->setup_eeprom();
|
||||||
this->eeprom_read_firewall_rules();
|
|
||||||
log_i("Starting Firewall-API on %i", port);
|
log_i("Starting Firewall-API on %i", port);
|
||||||
this->firewall_api = new AsyncWebServer(port);
|
this->firewall_api = new AsyncWebServer(port);
|
||||||
this->setup_routing();
|
this->setup_routing();
|
||||||
|
@ -58,6 +57,18 @@ firewall_target_t ESPFirewall::string_to_target(String &target)
|
||||||
void ESPFirewall::setup_eeprom()
|
void ESPFirewall::setup_eeprom()
|
||||||
{
|
{
|
||||||
EEPROM.begin(this->eeprom_size);
|
EEPROM.begin(this->eeprom_size);
|
||||||
|
this->amount_of_rules = EEPROM.read(this->eeprom_settings_head);
|
||||||
|
uint8_t security_number = EEPROM.read(this->eeprom_settings_head + 1);
|
||||||
|
log_i("Amount of existing Rules %i", this->amount_of_rules);
|
||||||
|
if (this->amount_of_rules > 50 || security_number != this->security_number)
|
||||||
|
{
|
||||||
|
this->amount_of_rules = 0;
|
||||||
|
EEPROM.write(this->eeprom_settings_head, this->amount_of_rules);
|
||||||
|
EEPROM.write(this->eeprom_settings_head + 1, this->security_number);
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
log_i("Amount of existing Rules %i", this->amount_of_rules);
|
||||||
|
this->eeprom_read_firewall_rules();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
|
void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
|
||||||
|
@ -74,9 +85,21 @@ void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address)
|
void ESPFirewall::eeprom_write_firewall_rules()
|
||||||
|
{
|
||||||
|
this->eeprom_rules_head = eeprom_start_firewall_rules;
|
||||||
|
firewall_rule_t *rule_ptr = this->head;
|
||||||
|
while (rule_ptr != NULL)
|
||||||
|
{
|
||||||
|
this->eeprom_write_firewall_rule(rule_ptr);
|
||||||
|
rule_ptr = rule_ptr->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &rule_nr)
|
||||||
{
|
{
|
||||||
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||||
|
rule_ptr->key = rule_nr;
|
||||||
strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str());
|
strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str());
|
||||||
eeprom_address += IP4ADDR_STRLEN_MAX;
|
eeprom_address += IP4ADDR_STRLEN_MAX;
|
||||||
strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str());
|
strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str());
|
||||||
|
@ -95,12 +118,10 @@ void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address)
|
||||||
|
|
||||||
void ESPFirewall::eeprom_read_firewall_rules()
|
void ESPFirewall::eeprom_read_firewall_rules()
|
||||||
{
|
{
|
||||||
this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head);
|
|
||||||
log_i("Amount of existing Rules %i", this->amount_of_rules);
|
|
||||||
uint8_t eeprom_address = eeprom_start_firewall_rules;
|
uint8_t eeprom_address = eeprom_start_firewall_rules;
|
||||||
for (uint8_t i = 0; i < this->amount_of_rules; i++)
|
for (uint8_t i = 1; i <= this->amount_of_rules; i++)
|
||||||
{
|
{
|
||||||
eeprom_read_firewall_rule(eeprom_address);
|
eeprom_read_firewall_rule(eeprom_address, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +144,7 @@ void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
firewall_rule_t *ESPFirewall::get_rule_from_firewall(uint8_t key)
|
||||||
{
|
{
|
||||||
firewall_rule_t *rule_ptr = this->head;
|
firewall_rule_t *rule_ptr = this->head;
|
||||||
if (this->head == NULL)
|
if (this->head == NULL)
|
||||||
|
@ -144,7 +165,7 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
||||||
return rule_ptr;
|
return rule_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPFirewall::delete_rule_from_firewall(int key)
|
bool ESPFirewall::delete_rule_from_firewall(uint8_t key)
|
||||||
{
|
{
|
||||||
if (this->head == NULL)
|
if (this->head == NULL)
|
||||||
{
|
{
|
||||||
|
@ -182,6 +203,7 @@ bool ESPFirewall::delete_rule_from_firewall(int key)
|
||||||
}
|
}
|
||||||
free(current_rule_ptr);
|
free(current_rule_ptr);
|
||||||
this->amount_of_rules--;
|
this->amount_of_rules--;
|
||||||
|
this->eeprom_write_firewall_rules();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,6 +213,8 @@ void ESPFirewall::setup_routing()
|
||||||
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1));
|
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1));
|
||||||
firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1));
|
firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1));
|
||||||
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_DELETE, std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1));
|
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_DELETE, std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1));
|
||||||
|
|
||||||
|
firewall_api->on("/api/v1/device/restart", HTTP_GET, std::bind(&ESPFirewall::restart_device_handler, this, std::placeholders::_1));
|
||||||
firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
|
firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
|
||||||
this->firewall_api->begin();
|
this->firewall_api->begin();
|
||||||
}
|
}
|
||||||
|
@ -223,6 +247,7 @@ String ESPFirewall::construct_json_firewall()
|
||||||
// Size for max 12 Rules
|
// Size for max 12 Rules
|
||||||
StaticJsonDocument<2048> doc;
|
StaticJsonDocument<2048> doc;
|
||||||
String response;
|
String response;
|
||||||
|
doc["amount_of_rules"] = this->amount_of_rules;
|
||||||
JsonArray rules = doc.createNestedArray("rules");
|
JsonArray rules = doc.createNestedArray("rules");
|
||||||
while (rule_ptr != NULL)
|
while (rule_ptr != NULL)
|
||||||
{
|
{
|
||||||
|
@ -243,6 +268,13 @@ void ESPFirewall::not_found(AsyncWebServerRequest *request)
|
||||||
json_message_response(request, "not found", 404);
|
json_message_response(request, "not found", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::restart_device_handler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
json_message_response(request, "restarting device in 2 sec", 200);
|
||||||
|
sleep(2000);
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
|
||||||
void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
|
void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
int rule_number = request->pathArg(0).toInt();
|
int rule_number = request->pathArg(0).toInt();
|
||||||
|
|
|
@ -44,6 +44,7 @@ class ESPFirewall
|
||||||
{
|
{
|
||||||
uint16_t eeprom_size = 512;
|
uint16_t eeprom_size = 512;
|
||||||
uint8_t amount_of_rules = 0;
|
uint8_t amount_of_rules = 0;
|
||||||
|
uint8_t security_number = 93;
|
||||||
int eeprom_settings_head = 0;
|
int eeprom_settings_head = 0;
|
||||||
int eeprom_rules_head = eeprom_start_firewall_rules;
|
int eeprom_rules_head = eeprom_start_firewall_rules;
|
||||||
struct firewall_rule *head = NULL;
|
struct firewall_rule *head = NULL;
|
||||||
|
@ -59,13 +60,14 @@ class ESPFirewall
|
||||||
// EEPROM
|
// EEPROM
|
||||||
void setup_eeprom();
|
void setup_eeprom();
|
||||||
void eeprom_write_firewall_rule(firewall_rule_t *rule);
|
void eeprom_write_firewall_rule(firewall_rule_t *rule);
|
||||||
void eeprom_read_firewall_rule(uint8_t &);
|
void eeprom_write_firewall_rules();
|
||||||
|
void eeprom_read_firewall_rule(uint8_t &, uint8_t &);
|
||||||
void eeprom_read_firewall_rules();
|
void eeprom_read_firewall_rules();
|
||||||
|
|
||||||
// Firewall Actions
|
// Firewall Actions
|
||||||
void add_rule_to_firewall(firewall_rule_t *);
|
void add_rule_to_firewall(firewall_rule_t *);
|
||||||
firewall_rule_t *get_rule_from_firewall(int);
|
firewall_rule_t *get_rule_from_firewall(uint8_t);
|
||||||
bool delete_rule_from_firewall(int);
|
bool delete_rule_from_firewall(uint8_t);
|
||||||
|
|
||||||
// Firewall-API Actions
|
// Firewall-API Actions
|
||||||
void setup_routing();
|
void setup_routing();
|
||||||
|
@ -73,6 +75,7 @@ class ESPFirewall
|
||||||
String construct_json_firewall_rule(firewall_rule_t *);
|
String construct_json_firewall_rule(firewall_rule_t *);
|
||||||
String construct_json_firewall();
|
String construct_json_firewall();
|
||||||
void not_found(AsyncWebServerRequest *);
|
void not_found(AsyncWebServerRequest *);
|
||||||
|
void restart_device_handler(AsyncWebServerRequest *);
|
||||||
void get_firewall_rule_handler(AsyncWebServerRequest *);
|
void get_firewall_rule_handler(AsyncWebServerRequest *);
|
||||||
void get_firewall_rules_handler(AsyncWebServerRequest *);
|
void get_firewall_rules_handler(AsyncWebServerRequest *);
|
||||||
bool request_has_firewall_parameter(AsyncWebServerRequest *);
|
bool request_has_firewall_parameter(AsyncWebServerRequest *);
|
||||||
|
|
Reference in a new issue