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.

244 lines
6.8 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
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. bool _telnetAuth = TELNET_AUTHENTICATION;
  13. bool _telnetClientsAuth[TELNET_MAX_CLIENTS];
  14. // -----------------------------------------------------------------------------
  15. // Private methods
  16. // -----------------------------------------------------------------------------
  17. #if WEB_SUPPORT
  18. bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) {
  19. return (strncmp(key, "telnet", 6) == 0);
  20. }
  21. void _telnetWebSocketOnSend(JsonObject& root) {
  22. root["telnetVisible"] = 1;
  23. root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  24. root["telnetAuth"] = getSetting("telnetAuth", TELNET_AUTHENTICATION).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. // Do not send broadcast messages to unauthenticated clients
  44. if (_telnetAuth && !_telnetClientsAuth[i]) {
  45. continue;
  46. }
  47. if (_telnetWrite(i, data, len)) ++count;
  48. }
  49. return count;
  50. }
  51. bool _telnetWrite(unsigned char clientId, const char * message) {
  52. return _telnetWrite(clientId, (void *) message, strlen(message));
  53. }
  54. void _telnetData(unsigned char clientId, void *data, size_t len) {
  55. // Skip first message since it's always garbage
  56. if (_telnetFirst) {
  57. _telnetFirst = false;
  58. return;
  59. }
  60. // Capture close connection
  61. char * p = (char *) data;
  62. // C-d is sent as two bytes (sometimes repeating)
  63. if (len >= 2) {
  64. if ((p[0] == 0xFF) && (p[1] == 0xEC)) {
  65. _telnetClients[clientId]->close(true);
  66. return;
  67. }
  68. }
  69. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  70. _telnetClients[clientId]->close();
  71. return;
  72. }
  73. // Password prompt (disable on CORE variant)
  74. #ifdef ESPURNA_CORE
  75. const bool authenticated = true;
  76. #else
  77. const bool authenticated = _telnetClientsAuth[clientId];
  78. #endif
  79. if (_telnetAuth && !authenticated) {
  80. String password = getAdminPass();
  81. if (strncmp(p, password.c_str(), password.length()) == 0) {
  82. DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId);
  83. _telnetWrite(clientId, "Welcome!\n");
  84. _telnetClientsAuth[clientId] = true;
  85. } else {
  86. _telnetWrite(clientId, "Password: ");
  87. }
  88. return;
  89. }
  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. crashDump();
  135. crashClear();
  136. #endif
  137. #ifdef ESPURNA_CORE
  138. _telnetClientsAuth[i] = true;
  139. #else
  140. _telnetClientsAuth[i] = !_telnetAuth;
  141. if (_telnetAuth) _telnetWrite(i, "Password: ");
  142. #endif
  143. _telnetFirst = true;
  144. wifiReconnectCheck();
  145. return;
  146. }
  147. }
  148. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  149. client->onDisconnect([](void *s, AsyncClient *c) {
  150. c->free();
  151. delete c;
  152. });
  153. client->close(true);
  154. }
  155. // -----------------------------------------------------------------------------
  156. // Public API
  157. // -----------------------------------------------------------------------------
  158. bool telnetConnected() {
  159. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  160. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  161. }
  162. return false;
  163. }
  164. unsigned char telnetWrite(unsigned char ch) {
  165. char data[1] = {ch};
  166. return _telnetWrite(data, 1);
  167. }
  168. void _telnetConfigure() {
  169. _telnetAuth = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
  170. }
  171. void telnetSetup() {
  172. _telnetServer = new AsyncServer(TELNET_PORT);
  173. _telnetServer->onClient([](void *s, AsyncClient* c) {
  174. _telnetNewClient(c);
  175. }, 0);
  176. _telnetServer->begin();
  177. #if WEB_SUPPORT
  178. wsOnSendRegister(_telnetWebSocketOnSend);
  179. wsOnReceiveRegister(_telnetWebSocketOnReceive);
  180. #endif
  181. espurnaRegisterReload(_telnetConfigure);
  182. _telnetConfigure();
  183. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  184. }
  185. #endif // TELNET_SUPPORT