From 1d9b34f9fff5d831ec734523ac7337f372daeb0c Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Fri, 29 Jul 2022 10:50:40 +0200 Subject: [PATCH] Include ESP8266 example --- esp8266example/.gitignore | 4 ++ esp8266example/include/theCerts-example.h | 59 +++++++++++++++++++++ esp8266example/include/theSecrets-example.h | 9 ++++ esp8266example/platformio.ini | 19 +++++++ esp8266example/src/main.cpp | 53 ++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 esp8266example/.gitignore create mode 100644 esp8266example/include/theCerts-example.h create mode 100644 esp8266example/include/theSecrets-example.h create mode 100644 esp8266example/platformio.ini create mode 100644 esp8266example/src/main.cpp diff --git a/esp8266example/.gitignore b/esp8266example/.gitignore new file mode 100644 index 0000000..529a7a6 --- /dev/null +++ b/esp8266example/.gitignore @@ -0,0 +1,4 @@ +.pio +.vscode +include/theSecrets.h +include/theCerts.h diff --git a/esp8266example/include/theCerts-example.h b/esp8266example/include/theCerts-example.h new file mode 100644 index 0000000..5523ed9 --- /dev/null +++ b/esp8266example/include/theCerts-example.h @@ -0,0 +1,59 @@ +#ifndef THECERTS_H +#define THECERTS_H + +#include "pgmspace.h" + +const char cert[] PROGMEM = R"EOF( +-----BEGIN CERTIFICATE----- +MIIDSzCCAjMCCQD2ahcfZAwXxDANBgkqhkiG9w0BAQsFADCBiTELMAkGA1UEBhMC +VVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU9yYW5nZSBDb3VudHkx +EDAOBgNVBAoMB1ByaXZhZG8xGjAYBgNVBAMMEXNlcnZlci56bGFiZWwuY29tMR8w +HQYJKoZIhvcNAQkBFhBlYXJsZUB6bGFiZWwuY29tMB4XDTE4MDMwNjA1NDg0NFoX +DTE5MDMwNjA1NDg0NFowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3Rh +dGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZI +hvcNAQEBBQADggEPADCCAQoCggEBAPVKBwbZ+KDSl40YCDkP6y8Sv4iNGvEOZg8Y +X7sGvf/xZH7UiCBWPFIRpNmDSaZ3yjsmFqm6sLiYSGSdrBCFqdt9NTp2r7hga6Sj +oASSZY4B9pf+GblDy5m10KDx90BFKXdPMCLT+o76Nx9PpCvw13A848wHNG3bpBgI +t+w/vJCX3bkRn8yEYAU6GdMbYe7v446hX3kY5UmgeJFr9xz1kq6AzYrMt/UHhNzO +S+QckJaY0OGWvmTNspY3xCbbFtIDkCdBS8CZAw+itnofvnWWKQEXlt6otPh5njwy ++O1t/Q+Z7OMDYQaH02IQx3188/kW3FzOY32knER1uzjmRO+jhA8CAwEAATANBgkq +hkiG9w0BAQsFAAOCAQEAnDrROGRETB0woIcI1+acY1yRq4yAcH2/hdq2MoM+DCyM +E8CJaOznGR9ND0ImWpTZqomHOUkOBpvu7u315blQZcLbL1LfHJGRTCHVhvVrcyEb +fWTnRtAQdlirUm/obwXIitoz64VSbIVzcqqfg9C6ZREB9JbEX98/9Wp2gVY+31oC +JfUvYadSYxh3nblvA4OL+iEZiW8NE3hbW6WPXxvS7Euge0uWMPc4uEcnsE0ZVG3m ++TGimzSdeWDvGBRWZHXczC2zD4aoE5vrl+GD2i++c6yjL/otHfYyUpzUfbI2hMAA +5tAF1D5vAAwA8nfPysumlLsIjohJZo4lgnhB++AlOg== +-----END CERTIFICATE----- +)EOF"; + +const char key[] PROGMEM = R"EOF( +-----BEGIN RSA PRIVATE KEY----- +MIIEpQIBAAKCAQEA9UoHBtn4oNKXjRgIOQ/rLxK/iI0a8Q5mDxhfuwa9//FkftSI +IFY8UhGk2YNJpnfKOyYWqbqwuJhIZJ2sEIWp2301OnavuGBrpKOgBJJljgH2l/4Z +uUPLmbXQoPH3QEUpd08wItP6jvo3H0+kK/DXcDzjzAc0bdukGAi37D+8kJfduRGf +zIRgBToZ0xth7u/jjqFfeRjlSaB4kWv3HPWSroDNisy39QeE3M5L5ByQlpjQ4Za+ +ZM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TuJCrCNcsgld3gLZ2QWYaMV +kCPgaj1BjHw4AmB0+EcajfKilcqtSroJ6MfMJ6IclVOizkjbByeTsE4lxDmPCDSt +/9MKanBxAoGAY9xo741Pn9WUxDyRplww606ccdNf/ksHWNc/Y2B5SPwxxSnIq8nO +j01SmsCUYVFAgZVOTiiycakjYLzxlc6p8BxSVqy6LlJqn95N8OXoQ+bkwUux/ekg +gz5JWYhbD6c38khSzJb0pNXCo3EuYAVa36kDM96k1BtWuhRS10Q1VXk= +-----END RSA PRIVATE KEY----- +)EOF"; + +#endif diff --git a/esp8266example/include/theSecrets-example.h b/esp8266example/include/theSecrets-example.h new file mode 100644 index 0000000..81eca13 --- /dev/null +++ b/esp8266example/include/theSecrets-example.h @@ -0,0 +1,9 @@ +#ifndef _THE_SECRETS_H_ +#define _THE_SECRETS_H_ + +const char *ssid = "Wifi"; +const char *psk = "password"; +const char *username = "username"; +const char *password = "password"; + +#endif diff --git a/esp8266example/platformio.ini b/esp8266example/platformio.ini new file mode 100644 index 0000000..5667ae0 --- /dev/null +++ b/esp8266example/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +default_envs = esp8266 + +[env:esp8266] +platform = espressif8266 +board = d1_mini +framework = arduino +monitor_speed = 115200 +lib_deps = https://gitlab.hs-esslingen.de/toheer/iot-security-tools.git diff --git a/esp8266example/src/main.cpp b/esp8266example/src/main.cpp new file mode 100644 index 0000000..7a5371c --- /dev/null +++ b/esp8266example/src/main.cpp @@ -0,0 +1,53 @@ +#include +#include "theSecrets.h" +#include "theCerts.h" +#include "ESP8266WiFi.h" + +#include "esp8266/Firewall.hpp" +#include "esp8266/API.hpp" + +fw::Firewall *firewall; +fw::API *firewallApi; + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +WiFiEventHandler wifiGotIpHandler; + +void initFirewall(const String ip) +{ + firewall = new fw::Firewall(); + firewallApi = new fw::API(firewall, cert, key, username, password, ip); +} + +void onConnect(const WiFiEventStationModeConnected &event) +{ + Serial.println("[WiFi] connected"); +} + +void onDisconnect(const WiFiEventStationModeDisconnected &event) +{ + Serial.println("[WiFi] disconnected"); + WiFi.reconnect(); +} + +void onGotIp(const WiFiEventStationModeGotIP &event) +{ + initFirewall(WiFi.localIP().toString()); +} + +void setup() +{ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + wifiConnectHandler = WiFi.onStationModeConnected(onConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onDisconnect); + wifiGotIpHandler = WiFi.onStationModeGotIP(onGotIp); + WiFi.begin(ssid, psk); + Serial.println("[WiFi] connecting..."); +} + +void loop() +{ + if (firewallApi != NULL) + firewallApi->handle_client(); +}