1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-13 05:53:06 +01:00

1st step to post request for config

This commit is contained in:
wellenvogel
2022-11-15 22:44:48 +01:00
parent 9d3a9a9c0d
commit 86139fc445
7 changed files with 220 additions and 72 deletions

View File

@@ -14,7 +14,7 @@ class FactoryResetRequest: public GwMessage{
protected:
virtual void processImpl(){
api->getLogger()->logDebug(GwLog::LOG,"reset request processing");
api->getConfig()->reset(true);
api->getConfig()->reset();
xTaskCreate([](void *p){
delay(500);
ESP.restart();

View File

@@ -1,6 +1,7 @@
#include "GWConfig.h"
#include <ArduinoJson.h>
#include <string.h>
#include <MD5Builder.h>
#define B(v) (v?"true":"false")
@@ -53,6 +54,7 @@ GwConfigInterface * GwConfigHandler::getConfigItem(const String name, bool dummy
#define PREF_NAME "gwprefs"
GwConfigHandler::GwConfigHandler(GwLog *logger): GwConfigDefinitions(){
this->logger=logger;
saltBase=esp_random();
}
bool GwConfigHandler::loadConfig(){
prefs.begin(PREF_NAME,true);
@@ -63,18 +65,6 @@ bool GwConfigHandler::loadConfig(){
prefs.end();
return true;
}
bool GwConfigHandler::saveConfig(){
prefs.begin(PREF_NAME,false);
for (int i=0;i<getNumConfig();i++){
if (configs[i]->hasChangedValue){
LOG_DEBUG(GwLog::LOG,"saving %s=%s",configs[i]->getName().c_str(),configs[i]->changedValue.c_str());
prefs.putString(configs[i]->getName().c_str(),configs[i]->changedValue);
}
}
prefs.end();
LOG_DEBUG(GwLog::LOG,"saved config");
return true;
}
bool GwConfigHandler::updateValue(String name, String value){
GwConfigInterface *i=getConfigItem(name);
@@ -83,18 +73,24 @@ bool GwConfigHandler::updateValue(String name, String value){
LOG_DEBUG(GwLog::LOG,"skip empty password %s",name.c_str());
}
else{
if (i->asString() == value){
return false;
}
LOG_DEBUG(GwLog::LOG,"update config %s=>%s",name.c_str(),i->isSecret()?"***":value.c_str());
i->updateValue(value);
prefs.begin(PREF_NAME,false);
prefs.putString(i->getName().c_str(),value);
prefs.end();
}
return true;
}
bool GwConfigHandler::reset(bool save){
bool GwConfigHandler::reset(){
LOG_DEBUG(GwLog::LOG,"reset config");
prefs.begin(PREF_NAME,false);
for (int i=0;i<getNumConfig();i++){
configs[i]->updateValue(configs[i]->getDefault());
prefs.putString(configs[i]->getName().c_str(),configs[i]->getDefault());
}
if (!save) return true;
return saveConfig();
prefs.end();
return true;
}
String GwConfigHandler::getString(const String name, String defaultv) const{
GwConfigInterface *i=getConfigItem(name,false);
@@ -122,6 +118,47 @@ bool GwConfigHandler::setValue(String name,String value){
return true;
}
bool GwConfigHandler::checkPass(String hash){
if (! getBool(useAdminPass)) return true;
String pass=getString(adminPassword);
unsigned long now=millis()/1000UL & ~0x7UL;
MD5Builder builder;
char buffer[2*sizeof(now)+1];
for (int i=0;i< 5 ;i++){
unsigned long base=saltBase+now;
toHex(base,buffer,2*sizeof(now)+1);
builder.begin();
builder.add(buffer);
builder.add(pass);
builder.calculate();
String md5=builder.toString();
bool rt=hash == md5;
logger->logDebug(GwLog::DEBUG,"checking pass %s, base=%ld, hash=%s, res=%d",
hash.c_str(),base,md5.c_str(),(int)rt);
if (rt) return true;
now -= 8;
}
return false;
}
static char hv(uint8_t nibble){
nibble=nibble&0xf;
if (nibble < 10) return (char)('0'+nibble);
return (char)('A'+nibble-10);
}
void GwConfigHandler::toHex(unsigned long v, char *buffer, size_t bsize)
{
uint8_t *bp = (uint8_t *)&v;
size_t i = 0;
for (; i < sizeof(v) && (2 * i + 1) < bsize; i++)
{
buffer[2 * i] = hv((*bp) >> 4);
buffer[2 * i + 1] = hv(*bp);
bp++;
}
if ((2 * i) < bsize)
buffer[2 * i] = 0;
}
void GwNmeaFilter::handleToken(String token, int index){
switch(index){
case 0:

View File

@@ -18,22 +18,25 @@ class GwConfigHandler: public GwConfigDefinitions{
public:
GwConfigHandler(GwLog *logger);
bool loadConfig();
bool saveConfig();
void stopChanges();
bool updateValue(String name, String value);
bool reset(bool save);
bool reset();
String toString() const;
String toJson() const;
String getString(const String name,const String defaultv="") const;
bool getBool(const String name,bool defaultv=false) const ;
int getInt(const String name,int defaultv=0) const;
GwConfigInterface * getConfigItem(const String name, bool dummy=false) const;
bool checkPass(String hash);
/**
* change the value of a config item
* will become a noop after stopChanges has been called
* !use with care! no checks of the value
*/
bool setValue(String name, String value);
static void toHex(unsigned long v,char *buffer,size_t bsize);
unsigned long getSaltBase(){return saltBase;}
private:
unsigned long saltBase=0;
};
#endif

View File

@@ -118,4 +118,12 @@ bool GwWebServer::registerMainHandler(const char *url,RequestCreator creator){
return true;
}
bool GwWebServer::registerPostHandler(const char *url, ArRequestHandlerFunction requestHandler,
ArBodyHandlerFunction bodyHandler){
server->on(url,HTTP_POST,requestHandler,
[](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final){},
bodyHandler);
return true;
}

View File

@@ -1,6 +1,7 @@
#ifndef _GWWEBSERVER_H
#define _GWWEBSERVER_H
#include <ESPAsyncWebServer.h>
#include <functional>
#include "GwMessage.h"
#include "GwLog.h"
class GwWebServer{
@@ -14,6 +15,7 @@ class GwWebServer{
~GwWebServer();
void begin();
bool registerMainHandler(const char *url,RequestCreator creator);
bool registerPostHandler(const char *url, ArRequestHandlerFunction requestHandler, ArBodyHandlerFunction bodyHandler);
void handleAsyncWebRequest(AsyncWebServerRequest *request, GwRequestMessage *msg);
AsyncWebServer * getServer(){return server;}
};