2022-04-10 20:46:54 +02:00
|
|
|
#ifndef FIREWALL_H
|
|
|
|
#define FIREWALL_H
|
|
|
|
|
2022-04-11 17:02:58 +02:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "ArduinoJson.h"
|
2022-04-18 11:16:01 +02:00
|
|
|
#include "EEPROM.h"
|
2022-04-18 22:56:30 +02:00
|
|
|
|
|
|
|
#include "HTTPSServer.hpp"
|
|
|
|
#include "SSLCert.hpp"
|
|
|
|
#include "HTTPRequest.hpp"
|
|
|
|
#include "HTTPResponse.hpp"
|
2022-04-10 20:46:54 +02:00
|
|
|
|
2022-04-18 17:35:52 +02:00
|
|
|
#define eeprom_start_firewall_rules 4
|
|
|
|
|
|
|
|
typedef enum firewall_target : uint8_t
|
2022-04-18 10:59:02 +02:00
|
|
|
{
|
2022-04-18 17:35:52 +02:00
|
|
|
FW_REJECT = 0,
|
|
|
|
FW_DROP = 1,
|
|
|
|
FW_ACCEPT = 2,
|
2022-04-18 10:59:02 +02:00
|
|
|
} firewall_target_t;
|
|
|
|
|
2022-04-18 17:35:52 +02:00
|
|
|
typedef enum firewall_protocol : uint8_t
|
2022-04-18 10:59:02 +02:00
|
|
|
{
|
2022-04-18 17:35:52 +02:00
|
|
|
FW_TCP = 0,
|
|
|
|
FW_UDP = 1,
|
|
|
|
FW_ALL = 255,
|
2022-04-18 10:59:02 +02:00
|
|
|
} firewall_protocol_t;
|
2022-04-11 18:06:21 +02:00
|
|
|
|
2022-04-11 11:47:50 +02:00
|
|
|
typedef struct firewall_rule
|
|
|
|
{
|
2022-04-18 10:59:02 +02:00
|
|
|
uint8_t key;
|
2022-04-11 17:46:44 +02:00
|
|
|
char source[IP4ADDR_STRLEN_MAX];
|
|
|
|
char destination[IP4ADDR_STRLEN_MAX];
|
2022-04-18 10:59:02 +02:00
|
|
|
firewall_protocol_t protocol;
|
|
|
|
firewall_target_t target;
|
2022-04-11 11:47:50 +02:00
|
|
|
struct firewall_rule *next;
|
|
|
|
} firewall_rule_t;
|
|
|
|
|
2022-04-10 20:46:54 +02:00
|
|
|
class ESPFirewall
|
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
uint16_t eeprom_size = 512;
|
2022-04-18 10:59:02 +02:00
|
|
|
uint8_t amount_of_rules = 0;
|
2022-04-18 18:40:23 +02:00
|
|
|
uint8_t security_number = 93;
|
2022-04-18 17:35:52 +02:00
|
|
|
int eeprom_settings_head = 0;
|
|
|
|
int eeprom_rules_head = eeprom_start_firewall_rules;
|
2022-04-11 17:02:58 +02:00
|
|
|
struct firewall_rule *head = NULL;
|
2022-04-11 11:47:50 +02:00
|
|
|
|
2022-04-18 22:56:30 +02:00
|
|
|
httpsserver::HTTPSServer *firewall_api;
|
|
|
|
httpsserver::SSLCert *certificate;
|
|
|
|
|
|
|
|
void setup_certificate();
|
2022-04-11 21:27:27 +02:00
|
|
|
|
2022-04-18 10:59:02 +02:00
|
|
|
// Protocol / Target conversion
|
|
|
|
String protocol_to_string(firewall_protocol_t &);
|
2022-04-18 22:56:30 +02:00
|
|
|
firewall_protocol_t string_to_protocol(std::string &);
|
2022-04-18 10:59:02 +02:00
|
|
|
String target_to_string(firewall_target_t &);
|
2022-04-18 22:56:30 +02:00
|
|
|
firewall_target_t string_to_target(std::string &);
|
2022-04-18 10:59:02 +02:00
|
|
|
|
2022-04-18 11:16:01 +02:00
|
|
|
// EEPROM
|
2022-04-18 13:11:02 +02:00
|
|
|
void setup_eeprom();
|
|
|
|
void eeprom_write_firewall_rule(firewall_rule_t *rule);
|
2022-04-18 18:40:23 +02:00
|
|
|
void eeprom_write_firewall_rules();
|
|
|
|
void eeprom_read_firewall_rule(uint8_t &, uint8_t &);
|
2022-04-18 13:11:02 +02:00
|
|
|
void eeprom_read_firewall_rules();
|
2022-04-18 11:16:01 +02:00
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
// Firewall Actions
|
|
|
|
void add_rule_to_firewall(firewall_rule_t *);
|
2022-04-18 18:40:23 +02:00
|
|
|
firewall_rule_t *get_rule_from_firewall(uint8_t);
|
|
|
|
bool delete_rule_from_firewall(uint8_t);
|
2022-04-11 21:27:27 +02:00
|
|
|
|
|
|
|
// Firewall-API Actions
|
2022-04-10 20:46:54 +02:00
|
|
|
void setup_routing();
|
2022-04-18 22:56:30 +02:00
|
|
|
void json_generic_response(httpsserver::HTTPResponse *, String, int);
|
|
|
|
void json_message_response(httpsserver::HTTPResponse *, String, int);
|
2022-04-11 21:27:27 +02:00
|
|
|
String construct_json_firewall_rule(firewall_rule_t *);
|
|
|
|
String construct_json_firewall();
|
2022-04-18 22:56:30 +02:00
|
|
|
void not_found(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
|
|
|
void restart_device_handler(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
|
|
|
void get_firewall_rule_handler(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
|
|
|
void get_firewall_rules_handler(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
|
|
|
bool request_has_firewall_parameter(httpsserver::ResourceParameters *);
|
|
|
|
void post_firewall_handler(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
|
|
|
void delete_firewall_handler(httpsserver::HTTPRequest *, httpsserver::HTTPResponse *);
|
2022-04-10 20:46:54 +02:00
|
|
|
|
|
|
|
public:
|
2022-04-11 11:47:50 +02:00
|
|
|
ESPFirewall(int port = 8080);
|
2022-04-10 20:46:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|