diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index fa85121..9aed52f 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -7,6 +7,15 @@ ESPFirewall::ESPFirewall(int port) this->setup_routing(); } +void ESPFirewall::prepare_firewall_json(DynamicJsonDocument &json, firewall_rule_t *rule) +{ + json["key"] = rule->key; + json["source"] = rule->source; + json["destination"] = rule->destination; + json["protocol"] = rule->protocol; + json["target"] = rule->target; +} + void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule) { firewall_rule_t *temp; @@ -52,10 +61,33 @@ void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request) int rule_number = request->pathArg(0).toInt(); DynamicJsonDocument json(1024); String response; - json["message"] = "get firewall rule"; - json["key"] = rule_number; + firewall_rule_t *current = this->head; + + if (head == NULL) + { + json["message"] = "list is empty"; + serializeJson(json, response); + request->send(500, "application/json", response); + return; + } + + while (current->key != rule_number) + { + if (current->next == NULL) + { + json["message"] = "rule not found"; + serializeJson(json, response); + request->send(500, "application/json", response); + return; + } + else + { + current = current->next; + } + } + prepare_firewall_json(json, current); serializeJson(json, response); - request->send(404, "application/json", response); + request->send(200, "application/json", response); } void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) @@ -82,11 +114,7 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) add_rule_to_firewall(rule); - json["key"] = rule->key; - json["source"] = rule->source; - json["destination"] = rule->destination; - json["protocol"] = rule->protocol; - json["target"] = rule->target; + prepare_firewall_json(json, rule); response_code = 200; } else diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index b41d0cf..b8b57cd 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -32,6 +32,7 @@ class ESPFirewall unsigned int amount_of_rules = 0; struct firewall_rule *head = NULL; + void prepare_firewall_json(DynamicJsonDocument &json, firewall_rule_t *rule); void add_rule_to_firewall(firewall_rule_t *rule); void get_firewall_rules_handler(AsyncWebServerRequest *request); void get_firewall_rule_handler(AsyncWebServerRequest *request);