forked from interactionresearchstudio/ESP32-SOCKETIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captivePortal.ino
164 lines (138 loc) · 5.79 KB
/
captivePortal.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
class CaptiveRequestHandler : public AsyncWebHandler {
public:
CaptiveRequestHandler() {}
virtual ~CaptiveRequestHandler() {}
bool canHandle(AsyncWebServerRequest *request) {
//we can handle anything!
return true;
}
void handleRequest(AsyncWebServerRequest *request) {
Serial.print("handleRequest: ");
Serial.println(request->url());
if (!isResetting) {
if (request->method() == HTTP_GET) {
if (request->url() == "/credentials") getCredentials(request);
else if (request->url() == "/scan") getScan(request);
else if (SPIFFS.exists(request->url())) sendFile(request, request->url());
else if (request->url().endsWith(".html") || request->url().endsWith("/") || request->url().endsWith("generate_204") || request->url().endsWith("redirect")) {
sendFile(request, "/index.html");
}
else if (request->url().endsWith("connecttest.txt") || request->url().endsWith("ncsi.txt")) {
request->send(200, "text/plain", "Microsoft NCSI");
} else if (strstr(request->url().c_str(), "generate_204_") != NULL) {
Serial.println("you must be huawei!");
sendFile(request, "/index.html");
}
else {
request->send(304);
}
}
}
}
void handleBody(AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
Serial.print("handleBody: ");
Serial.println(request->url());
if (!isResetting) {
if (request->method() == HTTP_POST) {
if (request->url() == "/credentials") {
String json = "";
for (int i = 0; i < len; i++) json += char(data[i]);
StaticJsonDocument<1024> credentialsJsonDoc;
if (!deserializeJson(credentialsJsonDoc, json)) {
if(setCredentials(credentialsJsonDoc.as<JsonObject>())) request->send(200);
else request->send(400);
}
}
else if(request->url() == "/reboot") {
String json = "";
for (int i = 0; i < len; i++) json += char(data[i]);
StaticJsonDocument<256> rebootJsonDoc;
if (!deserializeJson(rebootJsonDoc, json)) {
int delayMs = rebootJsonDoc["delay"];
softReset(delayMs);
socket_server.textAll("RESTART");
}
request->send(200);
}
else {
request->send(404);
}
}
}
}
void sendFile(AsyncWebServerRequest * request, String path) {
Serial.println("handleFileRead: " + path);
if (SPIFFS.exists(path)) {
request->send(SPIFFS, path, getContentType(path));
}
else {
request->send(404);
}
}
String getContentType(String filename) {
if (filename.endsWith(".htm")) return "text/html";
else if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".png")) return "image/png";
else if (filename.endsWith(".gif")) return "image/gif";
else if (filename.endsWith(".jpg")) return "image/jpeg";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".xml")) return "text/xml";
else if (filename.endsWith(".pdf")) return "application/x-pdf";
else if (filename.endsWith(".zip")) return "application/x-zip";
else if (filename.endsWith(".gz")) return "application/x-gzip";
else if (filename.endsWith(".json")) return "application/json";
return "text/plain";
}
void getCredentials(AsyncWebServerRequest *request) {
Serial.println("getCredentials");
AsyncResponseStream *response = request->beginResponseStream("application/json");
StaticJsonDocument<1024> settingsJsonDoc;
settingsJsonDoc["local_mac"] = myID;
settingsJsonDoc["local_ssid"] = "";
settingsJsonDoc["local_pass_len"] = 0; //local_pass.length;
settingsJsonDoc["remote_ssid"] = "";
settingsJsonDoc["remote_pass_len"] = 0; //remote_pass.length;
settingsJsonDoc["remote_mac"] = getRemoteMacAddress(1);
settingsJsonDoc["local_paired_status"] = getCurrentPairedStatusAsString();
Serial.println(getCurrentPairedStatusAsString());
String jsonString;
serializeJson(settingsJsonDoc, jsonString);
response->print(jsonString);
request->send(response);
}
bool setCredentials(JsonVariant json) {
bool result = false;
Serial.println("setCredentials");
String local_ssid = json["local_ssid"].as<String>();
String local_pass = json["local_pass"].as<String>();
String remote_ssid = json["remote_ssid"].as<String>();
String remote_pass = json["remote_pass"].as<String>();
String remote_mac = json["remote_mac"].as<String>();
if (remote_mac != "") {
addToMacAddressJSON(remote_mac);
result = true;
}
if (remote_pass != "" && remote_ssid != "" && local_ssid != "" && local_pass != "") {
addToWiFiJSON(local_ssid, local_pass);
addToWiFiJSON(remote_ssid, remote_pass);
sendWifiCredentials();
result = true;
}
else if (local_pass != "" && local_ssid != "" && remote_ssid == "" && remote_pass == "") {
addToWiFiJSON(local_ssid, local_pass);
sendWifiCredentials();
result = true;
}
return (result);
}
void getScan(AsyncWebServerRequest * request) {
AsyncResponseStream *response = request->beginResponseStream("application/json");
response->print(getScanAsJsonString());
request->send(response);
}
};
void setupCaptivePortal() {
dnsServer.start(DNS_PORT, "*", apIP);
}