simplify more
This commit is contained in:
parent
ef86c4f8f6
commit
7f3935ed15
6 changed files with 56 additions and 24 deletions
|
@ -2,8 +2,10 @@
|
||||||
|
|
||||||
namespace fw
|
namespace fw
|
||||||
{
|
{
|
||||||
API::API(const char *cert, const char *key, const char *username, const char *password, const uint16_t port)
|
API::API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port)
|
||||||
{
|
{
|
||||||
|
this->server_ip = ip;
|
||||||
|
this->server_port = port;
|
||||||
if (this->setup_auth(username, password) == ERROR)
|
if (this->setup_auth(username, password) == ERROR)
|
||||||
endless_loop();
|
endless_loop();
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
|
@ -52,7 +54,7 @@ namespace fw
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->json_message_response("unauthorized", 403);
|
this->json_message_response("please provide username and password", 403);
|
||||||
return DENIED;
|
return DENIED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,23 +69,28 @@ namespace fw
|
||||||
add_api_endpoint("/firewall", "GET", "Get all Firewall Rules");
|
add_api_endpoint("/firewall", "GET", "Get all Firewall Rules");
|
||||||
|
|
||||||
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this));
|
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_GET, std::bind(&API::get_firewall_rule_handler, this));
|
||||||
add_api_endpoint("/firewall/key", "GET", "Get Firewall Rule by key");
|
add_api_endpoint("/firewall/1", "GET", "Get Firewall Rule by key");
|
||||||
|
|
||||||
this->server->on("/firewall", HTTP_POST, std::bind(&API::post_firewall_handler, this));
|
this->server->on("/firewall", HTTP_POST, std::bind(&API::post_firewall_handler, this));
|
||||||
add_api_endpoint("/firewall", "POST", "Create Firewall Rule");
|
add_api_endpoint("/firewall", "POST", "Create Firewall Rule");
|
||||||
|
|
||||||
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
|
this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this));
|
||||||
add_api_endpoint("/firewall/key", "DELETE", "Delete Firewall Rule by key");
|
add_api_endpoint("/firewall/1", "DELETE", "Delete Firewall Rule by key");
|
||||||
|
|
||||||
this->server->onNotFound(std::bind(&API::not_found_handler, this));
|
this->server->onNotFound(std::bind(&API::not_found_handler, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
void API::add_api_endpoint(const char *uri, const char *method, const char *description)
|
void API::add_api_endpoint(const String uri, const char *method, const char *description)
|
||||||
{
|
{
|
||||||
api_endpoint_t *temp;
|
api_endpoint_t *temp;
|
||||||
|
#ifdef ESP32
|
||||||
|
const String url = "http://" + this->server_ip + ":" + this->server_port + uri;
|
||||||
|
#elif defined(ESP8266)
|
||||||
|
const String url = "https://" + this->server_ip + ":" + this->server_port + uri;
|
||||||
|
#endif
|
||||||
|
|
||||||
api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t));
|
api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t));
|
||||||
strncpy(api_ptr->uri, uri, sizeof(api_ptr->uri));
|
strncpy(api_ptr->uri, url.c_str(), sizeof(api_ptr->uri));
|
||||||
strncpy(api_ptr->method, method, sizeof(api_ptr->method));
|
strncpy(api_ptr->method, method, sizeof(api_ptr->method));
|
||||||
strncpy(api_ptr->description, description, sizeof(api_ptr->description));
|
strncpy(api_ptr->description, description, sizeof(api_ptr->description));
|
||||||
|
|
||||||
|
@ -105,7 +112,8 @@ namespace fw
|
||||||
|
|
||||||
void API::not_found_handler()
|
void API::not_found_handler()
|
||||||
{
|
{
|
||||||
this->json_generic_response(this->construct_json_endpoints(), 404);
|
uint16_t response_code = 404;
|
||||||
|
this->json_generic_response(this->construct_json_api(response_code), response_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
void API::get_firewall_rule_handler()
|
void API::get_firewall_rule_handler()
|
||||||
|
@ -116,7 +124,7 @@ namespace fw
|
||||||
int rule_number = atoi(param.c_str());
|
int rule_number = atoi(param.c_str());
|
||||||
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
firewall_rule_t *rule_ptr = get_rule_from_firewall(rule_number);
|
||||||
if (rule_ptr == NULL)
|
if (rule_ptr == NULL)
|
||||||
this->json_message_response("rule not found", 404);
|
this->json_message_response("rule does not exist", 404);
|
||||||
else
|
else
|
||||||
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200);
|
this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200);
|
||||||
}
|
}
|
||||||
|
@ -143,7 +151,7 @@ namespace fw
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this->json_message_response("not enough parameter", 400);
|
this->json_message_response("not enough parameter provided", 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,6 +202,7 @@ namespace fw
|
||||||
void API::json_message_response(String message, const uint16_t response_code)
|
void API::json_message_response(String message, const uint16_t response_code)
|
||||||
{
|
{
|
||||||
String serialized_string = "{";
|
String serialized_string = "{";
|
||||||
|
serialized_string += json_new_attribute("status", response_code_to_string(response_code));
|
||||||
serialized_string += json_new_attribute("message", message, true);
|
serialized_string += json_new_attribute("message", message, true);
|
||||||
serialized_string += "}";
|
serialized_string += "}";
|
||||||
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);
|
||||||
|
@ -215,7 +224,7 @@ namespace fw
|
||||||
{
|
{
|
||||||
firewall_rule_t *rule_ptr = rule_head;
|
firewall_rule_t *rule_ptr = rule_head;
|
||||||
String serialized_string = "{";
|
String serialized_string = "{";
|
||||||
serialized_string += json_new_attribute("amount_of_rules", amount_of_rules);
|
serialized_string += json_new_attribute("status", response_code_to_string(200));
|
||||||
serialized_string += "\"rules\": [";
|
serialized_string += "\"rules\": [";
|
||||||
while (rule_ptr != NULL)
|
while (rule_ptr != NULL)
|
||||||
{
|
{
|
||||||
|
@ -228,7 +237,7 @@ namespace fw
|
||||||
return serialized_string;
|
return serialized_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
String API::construct_json_endpoint(api_endpoint_t *api_ptr)
|
String API::construct_json_api_endpoint(api_endpoint_t *api_ptr)
|
||||||
{
|
{
|
||||||
String serialized_string = "{";
|
String serialized_string = "{";
|
||||||
serialized_string += json_new_attribute("endpoint", api_ptr->uri);
|
serialized_string += json_new_attribute("endpoint", api_ptr->uri);
|
||||||
|
@ -238,15 +247,15 @@ namespace fw
|
||||||
return serialized_string;
|
return serialized_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
String API::construct_json_endpoints()
|
String API::construct_json_api(const uint16_t response_code)
|
||||||
{
|
{
|
||||||
api_endpoint_t *api_ptr = this->endpoint_head;
|
api_endpoint_t *api_ptr = this->endpoint_head;
|
||||||
String serialized_string = "{";
|
String serialized_string = "{";
|
||||||
serialized_string += json_new_attribute("message", "route not found");
|
serialized_string += json_new_attribute("status", response_code_to_string(response_code));
|
||||||
serialized_string += "\"endpoints\": [";
|
serialized_string += "\"endpoints\": [";
|
||||||
while (api_ptr != NULL)
|
while (api_ptr != NULL)
|
||||||
{
|
{
|
||||||
serialized_string += construct_json_endpoint(api_ptr);
|
serialized_string += construct_json_api_endpoint(api_ptr);
|
||||||
api_ptr = api_ptr->next;
|
api_ptr = api_ptr->next;
|
||||||
if (api_ptr != NULL)
|
if (api_ptr != NULL)
|
||||||
serialized_string += ",";
|
serialized_string += ",";
|
||||||
|
|
|
@ -16,7 +16,7 @@ namespace fw
|
||||||
{
|
{
|
||||||
typedef struct api_endpoints
|
typedef struct api_endpoints
|
||||||
{
|
{
|
||||||
char uri[20];
|
char uri[40];
|
||||||
char method[7];
|
char method[7];
|
||||||
char description[30];
|
char description[30];
|
||||||
struct api_endpoints *next;
|
struct api_endpoints *next;
|
||||||
|
@ -38,7 +38,7 @@ namespace fw
|
||||||
auth_t check_auth();
|
auth_t check_auth();
|
||||||
|
|
||||||
void setup_routing(const char *cert, const char *key);
|
void setup_routing(const char *cert, const char *key);
|
||||||
void add_api_endpoint(const char *uri, const char *method, const char *description);
|
void add_api_endpoint(const String uri, const char *method, const char *description);
|
||||||
void get_firewall_rule_handler();
|
void get_firewall_rule_handler();
|
||||||
void get_firewall_rules_handler();
|
void get_firewall_rules_handler();
|
||||||
void post_firewall_handler();
|
void post_firewall_handler();
|
||||||
|
@ -52,14 +52,17 @@ namespace fw
|
||||||
void json_message_response(String message, 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_rule(firewall_rule_t *);
|
||||||
String construct_json_firewall();
|
String construct_json_firewall();
|
||||||
String construct_json_endpoint(api_endpoint_t *);
|
String construct_json_api_endpoint(api_endpoint_t *);
|
||||||
String construct_json_endpoints();
|
String construct_json_api(const uint16_t response_code);
|
||||||
|
String construct_json_array(String message, String array_name);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
String server_ip;
|
||||||
|
uint16_t server_port;
|
||||||
void handle_client();
|
void handle_client();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
API(const char *cert, const char *key, const char *username, const char *password, const uint16_t port);
|
API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port);
|
||||||
~API();
|
~API();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,13 @@ namespace fw
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
public:
|
public:
|
||||||
Firewall(const char *, const char *, const char *, const char *, const uint16_t = 8080);
|
Firewall(const char *, const char *, const char *, const char *, const String ip, const uint16_t = 8080);
|
||||||
~Firewall();
|
~Firewall();
|
||||||
void handle_api_client();
|
void handle_api_client();
|
||||||
};
|
};
|
||||||
|
|
||||||
Firewall::Firewall(const char *cert, const char *key, const char *username, const char *password, const uint16_t port)
|
Firewall::Firewall(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port)
|
||||||
: API(cert, key, username, password, port) {}
|
: API(cert, key, username, password, ip, port) {}
|
||||||
Firewall::~Firewall() {}
|
Firewall::~Firewall() {}
|
||||||
void Firewall::handle_api_client()
|
void Firewall::handle_api_client()
|
||||||
{
|
{
|
||||||
|
|
|
@ -48,6 +48,23 @@ namespace fw
|
||||||
return TARGET_ACCEPT;
|
return TARGET_ACCEPT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String response_code_to_string(const uint16_t response_code)
|
||||||
|
{
|
||||||
|
switch (response_code)
|
||||||
|
{
|
||||||
|
case 200:
|
||||||
|
return "success";
|
||||||
|
case 403:
|
||||||
|
return "unauthorized";
|
||||||
|
case 404:
|
||||||
|
return "not found";
|
||||||
|
case 500:
|
||||||
|
return "server error";
|
||||||
|
default:
|
||||||
|
return "unknown error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void endless_loop()
|
void endless_loop()
|
||||||
{
|
{
|
||||||
Serial.printf("Something went wrong. Running endless loop until fixed...");
|
Serial.printf("Something went wrong. Running endless loop until fixed...");
|
||||||
|
|
|
@ -56,6 +56,7 @@ namespace fw
|
||||||
firewall_protocol_t string_to_protocol(String &protocol);
|
firewall_protocol_t string_to_protocol(String &protocol);
|
||||||
String target_to_string(firewall_target_t &target);
|
String target_to_string(firewall_target_t &target);
|
||||||
firewall_target_t string_to_target(String &target);
|
firewall_target_t string_to_target(String &target);
|
||||||
|
String response_code_to_string(const uint16_t response_code);
|
||||||
void endless_loop();
|
void endless_loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "Firewall.hpp"
|
#include "Firewall.hpp"
|
||||||
|
|
||||||
fw::Firewall *firewall;
|
fw::Firewall *firewall;
|
||||||
|
String ip = "0.0.0.0";
|
||||||
|
|
||||||
void setup_wifi()
|
void setup_wifi()
|
||||||
{
|
{
|
||||||
|
@ -24,13 +25,14 @@ void setup_wifi()
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("IP Address: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(WiFi.localIP());
|
ip = WiFi.localIP().toString();
|
||||||
|
Serial.println(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
setup_wifi();
|
setup_wifi();
|
||||||
firewall = new fw::Firewall(cert, key, username, password, 8080);
|
firewall = new fw::Firewall(cert, key, username, password, ip, 8080);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|
Reference in a new issue