2022-04-10 20:46:54 +02:00
|
|
|
#include "Firewall.h"
|
|
|
|
|
2022-04-11 11:47:50 +02:00
|
|
|
ESPFirewall::ESPFirewall(int port)
|
2022-04-10 20:46:54 +02:00
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
this->setup_eeprom();
|
2022-04-11 11:47:50 +02:00
|
|
|
log_i("Starting Firewall-API on %i", port);
|
2022-04-11 17:02:58 +02:00
|
|
|
this->firewall_api = new AsyncWebServer(port);
|
2022-04-11 11:47:50 +02:00
|
|
|
this->setup_routing();
|
2022-04-10 20:46:54 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 10:59:02 +02:00
|
|
|
String ESPFirewall::protocol_to_string(firewall_protocol_t &protocol)
|
|
|
|
{
|
|
|
|
switch (protocol)
|
|
|
|
{
|
|
|
|
case FW_TCP:
|
|
|
|
return "TCP";
|
|
|
|
case FW_UDP:
|
|
|
|
return "UDP";
|
|
|
|
default:
|
|
|
|
return "ALL";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
firewall_protocol_t ESPFirewall::string_to_protocol(String &protocol)
|
|
|
|
{
|
|
|
|
if (protocol.equals("TCP"))
|
|
|
|
return FW_TCP;
|
|
|
|
else if (protocol.equals("UDP"))
|
|
|
|
return FW_UDP;
|
|
|
|
else
|
|
|
|
return FW_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ESPFirewall::target_to_string(firewall_target_t &target)
|
|
|
|
{
|
|
|
|
switch (target)
|
|
|
|
{
|
|
|
|
case FW_REJECT:
|
|
|
|
return "REJECT";
|
|
|
|
case FW_DROP:
|
|
|
|
return "DROP";
|
|
|
|
default:
|
|
|
|
return "ACCEPT";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
firewall_target_t ESPFirewall::string_to_target(String &target)
|
|
|
|
{
|
|
|
|
if (target.equals("REJECT"))
|
|
|
|
return FW_REJECT;
|
|
|
|
else if (target.equals("DROP"))
|
|
|
|
return FW_DROP;
|
|
|
|
else
|
|
|
|
return FW_ACCEPT;
|
|
|
|
}
|
|
|
|
|
2022-04-18 13:11:02 +02:00
|
|
|
void ESPFirewall::setup_eeprom()
|
2022-04-18 11:16:01 +02:00
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
EEPROM.begin(this->eeprom_size);
|
|
|
|
this->eeprom_settings_head = 0;
|
|
|
|
this->amount_of_rules = EEPROM.readUChar(this->eeprom_settings_head);
|
|
|
|
this->eeprom_rules_head = sizeof(this->amount_of_rules);
|
|
|
|
log_i("Amount of Rules %i", this->amount_of_rules);
|
2022-04-18 11:16:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-18 13:11:02 +02:00
|
|
|
void ESPFirewall::eeprom_write_settings()
|
2022-04-18 11:16:01 +02:00
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
EEPROM.writeUChar(this->eeprom_settings_head, this->amount_of_rules);
|
|
|
|
EEPROM.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-04-18 11:16:01 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 18:06:21 +02:00
|
|
|
void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule)
|
2022-04-11 11:47:50 +02:00
|
|
|
{
|
|
|
|
firewall_rule_t *temp;
|
|
|
|
if (head == NULL)
|
|
|
|
{
|
2022-04-11 18:06:21 +02:00
|
|
|
head = rule;
|
|
|
|
rule->next = NULL;
|
2022-04-11 17:02:58 +02:00
|
|
|
return;
|
2022-04-11 11:47:50 +02:00
|
|
|
}
|
|
|
|
temp = head;
|
|
|
|
while (temp->next != NULL)
|
|
|
|
{
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
2022-04-11 18:06:21 +02:00
|
|
|
temp->next = rule;
|
|
|
|
rule->next = NULL;
|
2022-04-11 17:02:58 +02:00
|
|
|
return;
|
2022-04-11 11:47:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
firewall_rule_t *ESPFirewall::get_rule_from_firewall(int key)
|
2022-04-11 13:49:48 +02:00
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
firewall_rule_t *rule_ptr = this->head;
|
|
|
|
if (head == NULL)
|
2022-04-11 13:49:48 +02:00
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (rule_ptr->key != key)
|
|
|
|
{
|
|
|
|
if (rule_ptr->next == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rule_ptr = rule_ptr->next;
|
|
|
|
}
|
2022-04-11 17:02:58 +02:00
|
|
|
}
|
2022-04-11 21:27:27 +02:00
|
|
|
return rule_ptr;
|
|
|
|
}
|
|
|
|
|
2022-04-11 22:17:55 +02:00
|
|
|
bool ESPFirewall::delete_rule_from_firewall(int key)
|
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
if (this->head == NULL)
|
2022-04-11 22:17:55 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
firewall_rule_t *current_rule_ptr = this->head;
|
|
|
|
firewall_rule_t *previous_rule_ptr = NULL;
|
|
|
|
firewall_rule_t *temp = NULL;
|
|
|
|
while (current_rule_ptr->key != key)
|
|
|
|
{
|
|
|
|
if (current_rule_ptr->next == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
previous_rule_ptr = current_rule_ptr;
|
|
|
|
current_rule_ptr = current_rule_ptr->next;
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 13:11:02 +02:00
|
|
|
if (current_rule_ptr == this->head)
|
2022-04-11 22:17:55 +02:00
|
|
|
{
|
2022-04-18 13:11:02 +02:00
|
|
|
this->head = head->next;
|
|
|
|
temp = this->head;
|
2022-04-11 22:17:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
previous_rule_ptr->next = current_rule_ptr->next;
|
|
|
|
temp = previous_rule_ptr->next;
|
|
|
|
}
|
|
|
|
while (temp != NULL)
|
|
|
|
{
|
|
|
|
temp->key--;
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
free(current_rule_ptr);
|
2022-04-18 13:11:02 +02:00
|
|
|
this->amount_of_rules--;
|
|
|
|
this->eeprom_write_settings();
|
2022-04-11 22:17:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
void ESPFirewall::setup_routing()
|
|
|
|
{
|
|
|
|
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1));
|
|
|
|
firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1));
|
|
|
|
firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1));
|
2022-04-11 22:17:55 +02:00
|
|
|
firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_DELETE, std::bind(&ESPFirewall::delete_firewall_handler, this, std::placeholders::_1));
|
2022-04-11 21:27:27 +02:00
|
|
|
firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1));
|
|
|
|
this->firewall_api->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPFirewall::json_message_response(AsyncWebServerRequest *request, String message, int response_code)
|
|
|
|
{
|
|
|
|
DynamicJsonDocument json(256);
|
|
|
|
String response;
|
|
|
|
json["message"] = message;
|
2022-04-11 17:02:58 +02:00
|
|
|
serializeJson(json, response);
|
2022-04-11 21:27:27 +02:00
|
|
|
request->send(response_code, "application/json", response);
|
2022-04-11 17:02:58 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
String ESPFirewall::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
|
2022-04-11 18:19:03 +02:00
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
StaticJsonDocument<192> doc;
|
|
|
|
doc["key"] = rule_ptr->key;
|
|
|
|
doc["source"] = rule_ptr->source;
|
|
|
|
doc["destination"] = rule_ptr->destination;
|
2022-04-18 10:59:02 +02:00
|
|
|
doc["protocol"] = protocol_to_string(rule_ptr->protocol);
|
|
|
|
doc["target"] = target_to_string(rule_ptr->target);
|
2022-04-11 18:19:03 +02:00
|
|
|
String response;
|
2022-04-11 21:27:27 +02:00
|
|
|
serializeJson(doc, response);
|
|
|
|
return response;
|
|
|
|
}
|
2022-04-11 18:40:57 +02:00
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
String ESPFirewall::construct_json_firewall()
|
|
|
|
{
|
|
|
|
firewall_rule_t *rule_ptr = this->head;
|
2022-04-18 10:59:02 +02:00
|
|
|
// Size for max 12 Rules
|
2022-04-11 21:27:27 +02:00
|
|
|
StaticJsonDocument<2048> doc;
|
|
|
|
String response;
|
|
|
|
JsonArray rules = doc.createNestedArray("rules");
|
|
|
|
while (rule_ptr != NULL)
|
2022-04-11 18:40:57 +02:00
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
JsonObject rule = rules.createNestedObject();
|
|
|
|
rule["key"] = rule_ptr->key;
|
|
|
|
rule["source"] = rule_ptr->source;
|
|
|
|
rule["destination"] = rule_ptr->destination;
|
2022-04-18 10:59:02 +02:00
|
|
|
rule["protocol"] = protocol_to_string(rule_ptr->protocol);
|
|
|
|
rule["target"] = target_to_string(rule_ptr->target);
|
2022-04-11 21:27:27 +02:00
|
|
|
rule_ptr = rule_ptr->next;
|
2022-04-11 18:40:57 +02:00
|
|
|
}
|
2022-04-11 21:27:27 +02:00
|
|
|
serializeJson(doc, response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPFirewall::not_found(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
json_message_response(request, "not found", 404);
|
|
|
|
}
|
2022-04-11 18:40:57 +02:00
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
int rule_number = request->pathArg(0).toInt();
|
|
|
|
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
|
|
|
if (rule_ptr == NULL)
|
2022-04-11 18:40:57 +02:00
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
json_message_response(request, "rule not found", 404);
|
2022-04-11 18:40:57 +02:00
|
|
|
}
|
2022-04-11 21:27:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
String response = construct_json_firewall();
|
2022-04-11 18:40:57 +02:00
|
|
|
request->send(200, "application/json", response);
|
2022-04-11 18:19:03 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 22:17:55 +02:00
|
|
|
bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
return request->hasParam("source") || request->hasParam("destination") || request->hasParam("protocol") || request->hasParam("target");
|
|
|
|
}
|
|
|
|
|
2022-04-11 17:02:58 +02:00
|
|
|
void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request)
|
|
|
|
{
|
2022-04-11 18:06:21 +02:00
|
|
|
if (request_has_firewall_parameter(request))
|
2022-04-11 17:02:58 +02:00
|
|
|
{
|
2022-04-11 22:17:55 +02:00
|
|
|
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
|
|
|
|
rule_ptr->key = ++amount_of_rules;
|
2022-04-11 18:06:21 +02:00
|
|
|
|
2022-04-11 22:17:55 +02:00
|
|
|
// carefully copying c-string that is shorter then the destination char-array length
|
2022-04-11 21:27:27 +02:00
|
|
|
String source = request->getParam("source")->value();
|
2022-04-11 22:17:55 +02:00
|
|
|
strcpy(rule_ptr->source, source.length() <= IP4ADDR_STRLEN_MAX ? source.c_str() : "");
|
2022-04-11 21:27:27 +02:00
|
|
|
String destination = request->getParam("destination")->value();
|
2022-04-11 22:17:55 +02:00
|
|
|
strcpy(rule_ptr->destination, destination.length() <= IP4ADDR_STRLEN_MAX ? destination.c_str() : "");
|
2022-04-18 10:59:02 +02:00
|
|
|
|
2022-04-11 21:27:27 +02:00
|
|
|
String protocol = request->getParam("protocol")->value();
|
2022-04-18 10:59:02 +02:00
|
|
|
rule_ptr->protocol = string_to_protocol(protocol);
|
2022-04-11 21:27:27 +02:00
|
|
|
String target = request->getParam("target")->value();
|
2022-04-18 10:59:02 +02:00
|
|
|
rule_ptr->target = string_to_target(target);
|
2022-04-11 18:06:21 +02:00
|
|
|
|
2022-04-11 22:17:55 +02:00
|
|
|
add_rule_to_firewall(rule_ptr);
|
2022-04-18 13:11:02 +02:00
|
|
|
eeprom_write_firewall_rule(rule_ptr);
|
2022-04-11 22:17:55 +02:00
|
|
|
request->send(200, "application/json", construct_json_firewall_rule(rule_ptr));
|
2022-04-11 13:49:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-11 21:27:27 +02:00
|
|
|
json_message_response(request, "not enough parameter", 200);
|
2022-04-11 13:49:48 +02:00
|
|
|
}
|
2022-04-11 11:47:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-11 22:17:55 +02:00
|
|
|
void ESPFirewall::delete_firewall_handler(AsyncWebServerRequest *request)
|
2022-04-11 18:06:21 +02:00
|
|
|
{
|
2022-04-11 22:17:55 +02:00
|
|
|
int rule_number = request->pathArg(0).toInt();
|
|
|
|
if (delete_rule_from_firewall(rule_number))
|
|
|
|
{
|
|
|
|
json_message_response(request, "firewall rule deleted", 200);
|
|
|
|
}
|
|
|
|
json_message_response(request, "cannot delete firewall rule", 500);
|
2022-04-11 17:02:58 +02:00
|
|
|
}
|