#ifndef FIREWALL_H #define FIREWALL_H #include "Arduino.h" #include "ArduinoJson.h" #include "EEPROM.h" #include "HTTPSServer.hpp" #include "SSLCert.hpp" #include "HTTPRequest.hpp" #include "HTTPResponse.hpp" #define eeprom_start_firewall_rules 4 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 { uint16_t eeprom_size = 512; uint8_t amount_of_rules = 0; uint8_t security_number = 93; int eeprom_settings_head = 0; int eeprom_rules_head = eeprom_start_firewall_rules; struct firewall_rule *head = NULL; HTTPSServer *firewall_api; SSLCert *certificate; // Protocol / Target conversion String protocol_to_string(firewall_protocol_t &); firewall_protocol_t string_to_protocol(std::string &); String target_to_string(firewall_target_t &); firewall_target_t string_to_target(std::string &); // EEPROM void setup_eeprom(); void eeprom_write_firewall_rule(firewall_rule_t *rule); void eeprom_write_firewall_rules(); void eeprom_read_firewall_rule(uint8_t &, uint8_t &); void eeprom_read_firewall_rules(); // Firewall Actions void add_rule_to_firewall(firewall_rule_t *); firewall_rule_t *get_rule_from_firewall(uint8_t); bool delete_rule_from_firewall(uint8_t); // Firewall-API Actions void setup_certificate(); void setup_firewall_api(int); void json_generic_response(HTTPResponse *, String, int); void json_message_response(HTTPResponse *, String, int); String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall(); void not_found_handler(HTTPRequest *, HTTPResponse *); void restart_device_handler(HTTPRequest *, HTTPResponse *); void get_firewall_rule_handler(HTTPRequest *, HTTPResponse *); void get_firewall_rules_handler(HTTPRequest *, HTTPResponse *); bool request_has_firewall_parameter(ResourceParameters *); void post_firewall_handler(HTTPRequest *, HTTPResponse *); void delete_firewall_handler(HTTPRequest *, HTTPResponse *); public: ESPFirewall(int port = 8080); void handle_firewall_api_clients(); }; #endif