speed and clean code
This commit is contained in:
parent
f1dbc95de5
commit
cd41ce6129
2 changed files with 122 additions and 109 deletions
|
@ -7,15 +7,6 @@ ESPFirewall::ESPFirewall(int port)
|
||||||
this->setup_routing();
|
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)
|
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
||||||
{
|
{
|
||||||
firewall_rule_t *temp;
|
firewall_rule_t *temp;
|
||||||
|
@ -35,109 +26,25 @@ void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
|
firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
||||||
{
|
{
|
||||||
firewall_rule_t *ptr = this->head;
|
firewall_rule_t *rule_ptr = this->head;
|
||||||
DynamicJsonDocument json(1024);
|
|
||||||
String response;
|
|
||||||
json["amount"] = amount_of_rules;
|
|
||||||
JsonArray rules = json.createNestedArray("rules");
|
|
||||||
while (ptr != NULL)
|
|
||||||
{
|
|
||||||
JsonObject rule = rules.createNestedObject();
|
|
||||||
rule["key"] = ptr->key;
|
|
||||||
rule["source"] = ptr->source;
|
|
||||||
rule["destination"] = ptr->destination;
|
|
||||||
rule["protocol"] = ptr->protocol;
|
|
||||||
rule["target"] = ptr->target;
|
|
||||||
ptr = ptr->next;
|
|
||||||
}
|
|
||||||
serializeJson(json, response);
|
|
||||||
request->send(200, "application/json", response);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
|
|
||||||
{
|
|
||||||
int rule_number = request->pathArg(0).toInt();
|
|
||||||
DynamicJsonDocument json(1024);
|
|
||||||
String response;
|
|
||||||
firewall_rule_t *current = this->head;
|
|
||||||
|
|
||||||
if (head == NULL)
|
if (head == NULL)
|
||||||
{
|
{
|
||||||
json["message"] = "list is empty";
|
return NULL;
|
||||||
serializeJson(json, response);
|
|
||||||
request->send(500, "application/json", response);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
while (rule_ptr->key != key)
|
||||||
while (current->key != rule_number)
|
|
||||||
{
|
{
|
||||||
if (current->next == NULL)
|
if (rule_ptr->next == NULL)
|
||||||
{
|
{
|
||||||
json["message"] = "rule not found";
|
return NULL;
|
||||||
serializeJson(json, response);
|
|
||||||
request->send(500, "application/json", response);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
current = current->next;
|
rule_ptr = rule_ptr->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prepare_firewall_json(json, current);
|
return rule_ptr;
|
||||||
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_has_firewall_parameter(request))
|
|
||||||
{
|
|
||||||
firewall_rule_t *rule = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
|
||||||
rule->key = ++amount_of_rules;
|
|
||||||
|
|
||||||
const char *source = request->arg("source").c_str();
|
|
||||||
strcpy(rule->source, strlen(source) <= IP4ADDR_STRLEN_MAX ? source : "-");
|
|
||||||
|
|
||||||
const char *destination = request->arg("destination").c_str();
|
|
||||||
strcpy(rule->destination, strlen(destination) <= IP4ADDR_STRLEN_MAX ? destination : "-");
|
|
||||||
|
|
||||||
const char *protocol = request->arg("protocol").c_str();
|
|
||||||
strcpy(rule->protocol, strlen(protocol) <= PROTOCOL_LENGTH ? protocol : "-");
|
|
||||||
|
|
||||||
const char *target = request->arg("target").c_str();
|
|
||||||
strcpy(rule->target, strlen(target) <= TARGET_LENGTH ? target : "-");
|
|
||||||
|
|
||||||
add_rule_to_firewall(rule);
|
|
||||||
|
|
||||||
prepare_firewall_json(json, rule);
|
|
||||||
response_code = 200;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
json["message"] = "not enough parameter provided";
|
|
||||||
response_code = 400;
|
|
||||||
}
|
|
||||||
serializeJson(json, response);
|
|
||||||
request->send(response_code, "application/json", response);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ESPFirewall::not_found(AsyncWebServerRequest *request)
|
|
||||||
{
|
|
||||||
DynamicJsonDocument json(1024);
|
|
||||||
String response;
|
|
||||||
json["message"] = "not found";
|
|
||||||
serializeJson(json, response);
|
|
||||||
request->send(404, "application/json", response);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
|
|
||||||
{
|
|
||||||
return request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::setup_routing()
|
void ESPFirewall::setup_routing()
|
||||||
|
@ -148,3 +55,102 @@ void ESPFirewall::setup_routing()
|
||||||
firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
|
firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
|
||||||
this->firewall_api->begin();
|
this->firewall_api->begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::json_message_response(AsyncWebServerRequest *request, String message, int response_code)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument json(256);
|
||||||
|
String response;
|
||||||
|
json["message"] = message;
|
||||||
|
serializeJson(json, response);
|
||||||
|
request->send(response_code, "application/json", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
|
||||||
|
{
|
||||||
|
StaticJsonDocument<192> doc;
|
||||||
|
doc["key"] = rule_ptr->key;
|
||||||
|
doc["source"] = rule_ptr->source;
|
||||||
|
doc["destination"] = rule_ptr->destination;
|
||||||
|
doc["protocol"] = rule_ptr->protocol;
|
||||||
|
doc["target"] = rule_ptr->target;
|
||||||
|
String response;
|
||||||
|
serializeJson(doc, response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
String ESPFirewall::construct_json_firewall()
|
||||||
|
{
|
||||||
|
firewall_rule_t *rule_ptr = this->head;
|
||||||
|
// Size for 12 Rules
|
||||||
|
StaticJsonDocument<2048> doc;
|
||||||
|
String response;
|
||||||
|
doc["amount"] = amount_of_rules;
|
||||||
|
JsonArray rules = doc.createNestedArray("rules");
|
||||||
|
while (rule_ptr != NULL)
|
||||||
|
{
|
||||||
|
JsonObject rule = rules.createNestedObject();
|
||||||
|
rule["key"] = rule_ptr->key;
|
||||||
|
rule["source"] = rule_ptr->source;
|
||||||
|
rule["destination"] = rule_ptr->destination;
|
||||||
|
rule["protocol"] = rule_ptr->protocol;
|
||||||
|
rule["target"] = rule_ptr->target;
|
||||||
|
rule_ptr = rule_ptr->next;
|
||||||
|
}
|
||||||
|
serializeJson(doc, response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::not_found(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
json_message_response(request, "not found", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
int rule_number = request->pathArg(0).toInt();
|
||||||
|
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
||||||
|
if (rule_ptr == NULL)
|
||||||
|
{
|
||||||
|
json_message_response(request, "rule not found", 404);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
String response = construct_json_firewall();
|
||||||
|
request->send(200, "application/json", response);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
if (request_has_firewall_parameter(request))
|
||||||
|
{
|
||||||
|
firewall_rule_t *rule = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||||
|
rule->key = ++amount_of_rules;
|
||||||
|
|
||||||
|
String source = request->getParam("source")->value();
|
||||||
|
strcpy(rule->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : "");
|
||||||
|
String destination = request->getParam("destination")->value();
|
||||||
|
strcpy(rule->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : "");
|
||||||
|
String protocol = request->getParam("protocol")->value();
|
||||||
|
strcpy(rule->protocol, protocol.length() <= PROTOCOL_LENGTH ? protocol.c_str() : "");
|
||||||
|
String target = request->getParam("target")->value();
|
||||||
|
strcpy(rule->target, target.length() <= TARGET_LENGTH ? target.c_str() : "");
|
||||||
|
|
||||||
|
add_rule_to_firewall(rule);
|
||||||
|
json_message_response(request, "success", 200);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
json_message_response(request, "not enough parameter", 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
return request->hasParam("source") || request->hasParam("destination") || request->hasParam("protocol") || request->hasParam("target");
|
||||||
|
}
|
||||||
|
|
|
@ -28,18 +28,25 @@ typedef struct firewall_rule
|
||||||
|
|
||||||
class ESPFirewall
|
class ESPFirewall
|
||||||
{
|
{
|
||||||
AsyncWebServer *firewall_api;
|
|
||||||
unsigned int amount_of_rules = 0;
|
unsigned int amount_of_rules = 0;
|
||||||
struct firewall_rule *head = NULL;
|
struct firewall_rule *head = NULL;
|
||||||
|
|
||||||
void prepare_firewall_json(DynamicJsonDocument &json, firewall_rule_t *rule);
|
AsyncWebServer *firewall_api;
|
||||||
void add_rule_to_firewall(firewall_rule_t *rule);
|
|
||||||
void get_firewall_rules_handler(AsyncWebServerRequest *request);
|
// Firewall Actions
|
||||||
void get_firewall_rule_handler(AsyncWebServerRequest *request);
|
void add_rule_to_firewall(firewall_rule_t *);
|
||||||
void post_firewall_handler(AsyncWebServerRequest *request);
|
firewall_rule_t *get_rule_from_firewall(int key);
|
||||||
void not_found(AsyncWebServerRequest *request);
|
|
||||||
bool request_has_firewall_parameter(AsyncWebServerRequest *request);
|
// Firewall-API Actions
|
||||||
void setup_routing();
|
void setup_routing();
|
||||||
|
void json_message_response(AsyncWebServerRequest *, String, int);
|
||||||
|
String construct_json_firewall_rule(firewall_rule_t *);
|
||||||
|
String construct_json_firewall();
|
||||||
|
void not_found(AsyncWebServerRequest *);
|
||||||
|
void get_firewall_rule_handler(AsyncWebServerRequest *);
|
||||||
|
void get_firewall_rules_handler(AsyncWebServerRequest *);
|
||||||
|
void post_firewall_handler(AsyncWebServerRequest *);
|
||||||
|
bool request_has_firewall_parameter(AsyncWebServerRequest *);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ESPFirewall(int port = 8080);
|
ESPFirewall(int port = 8080);
|
||||||
|
|
Reference in a new issue