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.

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