ESP32/ESP8266 Firewall 1.0.0
a software firewall for ESP23 or ESP8266
API.hpp
1#ifndef API_HPP
2#define API_HPP
3
4#ifdef ESP8266
5#include "ESP8266WebServerSecure.h"
6#else
7#include "WebServer.h"
8#endif
9#include "uri/UriBraces.h"
10#include "Firewall.hpp"
11#include "Utils.hpp"
12
13namespace fw
14{
20 class API
21 {
22 public:
37 API(Firewall *, const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port = 8080);
38
43 ~API();
44#ifdef ESP8266
45 void handle_client();
46#endif
47
48 private:
49#ifdef ESP8266
50 BearSSL::ESP8266WebServerSecure *server;
51 BearSSL::ServerSessions *serverCache;
52#else
53 WebServer *server;
54#endif
55 Firewall *firewall;
56 credential_t credentials;
57 api_endpoint_t *endpoint_head = NULL;
58 String api_ip = "0.0.0.0";
59 uint16_t api_port;
60 String json_response_type = "application/json; charset=utf-8";
61 const char *TAG = "[API]";
62
69 String get_url_base();
70
78 ok_t setup_auth(const char *username, const char *password);
79
85 auth_t check_auth();
86
93 void setup_routing(const char *cert, const char *key);
94
103 void add_endpoint_to_list(const String uri, const char *method, const char *description);
104
108 void not_found_handler();
109
114
119
124
129
134
139
148
157 String json_new_attribute(String key, String value, bool last = false);
158
167 String json_new_attribute(String key, uint32_t value, bool last = false);
168
175 void json_generic_response(String serialized_string, const uint16_t response_code);
176
183 void json_array_response(String serialized_string, const uint16_t response_code);
184
191 void json_message_response(String message, const uint16_t response_code);
192
200
207
215
222 String construct_json_api();
223 };
224}
225
226#endif
The API to create, edit or remove Firewall rules.
Definition: API.hpp:21
API(Firewall *, const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port=8080)
Construct a new API object, assign ip and port to generate endpoint list, setup authentication,...
Definition: API.cpp:5
String json_new_attribute(String key, String value, bool last=false)
add another attribute to the json object
Definition: API.cpp:225
String construct_json_api_endpoint(api_endpoint_t *api_ptr)
construct an API endpoint as json object
Definition: API.cpp:284
void json_array_response(String serialized_string, const uint16_t response_code)
json response that wraps the message in array brackets
Definition: API.cpp:244
void post_firewall_handler()
POST handler to create firewall rule.
Definition: API.cpp:156
ok_t setup_auth(const char *username, const char *password)
Set up authentication.
Definition: API.cpp:52
void delete_firewall_handler()
DELETE handler to delete firewall rule.
Definition: API.cpp:201
~API()
Destroy the API object.
Definition: API.cpp:31
String construct_json_firewall()
construct array of all firewall rules as json object
Definition: API.cpp:270
void json_message_response(String message, const uint16_t response_code)
json response to send message as json object
Definition: API.cpp:249
void put_firewall_handler()
PUT handler to update firewall rule.
Definition: API.cpp:176
void get_endpoint_list_handler()
GET handler to retrieve endpoint list.
Definition: API.cpp:131
void setup_routing(const char *cert, const char *key)
Set up the routing/endpoints and encryption.
Definition: API.cpp:79
String construct_json_api()
construct array of all endpoints as json object
Definition: API.cpp:294
void add_endpoint_to_list(const String uri, const char *method, const char *description)
add endpoint information to linked list that is used for the /api endpoint
Definition: API.cpp:100
void get_firewall_rules_handler()
GET handler to retrieve firewall rules.
Definition: API.cpp:149
bool request_has_all_firewall_parameter()
check if request to create/update firewall rule has all required parameter
Definition: API.cpp:213
String construct_json_firewall_rule(firewall_rule_t *rule_ptr)
construct a firewall rule as json object
Definition: API.cpp:257
void get_firewall_rule_handler()
GET handler to retrieve single firewall rule.
Definition: API.cpp:136
void json_generic_response(String serialized_string, const uint16_t response_code)
json response to send any string and response code
Definition: API.cpp:239
auth_t check_auth()
check if request to API can proceed or needs to stop
Definition: API.cpp:69
void not_found_handler()
handling not found
Definition: API.cpp:126
String get_url_base()
Get the url base string e.g. http://0.0.0.0:8080/api.
Definition: API.cpp:43
The Firewall will handle Firewall rules as linked list.
Definition: Firewall.hpp:20
Definition: Utils.hpp:93
Definition: Utils.hpp:85
Definition: Utils.hpp:51