@ -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 ) ;
}