add firewall rule, return all rules
This commit is contained in:
parent
4d23c1039b
commit
2cc20a1d62
2 changed files with 78 additions and 10 deletions
|
@ -1,15 +1,18 @@
|
|||
#include "Firewall.h"
|
||||
|
||||
ESPFirewall::ESPFirewall()
|
||||
ESPFirewall::ESPFirewall(int port)
|
||||
{
|
||||
log_i("Firewall!");
|
||||
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules, this));
|
||||
firewall_api->begin();
|
||||
this->amount_of_rules = 0;
|
||||
this->head = NULL;
|
||||
log_i("Starting Firewall-API on %i", port);
|
||||
this->firewall_api = new WebServer(port);
|
||||
this->setup_routing();
|
||||
}
|
||||
|
||||
void ESPFirewall::get_firewall_rules()
|
||||
void ESPFirewall::setup_routing()
|
||||
{
|
||||
this->custom_message_response("Firewall", 200);
|
||||
this->firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules, this));
|
||||
this->firewall_api->begin();
|
||||
}
|
||||
|
||||
void ESPFirewall::custom_message_response(const char *message, int response_code)
|
||||
|
@ -17,10 +20,59 @@ void ESPFirewall::custom_message_response(const char *message, int response_code
|
|||
cJSON *json_response = cJSON_CreateObject();
|
||||
cJSON_AddBoolToObject(json_response, "ok", true);
|
||||
cJSON_AddStringToObject(json_response, "message", message);
|
||||
firewall_api->send(response_code, "application/json", cJSON_Print(json_response));
|
||||
this->firewall_api->send(response_code, "application/json", cJSON_Print(json_response));
|
||||
cJSON_Delete(json_response);
|
||||
}
|
||||
|
||||
void ESPFirewall::prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link)
|
||||
{
|
||||
cJSON_AddStringToObject(jsonResponse, "status", "success");
|
||||
cJSON_AddNumberToObject(jsonResponse, "rule", link->key);
|
||||
cJSON_AddStringToObject(jsonResponse, "source", link->source);
|
||||
cJSON_AddStringToObject(jsonResponse, "destination", link->destination);
|
||||
cJSON_AddStringToObject(jsonResponse, "protocol", link->protocol);
|
||||
cJSON_AddStringToObject(jsonResponse, "target", link->target);
|
||||
}
|
||||
|
||||
firewall_rule_t *ESPFirewall::add_rule_to_firewall(char *source, char *destination, char *protocol, 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;
|
||||
link->next = NULL;
|
||||
return link;
|
||||
}
|
||||
temp = head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = link;
|
||||
link->next = NULL;
|
||||
return link;
|
||||
}
|
||||
|
||||
void ESPFirewall::get_firewall_rules()
|
||||
{
|
||||
firewall_rule_t *ptr = head;
|
||||
cJSON *json_response = cJSON_CreateArray();
|
||||
while (ptr != NULL)
|
||||
{
|
||||
cJSON *json_firewall_rule = cJSON_CreateObject();
|
||||
prepare_firewall_json(json_firewall_rule, ptr);
|
||||
cJSON_AddItemToArray(json_response, json_firewall_rule);
|
||||
ptr = ptr->next;
|
||||
}
|
||||
this->firewall_api->send(200, "application/json", cJSON_Print(json_response));
|
||||
}
|
||||
|
||||
void ESPFirewall::handle_clients()
|
||||
{
|
||||
this->firewall_api->handleClient();
|
||||
|
|
|
@ -5,15 +5,31 @@
|
|||
#include "cJSON.h"
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
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;
|
||||
|
||||
class ESPFirewall
|
||||
{
|
||||
WebServer *firewall_api = new WebServer(8080);
|
||||
void get_firewall_rules();
|
||||
WebServer *firewall_api;
|
||||
int amount_of_rules;
|
||||
struct firewall_rule *head;
|
||||
|
||||
void setup_routing();
|
||||
void custom_message_response(const char *message, int response_code);
|
||||
void prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link);
|
||||
firewall_rule_t *add_rule_to_firewall(char *source, char *destination, char *protocol, char *target);
|
||||
void get_firewall_rules();
|
||||
|
||||
public:
|
||||
ESPFirewall();
|
||||
ESPFirewall(int port = 8080);
|
||||
|
||||
void handle_clients();
|
||||
};
|
||||
|
||||
|
|
Reference in a new issue