This commit is contained in:
Florian Hoss 2022-04-18 18:45:37 +02:00
parent e966638f86
commit b635eb38df

View file

@ -59,7 +59,6 @@ void ESPFirewall::setup_eeprom()
EEPROM.begin(this->eeprom_size); EEPROM.begin(this->eeprom_size);
this->amount_of_rules = EEPROM.read(this->eeprom_settings_head); this->amount_of_rules = EEPROM.read(this->eeprom_settings_head);
uint8_t security_number = EEPROM.read(this->eeprom_settings_head + 1); 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) if (this->amount_of_rules > 50 || security_number != this->security_number)
{ {
this->amount_of_rules = 0; this->amount_of_rules = 0;
@ -209,13 +208,13 @@ bool ESPFirewall::delete_rule_from_firewall(uint8_t key)
void ESPFirewall::setup_routing() void ESPFirewall::setup_routing()
{ {
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1)); this->firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1));
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1)); this->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)); this->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)); this->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)); this->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)); this->firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
this->firewall_api->begin(); this->firewall_api->begin();
} }
@ -244,7 +243,7 @@ String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
String ESPFirewall::construct_json_firewall() String ESPFirewall::construct_json_firewall()
{ {
firewall_rule_t *rule_ptr = this->head; firewall_rule_t *rule_ptr = this->head;
// Size for max 12 Rules // Size for approx. 12 Rules
StaticJsonDocument<2048> doc; StaticJsonDocument<2048> doc;
String response; String response;
doc["amount_of_rules"] = this->amount_of_rules; doc["amount_of_rules"] = this->amount_of_rules;
@ -265,12 +264,12 @@ String ESPFirewall::construct_json_firewall()
void ESPFirewall::not_found(AsyncWebServerRequest *request) void ESPFirewall::not_found(AsyncWebServerRequest *request)
{ {
json_message_response(request, "not found", 404); this->json_message_response(request, "not found", 404);
} }
void ESPFirewall::restart_device_handler(AsyncWebServerRequest *request) void ESPFirewall::restart_device_handler(AsyncWebServerRequest *request)
{ {
json_message_response(request, "restarting device in 2 sec", 200); this->json_message_response(request, "restarting device in 2 sec", 200);
sleep(2000); sleep(2000);
esp_restart(); esp_restart();
} }
@ -278,20 +277,20 @@ void ESPFirewall::restart_device_handler(AsyncWebServerRequest *request)
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();
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number); firewall_rule_t *rule_ptr = this->get_rule_from_firewall(rule_number);
if (rule_ptr == NULL) if (rule_ptr == NULL)
{ {
json_message_response(request, "rule not found", 404); this->json_message_response(request, "rule not found", 404);
} }
else else
{ {
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr)); request->send(200, "application/json", this->construct_json_firewall_rule(rule_ptr));
} }
} }
void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request) void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
{ {
String response = construct_json_firewall(); String response = this->construct_json_firewall();
request->send(200, "application/json", response); request->send(200, "application/json", response);
} }
@ -318,22 +317,22 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
String target = request->getParam("target")->value(); String target = request->getParam("target")->value();
rule_ptr->target = string_to_target(target); rule_ptr->target = string_to_target(target);
add_rule_to_firewall(rule_ptr); this->add_rule_to_firewall(rule_ptr);
eeprom_write_firewall_rule(rule_ptr); this->eeprom_write_firewall_rule(rule_ptr);
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr)); request->send(200, "application/json", this->construct_json_firewall_rule(rule_ptr));
} }
else else
{ {
json_message_response(request, "not enough parameter", 200); this->json_message_response(request, "not enough parameter", 200);
} }
} }
void ESPFirewall::delete_firewall_handler(AsyncWebServerRequest *request) void ESPFirewall::delete_firewall_handler(AsyncWebServerRequest *request)
{ {
int rule_number = request->pathArg(0).toInt(); int rule_number = request->pathArg(0).toInt();
if (delete_rule_from_firewall(rule_number)) if (this->delete_rule_from_firewall(rule_number))
{ {
json_message_response(request, "firewall rule deleted", 200); this->json_message_response(request, "firewall rule deleted", 200);
} }
json_message_response(request, "cannot delete firewall rule", 500); this->json_message_response(request, "cannot delete firewall rule", 500);
} }