This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/SourceCode/arduino/lib/Firewall/Firewall.h

81 lines
2 KiB
C
Raw Normal View History

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 "AsyncJson.h"
#include "ArduinoJson.h"
2022-04-18 11:16:01 +02:00
#include "EEPROM.h"
2022-04-11 17:02:58 +02:00
#ifdef ESP32
#include "WiFi.h"
#include "AsyncTCP.h"
#elif defined(ESP8266)
#include "ESP8266WiFi.h"
#include "ESPAsyncTCP.h"
#endif
#include "ESPAsyncWebServer.h"
2022-04-10 20:46:54 +02:00
typedef enum
{
FW_REJECT = 0b0001,
FW_DROP = 0b0010,
FW_ACCEPT = 0b0011,
} firewall_target_t;
typedef enum
{
FW_TCP = 0b0001,
FW_UDP = 0b0010,
FW_ALL = 0b1111,
} firewall_protocol_t;
2022-04-11 18:06:21 +02:00
2022-04-11 11:47:50 +02:00
typedef struct firewall_rule
{
uint8_t key;
2022-04-11 17:46:44 +02:00
char source[IP4ADDR_STRLEN_MAX];
char destination[IP4ADDR_STRLEN_MAX];
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
{
uint8_t amount_of_rules = 0;
2022-04-18 11:16:01 +02:00
int eeprom_address = 0;
2022-04-11 17:02:58 +02:00
struct firewall_rule *head = NULL;
2022-04-11 11:47:50 +02:00
2022-04-11 21:27:27 +02:00
AsyncWebServer *firewall_api;
// Protocol / Target conversion
String protocol_to_string(firewall_protocol_t &);
firewall_protocol_t string_to_protocol(String &);
String target_to_string(firewall_target_t &);
firewall_target_t string_to_target(String &);
2022-04-18 11:16:01 +02:00
// EEPROM
void write_rule_to_eeprom(firewall_rule_t *rule);
void get_rules_from_eeprom();
2022-04-11 21:27:27 +02:00
// Firewall Actions
void add_rule_to_firewall(firewall_rule_t *);
firewall_rule_t *get_rule_from_firewall(int);
bool delete_rule_from_firewall(int);
2022-04-11 21:27:27 +02:00
// Firewall-API Actions
2022-04-10 20:46:54 +02:00
void setup_routing();
2022-04-11 21:27:27 +02:00
void json_message_response(AsyncWebServerRequest *, String, int);
String construct_json_firewall_rule(firewall_rule_t *);
String construct_json_firewall();
void not_found(AsyncWebServerRequest *);
void get_firewall_rule_handler(AsyncWebServerRequest *);
void get_firewall_rules_handler(AsyncWebServerRequest *);
bool request_has_firewall_parameter(AsyncWebServerRequest *);
2022-04-11 22:17:55 +02:00
void post_firewall_handler(AsyncWebServerRequest *);
void delete_firewall_handler(AsyncWebServerRequest *);
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