This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
esp-firewall/ESPFirewall/src/main.cpp

83 lines
1.5 KiB
C++
Raw Normal View History

2022-04-10 20:46:54 +02:00
#include "theSecrets.h"
2022-04-24 18:28:41 +02:00
#include "theCerts.h"
2022-04-23 23:36:44 +02:00
#ifdef ESP32
2022-04-24 12:48:01 +02:00
#include "WiFi.h"
2022-04-23 23:36:44 +02:00
#elif defined(ESP8266)
2022-04-24 12:48:01 +02:00
#include "ESP8266WiFi.h"
2022-04-23 23:36:44 +02:00
#endif
2022-04-24 00:13:20 +02:00
#include "Firewall.hpp"
2022-05-03 20:28:17 +02:00
#include "API.hpp"
2022-04-20 07:58:06 +02:00
2022-04-24 00:13:20 +02:00
fw::Firewall *firewall;
2022-05-03 20:28:17 +02:00
fw::API *firewallApi;
WiFiServer wifiServer(80);
2022-04-10 20:46:54 +02:00
2022-07-19 13:07:31 +02:00
void connectToWifi()
2022-04-10 20:46:54 +02:00
{
WiFi.begin(ssid, psk);
2022-04-21 16:10:31 +02:00
while (WiFi.status() != WL_CONNECTED)
2022-04-10 20:46:54 +02:00
{
2022-07-19 13:07:31 +02:00
delay(200);
2022-04-10 20:46:54 +02:00
}
2022-07-19 13:07:31 +02:00
}
void handle_wifi_events(WiFiEvent_t event, WiFiEventInfo_t info)
{
Serial.print("[WiFi] ");
switch (event)
{
case ARDUINO_EVENT_WIFI_STA_START:
Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid);
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("connected");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("disconnected");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
break;
default:
Serial.println(event);
}
}
void setup_wifi()
{
WiFi.mode(WIFI_STA);
WiFi.onEvent(handle_wifi_events, ARDUINO_EVENT_MAX);
connectToWifi();
2022-04-10 20:46:54 +02:00
}
2022-04-10 14:38:03 +02:00
void setup()
{
2022-07-19 13:07:31 +02:00
Serial.begin(115200);
2022-04-10 15:55:42 +02:00
setup_wifi();
2022-05-03 20:28:17 +02:00
firewall = new fw::Firewall();
firewallApi = new fw::API(firewall, cert, key, username, password);
2022-07-19 13:07:31 +02:00
wifiServer.begin();
2022-04-10 14:38:03 +02:00
}
void loop()
{
2022-05-03 20:28:17 +02:00
firewallApi->handle_client();
2022-05-03 20:32:40 +02:00
WiFiClient client = wifiServer.available();
if (client)
{
2022-05-03 20:40:42 +02:00
if (firewall->is_client_allowed(client))
2022-07-19 13:07:31 +02:00
{
2022-05-03 20:40:42 +02:00
Serial.println("allowed");
2022-07-19 13:07:31 +02:00
client.stop();
}
2022-05-03 20:40:42 +02:00
else
2022-07-19 13:07:31 +02:00
{
2022-05-03 20:40:42 +02:00
Serial.println("rejected");
2022-07-19 13:07:31 +02:00
client.flush();
}
2022-05-03 20:32:40 +02:00
}
2022-04-10 14:38:03 +02:00
}