clean filenames

This commit is contained in:
Florian Hoss 2022-04-19 21:26:43 +02:00
parent 69eaf1cead
commit 61966d6791
3 changed files with 41 additions and 43 deletions

View file

@ -1,6 +1,6 @@
#include "Firewall.h" #include "esp32Firewall.hpp"
ESPFirewall::ESPFirewall(const uint16_t api_port) esp32Firewall::esp32Firewall(const uint16_t api_port)
{ {
this->setup_eeprom(); this->setup_eeprom();
this->setup_certificate(); this->setup_certificate();
@ -14,12 +14,12 @@ ESPFirewall::ESPFirewall(const uint16_t api_port)
} }
} }
void ESPFirewall::handle_firewall_api_clients() void esp32Firewall::handle_firewall_api_clients()
{ {
this->firewall_api->loop(); this->firewall_api->loop();
} }
String ESPFirewall::protocol_to_string(firewall_protocol_t &protocol) String esp32Firewall::protocol_to_string(firewall_protocol_t &protocol)
{ {
switch (protocol) switch (protocol)
{ {
@ -32,7 +32,7 @@ String ESPFirewall::protocol_to_string(firewall_protocol_t &protocol)
} }
} }
firewall_protocol_t ESPFirewall::string_to_protocol(std::string &protocol) firewall_protocol_t esp32Firewall::string_to_protocol(std::string &protocol)
{ {
if (protocol.compare("TCP") == 0) if (protocol.compare("TCP") == 0)
return FW_TCP; return FW_TCP;
@ -42,7 +42,7 @@ firewall_protocol_t ESPFirewall::string_to_protocol(std::string &protocol)
return FW_ALL; return FW_ALL;
} }
String ESPFirewall::target_to_string(firewall_target_t &target) String esp32Firewall::target_to_string(firewall_target_t &target)
{ {
switch (target) switch (target)
{ {
@ -55,7 +55,7 @@ String ESPFirewall::target_to_string(firewall_target_t &target)
} }
} }
firewall_target_t ESPFirewall::string_to_target(std::string &target) firewall_target_t esp32Firewall::string_to_target(std::string &target)
{ {
if (target.compare("REJECT") == 0) if (target.compare("REJECT") == 0)
return FW_REJECT; return FW_REJECT;
@ -65,7 +65,7 @@ firewall_target_t ESPFirewall::string_to_target(std::string &target)
return FW_ACCEPT; return FW_ACCEPT;
} }
void ESPFirewall::setup_eeprom() void esp32Firewall::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);
@ -81,7 +81,7 @@ void ESPFirewall::setup_eeprom()
this->eeprom_read_firewall_rules(); this->eeprom_read_firewall_rules();
} }
void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr) void esp32Firewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
{ {
EEPROM.write(this->eeprom_settings_head, this->amount_of_rules); EEPROM.write(this->eeprom_settings_head, this->amount_of_rules);
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source); EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source);
@ -95,7 +95,7 @@ void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
EEPROM.commit(); EEPROM.commit();
} }
void ESPFirewall::eeprom_write_firewall_rules() void esp32Firewall::eeprom_write_firewall_rules()
{ {
this->eeprom_rules_head = eeprom_start_firewall_rules; this->eeprom_rules_head = eeprom_start_firewall_rules;
firewall_rule_t *rule_ptr = this->head; firewall_rule_t *rule_ptr = this->head;
@ -106,7 +106,7 @@ void ESPFirewall::eeprom_write_firewall_rules()
} }
} }
void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &rule_nr) void esp32Firewall::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; rule_ptr->key = rule_nr;
@ -126,7 +126,7 @@ void ESPFirewall::eeprom_read_firewall_rule(uint8_t &eeprom_address, uint8_t &ru
target_to_string(rule_ptr->target)); target_to_string(rule_ptr->target));
} }
void ESPFirewall::eeprom_read_firewall_rules() void esp32Firewall::eeprom_read_firewall_rules()
{ {
uint8_t eeprom_address = eeprom_start_firewall_rules; uint8_t eeprom_address = eeprom_start_firewall_rules;
for (uint8_t i = 1; i <= this->amount_of_rules; i++) for (uint8_t i = 1; i <= this->amount_of_rules; i++)
@ -135,7 +135,7 @@ void ESPFirewall::eeprom_read_firewall_rules()
} }
} }
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr) void esp32Firewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
{ {
firewall_rule_t *temp; firewall_rule_t *temp;
if (this->head == NULL) if (this->head == NULL)
@ -154,7 +154,7 @@ void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule_ptr)
return; return;
} }
firewall_rule_t *ESPFirewall::get_rule_from_firewall(uint8_t key) firewall_rule_t *esp32Firewall::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)
@ -175,7 +175,7 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(uint8_t key)
return rule_ptr; return rule_ptr;
} }
bool ESPFirewall::delete_rule_from_firewall(uint8_t key) bool esp32Firewall::delete_rule_from_firewall(uint8_t key)
{ {
if (this->head == NULL) if (this->head == NULL)
{ {
@ -217,7 +217,7 @@ bool ESPFirewall::delete_rule_from_firewall(uint8_t key)
return true; return true;
} }
void ESPFirewall::setup_certificate() void esp32Firewall::setup_certificate()
{ {
log_i("Creating the certificate..."); log_i("Creating the certificate...");
this->certificate = new SSLCert(); this->certificate = new SSLCert();
@ -236,14 +236,14 @@ void ESPFirewall::setup_certificate()
log_i("Creating the certificate was successful"); log_i("Creating the certificate was successful");
} }
void ESPFirewall::setup_routing() void esp32Firewall::setup_routing()
{ {
ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&esp32Firewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&esp32Firewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&esp32Firewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *delete_firewall = new ResourceNode("/api/v1/firewall/*", "DELETE", std::bind(&esp32Firewall::delete_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&ESPFirewall::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&esp32Firewall::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&ESPFirewall::not_found_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&esp32Firewall::not_found_handler, this, std::placeholders::_1, std::placeholders::_2));
this->firewall_api->registerNode(get_firewall_rule); this->firewall_api->registerNode(get_firewall_rule);
this->firewall_api->registerNode(get_firewall_rules); this->firewall_api->registerNode(get_firewall_rules);
this->firewall_api->registerNode(post_firewall); this->firewall_api->registerNode(post_firewall);
@ -252,14 +252,14 @@ void ESPFirewall::setup_routing()
this->firewall_api->setDefaultNode(not_found); this->firewall_api->setDefaultNode(not_found);
} }
void ESPFirewall::json_generic_response(HTTPResponse *response, String serialized, int response_code) void esp32Firewall::json_generic_response(HTTPResponse *response, String serialized, int response_code)
{ {
response->setHeader("Content-Type", "application/json"); response->setHeader("Content-Type", "application/json");
response->setStatusCode(response_code); response->setStatusCode(response_code);
response->println(serialized); response->println(serialized);
} }
void ESPFirewall::json_message_response(HTTPResponse *response, String message, int response_code) void esp32Firewall::json_message_response(HTTPResponse *response, String message, int response_code)
{ {
response->setHeader("Content-Type", "application/json"); response->setHeader("Content-Type", "application/json");
response->setStatusCode(response_code); response->setStatusCode(response_code);
@ -270,7 +270,7 @@ void ESPFirewall::json_message_response(HTTPResponse *response, String message,
response->println(serialized); response->println(serialized);
} }
String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr) String esp32Firewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
{ {
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc;
doc["key"] = rule_ptr->key; doc["key"] = rule_ptr->key;
@ -283,7 +283,7 @@ String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
return response; return response;
} }
String ESPFirewall::construct_json_firewall() String esp32Firewall::construct_json_firewall()
{ {
firewall_rule_t *rule_ptr = this->head; firewall_rule_t *rule_ptr = this->head;
// Size for approx. 12 Rules // Size for approx. 12 Rules
@ -305,19 +305,19 @@ String ESPFirewall::construct_json_firewall()
return response; return response;
} }
void ESPFirewall::not_found_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::not_found_handler(HTTPRequest *request, HTTPResponse *response)
{ {
this->json_message_response(response, "not found", 404); this->json_message_response(response, "not found", 404);
} }
void ESPFirewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response)
{ {
this->json_message_response(response, "restarting device in 2 sec", 200); this->json_message_response(response, "restarting device in 2 sec", 200);
sleep(2000); sleep(2000);
esp_restart(); esp_restart();
} }
void ESPFirewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response)
{ {
ResourceParameters *params = request->getParams(); ResourceParameters *params = request->getParams();
int rule_number = atoi(params->getPathParameter(0).c_str()); int rule_number = atoi(params->getPathParameter(0).c_str());
@ -334,17 +334,17 @@ void ESPFirewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *
} }
} }
void ESPFirewall::get_firewall_rules_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::get_firewall_rules_handler(HTTPRequest *request, HTTPResponse *response)
{ {
this->json_generic_response(response, this->construct_json_firewall(), 200); this->json_generic_response(response, this->construct_json_firewall(), 200);
} }
bool ESPFirewall::request_has_firewall_parameter(ResourceParameters *params) bool esp32Firewall::request_has_firewall_parameter(ResourceParameters *params)
{ {
return params->isQueryParameterSet("source") || params->isQueryParameterSet("destination") || params->isQueryParameterSet("protocol") || params->isQueryParameterSet("target"); return params->isQueryParameterSet("source") || params->isQueryParameterSet("destination") || params->isQueryParameterSet("protocol") || params->isQueryParameterSet("target");
} }
void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *response)
{ {
ResourceParameters *params = request->getParams(); ResourceParameters *params = request->getParams();
if (request_has_firewall_parameter(params)) if (request_has_firewall_parameter(params))
@ -377,7 +377,7 @@ void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *resp
} }
} }
void ESPFirewall::delete_firewall_handler(HTTPRequest *request, HTTPResponse *response) void esp32Firewall::delete_firewall_handler(HTTPRequest *request, HTTPResponse *response)
{ {
ResourceParameters *params = request->getParams(); ResourceParameters *params = request->getParams();
int rule_number = atoi(params->getPathParameter(0).c_str()); int rule_number = atoi(params->getPathParameter(0).c_str());

View file

@ -1,5 +1,5 @@
#ifndef FIREWALL_H #ifndef ESP32_FIREWALL_HPP
#define FIREWALL_H #define ESP32_FIREWALL_HPP
#include "Arduino.h" #include "Arduino.h"
#include "ArduinoJson.h" #include "ArduinoJson.h"
@ -15,7 +15,7 @@
#define eeprom_start_firewall_rules 4 #define eeprom_start_firewall_rules 4
using namespace httpsserver; using namespace httpsserver;
class ESPFirewall class esp32Firewall
{ {
uint16_t eeprom_size = 512; uint16_t eeprom_size = 512;
uint8_t amount_of_rules = 0; uint8_t amount_of_rules = 0;
@ -61,7 +61,7 @@ class ESPFirewall
void delete_firewall_handler(HTTPRequest *, HTTPResponse *); void delete_firewall_handler(HTTPRequest *, HTTPResponse *);
public: public:
ESPFirewall(const uint16_t = 8080); esp32Firewall(const uint16_t = 8080);
void handle_firewall_api_clients(); void handle_firewall_api_clients();
}; };

View file

@ -1,11 +1,9 @@
#include "Arduino.h"
#include "theSecrets.h" #include "theSecrets.h"
#include "WiFi.h" #include "WiFi.h"
#include "Firewall.h" #include "esp32Firewall.hpp"
#include "esp32-hal-log.h"
const char *esp_ip_address; const char *esp_ip_address;
ESPFirewall *firewall; esp32Firewall *firewall;
void setup_wifi() void setup_wifi()
{ {
@ -26,7 +24,7 @@ void setup_wifi()
void setup() void setup()
{ {
setup_wifi(); setup_wifi();
firewall = new ESPFirewall; firewall = new esp32Firewall;
} }
void loop() void loop()