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.

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