delete firewall rule works
This commit is contained in:
parent
cd41ce6129
commit
dff1f4f0aa
2 changed files with 66 additions and 11 deletions
|
@ -47,11 +47,53 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
||||||
return rule_ptr;
|
return rule_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ESPFirewall::delete_rule_from_firewall(int key)
|
||||||
|
{
|
||||||
|
if (head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
firewall_rule_t *current_rule_ptr = this->head;
|
||||||
|
firewall_rule_t *previous_rule_ptr = NULL;
|
||||||
|
firewall_rule_t *temp = NULL;
|
||||||
|
while (current_rule_ptr->key != key)
|
||||||
|
{
|
||||||
|
if (current_rule_ptr->next == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
previous_rule_ptr = current_rule_ptr;
|
||||||
|
current_rule_ptr = current_rule_ptr->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (current_rule_ptr == head)
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
temp = head;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
previous_rule_ptr->next = current_rule_ptr->next;
|
||||||
|
temp = previous_rule_ptr->next;
|
||||||
|
}
|
||||||
|
while (temp != NULL)
|
||||||
|
{
|
||||||
|
temp->key--;
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
free(current_rule_ptr);
|
||||||
|
amount_of_rules--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void ESPFirewall::setup_routing()
|
void ESPFirewall::setup_routing()
|
||||||
{
|
{
|
||||||
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1));
|
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1));
|
||||||
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1));
|
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_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->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1));
|
||||||
|
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_DELETE, std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1));
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
@ -125,24 +167,30 @@ void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
|
||||||
request->send(200, "application/json", response);
|
request->send(200, "application/json", response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
return request->hasParam("source") || request->hasParam("destination") || request->hasParam("protocol") || request->hasParam("target");
|
||||||
|
}
|
||||||
|
|
||||||
void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
if (request_has_firewall_parameter(request))
|
if (request_has_firewall_parameter(request))
|
||||||
{
|
{
|
||||||
firewall_rule_t *rule = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||||
rule->key = ++amount_of_rules;
|
rule_ptr->key = ++amount_of_rules;
|
||||||
|
|
||||||
|
// carefully copying c-string that is shorter then the destination char-array length
|
||||||
String source = request->getParam("source")->value();
|
String source = request->getParam("source")->value();
|
||||||
strcpy(rule->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : "");
|
strcpy(rule_ptr->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : "");
|
||||||
String destination = request->getParam("destination")->value();
|
String destination = request->getParam("destination")->value();
|
||||||
strcpy(rule->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : "");
|
strcpy(rule_ptr->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : "");
|
||||||
String protocol = request->getParam("protocol")->value();
|
String protocol = request->getParam("protocol")->value();
|
||||||
strcpy(rule->protocol, protocol.length() <= PROTOCOL_LENGTH ? protocol.c_str() : "");
|
strcpy(rule_ptr->protocol, protocol.length() <= PROTOCOL_LENGTH ? protocol.c_str() : "");
|
||||||
String target = request->getParam("target")->value();
|
String target = request->getParam("target")->value();
|
||||||
strcpy(rule->target, target.length() <= TARGET_LENGTH ? target.c_str() : "");
|
strcpy(rule_ptr->target, target.length() <= TARGET_LENGTH ? target.c_str() : "");
|
||||||
|
|
||||||
add_rule_to_firewall(rule);
|
add_rule_to_firewall(rule_ptr);
|
||||||
json_message_response(request, "success", 200);
|
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -150,7 +198,12 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
|
void ESPFirewall::delete_firewall_handler(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
return request->hasParam("source") || request->hasParam("destination") || request->hasParam("protocol") || request->hasParam("target");
|
int rule_number = request->pathArg(0).toInt();
|
||||||
|
if (delete_rule_from_firewall(rule_number))
|
||||||
|
{
|
||||||
|
json_message_response(request, "firewall rule deleted", 200);
|
||||||
|
}
|
||||||
|
json_message_response(request, "cannot delete firewall rule", 500);
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ class ESPFirewall
|
||||||
// Firewall Actions
|
// Firewall Actions
|
||||||
void add_rule_to_firewall(firewall_rule_t *);
|
void add_rule_to_firewall(firewall_rule_t *);
|
||||||
firewall_rule_t *get_rule_from_firewall(int key);
|
firewall_rule_t *get_rule_from_firewall(int key);
|
||||||
|
bool delete_rule_from_firewall(int key);
|
||||||
|
|
||||||
// Firewall-API Actions
|
// Firewall-API Actions
|
||||||
void setup_routing();
|
void setup_routing();
|
||||||
|
@ -45,8 +46,9 @@ class ESPFirewall
|
||||||
void not_found(AsyncWebServerRequest *);
|
void not_found(AsyncWebServerRequest *);
|
||||||
void get_firewall_rule_handler(AsyncWebServerRequest *);
|
void get_firewall_rule_handler(AsyncWebServerRequest *);
|
||||||
void get_firewall_rules_handler(AsyncWebServerRequest *);
|
void get_firewall_rules_handler(AsyncWebServerRequest *);
|
||||||
void post_firewall_handler(AsyncWebServerRequest *);
|
|
||||||
bool request_has_firewall_parameter(AsyncWebServerRequest *);
|
bool request_has_firewall_parameter(AsyncWebServerRequest *);
|
||||||
|
void post_firewall_handler(AsyncWebServerRequest *);
|
||||||
|
void delete_firewall_handler(AsyncWebServerRequest *);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ESPFirewall(int port = 8080);
|
ESPFirewall(int port = 8080);
|
||||||
|
|
Reference in a new issue