rewrite in progress...
This commit is contained in:
parent
e77ced2f0a
commit
17a1a5176f
6 changed files with 221 additions and 13 deletions
143
ESPFirewall/lib/Firewall/src/API.cpp
Normal file
143
ESPFirewall/lib/Firewall/src/API.cpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
#include "API.hpp"
|
||||
|
||||
namespace firewall
|
||||
{
|
||||
API::API(const char *username, const char *password, const uint16_t port)
|
||||
{
|
||||
if (this->setup_auth(username, password) == ERROR)
|
||||
endless_loop();
|
||||
#ifdef ESP32
|
||||
this->server = new WebServer(port);
|
||||
#elif defined(ESP8266)
|
||||
this->server = new ESP8266WebServer(port);
|
||||
#endif
|
||||
this->setup_routing();
|
||||
Serial.println("Starting server...");
|
||||
this->server->begin();
|
||||
}
|
||||
|
||||
API::~API()
|
||||
{
|
||||
}
|
||||
void API::handle_client()
|
||||
{
|
||||
this->server->handleClient();
|
||||
}
|
||||
|
||||
ok_t API::setup_auth(const char *username, const char *password)
|
||||
{
|
||||
if (!username || *username == 0x00 || strlen(username) > CREDENTIALS_LENGTH)
|
||||
{
|
||||
Serial.println("Username too long or missing!");
|
||||
return ERROR;
|
||||
}
|
||||
strncpy(credentials.username, username, CREDENTIALS_LENGTH);
|
||||
if (!password || *password == 0x00 || strlen(password) > CREDENTIALS_LENGTH)
|
||||
{
|
||||
Serial.println("Password too long or missing!");
|
||||
return ERROR;
|
||||
}
|
||||
strncpy(credentials.password, password, CREDENTIALS_LENGTH);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void API::setup_routing()
|
||||
{
|
||||
this->server->on(UriRegex("/api/v1/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this));
|
||||
this->server->on("/api/v1/firewall", HTTP_GET, std::bind(&API::get_firewall_rules_handler, this));
|
||||
this->server->on("/api/v1/firewall", HTTP_POST, std::bind(&API::post_firewall_handler, this));
|
||||
this->server->on(UriRegex("/api/v1/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
|
||||
this->server->onNotFound(std::bind(&API::not_found_handler, this));
|
||||
}
|
||||
|
||||
void API::not_found_handler()
|
||||
{
|
||||
this->json_message_response("not found", 404);
|
||||
}
|
||||
|
||||
void API::get_firewall_rule_handler()
|
||||
{
|
||||
String param = this->server->pathArg(0);
|
||||
int rule_number = atoi(param.c_str());
|
||||
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
||||
if (rule_ptr == NULL)
|
||||
{
|
||||
this->json_message_response("rule not found", 404);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200);
|
||||
}
|
||||
}
|
||||
|
||||
void API::get_firewall_rules_handler()
|
||||
{
|
||||
this->json_message_response("firewall rules", 200);
|
||||
}
|
||||
|
||||
void API::post_firewall_handler()
|
||||
{
|
||||
this->json_message_response("post firewall rule", 200);
|
||||
}
|
||||
|
||||
void API::delete_firewall_handler()
|
||||
{
|
||||
this->json_message_response("delete firewall rule: " + this->server->pathArg(0), 200);
|
||||
}
|
||||
|
||||
String API::json_new_attribute(String key, String value, bool last)
|
||||
{
|
||||
String json_string;
|
||||
json_string += "\"" + key + "\": \"" + value + "\"";
|
||||
if (!last)
|
||||
json_string += ",";
|
||||
return json_string;
|
||||
}
|
||||
|
||||
String API::json_new_attribute(String key, uint8_t value, bool last)
|
||||
{
|
||||
return json_new_attribute(key, String(value), last);
|
||||
}
|
||||
|
||||
void API::json_generic_response(String serialized_string, const uint16_t response_code)
|
||||
{
|
||||
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
||||
}
|
||||
|
||||
void API::json_message_response(String message, const uint16_t response_code)
|
||||
{
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("uri", this->server->uri());
|
||||
serialized_string += json_new_attribute("message", message, true);
|
||||
serialized_string += "}";
|
||||
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
||||
}
|
||||
|
||||
String API::construct_json_firewall_rule(firewall_rule_t *rule_ptr)
|
||||
{
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("key", rule_ptr->key);
|
||||
serialized_string += json_new_attribute("source", rule_ptr->source);
|
||||
serialized_string += json_new_attribute("destination", rule_ptr->destination);
|
||||
serialized_string += json_new_attribute("protocol", protocol_to_string(rule_ptr->protocol));
|
||||
serialized_string += json_new_attribute("target", target_to_string(rule_ptr->target), true);
|
||||
serialized_string += "}";
|
||||
return serialized_string;
|
||||
}
|
||||
|
||||
String API::construct_json_firewall()
|
||||
{
|
||||
firewall_rule_t *rule_ptr = head;
|
||||
String serialized_string = "{";
|
||||
serialized_string += json_new_attribute("amount_of_rules", amount_of_rules);
|
||||
serialized_string += "\"rules\": [";
|
||||
while (rule_ptr != NULL)
|
||||
{
|
||||
serialized_string += construct_json_firewall_rule(rule_ptr);
|
||||
serialized_string += ",";
|
||||
rule_ptr = rule_ptr->next;
|
||||
}
|
||||
serialized_string += "]}";
|
||||
return serialized_string;
|
||||
}
|
||||
}
|
49
ESPFirewall/lib/Firewall/src/API.hpp
Normal file
49
ESPFirewall/lib/Firewall/src/API.hpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef ESP32_API_HPP
|
||||
#define ESP32_API_HPP
|
||||
|
||||
#ifdef ESP32
|
||||
#include <WebServer.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WebServer.h>
|
||||
#endif
|
||||
#include <uri/UriRegex.h>
|
||||
|
||||
#include "esp32Firewall.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace firewall
|
||||
{
|
||||
class API : public Firewall
|
||||
{
|
||||
private:
|
||||
#ifdef ESP32
|
||||
WebServer *server;
|
||||
#elif defined(ESP8266)
|
||||
ESP8266WebServer *server;
|
||||
#endif
|
||||
credential_t credentials;
|
||||
|
||||
ok_t setup_auth(const char *, const char *);
|
||||
|
||||
void setup_routing();
|
||||
void get_firewall_rule_handler();
|
||||
void get_firewall_rules_handler();
|
||||
void post_firewall_handler();
|
||||
void delete_firewall_handler();
|
||||
void not_found_handler();
|
||||
|
||||
String json_new_attribute(String key, String value, bool last = false);
|
||||
String json_new_attribute(String key, uint8_t value, bool last = false);
|
||||
void json_generic_response(String serialized_string, const uint16_t response_code);
|
||||
void json_message_response(String message, const uint16_t response_code);
|
||||
String construct_json_firewall_rule(firewall_rule_t *);
|
||||
String construct_json_firewall();
|
||||
|
||||
public:
|
||||
API(const char *, const char *, const uint16_t = 8080);
|
||||
~API();
|
||||
void handle_client();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -50,8 +50,8 @@ namespace firewall
|
|||
|
||||
void endless_loop()
|
||||
{
|
||||
log_e("Something went wrong. Running endless loop until fixed...");
|
||||
Serial.printf("Something went wrong. Running endless loop until fixed...");
|
||||
while (true)
|
||||
sleep(500);
|
||||
delay(500);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef UTILS_HPP
|
||||
#define UTILS_HPP
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "string"
|
||||
#include "WString.h"
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
namespace firewall
|
||||
{
|
||||
|
|
|
@ -13,7 +13,13 @@ platform = espressif32
|
|||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
-DHTTPS_LOGLEVEL=1
|
||||
-DCORE_DEBUG_LEVEL=3
|
||||
lib_deps = bblanchon/ArduinoJson@^6.19.4
|
||||
build_flags = -DCORE_DEBUG_LEVEL=3
|
||||
lib_deps = me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
||||
[env:esp8266]
|
||||
board = d1_mini
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags = -DCORE_DEBUG_LEVEL=3
|
||||
platform = espressif8266
|
||||
lib_deps = me-no-dev/ESP Async WebServer@^1.2.3
|
||||
|
|
|
@ -1,19 +1,29 @@
|
|||
#include "theSecrets.h"
|
||||
#include "WiFi.h"
|
||||
#include "esp32API.hpp"
|
||||
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif
|
||||
|
||||
#include "API.hpp"
|
||||
|
||||
firewall::API *firewall_api;
|
||||
|
||||
void setup_wifi()
|
||||
{
|
||||
log_d("Attempting to connect to WPA SSID: %s", ssid);
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, psk);
|
||||
Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid);
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(2000);
|
||||
Serial.print('.');
|
||||
delay(1000);
|
||||
}
|
||||
log_i("IP Address: %s", WiFi.localIP().toString().c_str());
|
||||
Serial.println();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
void setup()
|
||||
|
@ -24,5 +34,5 @@ void setup()
|
|||
|
||||
void loop()
|
||||
{
|
||||
firewall_api->handle_clients();
|
||||
firewall_api->handle_client();
|
||||
}
|
Reference in a new issue