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.

246 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. #if TERMINAL_SUPPORT
  92. terminalInject(data, len);
  93. #endif
  94. }
  95. void _telnetNewClient(AsyncClient *client) {
  96. if (client->localIP() != WiFi.softAPIP()) {
  97. // Telnet is always available for the ESPurna Core image
  98. #ifdef ESPURNA_CORE
  99. bool telnetSTA = true;
  100. #else
  101. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  102. #endif
  103. if (!telnetSTA) {
  104. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  105. client->onDisconnect([](void *s, AsyncClient *c) {
  106. c->free();
  107. delete c;
  108. });
  109. client->close(true);
  110. return;
  111. }
  112. }
  113. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  114. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  115. _telnetClients[i] = client;
  116. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  117. }, 0);
  118. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  119. _telnetData(i, data, len);
  120. }, 0);
  121. client->onDisconnect([i](void *s, AsyncClient *c) {
  122. _telnetDisconnect(i);
  123. }, 0);
  124. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  125. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  126. }, 0);
  127. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  128. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  129. c->close();
  130. }, 0);
  131. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  132. // If there is no terminal support automatically dump info and crash data
  133. #if TERMINAL_SUPPORT == 0
  134. info();
  135. wifiDebug();
  136. crashDump();
  137. crashClear();
  138. #endif
  139. #ifdef ESPURNA_CORE
  140. _telnetClientsAuth[i] = true;
  141. #else
  142. _telnetClientsAuth[i] = !_telnetAuth;
  143. if (_telnetAuth) _telnetWrite(i, "Password: ");
  144. #endif
  145. _telnetFirst = true;
  146. wifiReconnectCheck();
  147. return;
  148. }
  149. }
  150. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  151. client->onDisconnect([](void *s, AsyncClient *c) {
  152. c->free();
  153. delete c;
  154. });
  155. client->close(true);
  156. }
  157. // -----------------------------------------------------------------------------
  158. // Public API
  159. // -----------------------------------------------------------------------------
  160. bool telnetConnected() {
  161. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  162. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  163. }
  164. return false;
  165. }
  166. unsigned char telnetWrite(unsigned char ch) {
  167. char data[1] = {ch};
  168. return _telnetWrite(data, 1);
  169. }
  170. void _telnetConfigure() {
  171. _telnetAuth = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
  172. }
  173. void telnetSetup() {
  174. _telnetServer = new AsyncServer(TELNET_PORT);
  175. _telnetServer->onClient([](void *s, AsyncClient* c) {
  176. _telnetNewClient(c);
  177. }, 0);
  178. _telnetServer->begin();
  179. #if WEB_SUPPORT
  180. wsOnSendRegister(_telnetWebSocketOnSend);
  181. wsOnReceiveRegister(_telnetWebSocketOnReceive);
  182. #endif
  183. espurnaRegisterReload(_telnetConfigure);
  184. _telnetConfigure();
  185. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  186. }
  187. #endif // TELNET_SUPPORT