diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 307c6eb7..260b12d9 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -110,8 +110,8 @@ #define TELNET_STA 0 // By default, disallow connections via STA interface #endif -#ifndef TELNET_PASSWORD -#define TELNET_PASSWORD 1 // Request password to start telnet session by default +#ifndef TELNET_AUTHENTICATION +#define TELNET_AUTHENTICATION 1 // Request password to start telnet session by default #endif #define TELNET_PORT 23 // Port to listen to telnet clients diff --git a/code/espurna/telnet.ino b/code/espurna/telnet.ino index 3b2e6338..9e933282 100644 --- a/code/espurna/telnet.ino +++ b/code/espurna/telnet.ino @@ -15,9 +15,9 @@ Parts of the code have been borrowed from Thomas Sarlandie's NetServer AsyncServer * _telnetServer; AsyncClient * _telnetClients[TELNET_MAX_CLIENTS]; bool _telnetFirst = true; -#if TELNET_PASSWORD - bool _authenticated[TELNET_MAX_CLIENTS]; -#endif + +bool _telnetAuth = TELNET_AUTHENTICATION; +bool _telnetClientsAuth[TELNET_MAX_CLIENTS]; // ----------------------------------------------------------------------------- // Private methods @@ -32,6 +32,7 @@ bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) { void _telnetWebSocketOnSend(JsonObject& root) { root["telnetVisible"] = 1; root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1; + root["telnetAuth"] = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1; } #endif @@ -54,16 +55,12 @@ bool _telnetWrite(unsigned char clientId, void *data, size_t len) { unsigned char _telnetWrite(void *data, size_t len) { unsigned char count = 0; for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) { + // Do not send broadcast messages to unauthenticated clients + if (_telnetAuth && !_telnetClientsAuth[i]) { + continue; + } - #if TELNET_PASSWORD - // Do not send broadcast messages to unauthenticated clients - if (_authenticated[i]) { - if (_telnetWrite(i, data, len)) ++count; - } - #else - if (_telnetWrite(i, data, len)) ++count; - #endif - + if (_telnetWrite(i, data, len)) ++count; } return count; } @@ -96,20 +93,24 @@ void _telnetData(unsigned char clientId, void *data, size_t len) { return; } - // Password - #if TELNET_PASSWORD - if (!_authenticated[clientId]) { - String password = getAdminPass(); - if (strncmp(p, password.c_str(), password.length()) == 0) { - DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId); - _telnetWrite(clientId, "Welcome!\n"); - _authenticated[clientId] = true; - } else { - _telnetWrite(clientId, "Password: "); - } - return; + // Password prompt (disable on CORE variant) + #ifdef ESPURNA_CORE + const bool authenticated = true; + #else + const bool authenticated = _telnetClientsAuth[clientId]; + #endif + + if (_telnetAuth && !authenticated) { + String password = getAdminPass(); + if (strncmp(p, password.c_str(), password.length()) == 0) { + DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId); + _telnetWrite(clientId, "Welcome!\n"); + _telnetClientsAuth[clientId] = true; + } else { + _telnetWrite(clientId, "Password: "); } - #endif // TELNET_PASSWORD + return; + } // Inject command settingsInject(data, len); @@ -175,9 +176,11 @@ void _telnetNewClient(AsyncClient *client) { debugClearCrashInfo(); #endif - #if TELNET_PASSWORD - _authenticated[i] = false; - _telnetWrite(i, "Password: "); + #ifdef ESPURNA_CORE + _telnetClientsAuth[i] = true; + #else + _telnetClientsAuth[i] = !_telnetAuth; + if (_telnetAuth) _telnetWrite(i, "Password: "); #endif _telnetFirst = true; @@ -214,6 +217,10 @@ unsigned char telnetWrite(unsigned char ch) { return _telnetWrite(data, 1); } +void _telnetConfigure() { + _telnetAuth = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1; +} + void telnetSetup() { _telnetServer = new AsyncServer(TELNET_PORT); @@ -227,6 +234,9 @@ void telnetSetup() { wsOnReceiveRegister(_telnetWebSocketOnReceive); #endif + espurnaRegisterReload(_telnetConfigure); + _telnetConfigure(); + DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT); } diff --git a/code/html/index.html b/code/html/index.html index 1ef31ba6..54e42640 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -608,6 +608,13 @@
Turn ON to be able to telnet to your device while connected to your home router.
TELNET is always enabled in AP mode.
+
+ +
+
+
+
Request password when starting telnet session
+