From 0a7f0faa390f1d8352aac084c853593b769b31d6 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Sat, 23 Apr 2022 23:50:46 +0200 Subject: [PATCH] fix rest api --- ESPFirewall/lib/Firewall/src/API.cpp | 52 ++++++++++++++++++++++++-- ESPFirewall/lib/Firewall/src/API.hpp | 1 + ESPFirewall/lib/Firewall/src/Utils.cpp | 12 +++--- ESPFirewall/lib/Firewall/src/Utils.hpp | 5 +-- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/API.cpp b/ESPFirewall/lib/Firewall/src/API.cpp index 54513d3..1d1e426 100644 --- a/ESPFirewall/lib/Firewall/src/API.cpp +++ b/ESPFirewall/lib/Firewall/src/API.cpp @@ -72,17 +72,62 @@ namespace firewall void API::get_firewall_rules_handler() { - this->json_message_response("firewall rules", 200); + this->json_generic_response(this->construct_json_firewall(), 200); } void API::post_firewall_handler() { - this->json_message_response("post firewall rule", 200); + if (request_has_firewall_parameter()) + { + firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); + rule_ptr->key = ++amount_of_rules; + + String source = this->server->arg("source"); + strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source)); + String destination = this->server->arg("destination"); + strncpy(rule_ptr->destination, destination.c_str(), sizeof(rule_ptr->destination)); + + String protocol = this->server->arg("protocol"); + rule_ptr->protocol = string_to_protocol(protocol); + String target = this->server->arg("target"); + rule_ptr->target = string_to_target(target); + + add_rule_to_firewall(rule_ptr); + this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200); + } + else + { + this->json_message_response("not enough parameter", 400); + } } void API::delete_firewall_handler() { - this->json_message_response("delete firewall rule: " + this->server->pathArg(0), 200); + String param = this->server->pathArg(0); + int rule_number = atoi(param.c_str()); + if (delete_rule_from_firewall(rule_number) == SUCCESS) + { + this->json_message_response("firewall rule deleted", 200); + } + else + { + this->json_message_response("cannot delete firewall rule", 500); + } + } + + bool API::request_has_firewall_parameter() + { + if (!this->server->args()) + { + return false; + } + else + { + return this->server->hasArg("source") || + this->server->hasArg("destination") || + this->server->hasArg("protocol") || + this->server->hasArg("target"); + } } String API::json_new_attribute(String key, String value, bool last) @@ -107,7 +152,6 @@ namespace firewall void API::json_message_response(String message, const uint16_t response_code) { String serialized_string = "{"; - serialized_string += json_new_attribute("uri", this->server->uri()); serialized_string += json_new_attribute("message", message, true); serialized_string += "}"; this->server->send(response_code, "application/json; charset=utf-8", serialized_string); diff --git a/ESPFirewall/lib/Firewall/src/API.hpp b/ESPFirewall/lib/Firewall/src/API.hpp index f13277b..e386785 100644 --- a/ESPFirewall/lib/Firewall/src/API.hpp +++ b/ESPFirewall/lib/Firewall/src/API.hpp @@ -32,6 +32,7 @@ namespace firewall void delete_firewall_handler(); void not_found_handler(); + bool request_has_firewall_parameter(); String json_new_attribute(String key, String value, bool last = false); String json_new_attribute(String key, uint8_t value, bool last = false); void json_generic_response(String serialized_string, const uint16_t response_code); diff --git a/ESPFirewall/lib/Firewall/src/Utils.cpp b/ESPFirewall/lib/Firewall/src/Utils.cpp index 237a75d..67193b9 100644 --- a/ESPFirewall/lib/Firewall/src/Utils.cpp +++ b/ESPFirewall/lib/Firewall/src/Utils.cpp @@ -15,11 +15,11 @@ namespace firewall } } - firewall_protocol_t string_to_protocol(std::string &protocol) + firewall_protocol_t string_to_protocol(String &protocol) { - if (protocol.compare("TCP") == 0) + if (protocol.equals("TCP")) return PROTOCOL_TCP; - else if (protocol.compare("UDP") == 0) + else if (protocol.equals("UDP")) return PROTOCOL_UDP; else return PROTOCOL_ALL; @@ -38,11 +38,11 @@ namespace firewall } } - firewall_target_t string_to_target(std::string &target) + firewall_target_t string_to_target(String &target) { - if (target.compare("REJECT") == 0) + if (target.equals("REJECT")) return TARGET_REJECT; - else if (target.compare("DROP") == 0) + else if (target.equals("DROP")) return TARGET_DROP; else return TARGET_ACCEPT; diff --git a/ESPFirewall/lib/Firewall/src/Utils.hpp b/ESPFirewall/lib/Firewall/src/Utils.hpp index f970169..392a338 100644 --- a/ESPFirewall/lib/Firewall/src/Utils.hpp +++ b/ESPFirewall/lib/Firewall/src/Utils.hpp @@ -2,7 +2,6 @@ #define UTILS_HPP #include "Arduino.h" -#include "string" #include "WString.h" namespace firewall @@ -54,9 +53,9 @@ namespace firewall } credential_t; String protocol_to_string(firewall_protocol_t &protocol); - firewall_protocol_t string_to_protocol(std::string &protocol); + firewall_protocol_t string_to_protocol(String &protocol); String target_to_string(firewall_target_t &target); - firewall_target_t string_to_target(std::string &target); + firewall_target_t string_to_target(String &target); void endless_loop(); }