check for parameter
This commit is contained in:
parent
c33414ade1
commit
0e1bf6a915
2 changed files with 36 additions and 20 deletions
|
@ -7,19 +7,13 @@ ESPFirewall::ESPFirewall(int port)
|
||||||
this->setup_routing();
|
this->setup_routing();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target)
|
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
||||||
{
|
{
|
||||||
firewall_rule_t *temp;
|
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)
|
if (head == NULL)
|
||||||
{
|
{
|
||||||
head = link;
|
head = rule;
|
||||||
link->next = NULL;
|
rule->next = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
temp = head;
|
temp = head;
|
||||||
|
@ -27,8 +21,8 @@ void ESPFirewall::add_rule_to_firewall(const char *source, const char *destinati
|
||||||
{
|
{
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
temp->next = link;
|
temp->next = rule;
|
||||||
link->next = NULL;
|
rule->next = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,17 +52,30 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
||||||
DynamicJsonDocument json(1024);
|
DynamicJsonDocument json(1024);
|
||||||
String response;
|
String response;
|
||||||
int response_code;
|
int response_code;
|
||||||
if (request->hasArg("source") || request->hasArg("destination") || request->hasArg("protocol") || request->hasArg("target"))
|
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();
|
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();
|
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();
|
const char *protocol = request->arg("protocol").c_str();
|
||||||
|
strcpy(rule->protocol, strlen(protocol) <= PROTOCOL_LENGTH ? protocol : "-");
|
||||||
|
|
||||||
const char *target = request->arg("target").c_str();
|
const char *target = request->arg("target").c_str();
|
||||||
json["source"] = source;
|
strcpy(rule->target, strlen(target) <= TARGET_LENGTH ? target : "-");
|
||||||
json["destination"] = destination;
|
|
||||||
json["protocol"] = protocol;
|
add_rule_to_firewall(rule);
|
||||||
json["target"] = target;
|
|
||||||
add_rule_to_firewall(source, destination, protocol, target);
|
json["key"] = rule->key;
|
||||||
|
json["source"] = rule->source;
|
||||||
|
json["destination"] = rule->destination;
|
||||||
|
json["protocol"] = rule->protocol;
|
||||||
|
json["target"] = rule->target;
|
||||||
response_code = 200;
|
response_code = 200;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -89,6 +96,11 @@ void ESPFirewall::not_found(AsyncWebServerRequest *request)
|
||||||
request->send(404, "application/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()
|
||||||
{
|
{
|
||||||
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_GET, std::bind(&ESPFirewall::get_firewall_handler, this, std::placeholders::_1));
|
||||||
|
|
|
@ -13,13 +13,16 @@
|
||||||
#endif
|
#endif
|
||||||
#include "ESPAsyncWebServer.h"
|
#include "ESPAsyncWebServer.h"
|
||||||
|
|
||||||
|
#define PROTOCOL_LENGTH 4
|
||||||
|
#define TARGET_LENGTH 7
|
||||||
|
|
||||||
typedef struct firewall_rule
|
typedef struct firewall_rule
|
||||||
{
|
{
|
||||||
int key;
|
int key;
|
||||||
char source[IP4ADDR_STRLEN_MAX];
|
char source[IP4ADDR_STRLEN_MAX];
|
||||||
char destination[IP4ADDR_STRLEN_MAX];
|
char destination[IP4ADDR_STRLEN_MAX];
|
||||||
char protocol[4];
|
char protocol[PROTOCOL_LENGTH];
|
||||||
char target[7];
|
char target[TARGET_LENGTH];
|
||||||
struct firewall_rule *next;
|
struct firewall_rule *next;
|
||||||
} firewall_rule_t;
|
} firewall_rule_t;
|
||||||
|
|
||||||
|
@ -29,10 +32,11 @@ class ESPFirewall
|
||||||
unsigned int amount_of_rules = 0;
|
unsigned int amount_of_rules = 0;
|
||||||
struct firewall_rule *head = NULL;
|
struct firewall_rule *head = NULL;
|
||||||
|
|
||||||
void add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target);
|
void add_rule_to_firewall(firewall_rule_t *rule);
|
||||||
void get_firewall_handler(AsyncWebServerRequest *request);
|
void get_firewall_handler(AsyncWebServerRequest *request);
|
||||||
void post_firewall_handler(AsyncWebServerRequest *request);
|
void post_firewall_handler(AsyncWebServerRequest *request);
|
||||||
void not_found(AsyncWebServerRequest *request);
|
void not_found(AsyncWebServerRequest *request);
|
||||||
|
bool request_has_firewall_parameter(AsyncWebServerRequest *request);
|
||||||
void setup_routing();
|
void setup_routing();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
Reference in a new issue