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-07-20 13:17:10 +02:00
|
|
|
#include "esp_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;
|
|
|
|
|
2022-05-02 21:01:08 +02:00
|
|
|
WiFiServer wifiServer(80);
|
2022-04-10 20:46:54 +02:00
|
|
|
|
2022-07-20 13:17:10 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned frame_ctrl : 16;
|
|
|
|
unsigned duration_id : 16;
|
|
|
|
uint8_t addr1[6]; /* receiver address */
|
|
|
|
uint8_t addr2[6]; /* sender address */
|
|
|
|
uint8_t addr3[6]; /* filtering address */
|
|
|
|
unsigned sequence_ctrl : 16;
|
|
|
|
uint8_t addr4[6]; /* optional */
|
|
|
|
} wifi_ieee80211_mac_hdr_t;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
wifi_ieee80211_mac_hdr_t hdr;
|
|
|
|
uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
|
|
|
|
} wifi_ieee80211_packet_t;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:17:10 +02:00
|
|
|
const char *wifi_sniffer_packet_type2str(wifi_promiscuous_pkt_type_t type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case WIFI_PKT_DATA:
|
|
|
|
return "DATA";
|
|
|
|
default:
|
|
|
|
case WIFI_PKT_MISC:
|
|
|
|
return "MISC";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type)
|
|
|
|
{
|
|
|
|
if (type != WIFI_PKT_MGMT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;
|
|
|
|
const wifi_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)ppkt->payload;
|
|
|
|
const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr;
|
|
|
|
|
|
|
|
printf("PACKET TYPE=%s, CHAN=%02d, RSSI=%02d,"
|
|
|
|
" ADDR1=%02x:%02x:%02x:%02x:%02x:%02x,"
|
|
|
|
" ADDR2=%02x:%02x:%02x:%02x:%02x:%02x,"
|
|
|
|
" ADDR3=%02x:%02x:%02x:%02x:%02x:%02x\n",
|
|
|
|
wifi_sniffer_packet_type2str(type),
|
|
|
|
ppkt->rx_ctrl.channel,
|
|
|
|
ppkt->rx_ctrl.rssi,
|
|
|
|
/* ADDR1 */
|
|
|
|
hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
|
|
|
|
hdr->addr1[3], hdr->addr1[4], hdr->addr1[5],
|
|
|
|
/* ADDR2 */
|
|
|
|
hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
|
|
|
|
hdr->addr2[3], hdr->addr2[4], hdr->addr2[5],
|
|
|
|
/* ADDR3 */
|
|
|
|
hdr->addr3[0], hdr->addr3[1], hdr->addr3[2],
|
|
|
|
hdr->addr3[3], hdr->addr3[4], hdr->addr3[5]);
|
|
|
|
}
|
|
|
|
|
2022-07-19 13:07:31 +02:00
|
|
|
void setup_wifi()
|
|
|
|
{
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.onEvent(handle_wifi_events, ARDUINO_EVENT_MAX);
|
|
|
|
connectToWifi();
|
2022-07-20 13:17:10 +02:00
|
|
|
esp_wifi_set_promiscuous(true);
|
|
|
|
esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler);
|
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
|
|
|
}
|