PsychicHTTP

This commit is contained in:
iranl
2024-08-28 23:59:36 +02:00
parent f7ef28e031
commit 52bd4f74c1
7 changed files with 64 additions and 61 deletions

View File

@@ -31,9 +31,8 @@ class PsychicHttpsServer : public PsychicHttpServer
virtual esp_err_t _startServer() override final; virtual esp_err_t _startServer() override final;
virtual void stop() override final; virtual void stop() override final;
}; };
#endif // PsychicHttpsServer_h
#else #else
#warning ESP-IDF https server support not enabled. #warning ESP-IDF https server support not enabled.
#endif // CONFIG_ESP_HTTPS_SERVER_ENABLE #endif // CONFIG_ESP_HTTPS_SERVER_ENABLE
#endif // PsychicHttpsServer_h

View File

@@ -627,6 +627,7 @@ boolean WiFiManager::configPortalHasTimeout(){
} }
void WiFiManager::setupHTTPServer(){ void WiFiManager::setupHTTPServer(){
server = new PsychicHttpServer;
server->listen(_httpPort); server->listen(_httpPort);
/* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */ /* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */
@@ -636,7 +637,7 @@ void WiFiManager::setupHTTPServer(){
{ {
using namespace std::placeholders; using namespace std::placeholders;
server->on(String(FPSTR(R_root)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleRoot, this,_1)); server->on(String(FPSTR(R_root)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleRoot, this,_1));
server->on(String(FPSTR(R_wifisave)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWifiSave, this,_1)); server->on(String(FPSTR(R_wifisave)).c_str(), HTTP_POST, (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleWifiSave, this,_1));
server->on(String(FPSTR(R_info)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleInfo, this,_1)); server->on(String(FPSTR(R_info)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleInfo, this,_1));
server->on(String(FPSTR(R_param)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParam, this,_1)); server->on(String(FPSTR(R_param)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParam, this,_1));
server->on(String(FPSTR(R_paramsave)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParamSave, this,_1)); server->on(String(FPSTR(R_paramsave)).c_str(), (PsychicHttpRequestCallback)std::bind(&WiFiManager::handleParamSave, this,_1));
@@ -899,6 +900,7 @@ uint8_t WiFiManager::processConfigPortal(){
DEBUG_WM(F("Connect to new AP [SUCCESS]")); DEBUG_WM(F("Connect to new AP [SUCCESS]"));
DEBUG_WM(F("Got IP Address:")); DEBUG_WM(F("Got IP Address:"));
DEBUG_WM(WiFi.localIP()); DEBUG_WM(WiFi.localIP());
reboot();
} }
#endif #endif
@@ -981,7 +983,6 @@ bool WiFiManager::shutdownConfigPortal(){
#else #else
server->stop(); server->stop();
#endif #endif
server.reset();
dnsServer->stop(); // free heap ? dnsServer->stop(); // free heap ?
dnsServer.reset(); dnsServer.reset();
@@ -1128,7 +1129,7 @@ bool WiFiManager::wifiConnectNew(String ssid, String pass,bool connect){
DEBUG_WM(F("find best RSSI: TRUE")); DEBUG_WM(F("find best RSSI: TRUE"));
#endif #endif
if (!_numNetworks) if (!_numNetworks)
WiFi_scanNetworks(false, false); // scan in case this gets called before any scans WiFi_scanNetworks(false, _enableCaptivePortal); // scan in case this gets called before any scans
int n = _numNetworks; int n = _numNetworks;
if (n == 0) { if (n == 0) {
@@ -1217,7 +1218,7 @@ bool WiFiManager::wifiConnectDefault(){
DEBUG_WM(F("find best RSSI: TRUE")); DEBUG_WM(F("find best RSSI: TRUE"));
#endif #endif
if (!_numNetworks) if (!_numNetworks)
WiFi_scanNetworks(false, false); // scan in case this gets called before any scans WiFi_scanNetworks(false, _enableCaptivePortal); // scan in case this gets called before any scans
int n = _numNetworks; int n = _numNetworks;
if (n == 0) { if (n == 0) {
@@ -1464,7 +1465,7 @@ esp_err_t WiFiManager::handleRoot(PsychicRequest *request) {
reportStatus(page); reportStatus(page);
page += FPSTR(HTTP_END); page += FPSTR(HTTP_END);
if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async //if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async
// @todo buggy, captive portals make a query on every page load, causing this to run every time in addition to the real page load // @todo buggy, captive portals make a query on every page load, causing this to run every time in addition to the real page load
// I dont understand why, when you are already in the captive portal, I guess they want to know that its still up and not done or gone // I dont understand why, when you are already in the captive portal, I guess they want to know that its still up and not done or gone
// if we can detect these and ignore them that would be great, since they come from the captive portal redirect maybe there is a refferer // if we can detect these and ignore them that would be great, since they come from the captive portal redirect maybe there is a refferer
@@ -1486,7 +1487,7 @@ esp_err_t WiFiManager::handleWifi(PsychicRequest *request,bool scan = true) {
#ifdef WM_DEBUG_LEVEL #ifdef WM_DEBUG_LEVEL
// DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",request->hasArg(F("refresh"))); // DEBUG_WM(WM_DEBUG_DEV,"refresh flag:",request->hasArg(F("refresh")));
#endif #endif
WiFi_scanNetworks(request->hasParam("refresh")); //wifiscan, force if arg refresh WiFi_scanNetworks(request->hasParam("refresh"), true); //wifiscan, force if arg refresh
page += getScanItemOut(); page += getScanItemOut();
} }
String pitem = ""; String pitem = "";
@@ -1684,7 +1685,7 @@ void WiFiManager::resetScan(){
String WiFiManager::WiFiManager::getScanItemOut(){ String WiFiManager::WiFiManager::getScanItemOut(){
String page; String page;
if(!_numNetworks) WiFi_scanNetworks(); // scan in case this gets called before any scans if(!_numNetworks) WiFi_scanNetworks(true, true); // scan in case this gets called before any scans
int n = _numNetworks; int n = _numNetworks;
if (n == 0) { if (n == 0) {
@@ -1943,8 +1944,12 @@ esp_err_t WiFiManager::handleWifiSave(PsychicRequest *request) {
handleRequest(); handleRequest();
//SAVE/connect here //SAVE/connect here
_ssid = request->getParam("s")->value(); if(request->hasParam("s")){
_pass = request->getParam("p")->value(); _ssid = request->getParam("s")->value();
}
if(request->hasParam("p")){
_pass = request->getParam("p")->value();
}
if(_ssid == "" && _pass != ""){ if(_ssid == "" && _pass != ""){
_ssid = WiFi_SSID(true); // password change, placeholder ssid, @todo compare pass to old?, confirm ssid is clean _ssid = WiFi_SSID(true); // password change, placeholder ssid, @todo compare pass to old?, confirm ssid is clean
@@ -1970,36 +1975,43 @@ esp_err_t WiFiManager::handleWifiSave(PsychicRequest *request) {
#endif #endif
// set static ips from server args // set static ips from server args
if (request->getParam(S_ip)->value() != "") { if(request->hasParam(S_ip)){
//_sta_static_ip.fromString(request->arg(FPSTR(S_ip)); if (request->getParam(S_ip)->value() != "") {
String ip = request->getParam(S_ip)->value(); //_sta_static_ip.fromString(request->arg(FPSTR(S_ip));
optionalIPFromString(&_sta_static_ip, ip.c_str()); String ip = request->getParam(S_ip)->value();
#ifdef WM_DEBUG_LEVEL optionalIPFromString(&_sta_static_ip, ip.c_str());
DEBUG_WM(WM_DEBUG_DEV,F("static ip:"),ip); #ifdef WM_DEBUG_LEVEL
#endif DEBUG_WM(WM_DEBUG_DEV,F("static ip:"),ip);
#endif
}
} }
if (request->getParam(S_gw)->value() != "") { if(request->hasParam(S_gw)){
String gw = request->getParam(S_gw)->value(); if (request->getParam(S_gw)->value() != "") {
optionalIPFromString(&_sta_static_gw, gw.c_str()); String gw = request->getParam(S_gw)->value();
#ifdef WM_DEBUG_LEVEL optionalIPFromString(&_sta_static_gw, gw.c_str());
DEBUG_WM(WM_DEBUG_DEV,F("static gateway:"),gw); #ifdef WM_DEBUG_LEVEL
#endif DEBUG_WM(WM_DEBUG_DEV,F("static gateway:"),gw);
#endif
}
} }
if (request->getParam(S_sn)->value() != "") { if(request->hasParam(S_sn)){
String sn = request->getParam(S_sn)->value(); if (request->getParam(S_sn)->value() != "") {
optionalIPFromString(&_sta_static_sn, sn.c_str()); String sn = request->getParam(S_sn)->value();
#ifdef WM_DEBUG_LEVEL optionalIPFromString(&_sta_static_sn, sn.c_str());
DEBUG_WM(WM_DEBUG_DEV,F("static netmask:"),sn); #ifdef WM_DEBUG_LEVEL
#endif DEBUG_WM(WM_DEBUG_DEV,F("static netmask:"),sn);
#endif
}
} }
if (request->getParam(S_dns)->value() != "") { if(request->hasParam(S_dns)){
String dns = request->getParam(S_dns)->value(); if (request->getParam(S_dns)->value() != "") {
optionalIPFromString(&_sta_static_dns, dns.c_str()); String dns = request->getParam(S_dns)->value();
#ifdef WM_DEBUG_LEVEL optionalIPFromString(&_sta_static_dns, dns.c_str());
DEBUG_WM(WM_DEBUG_DEV,F("static DNS:"),dns); #ifdef WM_DEBUG_LEVEL
#endif DEBUG_WM(WM_DEBUG_DEV,F("static DNS:"),dns);
#endif
}
} }
if (_presavewificallback != NULL) { if (_presavewificallback != NULL) {
_presavewificallback(); // @CALLBACK _presavewificallback(); // @CALLBACK
} }

View File

@@ -518,22 +518,7 @@ class WiFiManager
std::unique_ptr<DNSServer> dnsServer; std::unique_ptr<DNSServer> dnsServer;
PsychicHttpServer* server;
#if defined(ESP32) && defined(WM_WEBSERVERSHIM)
#ifdef WM_ASYNCWEBSERVER
using WM_WebServer = PsychicHttpServer;
#else
using WM_WebServer = WebServer;
#endif
#else
#ifdef WM_ASYNCWEBSERVER
using WM_WebServer = AsyncWebServer;
#else
using WM_WebServer = ESP8266WebServer;
#endif
#endif
std::unique_ptr<WM_WebServer> server;
protected: protected:
// vars // vars
@@ -651,9 +636,9 @@ class WiFiManager
// preload scanning causes AP to delay showing for users, but also caches and lets the cp load faster once its open // preload scanning causes AP to delay showing for users, but also caches and lets the cp load faster once its open
// my scan takes 7-10 seconds // my scan takes 7-10 seconds
public: public:
boolean _preloadwifiscan = false; // preload wifiscan if true boolean _preloadwifiscan = true; // preload wifiscan if true
unsigned int _scancachetime = 30000; // ms cache time for preload scans unsigned int _scancachetime = 30000; // ms cache time for preload scans
boolean _asyncScan = false; // perform wifi network scan async boolean _asyncScan = true; // perform wifi network scan async
bool wifiConnectDefault(); bool wifiConnectDefault();
protected: protected:

View File

@@ -131,6 +131,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT
@@ -152,6 +153,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT
@@ -173,6 +175,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT
@@ -195,6 +198,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT
@@ -216,6 +220,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT
@@ -237,6 +242,7 @@ build_flags =
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DDEBUG_NUKIHUB -DDEBUG_NUKIHUB
-DWM_DEBUG_LEVEL=4
-DDEBUG_SENSE_NUKI -DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND -DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT -DDEBUG_NUKI_CONNECT

View File

@@ -4,7 +4,7 @@
#define NUKI_HUB_VERSION "9.01" #define NUKI_HUB_VERSION "9.01"
#define NUKI_HUB_BUILD "unknownbuildnr" #define NUKI_HUB_BUILD "unknownbuildnr"
#define NUKI_HUB_DATE "2024-08-27" #define NUKI_HUB_DATE "2024-08-28"
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest" #define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json" #define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"

View File

@@ -164,7 +164,7 @@ void WebCfgServer::initialize()
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in."); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
return processFactoryReset(request); return processFactoryReset(request);
}); });
_psychicServer->on("/info", HTTP_GET, [&](PsychicRequest *request){ _psychicServer->on("/infopg", HTTP_GET, [&](PsychicRequest *request){
if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in."); if(strlen(_credUser) > 0 && strlen(_credPassword) > 0) if(!request->authenticate(_credUser, _credPassword)) return request->requestAuthentication(BASIC_AUTH, "Nuki Hub", "You must log in.");
return buildInfoHtml(request); return buildInfoHtml(request);
}); });
@@ -2650,7 +2650,7 @@ esp_err_t WebCfgServer::buildHtml(PsychicRequest *request)
printParameter(&response, "Nuki Opener PIN status", openerState.c_str(), "", "openerPin"); printParameter(&response, "Nuki Opener PIN status", openerState.c_str(), "", "openerPin");
} }
} }
printParameter(&response, "Firmware", NUKI_HUB_VERSION, "/info", "firmware"); printParameter(&response, "Firmware", NUKI_HUB_VERSION, "/infopg", "firmware");
if(_preferences->getBool(preference_check_updates)) if(_preferences->getBool(preference_check_updates))
{ {
printParameter(&response, "Latest Firmware", _preferences->getString(preference_latest_version).c_str(), "/ota", "ota"); printParameter(&response, "Latest Firmware", _preferences->getString(preference_latest_version).c_str(), "/ota", "ota");
@@ -3232,6 +3232,7 @@ esp_err_t WebCfgServer::buildConfigureWifiHtml(PsychicRequest *request)
esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request) esp_err_t WebCfgServer::buildInfoHtml(PsychicRequest *request)
{ {
Log->println("INFO HTML");
uint32_t aclPrefs[17]; uint32_t aclPrefs[17];
_preferences->getBytes(preference_acl, &aclPrefs, sizeof(aclPrefs)); _preferences->getBytes(preference_acl, &aclPrefs, sizeof(aclPrefs));
PsychicStreamResponse response(request, "text/plain"); PsychicStreamResponse response(request, "text/plain");

View File

@@ -422,8 +422,8 @@ void setup()
psychicServer = new PsychicHttpServer; psychicServer = new PsychicHttpServer;
webCfgServer = new WebCfgServer(network, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer); webCfgServer = new WebCfgServer(network, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, psychicServer);
webCfgServer->initialize(); webCfgServer->initialize();
psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/"); });
psychicServer->listen(80); psychicServer->listen(80);
psychicServer->onNotFound([](PsychicRequest* request) { return request->redirect("/"); });
} }
#else #else
Log->print(F("Nuki Hub version ")); Log->print(F("Nuki Hub version "));