correctly handle pass and sizes
This commit is contained in:
parent
86139fc445
commit
c02be797db
|
@ -10,18 +10,6 @@ class GwConfigInterface{
|
||||||
const char * initialValue;
|
const char * initialValue;
|
||||||
String value;
|
String value;
|
||||||
bool secret=false;
|
bool secret=false;
|
||||||
String changedValue;
|
|
||||||
bool hasChangedValue=false;
|
|
||||||
void updateValue(String value)
|
|
||||||
{
|
|
||||||
hasChangedValue = false;
|
|
||||||
if (value != this->value)
|
|
||||||
{
|
|
||||||
changedValue = value;
|
|
||||||
hasChangedValue = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GwConfigInterface(const String &name, const char * initialValue, bool secret=false){
|
GwConfigInterface(const String &name, const char * initialValue, bool secret=false){
|
||||||
this->name=name;
|
this->name=name;
|
||||||
|
|
|
@ -21,6 +21,7 @@ void GwLog::logString(const char *fmt,...){
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args,fmt);
|
va_start(args,fmt);
|
||||||
xSemaphoreTake(locker, portMAX_DELAY);
|
xSemaphoreTake(locker, portMAX_DELAY);
|
||||||
|
recordCounter++;
|
||||||
vsnprintf(buffer,bufferSize-1,fmt,args);
|
vsnprintf(buffer,bufferSize-1,fmt,args);
|
||||||
buffer[bufferSize-1]=0;
|
buffer[bufferSize-1]=0;
|
||||||
if (! writer) {
|
if (! writer) {
|
||||||
|
@ -40,6 +41,7 @@ void GwLog::logDebug(int level,const char *fmt,...){
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args,fmt);
|
va_start(args,fmt);
|
||||||
xSemaphoreTake(locker, portMAX_DELAY);
|
xSemaphoreTake(locker, portMAX_DELAY);
|
||||||
|
recordCounter++;
|
||||||
vsnprintf(buffer,bufferSize-1,fmt,args);
|
vsnprintf(buffer,bufferSize-1,fmt,args);
|
||||||
buffer[bufferSize-1]=0;
|
buffer[bufferSize-1]=0;
|
||||||
if (! writer) {
|
if (! writer) {
|
||||||
|
|
|
@ -15,6 +15,7 @@ class GwLog{
|
||||||
int logLevel=1;
|
int logLevel=1;
|
||||||
GwLogWriter *writer;
|
GwLogWriter *writer;
|
||||||
SemaphoreHandle_t locker;
|
SemaphoreHandle_t locker;
|
||||||
|
long long recordCounter=0;
|
||||||
public:
|
public:
|
||||||
static const int LOG=1;
|
static const int LOG=1;
|
||||||
static const int ERROR=0;
|
static const int ERROR=0;
|
||||||
|
@ -29,6 +30,7 @@ class GwLog{
|
||||||
int isActive(int level){return level <= logLevel;};
|
int isActive(int level){return level <= logLevel;};
|
||||||
void flush();
|
void flush();
|
||||||
void setLevel(int level){this->logLevel=level;}
|
void setLevel(int level){this->logLevel=level;}
|
||||||
|
long long getRecordCounter(){return recordCounter;}
|
||||||
};
|
};
|
||||||
#define LOG_DEBUG(level,...){ if (logger != NULL && logger->isActive(level)) logger->logDebug(level,__VA_ARGS__);}
|
#define LOG_DEBUG(level,...){ if (logger != NULL && logger->isActive(level)) logger->logDebug(level,__VA_ARGS__);}
|
||||||
|
|
||||||
|
|
92
src/main.cpp
92
src/main.cpp
|
@ -441,69 +441,7 @@ protected:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SetConfigRequest : public GwRequestMessage
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
//we rely on the message living not longer then the request
|
|
||||||
AsyncWebServerRequest *request;
|
|
||||||
SetConfigRequest(AsyncWebServerRequest *rq) : GwRequestMessage(F("application/json"),F("setConfig")),
|
|
||||||
request(rq)
|
|
||||||
{};
|
|
||||||
virtual int getTimeout(){return 4000;}
|
|
||||||
protected:
|
|
||||||
virtual void processRequest()
|
|
||||||
{
|
|
||||||
bool ok = true;
|
|
||||||
const char * hashArg="_hash";
|
|
||||||
String error;
|
|
||||||
String hash;
|
|
||||||
if (request->hasArg(hashArg)){
|
|
||||||
hash=request->arg(hashArg);
|
|
||||||
}
|
|
||||||
if (! checkPass(hash)){
|
|
||||||
result=JSON_INVALID_PASS;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
|
|
||||||
(long)xPortGetFreeHeapSize(),
|
|
||||||
(long)xPortGetMinimumEverFreeHeapSize()
|
|
||||||
);
|
|
||||||
for (int i = 0; i < request->args(); i++){
|
|
||||||
String name=request->argName(i);
|
|
||||||
String value=request->arg(i);
|
|
||||||
if (name.indexOf("_")>= 0) continue;
|
|
||||||
if (name == GwConfigDefinitions::apPassword && fixedApPass) continue;
|
|
||||||
bool rt = config.updateValue(name, value);
|
|
||||||
if (!rt)
|
|
||||||
{
|
|
||||||
logger.logDebug(GwLog::ERROR,"ERR: unable to update %s to %s", name.c_str(), value.c_str());
|
|
||||||
error += name;
|
|
||||||
error += "=";
|
|
||||||
error += value;
|
|
||||||
error += ",";
|
|
||||||
}
|
|
||||||
logger.flush();
|
|
||||||
}
|
|
||||||
if (ok)
|
|
||||||
{
|
|
||||||
result = JSON_OK;
|
|
||||||
logger.logDebug(GwLog::ERROR,"update config and restart");
|
|
||||||
logger.flush();
|
|
||||||
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
|
|
||||||
(long)xPortGetFreeHeapSize(),
|
|
||||||
(long)xPortGetMinimumEverFreeHeapSize()
|
|
||||||
);
|
|
||||||
logger.flush();
|
|
||||||
delayedRestart();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
GwJsonDocument rt(100);
|
|
||||||
rt["status"] = error;
|
|
||||||
serializeJson(rt, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
class ResetConfigRequest : public GwRequestMessage
|
class ResetConfigRequest : public GwRequestMessage
|
||||||
{
|
{
|
||||||
String hash;
|
String hash;
|
||||||
|
@ -595,10 +533,11 @@ void handleConfigRequestData(AsyncWebServerRequest *request, uint8_t *data, size
|
||||||
char hashChecked;
|
char hashChecked;
|
||||||
char parsingValue;
|
char parsingValue;
|
||||||
int bName;
|
int bName;
|
||||||
char name[16];
|
char name[33];
|
||||||
int bValue;
|
int bValue;
|
||||||
char value[512];
|
char value[512];
|
||||||
}RequestNV;
|
}RequestNV;
|
||||||
|
long long lastRecords=logger.getRecordCounter();
|
||||||
logger.logDebug(GwLog::DEBUG,"handleConfigRequestData len=%d,idx=%d,total=%d",(int)len,(int)index,(int)total);
|
logger.logDebug(GwLog::DEBUG,"handleConfigRequestData len=%d,idx=%d,total=%d",(int)len,(int)index,(int)total);
|
||||||
if (request->_tempObject == NULL){
|
if (request->_tempObject == NULL){
|
||||||
logger.logDebug(GwLog::DEBUG,"handleConfigRequestData create receive struct");
|
logger.logDebug(GwLog::DEBUG,"handleConfigRequestData create receive struct");
|
||||||
|
@ -634,12 +573,6 @@ void handleConfigRequestData(AsyncWebServerRequest *request, uint8_t *data, size
|
||||||
nv->parsingValue = 1;
|
nv->parsingValue = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (nv->bName >= maxSize)
|
|
||||||
{
|
|
||||||
nv->name[maxSize] = 0;
|
|
||||||
logger.logDebug(GwLog::DEBUG, "parse error name too long %s", nv->name);
|
|
||||||
nv->bName = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool valueDone = false;
|
bool valueDone = false;
|
||||||
|
@ -659,12 +592,7 @@ void handleConfigRequestData(AsyncWebServerRequest *request, uint8_t *data, size
|
||||||
nv->bValue++;
|
nv->bValue++;
|
||||||
parsed++;
|
parsed++;
|
||||||
data++;
|
data++;
|
||||||
if (nv->bValue >= maxSize)
|
if (valueDone) break;
|
||||||
{
|
|
||||||
nv->value[maxSize] = 0;
|
|
||||||
logger.logDebug(GwLog::DEBUG, "parse error value too long %s:%s", nv->name, nv->value);
|
|
||||||
nv->bValue = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (! valueDone){
|
if (! valueDone){
|
||||||
if (parsed >= len && (len+index) >= total){
|
if (parsed >= len && (len+index) >= total){
|
||||||
|
@ -694,7 +622,11 @@ void handleConfigRequestData(AsyncWebServerRequest *request, uint8_t *data, size
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if (nv->hashChecked){
|
if (nv->hashChecked){
|
||||||
logger.logDebug(GwLog::DEBUG,"update value ns=%d,n=%d,vs=%d,v=%d",nv->bName,nv->name,nv->bValue,nv->value);
|
logger.logDebug(GwLog::DEBUG,"value ns=%d,n=%s,vs=%d,v=%s",nv->bName,nv->name,nv->bValue,nv->value);
|
||||||
|
if ((logger.getRecordCounter() - lastRecords) > 20){
|
||||||
|
logger.flush();
|
||||||
|
lastRecords=logger.getRecordCounter();
|
||||||
|
}
|
||||||
config.updateValue(request->urlDecode(name),request->urlDecode(value));
|
config.updateValue(request->urlDecode(name),request->urlDecode(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -708,6 +640,12 @@ void handleConfigRequestData(AsyncWebServerRequest *request, uint8_t *data, size
|
||||||
if (nv->notFirst){
|
if (nv->notFirst){
|
||||||
if (nv->hashChecked){
|
if (nv->hashChecked){
|
||||||
request->send(200,"application/json",JSON_OK);
|
request->send(200,"application/json",JSON_OK);
|
||||||
|
logger.flush();
|
||||||
|
logger.logDebug(GwLog::DEBUG,"Heap free=%ld, minFree=%ld",
|
||||||
|
(long)xPortGetFreeHeapSize(),
|
||||||
|
(long)xPortGetMinimumEverFreeHeapSize()
|
||||||
|
);
|
||||||
|
logger.flush();
|
||||||
delayedRestart();
|
delayedRestart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -237,7 +237,7 @@ function changeConfig() {
|
||||||
.then(function (pass) {
|
.then(function (pass) {
|
||||||
let newAdminPass;
|
let newAdminPass;
|
||||||
let url = "/api/setConfig"
|
let url = "/api/setConfig"
|
||||||
let body="hash="+encodeURIComponent(pass)+"&";
|
let body="_hash="+encodeURIComponent(pass)+"&";
|
||||||
let allValues=getAllConfigs();
|
let allValues=getAllConfigs();
|
||||||
if (!allValues) return;
|
if (!allValues) return;
|
||||||
for (let name in allValues){
|
for (let name in allValues){
|
||||||
|
|
Loading…
Reference in New Issue