diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index 3810be9a..37b33293 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a61" +#define FW_VERSION "3.0.0.a62" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/authentication/authentication_service.cpp b/esp3d/src/modules/authentication/authentication_service.cpp index 9716bbe1..086bd6a4 100644 --- a/esp3d/src/modules/authentication/authentication_service.cpp +++ b/esp3d/src/modules/authentication/authentication_service.cpp @@ -269,9 +269,6 @@ bool AuthenticationService::ClearAuthIP (IPAddress ip, const char * sessionID) auth_ip * AuthenticationService::GetAuth (IPAddress ip, const char * sessionID) { auth_ip * current = _head; - //auth_ip * previous = NULL; - //get time - //uint32_t now = millis(); while (current) { if (ip == current->ip) { if (strcmp (sessionID, current->sessionID) == 0) { @@ -285,6 +282,28 @@ auth_ip * AuthenticationService::GetAuth (IPAddress ip, const char * sessionID) return NULL; } +//Get time left for specific session +uint32_t AuthenticationService::getSessionRemaining(const char * sessionID) +{ + auth_ip * current = _head; + if ((sessionID == nullptr) || (strlen(sessionID) == 0)) { + return 0; + } + while (current) { + if (strcmp (sessionID, current->sessionID) == 0) { + //found + uint32_t now = millis(); + if ((now - current->last_time) > _sessionTimeout) { + return 0; + } + return _sessionTimeout - (now-current->last_time); + } + //previous = current; + current = current->_next; + } + return 0; +} + //Review all IP to reset timers level_authenticate_type AuthenticationService::ResetAuthIP (IPAddress ip, const char * sessionID) { diff --git a/esp3d/src/modules/authentication/authentication_service.h b/esp3d/src/modules/authentication/authentication_service.h index 81cbeefb..07479af1 100644 --- a/esp3d/src/modules/authentication/authentication_service.h +++ b/esp3d/src/modules/authentication/authentication_service.h @@ -69,6 +69,7 @@ public: #if defined (HTTP_FEATURE) static uint32_t setSessionTimeout(uint32_t timeout); static uint32_t getSessionTimeout(); + static uint32_t getSessionRemaining(const char * sessionID); static char * create_session_ID(); static bool ClearCurrentSession (); static bool ClearAllSessions (); diff --git a/esp3d/src/modules/http/handles/handle-command.cpp b/esp3d/src/modules/http/handles/handle-command.cpp index f23041a5..ee99a2d0 100644 --- a/esp3d/src/modules/http/handles/handle-command.cpp +++ b/esp3d/src/modules/http/handles/handle-command.cpp @@ -49,8 +49,7 @@ void HTTP_Server::handle_web_command () esp3d_commands.process((uint8_t*)cmd.c_str(), cmd.length(), &output, auth_level); } else if (_webserver->hasArg ("ping")) { _webserver->send (200); - } - else { + } else { _webserver->send (400, "text/plain", "Invalid command"); } return; diff --git a/esp3d/src/modules/websocket/websocket_server.cpp b/esp3d/src/modules/websocket/websocket_server.cpp index c2d0c1d8..c27d8300 100644 --- a/esp3d/src/modules/websocket/websocket_server.cpp +++ b/esp3d/src/modules/websocket/websocket_server.cpp @@ -29,6 +29,7 @@ #include "../../core/settings_esp3d.h" #include "../../core/esp3doutput.h" #include "../../core/commands.h" +#include "../authentication/authentication_service.h" WebSocket_Server websocket_terminal_server; #if defined(WS_DATA_FEATURE) @@ -89,23 +90,35 @@ void handle_Websocket_Terminal_Event(uint8_t num, uint8_t type, uint8_t * payloa { (void)payload; (void)length; + String msg; switch(type) { case WStype_DISCONNECTED: log_esp3d("[%u] Socket Disconnected port %d!", num,websocket_terminal_server.port()); break; case WStype_CONNECTED: { - String s = "currentID:" + String(num); + msg = "currentID:" + String(num); // send message to client websocket_terminal_server.set_currentID(num); - websocket_terminal_server.pushMSG(num, s.c_str()); - s = "activeID:" + String(num); - websocket_terminal_server.pushMSG(s.c_str()); + websocket_terminal_server.pushMSG(num, msg.c_str()); + msg = "activeID:" + String(num); + websocket_terminal_server.pushMSG(msg.c_str()); log_esp3d("[%u] Socket connected port %d", num,websocket_terminal_server.port()); } break; case WStype_TEXT: - //we do not expect any input - //log_esp3d("[IGNORED][%u] get Text: %s port %d", num, payload, websocket_terminal_server.port()); +#if defined (AUTHENTICATION_FEATURE) + //we do not expect any input but ping to get session timeout if any + if (AuthenticationService::getSessionTimeout() != 0) { + msg = (const char*)payload; + if (msg.startsWith("PING:")) { + String session = msg.substring(5); + String response = "PING:"+String(AuthenticationService::getSessionRemaining(session.c_str())); + response += ":"+String(AuthenticationService::getSessionTimeout()); + websocket_terminal_server.pushMSG(num, response.c_str()); + } + } +#endif //AUTHENTICATION_FEATURE + log_esp3d("[IGNORED][%u] get Text: %s port %d", num, payload, websocket_terminal_server.port()); break; case WStype_BIN: //we do not expect any input