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
2022-07-19 13:07:31 +02:00

83 lines
No EOL
1.5 KiB
C++

#include "theSecrets.h"
#include "theCerts.h"
#ifdef ESP32
#include "WiFi.h"
#elif defined(ESP8266)
#include "ESP8266WiFi.h"
#endif
#include "Firewall.hpp"
#include "API.hpp"
fw::Firewall *firewall;
fw::API *firewallApi;
WiFiServer wifiServer(80);
void connectToWifi()
{
WiFi.begin(ssid, psk);
while (WiFi.status() != WL_CONNECTED)
{
delay(200);
}
}
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();
}
void setup()
{
Serial.begin(115200);
setup_wifi();
firewall = new fw::Firewall();
firewallApi = new fw::API(firewall, cert, key, username, password);
wifiServer.begin();
}
void loop()
{
firewallApi->handle_client();
WiFiClient client = wifiServer.available();
if (client)
{
if (firewall->is_client_allowed(client))
{
Serial.println("allowed");
client.stop();
}
else
{
Serial.println("rejected");
client.flush();
}
}
}