Fix TCPConsole to use runtime NodePrefs password instead of compile-time ADMIN_PASSWORD

This commit is contained in:
Piero Andreini 2026-03-31 17:23:47 +02:00
parent b2be547c33
commit b849d1c01b
4 changed files with 20 additions and 11 deletions

View file

@ -4,7 +4,7 @@
#if defined(ESP32) && defined(TCP_CONSOLE_PORT) && defined(ADMIN_PASSWORD) #if defined(ESP32) && defined(TCP_CONSOLE_PORT) && defined(ADMIN_PASSWORD)
#include <helpers/esp32/TCPConsole.h> #include <helpers/esp32/TCPConsole.h>
TCPConsole tcp_console(ADMIN_PASSWORD, ADVERT_NAME); TCPConsole tcp_console(nullptr); // prefs set in setup()
#endif #endif
// Believe it or not, this std C function is busted on some platforms! // Believe it or not, this std C function is busted on some platforms!
@ -152,6 +152,9 @@ void setup() {
#endif #endif
store.begin(); store.begin();
the_mesh.begin( the_mesh.begin(
tcp_console.setPrefs(the_mesh.getNodePrefs());
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
disp != NULL disp != NULL
#else #else

View file

@ -5,7 +5,7 @@
#if defined(ESP32) && defined(TCP_CONSOLE_PORT) #if defined(ESP32) && defined(TCP_CONSOLE_PORT)
#include <helpers/esp32/TCPConsole.h> #include <helpers/esp32/TCPConsole.h>
TCPConsole tcp_console(ADMIN_PASSWORD, ADVERT_NAME); TCPConsole tcp_console(nullptr); // prefs set in setup()
#endif #endif
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
@ -104,6 +104,8 @@ void setup() {
the_mesh.begin(fs); the_mesh.begin(fs);
tcp_console.setPrefs(the_mesh.getNodePrefs());
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif #endif

View file

@ -5,7 +5,7 @@
#if defined(ESP32) && defined(TCP_CONSOLE_PORT) #if defined(ESP32) && defined(TCP_CONSOLE_PORT)
#include <helpers/esp32/TCPConsole.h> #include <helpers/esp32/TCPConsole.h>
TCPConsole tcp_console(ADMIN_PASSWORD, ADVERT_NAME); TCPConsole tcp_console(nullptr); // prefs set in setup()
#endif #endif
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
@ -81,6 +81,8 @@ void setup() {
the_mesh.begin(fs); the_mesh.begin(fs);
tcp_console.setPrefs(the_mesh.getNodePrefs());
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
#endif #endif

View file

@ -6,6 +6,7 @@
#include <WiFi.h> #include <WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiServer.h> #include <WiFiServer.h>
#include <helpers/CommonCLI.h> // for NodePrefs
#ifndef TCP_CONSOLE_MAX_CLIENTS #ifndef TCP_CONSOLE_MAX_CLIENTS
#define TCP_CONSOLE_MAX_CLIENTS 2 #define TCP_CONSOLE_MAX_CLIENTS 2
@ -22,8 +23,7 @@ class TCPConsole {
unsigned long _last_active[TCP_CONSOLE_MAX_CLIENTS]; unsigned long _last_active[TCP_CONSOLE_MAX_CLIENTS];
char _cmd_buf[TCP_CONSOLE_MAX_CLIENTS][160]; char _cmd_buf[TCP_CONSOLE_MAX_CLIENTS][160];
int _cmd_len[TCP_CONSOLE_MAX_CLIENTS]; int _cmd_len[TCP_CONSOLE_MAX_CLIENTS];
const char* _password; NodePrefs* _prefs; // pointer to live NodePrefs — password is read at runtime
const char* _node_name;
void sendToClient(int i, const char* msg) { void sendToClient(int i, const char* msg) {
if (_clients[i] && _clients[i].connected()) { if (_clients[i] && _clients[i].connected()) {
@ -39,8 +39,8 @@ class TCPConsole {
} }
public: public:
TCPConsole(const char* password, const char* node_name) TCPConsole(NodePrefs* prefs)
: _server(TCP_CONSOLE_PORT), _password(password), _node_name(node_name) { : _server(TCP_CONSOLE_PORT), _prefs(prefs) {
for (int i = 0; i < TCP_CONSOLE_MAX_CLIENTS; i++) { for (int i = 0; i < TCP_CONSOLE_MAX_CLIENTS; i++) {
_authenticated[i] = false; _authenticated[i] = false;
_cmd_buf[i][0] = 0; _cmd_buf[i][0] = 0;
@ -54,6 +54,8 @@ public:
Serial.printf("TCP Console listening on port %d\n", TCP_CONSOLE_PORT); Serial.printf("TCP Console listening on port %d\n", TCP_CONSOLE_PORT);
} }
void setPrefs(NodePrefs* prefs) { _prefs = prefs; }
// Call this from loop(), passing the mesh's handleCommand function // Call this from loop(), passing the mesh's handleCommand function
template<typename T> template<typename T>
void loop(T& mesh) { void loop(T& mesh) {
@ -107,11 +109,11 @@ public:
_cmd_buf[i][_cmd_len[i]] = 0; _cmd_buf[i][_cmd_len[i]] = 0;
if (!_authenticated[i]) { if (!_authenticated[i]) {
// Authentication // Authentication — always read from live NodePrefs, not compile-time constant
if (strcmp(_cmd_buf[i], _password) == 0) { if (_prefs != nullptr && strcmp(_cmd_buf[i], _prefs->password) == 0) {
_authenticated[i] = true; _authenticated[i] = true;
char welcome[80]; char welcome[80];
snprintf(welcome, sizeof(welcome), "Welcome to %s console.\r\n> ", _node_name); snprintf(welcome, sizeof(welcome), "Welcome to %s console.\r\n> ", _prefs->node_name);
sendToClient(i, welcome); sendToClient(i, welcome);
} else { } else {
sendToClient(i, "Wrong password. Disconnecting.\r\n"); sendToClient(i, "Wrong password. Disconnecting.\r\n");
@ -139,4 +141,4 @@ public:
} }
}; };
#endif // ESP32 && TCP_CONSOLE_PORT #endif // ESP32 && TCP_CONSOLE_PORT && ADMIN_PASSWORD