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