firewall routes can be created

This commit is contained in:
Florian Hoss 2022-04-11 17:46:44 +02:00
parent 9dfd1d06ab
commit c33414ade1
2 changed files with 19 additions and 7 deletions

View file

@ -7,11 +7,15 @@ ESPFirewall::ESPFirewall(int port)
this->setup_routing();
}
void ESPFirewall::add_rule_to_firewall()
void ESPFirewall::add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target)
{
firewall_rule_t *temp;
firewall_rule_t *link = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
link->key = ++amount_of_rules;
strcpy(link->source, source);
strcpy(link->destination, destination);
strcpy(link->protocol, protocol);
strcpy(link->target, target);
if (head == NULL)
{
head = link;
@ -39,6 +43,10 @@ void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request)
{
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);
@ -52,15 +60,15 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
int response_code;
if (request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target"))
{
String source = request->arg("source");
String destination = request->arg("destination");
String protocol = request->arg("protocol");
String target = request->arg("target");
const char *source = request->arg("source").c_str();
const char *destination = request->arg("destination").c_str();
const char *protocol = request->arg("protocol").c_str();
const char *target = request->arg("target").c_str();
json["source"] = source;
json["destination"] = destination;
json["protocol"] = protocol;
json["target"] = target;
add_rule_to_firewall();
add_rule_to_firewall(source, destination, protocol, target);
response_code = 200;
}
else

View file

@ -16,6 +16,10 @@
typedef struct firewall_rule
{
int key;
char source[IP4ADDR_STRLEN_MAX];
char destination[IP4ADDR_STRLEN_MAX];
char protocol[4];
char target[7];
struct firewall_rule *next;
} firewall_rule_t;
@ -25,7 +29,7 @@ class ESPFirewall
unsigned int amount_of_rules = 0;
struct firewall_rule *head = NULL;
void add_rule_to_firewall();
void add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target);
void get_firewall_handler(AsyncWebServerRequest *request);
void post_firewall_handler(AsyncWebServerRequest *request);
void not_found(AsyncWebServerRequest *request);