fix rest api
This commit is contained in:
parent
17a1a5176f
commit
0a7f0faa39
4 changed files with 57 additions and 13 deletions
|
@ -72,17 +72,62 @@ namespace firewall
|
||||||
|
|
||||||
void API::get_firewall_rules_handler()
|
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()
|
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()
|
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)
|
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)
|
void API::json_message_response(String message, const uint16_t response_code)
|
||||||
{
|
{
|
||||||
String serialized_string = "{";
|
String serialized_string = "{";
|
||||||
serialized_string += json_new_attribute("uri", this->server->uri());
|
|
||||||
serialized_string += json_new_attribute("message", message, true);
|
serialized_string += json_new_attribute("message", message, true);
|
||||||
serialized_string += "}";
|
serialized_string += "}";
|
||||||
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
||||||
|
|
|
@ -32,6 +32,7 @@ namespace firewall
|
||||||
void delete_firewall_handler();
|
void delete_firewall_handler();
|
||||||
void not_found_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, String value, bool last = false);
|
||||||
String json_new_attribute(String key, uint8_t 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);
|
void json_generic_response(String serialized_string, const uint16_t response_code);
|
||||||
|
|
|
@ -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;
|
return PROTOCOL_TCP;
|
||||||
else if (protocol.compare("UDP") == 0)
|
else if (protocol.equals("UDP"))
|
||||||
return PROTOCOL_UDP;
|
return PROTOCOL_UDP;
|
||||||
else
|
else
|
||||||
return PROTOCOL_ALL;
|
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;
|
return TARGET_REJECT;
|
||||||
else if (target.compare("DROP") == 0)
|
else if (target.equals("DROP"))
|
||||||
return TARGET_DROP;
|
return TARGET_DROP;
|
||||||
else
|
else
|
||||||
return TARGET_ACCEPT;
|
return TARGET_ACCEPT;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define UTILS_HPP
|
#define UTILS_HPP
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "string"
|
|
||||||
#include "WString.h"
|
#include "WString.h"
|
||||||
|
|
||||||
namespace firewall
|
namespace firewall
|
||||||
|
@ -54,9 +53,9 @@ namespace firewall
|
||||||
} credential_t;
|
} credential_t;
|
||||||
|
|
||||||
String protocol_to_string(firewall_protocol_t &protocol);
|
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);
|
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();
|
void endless_loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue