Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. TELNET MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Parts of the code have been borrowed from Thomas Sarlandie's NetServer
  5. (https://github.com/sarfata/kbox-firmware/tree/master/src/esp)
  6. */
  7. #if TELNET_SUPPORT
  8. #include <ESPAsyncTCP.h>
  9. AsyncServer * _telnetServer;
  10. AsyncClient * _telnetClients[TELNET_MAX_CLIENTS];
  11. bool _authenticated[TELNET_MAX_CLIENTS];
  12. bool _telnetFirst = true;
  13. // -----------------------------------------------------------------------------
  14. // Private methods
  15. // -----------------------------------------------------------------------------
  16. #if WEB_SUPPORT
  17. bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) {
  18. return (strncmp(key, "telnet", 6) == 0);
  19. }
  20. void _telnetWebSocketOnSend(JsonObject& root) {
  21. root["telnetVisible"] = 1;
  22. root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  23. }
  24. #endif
  25. void _telnetDisconnect(unsigned char clientId) {
  26. _telnetClients[clientId]->free();
  27. _telnetClients[clientId] = NULL;
  28. delete _telnetClients[clientId];
  29. wifiReconnectCheck();
  30. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  31. }
  32. bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
  33. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  34. return (_telnetClients[clientId]->write((const char*) data, len) > 0);
  35. }
  36. return false;
  37. }
  38. unsigned char _telnetWrite(void *data, size_t len) {
  39. unsigned char count = 0;
  40. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  41. // Do not send broadcast messages to unauthenticated clients
  42. if (_authenticated[i]) {
  43. if (_telnetWrite(i, data, len)) ++count;
  44. }
  45. }
  46. return count;
  47. }
  48. bool _telnetWrite(unsigned char clientId, const char * message) {
  49. _telnetWrite(clientId, (void *) message, strlen(message));
  50. }
  51. void _telnetData(unsigned char clientId, void *data, size_t len) {
  52. // Skip first message since it's always garbage
  53. if (_telnetFirst) {
  54. _telnetFirst = false;
  55. return;
  56. }
  57. // Capture close connection
  58. char * p = (char *) data;
  59. // C-d is sent as two bytes (sometimes repeating)
  60. if (len >= 2) {
  61. if ((p[0] == 0xFF) && (p[1] == 0xEC)) {
  62. _telnetClients[clientId]->close(true);
  63. return;
  64. }
  65. }
  66. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  67. _telnetClients[clientId]->close();
  68. return;
  69. }
  70. // Password
  71. if (!_authenticated[clientId]) {
  72. String password = getAdminPass();
  73. if (strncmp(p, password.c_str(), password.length()) == 0) {
  74. DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId);
  75. _telnetWrite(clientId, "Welcome!\n");
  76. _authenticated[clientId] = true;
  77. } else {
  78. _telnetWrite(clientId, "Password: ");
  79. }
  80. return;
  81. }
  82. // Inject command
  83. settingsInject(data, len);
  84. }
  85. void _telnetNewClient(AsyncClient *client) {
  86. if (client->localIP() != WiFi.softAPIP()) {
  87. // Telnet is always available for the ESPurna Core image
  88. #ifdef ESPURNA_CORE
  89. bool telnetSTA = true;
  90. #else
  91. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  92. #endif
  93. if (!telnetSTA) {
  94. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  95. client->onDisconnect([](void *s, AsyncClient *c) {
  96. c->free();
  97. delete c;
  98. });
  99. client->close(true);
  100. return;
  101. }
  102. }
  103. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  104. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  105. _telnetClients[i] = client;
  106. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  107. }, 0);
  108. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  109. _telnetData(i, data, len);
  110. }, 0);
  111. client->onDisconnect([i](void *s, AsyncClient *c) {
  112. _telnetDisconnect(i);
  113. }, 0);
  114. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  115. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  116. }, 0);
  117. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  118. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  119. c->close();
  120. }, 0);
  121. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  122. // If there is no terminal support automatically dump info and crash data
  123. #if TERMINAL_SUPPORT == 0
  124. info();
  125. wifiDebug();
  126. debugDumpCrashInfo();
  127. debugClearCrashInfo();
  128. #endif
  129. _telnetFirst = true;
  130. _authenticated[i] = false;
  131. _telnetWrite(i, "Password: ");
  132. wifiReconnectCheck();
  133. return;
  134. }
  135. }
  136. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  137. client->onDisconnect([](void *s, AsyncClient *c) {
  138. c->free();
  139. delete c;
  140. });
  141. client->close(true);
  142. }
  143. // -----------------------------------------------------------------------------
  144. // Public API
  145. // -----------------------------------------------------------------------------
  146. bool telnetConnected() {
  147. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  148. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  149. }
  150. return false;
  151. }
  152. unsigned char telnetWrite(unsigned char ch) {
  153. char data[1] = {ch};
  154. return _telnetWrite(data, 1);
  155. }
  156. void telnetSetup() {
  157. _telnetServer = new AsyncServer(TELNET_PORT);
  158. _telnetServer->onClient([](void *s, AsyncClient* c) {
  159. _telnetNewClient(c);
  160. }, 0);
  161. _telnetServer->begin();
  162. #if WEB_SUPPORT
  163. wsOnSendRegister(_telnetWebSocketOnSend);
  164. wsOnReceiveRegister(_telnetWebSocketOnReceive);
  165. #endif
  166. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  167. }
  168. #endif // TELNET_SUPPORT