This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/SourceCode/arduino/lib/Firewall/Firewall.cpp

111 lines
3.4 KiB
C++
Raw Normal View History

2022-04-10 20:46:54 +02:00
#include "Firewall.h"
2022-04-11 11:47:50 +02:00
ESPFirewall::ESPFirewall(int port)
2022-04-10 20:46:54 +02:00
{
2022-04-11 11:47:50 +02:00
log_i("Starting Firewall-API on %i", port);
2022-04-11 17:02:58 +02:00
this->firewall_api = new AsyncWebServer(port);
2022-04-11 11:47:50 +02:00
this->setup_routing();
2022-04-10 20:46:54 +02:00
}
2022-04-11 18:06:21 +02:00
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
2022-04-11 11:47:50 +02:00
{
firewall_rule_t *temp;
if (head == NULL)
{
2022-04-11 18:06:21 +02:00
head = rule;
rule->next = NULL;
2022-04-11 17:02:58 +02:00
return;
2022-04-11 11:47:50 +02:00
}
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
2022-04-11 18:06:21 +02:00
temp->next = rule;
rule->next = NULL;
2022-04-11 17:02:58 +02:00
return;
2022-04-11 11:47:50 +02:00
}
2022-04-11 17:02:58 +02:00
void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request)
2022-04-11 13:49:48 +02:00
{
2022-04-11 17:02:58 +02:00
firewall_rule_t *ptr = this->head;
DynamicJsonDocument json(1024);
String response;
json["amount"] = amount_of_rules;
JsonArray rules = json.createNestedArray("rules");
while (ptr != NULL)
2022-04-11 13:49:48 +02:00
{
2022-04-11 17:02:58 +02:00
JsonObject rule = rules.createNestedObject();
rule["key"] = ptr->key;
2022-04-11 17:46:44 +02:00
rule["source"] = ptr->source;
rule["destination"] = ptr->destination;
rule["protocol"] = ptr->protocol;
rule["target"] = ptr->target;
2022-04-11 17:02:58 +02:00
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;
2022-04-11 18:06:21 +02:00
if (request_has_firewall_parameter(request))
2022-04-11 17:02:58 +02:00
{
2022-04-11 18:06:21 +02:00
firewall_rule_t *rule = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
rule->key = ++amount_of_rules;
2022-04-11 17:46:44 +02:00
const char *source = request->arg("source").c_str();
2022-04-11 18:06:21 +02:00
strcpy(rule->source, strlen(source) <= IP4ADDR_STRLEN_MAX ? source : "-");
2022-04-11 17:46:44 +02:00
const char *destination = request->arg("destination").c_str();
2022-04-11 18:06:21 +02:00
strcpy(rule->destination, strlen(destination) <= IP4ADDR_STRLEN_MAX ? destination : "-");
2022-04-11 17:46:44 +02:00
const char *protocol = request->arg("protocol").c_str();
2022-04-11 18:06:21 +02:00
strcpy(rule->protocol, strlen(protocol) <= PROTOCOL_LENGTH ? protocol : "-");
2022-04-11 17:46:44 +02:00
const char *target = request->arg("target").c_str();
2022-04-11 18:06:21 +02:00
strcpy(rule->target, strlen(target) <= TARGET_LENGTH ? target : "-");
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;
2022-04-11 17:02:58 +02:00
response_code = 200;
2022-04-11 13:49:48 +02:00
}
else
{
2022-04-11 17:02:58 +02:00
json["message"] = "not enough parameter provided";
response_code = 400;
2022-04-11 13:49:48 +02:00
}
2022-04-11 17:02:58 +02:00
serializeJson(json, response);
request->send(response_code, "application/json", response);
2022-04-11 13:49:48 +02:00
}
2022-04-11 17:02:58 +02:00
void ESPFirewall::not_found(AsyncWebServerRequest *request)
2022-04-11 11:47:50 +02:00
{
2022-04-11 17:02:58 +02:00
DynamicJsonDocument json(1024);
String response;
json["message"] = "not found";
serializeJson(json, response);
request->send(404, "application/json", response);
2022-04-11 11:47:50 +02:00
}
2022-04-11 18:06:21 +02:00
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
{
return request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target");
}
2022-04-11 17:02:58 +02:00
void ESPFirewall::setup_routing()
2022-04-10 20:46:54 +02:00
{
2022-04-11 17:02:58 +02:00
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();
}