store amount of rules in eeprom
This commit is contained in:
parent
d269eff8ee
commit
d7520c15fd
3 changed files with 66 additions and 23 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
ESPFirewall::ESPFirewall(int port)
|
ESPFirewall::ESPFirewall(int port)
|
||||||
{
|
{
|
||||||
|
this->setup_eeprom();
|
||||||
log_i("Starting Firewall-API on %i", port);
|
log_i("Starting Firewall-API on %i", port);
|
||||||
this->firewall_api = new AsyncWebServer(port);
|
this->firewall_api = new AsyncWebServer(port);
|
||||||
this->setup_routing();
|
this->setup_routing();
|
||||||
|
@ -53,23 +54,57 @@ firewall_target_t ESPFirewall::string_to_target(String &target)
|
||||||
return FW_ACCEPT;
|
return FW_ACCEPT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::write_rule_to_eeprom(firewall_rule_t *rule)
|
void ESPFirewall::setup_eeprom()
|
||||||
{
|
{
|
||||||
EEPROM.put(this->eeprom_address, rule->source);
|
EEPROM.begin(this->eeprom_size);
|
||||||
this->eeprom_address + IP4ADDR_STRLEN_MAX;
|
this->eeprom_settings_head = 0;
|
||||||
EEPROM.put(this->eeprom_address, rule->destination);
|
this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head);
|
||||||
this->eeprom_address + IP4ADDR_STRLEN_MAX;
|
this->eeprom_rules_head = sizeof(this->amount_of_rules);
|
||||||
EEPROM.put(this->eeprom_address, rule->protocol);
|
log_i("Amount of Rules %i", this->amount_of_rules);
|
||||||
this->eeprom_address + sizeof(rule->protocol);
|
|
||||||
EEPROM.put(this->eeprom_address, rule->target);
|
|
||||||
this->eeprom_address + sizeof(rule->target);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::get_rules_from_eeprom()
|
void ESPFirewall::eeprom_write_settings()
|
||||||
{
|
{
|
||||||
int temp_eeprom_address = 0;
|
EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules);
|
||||||
char source[IP4ADDR_STRLEN_MAX];
|
EEPROM.commit();
|
||||||
EEPROM.get(temp_eeprom_address, source);
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::eeprom_write_firewall_rule(firewall_rule_t *rule_ptr)
|
||||||
|
{
|
||||||
|
EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules);
|
||||||
|
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->source);
|
||||||
|
this->eeprom_rules_head += IP4ADDR_STRLEN_MAX;
|
||||||
|
EEPROM.writeString(this->eeprom_rules_head, rule_ptr->destination);
|
||||||
|
this->eeprom_rules_head += IP4ADDR_STRLEN_MAX;
|
||||||
|
EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->protocol);
|
||||||
|
this->eeprom_rules_head += sizeof(firewall_protocol_t);
|
||||||
|
EEPROM.writeUChar(this->eeprom_rules_head, rule_ptr->target);
|
||||||
|
this->eeprom_rules_head += sizeof(firewall_target_t);
|
||||||
|
EEPROM.commit();
|
||||||
|
// eeprom_read_firewall_rules();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPFirewall::eeprom_read_firewall_rules()
|
||||||
|
{
|
||||||
|
int eeprom_address = sizeof(this->amount_of_rules);
|
||||||
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
||||||
|
|
||||||
|
strcpy(rule_ptr->source, EEPROM.readString(eeprom_address).c_str());
|
||||||
|
eeprom_address += IP4ADDR_STRLEN_MAX;
|
||||||
|
strcpy(rule_ptr->destination, EEPROM.readString(eeprom_address).c_str());
|
||||||
|
eeprom_address += IP4ADDR_STRLEN_MAX;
|
||||||
|
// rule_ptr->protocol = firewall_protocol_t(EEPROM.readUChar(eeprom_address));
|
||||||
|
// eeprom_address += sizeof(rule_ptr->protocol);
|
||||||
|
// rule_ptr->target = firewall_target_t(EEPROM.readUChar(eeprom_address));
|
||||||
|
// eeprom_address += sizeof(rule_ptr->target);
|
||||||
|
log_i("Amount: %i, Source: %s, Destination: %s, Protocol: %s, Target: %s",
|
||||||
|
this->amount_of_rules,
|
||||||
|
rule_ptr->source,
|
||||||
|
rule_ptr->destination,
|
||||||
|
rule_ptr->protocol,
|
||||||
|
rule_ptr->target);
|
||||||
|
|
||||||
|
free(rule_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
||||||
|
@ -114,7 +149,7 @@ firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
||||||
|
|
||||||
bool ESPFirewall::delete_rule_from_firewall(int key)
|
bool ESPFirewall::delete_rule_from_firewall(int key)
|
||||||
{
|
{
|
||||||
if (head == NULL)
|
if (this->head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -133,10 +168,10 @@ bool ESPFirewall::delete_rule_from_firewall(int key)
|
||||||
current_rule_ptr = current_rule_ptr->next;
|
current_rule_ptr = current_rule_ptr->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (current_rule_ptr == head)
|
if (current_rule_ptr == this->head)
|
||||||
{
|
{
|
||||||
head = head->next;
|
this->head = head->next;
|
||||||
temp = head;
|
temp = this->head;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -149,7 +184,8 @@ bool ESPFirewall::delete_rule_from_firewall(int key)
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
free(current_rule_ptr);
|
free(current_rule_ptr);
|
||||||
amount_of_rules--;
|
this->amount_of_rules--;
|
||||||
|
this->eeprom_write_settings();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,7 +227,6 @@ String ESPFirewall::construct_json_firewall()
|
||||||
// Size for max 12 Rules
|
// Size for max 12 Rules
|
||||||
StaticJsonDocument<2048> doc;
|
StaticJsonDocument<2048> doc;
|
||||||
String response;
|
String response;
|
||||||
doc["amount"] = amount_of_rules;
|
|
||||||
JsonArray rules = doc.createNestedArray("rules");
|
JsonArray rules = doc.createNestedArray("rules");
|
||||||
while (rule_ptr != NULL)
|
while (rule_ptr != NULL)
|
||||||
{
|
{
|
||||||
|
@ -256,6 +291,7 @@ void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
||||||
rule_ptr->target = string_to_target(target);
|
rule_ptr->target = string_to_target(target);
|
||||||
|
|
||||||
add_rule_to_firewall(rule_ptr);
|
add_rule_to_firewall(rule_ptr);
|
||||||
|
eeprom_write_firewall_rule(rule_ptr);
|
||||||
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -40,8 +40,10 @@ typedef struct firewall_rule
|
||||||
|
|
||||||
class ESPFirewall
|
class ESPFirewall
|
||||||
{
|
{
|
||||||
|
uint16_t eeprom_size = 512;
|
||||||
uint8_t amount_of_rules = 0;
|
uint8_t amount_of_rules = 0;
|
||||||
int eeprom_address = 0;
|
int eeprom_settings_head;
|
||||||
|
int eeprom_rules_head;
|
||||||
struct firewall_rule *head = NULL;
|
struct firewall_rule *head = NULL;
|
||||||
|
|
||||||
AsyncWebServer *firewall_api;
|
AsyncWebServer *firewall_api;
|
||||||
|
@ -53,8 +55,10 @@ class ESPFirewall
|
||||||
firewall_target_t string_to_target(String &);
|
firewall_target_t string_to_target(String &);
|
||||||
|
|
||||||
// EEPROM
|
// EEPROM
|
||||||
void write_rule_to_eeprom(firewall_rule_t *rule);
|
void setup_eeprom();
|
||||||
void get_rules_from_eeprom();
|
void eeprom_write_settings();
|
||||||
|
void eeprom_write_firewall_rule(firewall_rule_t *rule);
|
||||||
|
void eeprom_read_firewall_rules();
|
||||||
|
|
||||||
// Firewall Actions
|
// Firewall Actions
|
||||||
void add_rule_to_firewall(firewall_rule_t *);
|
void add_rule_to_firewall(firewall_rule_t *);
|
||||||
|
|
|
@ -8,12 +8,15 @@ ESPFirewall *firewall;
|
||||||
|
|
||||||
void setup_wifi()
|
void setup_wifi()
|
||||||
{
|
{
|
||||||
|
uint8_t max_retries = 20;
|
||||||
|
uint8_t retries = 1;
|
||||||
log_i("Attempting to connect to WPA SSID: %s", ssid);
|
log_i("Attempting to connect to WPA SSID: %s", ssid);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(ssid, psk);
|
WiFi.begin(ssid, psk);
|
||||||
while (WiFi.status() != WL_CONNECTED)
|
while (WiFi.status() != WL_CONNECTED && retries <= max_retries)
|
||||||
{
|
{
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
log_i("Connecting... (%i/%i)", retries++, max_retries);
|
||||||
}
|
}
|
||||||
esp_ip_address = WiFi.localIP().toString().c_str();
|
esp_ip_address = WiFi.localIP().toString().c_str();
|
||||||
log_i("Connected, IP Address: %s", esp_ip_address);
|
log_i("Connected, IP Address: %s", esp_ip_address);
|
||||||
|
|
Reference in a new issue