Load SSL certificate from file, add file loading for http server
This commit is contained in:
parent
621e8cd18c
commit
5d879bcb7a
@ -143,6 +143,9 @@ void getScanResults(int count) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
#ifdef ESP32
|
||||
networks.at(i) = (bss_info *)WiFiScanClass::getScanInfoByIndex(i);
|
||||
// networks.at(i) = new bss_info;
|
||||
// networks.at(i)->ssid = (uint8_t[33])WiFi.SSID(i).c_str();
|
||||
// strncpy(networks.at(i)->ssid, (uint8_t *)(WiFi.SSID(i).c_str()), sizeof(bss_info::ssid));
|
||||
#elif defined(ESP8266)
|
||||
networks.at(i) = WiFi.getScanInfoByIndex(i);
|
||||
#endif
|
||||
@ -189,6 +192,7 @@ void getScanResults(int count) {
|
||||
}
|
||||
|
||||
void getScanResultsESP32(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
// Serial.println(info.wifi_scan_done.status);
|
||||
getScanResults(WiFi.scanComplete()); // assume scan always completed since we are in scan complete CB
|
||||
}
|
||||
|
||||
@ -242,6 +246,7 @@ bool WiFiManager::loadFromJson(File file) {
|
||||
setupAP = doc["SetupAP"].as<String>();
|
||||
|
||||
for (JsonVariant i : doc["WiFiSta"].as<JsonArray>()) {
|
||||
// Serial.println(i["name"].as<String>() + i["ssid"].as<String>() + i["psk"].as<String>());
|
||||
Log.noticeln("Name: %s", i["name"].as<String>().c_str());
|
||||
Log.noticeln("SSID: %s", i["ssid"].as<String>().c_str());
|
||||
Log.noticeln("PSK: %s", i["psk"].as<String>().c_str());
|
||||
|
||||
111
src/main.cpp
111
src/main.cpp
@ -2,6 +2,8 @@
|
||||
#include "logger/logger.h"
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoLog.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <DNSServer.h>
|
||||
#include <LittleFS.h>
|
||||
#include <WiFiManager.h>
|
||||
|
||||
@ -27,8 +29,12 @@ PsychicHttpServer server;
|
||||
|
||||
#elif defined(ESP8266)
|
||||
#include <ESP8266WebServerSecure.h>
|
||||
#ifdef HTTP_SERVE_SSL
|
||||
BearSSL::ESP8266WebServerSecure server(443);
|
||||
BearSSL::ServerSessions serverCache(5);
|
||||
#else
|
||||
ESP8266WebServer server(80);
|
||||
FS *fileSystem = &LittleFS;
|
||||
#endif
|
||||
|
||||
#ifndef SERIAL_BAUD_RATE
|
||||
@ -39,42 +45,93 @@ uint8_t mac[WL_MAC_ADDR_LENGTH];
|
||||
|
||||
WiFiManager manager;
|
||||
|
||||
DNSServer dnsServer;
|
||||
|
||||
unsigned long currentMillis, oldMillis;
|
||||
|
||||
void setup() {
|
||||
WiFi.persistent(false);
|
||||
#ifdef ESP8266
|
||||
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
|
||||
|
||||
// Cache SSL sessions to accelerate the TLS handshake.
|
||||
server.getServer().setCache(&serverCache);
|
||||
|
||||
server.on("/", []() {
|
||||
server.send(200, "text/plain", "Hello from esp8266 over HTTPS!");
|
||||
});
|
||||
server.begin();
|
||||
#elif ESP32
|
||||
WiFi.mode(WIFI_MODE_STA); // required by PsychicHttp
|
||||
server.listen(443, serverCert, serverKey);
|
||||
server.on("/", [](PsychicRequest *request) {
|
||||
return request->reply(200, "text/plain", "Hello from esp32 over HTTPS!");
|
||||
});
|
||||
#endif
|
||||
|
||||
Serial.begin(SERIAL_BAUD_RATE);
|
||||
Log.setPrefix(printPrefix);
|
||||
Log.setSuffix(printSuffix);
|
||||
Log.begin(LOG_LEVEL_VERBOSE, &Serial);
|
||||
delay(2000);
|
||||
|
||||
Serial.println("Starting up");
|
||||
|
||||
#ifdef ESP32
|
||||
LittleFS.begin(true);
|
||||
#else
|
||||
LittleFS.begin();
|
||||
#endif
|
||||
|
||||
#ifdef HTTP_SERVE_SSL
|
||||
File serverCertFile = LittleFS.open("/web/certificate.pem", "r"); // read bytes instead of string
|
||||
File serverKeyFile = LittleFS.open("/web/private.pem", "r"); // read bytes instead of string
|
||||
#endif
|
||||
|
||||
#ifdef ESP8266
|
||||
|
||||
#ifdef HTTP_SERVE_SSL
|
||||
server.getServer().setRSACert(new BearSSL::X509List(serverCertFile.readString().c_str()), new BearSSL::PrivateKey(serverKeyFile.readString().c_str()));
|
||||
|
||||
// Cache SSL sessions to accelerate the TLS handshake.
|
||||
server.getServer().setCache(&serverCache);
|
||||
#endif
|
||||
|
||||
server.on("/", []() {
|
||||
server.send(200, "text/plain", "Hello from esp8266 over HTTPS!");
|
||||
});
|
||||
|
||||
server.onNotFound([]() {
|
||||
Log.noticeln("Trying to serve file!");
|
||||
String uri = ESP8266WebServer::urlDecode(server.uri()); // required to read paths with blanks
|
||||
|
||||
if (uri.endsWith("/")) {
|
||||
uri += "index.htm";
|
||||
}
|
||||
|
||||
String contentType = mime::getContentType(uri);
|
||||
|
||||
if (!fileSystem->exists(uri)) {
|
||||
// File not found, try gzip version
|
||||
uri = uri + ".gz";
|
||||
}
|
||||
if (fileSystem->exists(uri)) {
|
||||
File file = fileSystem->open(uri, "r");
|
||||
if (server.streamFile(file, contentType) != file.size()) {
|
||||
Log.noticeln("Sent less data than expected!");
|
||||
}
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
server.send(404, "text/plain", "not found");
|
||||
return false;
|
||||
});
|
||||
|
||||
server.begin();
|
||||
#elif ESP32
|
||||
WiFi.mode(WIFI_MODE_STA); // required by PsychicHttp
|
||||
|
||||
#ifdef HTTP_SERVE_SSL
|
||||
server.listen(443, serverCertFile.readString().c_str(), serverKeyFile.readString().c_str());
|
||||
#else
|
||||
server.listen(80);
|
||||
#endif
|
||||
server.on("/", [](PsychicRequest *request) {
|
||||
return request->reply(200, "text/plain", "Hello from esp32 over HTTPS!");
|
||||
});
|
||||
|
||||
PsychicStaticFileHandler *handler = server.serveStatic("/", LittleFS, "/web/www/");
|
||||
handler->setCacheControl("max-age=60");
|
||||
#endif
|
||||
#ifdef HTTP_SERVE_SSL
|
||||
serverCertFile.close();
|
||||
serverKeyFile.close();
|
||||
#endif
|
||||
Serial.begin(SERIAL_BAUD_RATE);
|
||||
|
||||
Log.setPrefix(printPrefix);
|
||||
Log.setSuffix(printSuffix);
|
||||
Log.begin(LOG_LEVEL_VERBOSE, &Serial);
|
||||
delay(2000);
|
||||
ArduinoOTA.begin();
|
||||
|
||||
Serial.println("Starting up");
|
||||
|
||||
File file = LittleFS.open("/config/WiFiManager.json", "r");
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
@ -84,6 +141,8 @@ void setup() {
|
||||
|
||||
file.close();
|
||||
}
|
||||
dnsServer.start(53, "*", WiFi.softAPIP());
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user