create seperate types file

This commit is contained in:
Florian Hoss 2022-04-19 21:15:55 +02:00
parent aa725f995f
commit 69eaf1cead
3 changed files with 48 additions and 39 deletions

View file

@ -1,10 +1,17 @@
#include "Firewall.h" #include "Firewall.h"
ESPFirewall::ESPFirewall(int api_port) ESPFirewall::ESPFirewall(const uint16_t api_port)
{ {
this->setup_eeprom(); this->setup_eeprom();
this->setup_certificate(); this->setup_certificate();
this->setup_firewall_api(api_port); this->firewall_api = new HTTPSServer(this->certificate, api_port, 5);
this->setup_routing();
log_i("Starting server...");
this->firewall_api->start();
if (this->firewall_api->isRunning())
{
log_i("Server ready.");
}
} }
void ESPFirewall::handle_firewall_api_clients() void ESPFirewall::handle_firewall_api_clients()
@ -229,9 +236,8 @@ void ESPFirewall::setup_certificate()
log_i("Creating the certificate was successful"); log_i("Creating the certificate was successful");
} }
void ESPFirewall::setup_firewall_api(int api_port) void ESPFirewall::setup_routing()
{ {
this->firewall_api = new HTTPSServer(this->certificate, api_port, 5);
ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *get_firewall_rule = new ResourceNode("/api/v1/firewall/*", "GET", std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *get_firewall_rules = new ResourceNode("/api/v1/firewall", "GET", std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1, std::placeholders::_2));
ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2)); ResourceNode *post_firewall = new ResourceNode("/api/v1/firewall", "POST", std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1, std::placeholders::_2));
@ -244,13 +250,6 @@ void ESPFirewall::setup_firewall_api(int api_port)
this->firewall_api->registerNode(delete_firewall); this->firewall_api->registerNode(delete_firewall);
this->firewall_api->setDefaultNode(restart_device); this->firewall_api->setDefaultNode(restart_device);
this->firewall_api->setDefaultNode(not_found); this->firewall_api->setDefaultNode(not_found);
log_i("Starting server...");
this->firewall_api->start();
if (this->firewall_api->isRunning())
{
log_i("Server ready.");
}
} }
void ESPFirewall::json_generic_response(HTTPResponse *response, String serialized, int response_code) void ESPFirewall::json_generic_response(HTTPResponse *response, String serialized, int response_code)
@ -356,10 +355,10 @@ void ESPFirewall::post_firewall_handler(HTTPRequest *request, HTTPResponse *resp
// carefully copying c-string that is shorter then the destination char-array length // carefully copying c-string that is shorter then the destination char-array length
std::string source; std::string source;
params->getQueryParameter("source", source); params->getQueryParameter("source", source);
strcpy(rule_ptr->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : ""); strcpy(rule_ptr->source, source.length() <= IPV4ADDRESS_LENGTH ? source.c_str() : "");
std::string destination; std::string destination;
params->getQueryParameter("destination", destination); params->getQueryParameter("destination", destination);
strcpy(rule_ptr->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : ""); strcpy(rule_ptr->destination, destination.length() <= IPV4ADDRESS_LENGTH ? destination.c_str() : "");
std::string protocol; std::string protocol;
params->getQueryParameter("protocol", protocol); params->getQueryParameter("protocol", protocol);

View file

@ -10,33 +10,11 @@
#include "HTTPRequest.hpp" #include "HTTPRequest.hpp"
#include "HTTPResponse.hpp" #include "HTTPResponse.hpp"
#include "FirewallTypes.h"
#define eeprom_start_firewall_rules 4 #define eeprom_start_firewall_rules 4
using namespace httpsserver; using namespace httpsserver;
typedef enum firewall_target : uint8_t
{
FW_REJECT = 0,
FW_DROP = 1,
FW_ACCEPT = 2,
} firewall_target_t;
typedef enum firewall_protocol : uint8_t
{
FW_TCP = 0,
FW_UDP = 1,
FW_ALL = 255,
} firewall_protocol_t;
typedef struct firewall_rule
{
uint8_t key;
char source[IP4ADDR_STRLEN_MAX];
char destination[IP4ADDR_STRLEN_MAX];
firewall_protocol_t protocol;
firewall_target_t target;
struct firewall_rule *next;
} firewall_rule_t;
class ESPFirewall class ESPFirewall
{ {
uint16_t eeprom_size = 512; uint16_t eeprom_size = 512;
@ -69,7 +47,7 @@ class ESPFirewall
// Firewall-API Actions // Firewall-API Actions
void setup_certificate(); void setup_certificate();
void setup_firewall_api(int); void setup_routing();
void json_generic_response(HTTPResponse *, String, int); void json_generic_response(HTTPResponse *, String, int);
void json_message_response(HTTPResponse *, String, int); void json_message_response(HTTPResponse *, String, int);
String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall_rule(firewall_rule_t *);
@ -83,7 +61,7 @@ class ESPFirewall
void delete_firewall_handler(HTTPRequest *, HTTPResponse *); void delete_firewall_handler(HTTPRequest *, HTTPResponse *);
public: public:
ESPFirewall(int port = 8080); ESPFirewall(const uint16_t = 8080);
void handle_firewall_api_clients(); void handle_firewall_api_clients();
}; };

View file

@ -0,0 +1,32 @@
#ifndef FIREWALL_TYPES_H
#define FIREWALL_TYPES_H
#include "stdint.h"
static const uint8_t IPV4ADDRESS_LENGTH = 16;
typedef enum firewall_target : uint8_t
{
FW_REJECT = 0,
FW_DROP = 1,
FW_ACCEPT = 2,
} firewall_target_t;
typedef enum firewall_protocol : uint8_t
{
FW_TCP = 0,
FW_UDP = 1,
FW_ALL = 255,
} firewall_protocol_t;
typedef struct firewall_rule
{
uint8_t key;
char source[IPV4ADDRESS_LENGTH];
char destination[IPV4ADDRESS_LENGTH];
firewall_protocol_t protocol;
firewall_target_t target;
struct firewall_rule *next;
} firewall_rule_t;
#endif