From b2c2a967d919b3ff3dea843a29bfc68dc4a26509 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 20 Apr 2022 07:58:06 +0200 Subject: [PATCH] create seperate api class --- .../arduino/lib/Firewall/esp32Firewall.cpp | 32 +------- .../arduino/lib/Firewall/esp32Firewall.hpp | 5 +- .../arduino/lib/Firewall/esp32FirewallAPI.cpp | 76 +++++++++++++++++++ .../arduino/lib/Firewall/esp32FirewallAPI.hpp | 29 +++++++ SourceCode/arduino/src/main.cpp | 12 +-- 5 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 SourceCode/arduino/lib/Firewall/esp32FirewallAPI.cpp create mode 100644 SourceCode/arduino/lib/Firewall/esp32FirewallAPI.hpp diff --git a/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp b/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp index 14236fd..2bcd5b2 100644 --- a/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/esp32Firewall.cpp @@ -1,22 +1,8 @@ #include "esp32Firewall.hpp" -esp32Firewall::esp32Firewall(const uint16_t api_port) +esp32Firewall::esp32Firewall() { this->setup_eeprom(); - this->setup_certificate(); - this->firewall_api = new HTTPSServer(this->certificate, api_port, 5); - this->setup_routing(); - log_i("Starting server..."); - this->firewall_api->start(); - if (this->firewall_api->isRunning()) - { - log_i("Server ready."); - } -} - -void esp32Firewall::handle_firewall_api_clients() -{ - this->firewall_api->loop(); } String esp32Firewall::protocol_to_string(firewall_protocol_t &protocol) @@ -242,14 +228,10 @@ void esp32Firewall::setup_routing() 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(&esp32Firewall::post_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(&esp32Firewall::restart_device_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_rules); this->firewall_api->registerNode(post_firewall); this->firewall_api->registerNode(delete_firewall); - this->firewall_api->setDefaultNode(restart_device); - this->firewall_api->setDefaultNode(not_found); } void esp32Firewall::json_generic_response(HTTPResponse *response, String serialized, const uint16_t response_code) @@ -305,18 +287,6 @@ String esp32Firewall::construct_json_firewall() return response; } -void esp32Firewall::not_found_handler(HTTPRequest *request, HTTPResponse *response) -{ - this->json_message_response(response, "not found", 404); -} - -void esp32Firewall::restart_device_handler(HTTPRequest *request, HTTPResponse *response) -{ - this->json_message_response(response, "restarting device in 2 sec", 200); - sleep(2000); - esp_restart(); -} - void esp32Firewall::get_firewall_rule_handler(HTTPRequest *request, HTTPResponse *response) { ResourceParameters *params = request->getParams(); diff --git a/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp b/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp index 5dd9ae6..bc6c9ae 100644 --- a/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp +++ b/SourceCode/arduino/lib/Firewall/esp32Firewall.hpp @@ -52,8 +52,6 @@ class esp32Firewall void json_message_response(HTTPResponse *, String, const uint16_t); String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall(); - void not_found_handler(HTTPRequest *, HTTPResponse *); - void restart_device_handler(HTTPRequest *, HTTPResponse *); void get_firewall_rule_handler(HTTPRequest *, HTTPResponse *); void get_firewall_rules_handler(HTTPRequest *, HTTPResponse *); bool request_has_firewall_parameter(ResourceParameters *); @@ -61,8 +59,7 @@ class esp32Firewall void delete_firewall_handler(HTTPRequest *, HTTPResponse *); public: - esp32Firewall(const uint16_t = 8080); - void handle_firewall_api_clients(); + esp32Firewall(); }; #endif diff --git a/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.cpp b/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.cpp new file mode 100644 index 0000000..68b4546 --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.cpp @@ -0,0 +1,76 @@ +#include "esp32FirewallAPI.hpp" + +esp32FirewallApi::esp32FirewallApi(const uint16_t port) +{ + this->setup_certificate(); + this->server = new HTTPSServer(this->certificate, port, 5); + this->setup_routing(); + log_i("Starting server..."); + this->server->start(); + if (this->server->isRunning()) + { + log_i("Server ready."); + } +} + +void esp32FirewallApi::handle_clients() +{ + this->server->loop(); +} + +void esp32FirewallApi::setup_certificate() +{ + log_i("Creating the certificate..."); + this->certificate = new SSLCert(); + int createCertResult = createSelfSignedCert( + *this->certificate, + KEYSIZE_2048, + "CN=myesp32.local,O=Firewall,C=DE", + "20220101000000", + "20320101000000"); + if (createCertResult != 0) + { + log_e("Cerating certificate failed. Error Code = 0x%02X, check SSLCert.hpp for details", createCertResult); + while (true) + delay(500); + } + log_i("Creating the certificate was successful"); +} + +void esp32FirewallApi::setup_routing() +{ + ResourceNode *restart_device = new ResourceNode("/api/v1/device/restart", "GET", std::bind(&esp32FirewallApi::restart_device_handler, this, std::placeholders::_1, std::placeholders::_2)); + ResourceNode *not_found = new ResourceNode("", "GET", std::bind(&esp32FirewallApi::not_found_handler, this, std::placeholders::_1, std::placeholders::_2)); + this->server->registerNode(restart_device); + this->server->setDefaultNode(not_found); +} + +void esp32FirewallApi::restart_device_handler(HTTPRequest *request, HTTPResponse *response) +{ + this->json_message_response(response, "restarting device in 2 sec", 200); + sleep(2000); + esp_restart(); +} + +void esp32FirewallApi::not_found_handler(HTTPRequest *request, HTTPResponse *response) +{ + this->json_message_response(response, "not found", 404); +} + +void esp32FirewallApi::json_generic_response(HTTPResponse *response, String serialized, const uint16_t response_code) +{ + response->setHeader("Content-Type", "application/json"); + response->setStatusCode(response_code); + response->println(serialized); +} + +void esp32FirewallApi::json_message_response(HTTPResponse *response, String message, const uint16_t response_code) +{ + response->setHeader("Content-Type", "application/json"); + response->setStatusCode(response_code); + StaticJsonDocument<96> json; + String serialized; + json["message"] = message; + serializeJson(json, serialized); + response->println(serialized); +} diff --git a/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.hpp b/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.hpp new file mode 100644 index 0000000..94ed396 --- /dev/null +++ b/SourceCode/arduino/lib/Firewall/esp32FirewallAPI.hpp @@ -0,0 +1,29 @@ +#ifndef ESP32_FIREWALL_API_HPP +#define ESP32_FIREWALL_API_HPP + +#include "HTTPSServer.hpp" +#include "SSLCert.hpp" +#include "HTTPRequest.hpp" +#include "HTTPResponse.hpp" +#include "ArduinoJson.h" + +using namespace httpsserver; + +class esp32FirewallApi +{ + HTTPSServer *server; + SSLCert *certificate; + + void setup_certificate(); + void setup_routing(); + void restart_device_handler(HTTPRequest *, HTTPResponse *); + void not_found_handler(HTTPRequest *, HTTPResponse *); + +public: + esp32FirewallApi(const uint16_t = 8080); + void handle_clients(); + void json_generic_response(HTTPResponse *, String, const uint16_t); + void json_message_response(HTTPResponse *, String, const uint16_t); +}; + +#endif diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index 014c894..914b519 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -1,8 +1,10 @@ #include "theSecrets.h" #include "WiFi.h" -#include "esp32Firewall.hpp" -const char *esp_ip_address; +#include "esp32Firewall.hpp" +#include "esp32FirewallAPI.hpp" + +esp32FirewallApi *firewall_api; esp32Firewall *firewall; void setup_wifi() @@ -17,17 +19,17 @@ void setup_wifi() delay(2000); log_d("Connecting... (%i/%i)", retries++, max_retries); } - esp_ip_address = WiFi.localIP().toString().c_str(); - log_i("Connected, IP Address: %s", esp_ip_address); + log_i("Connected, IP Address: %s", WiFi.localIP().toString()); } void setup() { setup_wifi(); firewall = new esp32Firewall; + firewall_api = new esp32FirewallApi; } void loop() { - firewall->handle_firewall_api_clients(); + firewall_api->handle_clients(); } \ No newline at end of file