diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index 3f68a61..2ce6168 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -2,55 +2,21 @@ ESPFirewall::ESPFirewall(int port) { - this->amount_of_rules = 0; - this->head = NULL; log_i("Starting Firewall-API on %i", port); - this->firewall_api = new WebServer(port); + this->firewall_api = new AsyncWebServer(port); this->setup_routing(); - this->add_rule_to_firewall("192.168.0.1", "192.168.0.10", "TCP", "DROP"); - this->add_rule_to_firewall("192.168.0.2", "192.168.0.15", "UDP", "REJECT"); } -void ESPFirewall::setup_routing() -{ - this->firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_handler, this)); - this->firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this)); - this->firewall_api->begin(); -} - -void ESPFirewall::custom_message_response(const char *message, int response_code) -{ - cJSON *json_response = cJSON_CreateObject(); - cJSON_AddBoolToObject(json_response, "ok", true); - cJSON_AddStringToObject(json_response, "message", message); - this->firewall_api->send(response_code, "application/json", cJSON_Print(json_response)); - cJSON_Delete(json_response); -} - -void ESPFirewall::prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link) -{ - cJSON_AddBoolToObject(jsonResponse, "ok", true); - cJSON_AddNumberToObject(jsonResponse, "number", link->key); - cJSON_AddStringToObject(jsonResponse, "source", link->source); - cJSON_AddStringToObject(jsonResponse, "destination", link->destination); - cJSON_AddStringToObject(jsonResponse, "protocol", link->protocol); - cJSON_AddStringToObject(jsonResponse, "target", link->target); -} - -firewall_rule_t *ESPFirewall::add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target) +void ESPFirewall::add_rule_to_firewall() { firewall_rule_t *temp; firewall_rule_t *link = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); link->key = ++amount_of_rules; - strcpy(link->source, source); - strcpy(link->destination, destination); - strcpy(link->protocol, protocol); - strcpy(link->target, target); if (head == NULL) { head = link; link->next = NULL; - return link; + return; } temp = head; while (temp->next != NULL) @@ -59,43 +25,66 @@ firewall_rule_t *ESPFirewall::add_rule_to_firewall(const char *source, const cha } temp->next = link; link->next = NULL; - return link; + return; } -void ESPFirewall::post_firewall_handler() +void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request) { - if ((firewall_api->hasArg("source") || firewall_api->hasArg("destination") || firewall_api->hasArg("protocol") || firewall_api->hasArg("target")) == false) + firewall_rule_t *ptr = this->head; + DynamicJsonDocument json(1024); + String response; + json["amount"] = amount_of_rules; + JsonArray rules = json.createNestedArray("rules"); + while (ptr != NULL) { - this->custom_message_response("not enough erguments provided", 400); + JsonObject rule = rules.createNestedObject(); + rule["key"] = ptr->key; + ptr = ptr->next; + } + serializeJson(json, response); + request->send(200, "application/json", response); +} + +void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) +{ + DynamicJsonDocument json(1024); + String response; + int response_code; + if (request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target")) + { + String source = request->arg("source"); + String destination = request->arg("destination"); + String protocol = request->arg("protocol"); + String target = request->arg("target"); + json["source"] = source; + json["destination"] = destination; + json["protocol"] = protocol; + json["target"] = target; + add_rule_to_firewall(); + response_code = 200; } else { - const char *source = firewall_api->arg("source").c_str(); - const char *destination = firewall_api->arg("destination").c_str(); - const char *protocol = firewall_api->arg("protocol").c_str(); - const char *target = firewall_api->arg("target").c_str(); - firewall_rule_t *ptr = this->add_rule_to_firewall(source, destination, protocol, target); - cJSON *json_response = cJSON_CreateObject(); - prepare_firewall_json(json_response, ptr); - this->firewall_api->send(200, "application/json", cJSON_Print(json_response)); + json["message"] = "not enough parameter provided"; + response_code = 400; } + serializeJson(json, response); + request->send(response_code, "application/json", response); } -void ESPFirewall::get_firewall_handler() +void ESPFirewall::not_found(AsyncWebServerRequest *request) { - firewall_rule_t *ptr = head; - cJSON *json_response = cJSON_CreateArray(); - while (ptr != NULL) - { - cJSON *json_firewall_rule = cJSON_CreateObject(); - prepare_firewall_json(json_firewall_rule, ptr); - cJSON_AddItemToArray(json_response, json_firewall_rule); - ptr = ptr->next; - } - this->firewall_api->send(200, "application/json", cJSON_Print(json_response)); + DynamicJsonDocument json(1024); + String response; + json["message"] = "not found"; + serializeJson(json, response); + request->send(404, "application/json", response); } -void ESPFirewall::handle_clients() +void ESPFirewall::setup_routing() { - this->firewall_api->handleClient(); -} \ No newline at end of file + firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_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->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1)); + this->firewall_api->begin(); +} diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index 9d17314..1eb1188 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -1,37 +1,38 @@ #ifndef FIREWALL_H #define FIREWALL_H -#include "WebServer.h" -#include "cJSON.h" -#include "esp32-hal-log.h" +#include "Arduino.h" +#include "AsyncJson.h" +#include "ArduinoJson.h" +#ifdef ESP32 +#include "WiFi.h" +#include "AsyncTCP.h" +#elif defined(ESP8266) +#include "ESP8266WiFi.h" +#include "ESPAsyncTCP.h" +#endif +#include "ESPAsyncWebServer.h" typedef struct firewall_rule { int key; - char source[IP4ADDR_STRLEN_MAX]; - char destination[IP4ADDR_STRLEN_MAX]; - char protocol[4]; - char target[7]; struct firewall_rule *next; } firewall_rule_t; class ESPFirewall { - WebServer *firewall_api; - int amount_of_rules; - struct firewall_rule *head; + AsyncWebServer *firewall_api; + unsigned int amount_of_rules = 0; + struct firewall_rule *head = NULL; + void add_rule_to_firewall(); + void get_firewall_handler(AsyncWebServerRequest *request); + void post_firewall_handler(AsyncWebServerRequest *request); + void not_found(AsyncWebServerRequest *request); void setup_routing(); - void custom_message_response(const char *message, int response_code); - void prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link); - firewall_rule_t *add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target); - void post_firewall_handler(); - void get_firewall_handler(); public: ESPFirewall(int port = 8080); - - void handle_clients(); }; #endif diff --git a/SourceCode/arduino/platformio.ini b/SourceCode/arduino/platformio.ini index 534eaca..ad40439 100644 --- a/SourceCode/arduino/platformio.ini +++ b/SourceCode/arduino/platformio.ini @@ -14,6 +14,9 @@ board = esp32-evb framework = arduino monitor_speed = 115200 build_flags = -DCORE_DEBUG_LEVEL=3 +lib_deps = + bblanchon/ArduinoJson@^6.19.4 + ottowinter/ESPAsyncWebServer-esphome@^2.1.0 [env:esp32-dev] platform = espressif32 @@ -21,3 +24,6 @@ board = az-delivery-devkit-v4 framework = arduino monitor_speed = 115200 build_flags = -DCORE_DEBUG_LEVEL=3 +lib_deps = + bblanchon/ArduinoJson@^6.19.4 + ottowinter/ESPAsyncWebServer-esphome@^2.1.0 diff --git a/SourceCode/arduino/src/main.cpp b/SourceCode/arduino/src/main.cpp index 78df614..618f54f 100644 --- a/SourceCode/arduino/src/main.cpp +++ b/SourceCode/arduino/src/main.cpp @@ -27,5 +27,4 @@ void setup() void loop() { - firewall->handle_clients(); } \ No newline at end of file