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.

563 lines
16 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
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. AsyncBufferedClient based on ESPAsyncTCPbuffer, distributed with the ESPAsyncTCP
  7. (https://github.com/me-no-dev/ESPAsyncTCP/blob/master/src/ESPAsyncTCPbuffer.cpp)
  8. */
  9. #if TELNET_SUPPORT
  10. #define TELNET_IAC 0xFF
  11. #define TELNET_XEOF 0xEC
  12. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  13. using TTelnetServer = WiFiServer;
  14. using TTelnetClient = WiFiClient;
  15. #elif TELNET_SERVER == TELNET_SERVER_ASYNC
  16. #include <ESPAsyncTCP.h>
  17. #include <Schedule.h>
  18. using TTelnetServer = AsyncServer;
  19. #if TELNET_SERVER_ASYNC_BUFFERED
  20. #include <list>
  21. struct AsyncBufferedClient {
  22. constexpr static const size_t BUFFERS_MAX = 5;
  23. using buffer_t = std::vector<uint8_t>;
  24. AsyncBufferedClient(AsyncClient* client) :
  25. _client(client)
  26. {
  27. _client->onAck(_s_onAck, this);
  28. _client->onPoll(_s_onPoll, this);
  29. }
  30. void _addBuffer();
  31. static void _trySend(AsyncBufferedClient* client);
  32. static void _s_onAck(void* client_ptr, AsyncClient*, size_t, uint32_t);
  33. static void _s_onPoll(void* client_ptr, AsyncClient* client);
  34. size_t write(char c);
  35. size_t write(const char* data, size_t size=0);
  36. void flush();
  37. size_t available();
  38. bool connect(const char *host, uint16_t port);
  39. void close(bool now = false);
  40. bool connected();
  41. std::unique_ptr<AsyncClient> _client;
  42. std::list<buffer_t> _buffers;
  43. };
  44. using TTelnetClient = AsyncBufferedClient;
  45. #else
  46. using TTelnetClient = AsyncClient;
  47. #endif // TELNET_SERVER_ASYNC_BUFFERED
  48. #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER
  49. TTelnetServer _telnetServer(TELNET_PORT);
  50. std::unique_ptr<TTelnetClient> _telnetClients[TELNET_MAX_CLIENTS];
  51. bool _telnetAuth = TELNET_AUTHENTICATION;
  52. bool _telnetClientsAuth[TELNET_MAX_CLIENTS];
  53. // -----------------------------------------------------------------------------
  54. // Private methods
  55. // -----------------------------------------------------------------------------
  56. #if WEB_SUPPORT
  57. bool _telnetWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  58. return (strncmp(key, "telnet", 6) == 0);
  59. }
  60. void _telnetWebSocketOnConnected(JsonObject& root) {
  61. root["telnetSTA"] = getSetting("telnetSTA", 1 == TELNET_STA);
  62. root["telnetAuth"] = getSetting("telnetAuth", 1 == TELNET_AUTHENTICATION);
  63. }
  64. #endif
  65. #if TELNET_REVERSE_SUPPORT
  66. void _telnetReverse(const char * host, uint16_t port) {
  67. DEBUG_MSG_P(PSTR("[TELNET] Connecting to reverse telnet on %s:%d\n"), host, port);
  68. unsigned char i;
  69. for (i = 0; i < TELNET_MAX_CLIENTS; i++) {
  70. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  71. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  72. _telnetClients[i] = std::make_unique<TTelnetClient>();
  73. #else // TELNET_SERVER == TELNET_SERVER_ASYNC
  74. _telnetSetupClient(i, new AsyncClient());
  75. #endif
  76. if (_telnetClients[i]->connect(host, port)) {
  77. _telnetNotifyConnected(i);
  78. return;
  79. } else {
  80. DEBUG_MSG_P(PSTR("[TELNET] Error connecting reverse telnet\n"));
  81. _telnetDisconnect(i);
  82. return;
  83. }
  84. }
  85. }
  86. //no free/disconnected spot so reject
  87. if (i == TELNET_MAX_CLIENTS) {
  88. DEBUG_MSG_P(PSTR("[TELNET] Failed too connect - too many clients connected\n"));
  89. }
  90. }
  91. #if MQTT_SUPPORT
  92. void _telnetReverseMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  93. if (type == MQTT_CONNECT_EVENT) {
  94. mqttSubscribe(MQTT_TOPIC_TELNET_REVERSE);
  95. } else if (type == MQTT_MESSAGE_EVENT) {
  96. String t = mqttMagnitude((char *) topic);
  97. if (t.equals(MQTT_TOPIC_TELNET_REVERSE)) {
  98. String pl = String(payload);
  99. int col = pl.indexOf(':');
  100. if (col != -1) {
  101. String host = pl.substring(0, col);
  102. uint16_t port = pl.substring(col + 1).toInt();
  103. _telnetReverse(host.c_str(), port);
  104. } else {
  105. DEBUG_MSG_P(PSTR("[TELNET] Incorrect reverse telnet value given, use the form \"host:ip\""));
  106. }
  107. }
  108. }
  109. }
  110. #endif // MQTT_SUPPORT
  111. #endif // TELNET_REVERSE_SUPPORT
  112. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  113. void _telnetDisconnect(unsigned char clientId) {
  114. _telnetClients[clientId]->stop();
  115. _telnetClients[clientId] = nullptr;
  116. wifiReconnectCheck();
  117. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  118. }
  119. #elif TELNET_SERVER == TELNET_SERVER_ASYNC
  120. void _telnetCleanUp() {
  121. schedule_function([] () {
  122. for (unsigned char clientId=0; clientId < TELNET_MAX_CLIENTS; ++clientId) {
  123. if (!_telnetClients[clientId]->connected()) {
  124. _telnetClients[clientId] = nullptr;
  125. wifiReconnectCheck();
  126. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  127. }
  128. }
  129. });
  130. }
  131. // just close, clean-up method above will destroy the object later
  132. void _telnetDisconnect(unsigned char clientId) {
  133. _telnetClients[clientId]->close(true);
  134. }
  135. #if TELNET_SERVER_ASYNC_BUFFERED
  136. void AsyncBufferedClient::_trySend(AsyncBufferedClient* client) {
  137. while (!client->_buffers.empty()) {
  138. auto& chunk = client->_buffers.front();
  139. if (client->_client->space() >= chunk.size()) {
  140. client->_client->write((const char*)chunk.data(), chunk.size());
  141. client->_buffers.pop_front();
  142. continue;
  143. }
  144. return;
  145. }
  146. }
  147. void AsyncBufferedClient::_s_onAck(void* client_ptr, AsyncClient*, size_t, uint32_t) {
  148. _trySend(reinterpret_cast<AsyncBufferedClient*>(client_ptr));
  149. }
  150. void AsyncBufferedClient::_s_onPoll(void* client_ptr, AsyncClient* client) {
  151. _trySend(reinterpret_cast<AsyncBufferedClient*>(client_ptr));
  152. }
  153. void AsyncBufferedClient::_addBuffer() {
  154. // Note: c++17 emplace returns created object reference
  155. _buffers.emplace_back();
  156. _buffers.back().reserve(TCP_MSS);
  157. }
  158. size_t AsyncBufferedClient::write(const char* data, size_t size) {
  159. if (_buffers.size() > AsyncBufferedClient::BUFFERS_MAX) return 0;
  160. size_t written = 0;
  161. if (_buffers.empty()) {
  162. written = _client->add(data, size);
  163. if (written == size) return size;
  164. }
  165. const size_t full_size = size;
  166. char* data_ptr = const_cast<char*>(data + written);
  167. size -= written;
  168. while (size) {
  169. if (_buffers.empty()) _addBuffer();
  170. auto& current = _buffers.back();
  171. const auto have = current.capacity() - current.size();
  172. if (have >= size) {
  173. current.insert(current.end(), data_ptr, data_ptr + size);
  174. size = 0;
  175. } else {
  176. current.insert(current.end(), data_ptr, data_ptr + have);
  177. _addBuffer();
  178. data_ptr += have;
  179. size -= have;
  180. }
  181. }
  182. return full_size;
  183. }
  184. size_t AsyncBufferedClient::write(char c) {
  185. char _c[1] {c};
  186. return write(_c, 1);
  187. }
  188. void AsyncBufferedClient::flush() {
  189. _client->send();
  190. }
  191. size_t AsyncBufferedClient::available() {
  192. return _client->space();
  193. }
  194. bool AsyncBufferedClient::connect(const char *host, uint16_t port) {
  195. return _client->connect(host, port);
  196. }
  197. void AsyncBufferedClient::close(bool now) {
  198. _client->close(now);
  199. }
  200. bool AsyncBufferedClient::connected() {
  201. return _client->connected();
  202. }
  203. #endif // TELNET_SERVER_ASYNC_BUFFERED
  204. #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER
  205. size_t _telnetWrite(unsigned char clientId, const char *data, size_t len) {
  206. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  207. return _telnetClients[clientId]->write(data, len);
  208. }
  209. return 0;
  210. }
  211. size_t _telnetWrite(const char *data, size_t len) {
  212. unsigned char count = 0;
  213. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  214. // Do not send broadcast messages to unauthenticated clients
  215. if (_telnetAuth && !_telnetClientsAuth[i]) {
  216. continue;
  217. }
  218. if (_telnetWrite(i, data, len)) ++count;
  219. }
  220. return count;
  221. }
  222. size_t _telnetWrite(const char *data) {
  223. return _telnetWrite(data, strlen(data));
  224. }
  225. size_t _telnetWrite(unsigned char clientId, const char * message) {
  226. return _telnetWrite(clientId, message, strlen(message));
  227. }
  228. void _telnetData(unsigned char clientId, char * data, size_t len) {
  229. if ((len >= 2) && (data[0] == TELNET_IAC)) {
  230. // C-d is sent as two bytes (sometimes repeating)
  231. if (data[1] == TELNET_XEOF) {
  232. _telnetDisconnect(clientId);
  233. }
  234. return; // Ignore telnet negotiation
  235. }
  236. if ((strncmp(data, "close", 5) == 0) || (strncmp(data, "quit", 4) == 0)) {
  237. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  238. _telnetDisconnect(clientId);
  239. #else
  240. _telnetClients[clientId]->close();
  241. #endif
  242. return;
  243. }
  244. // Password prompt (disable on CORE variant)
  245. #ifdef ESPURNA_CORE
  246. const bool authenticated = true;
  247. #else
  248. const bool authenticated = _telnetClientsAuth[clientId];
  249. #endif
  250. if (_telnetAuth && !authenticated) {
  251. String password = getAdminPass();
  252. if (strncmp(data, password.c_str(), password.length()) == 0) {
  253. DEBUG_MSG_P(PSTR("[TELNET] Client #%d authenticated\n"), clientId);
  254. _telnetWrite(clientId, "Password correct, welcome!\n");
  255. _telnetClientsAuth[clientId] = true;
  256. } else {
  257. _telnetWrite(clientId, "Password (try again): ");
  258. }
  259. return;
  260. }
  261. // Inject command
  262. #if TERMINAL_SUPPORT
  263. terminalInject((void*)data, len);
  264. #endif
  265. }
  266. void _telnetNotifyConnected(unsigned char i) {
  267. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  268. // If there is no terminal support automatically dump info and crash data
  269. #if DEBUG_SUPPORT
  270. #if not TERMINAL_SUPPORT
  271. info();
  272. wifiDebug();
  273. crashDump();
  274. crashClear();
  275. #endif
  276. #endif
  277. #ifdef ESPURNA_CORE
  278. _telnetClientsAuth[i] = true;
  279. #else
  280. _telnetClientsAuth[i] = !_telnetAuth;
  281. if (_telnetAuth) {
  282. if (getAdminPass().length()) {
  283. _telnetWrite(i, "Password: ");
  284. } else {
  285. _telnetClientsAuth[i] = true;
  286. }
  287. }
  288. #endif
  289. wifiReconnectCheck();
  290. }
  291. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  292. void _telnetLoop() {
  293. if (_telnetServer.hasClient()) {
  294. int i;
  295. for (i = 0; i < TELNET_MAX_CLIENTS; i++) {
  296. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  297. _telnetClients[i] = std::make_unique<TTelnetClient>(_telnetServer.available());
  298. if (_telnetClients[i]->localIP() != WiFi.softAPIP()) {
  299. // Telnet is always available for the ESPurna Core image
  300. #ifdef ESPURNA_CORE
  301. bool telnetSTA = true;
  302. #else
  303. bool telnetSTA = getSetting("telnetSTA", 1 == TELNET_STA);
  304. #endif
  305. if (!telnetSTA) {
  306. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  307. _telnetDisconnect(i);
  308. return;
  309. }
  310. }
  311. _telnetNotifyConnected(i);
  312. break;
  313. }
  314. }
  315. //no free/disconnected spot so reject
  316. if (i == TELNET_MAX_CLIENTS) {
  317. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  318. _telnetServer.available().stop();
  319. return;
  320. }
  321. }
  322. for (int i = 0; i < TELNET_MAX_CLIENTS; i++) {
  323. if (_telnetClients[i]) {
  324. // Handle client timeouts
  325. if (!_telnetClients[i]->connected()) {
  326. _telnetDisconnect(i);
  327. } else {
  328. // Read data from clients
  329. while (_telnetClients[i] && _telnetClients[i]->available()) {
  330. char data[TERMINAL_BUFFER_SIZE];
  331. size_t len = _telnetClients[i]->available();
  332. unsigned int r = _telnetClients[i]->readBytes(data, min(sizeof(data), len));
  333. _telnetData(i, data, r);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. #elif TELNET_SERVER == TELNET_SERVER_ASYNC
  340. void _telnetSetupClient(unsigned char i, AsyncClient *client) {
  341. client->onError([i](void *s, AsyncClient *client, int8_t error) {
  342. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), client->errorToString(error), error, i);
  343. });
  344. client->onData([i](void*, AsyncClient*, void *data, size_t len){
  345. _telnetData(i, reinterpret_cast<char*>(data), len);
  346. });
  347. client->onDisconnect([i](void*, AsyncClient*) {
  348. _telnetCleanUp();
  349. });
  350. // XXX: AsyncClient does not have copy ctor
  351. #if TELNET_SERVER_ASYNC_BUFFERED
  352. _telnetClients[i] = std::make_unique<TTelnetClient>(client);
  353. #else
  354. _telnetClients[i] = std::unique_ptr<TTelnetClient>(client);
  355. #endif // TELNET_SERVER_ASYNC_BUFFERED
  356. }
  357. void _telnetNewClient(AsyncClient* client) {
  358. if (client->localIP() != WiFi.softAPIP()) {
  359. // Telnet is always available for the ESPurna Core image
  360. #ifdef ESPURNA_CORE
  361. bool telnetSTA = true;
  362. #else
  363. bool telnetSTA = getSetting("telnetSTA", 1 == TELNET_STA);
  364. #endif
  365. if (!telnetSTA) {
  366. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  367. client->onDisconnect([](void *s, AsyncClient *c) {
  368. delete c;
  369. });
  370. client->close(true);
  371. return;
  372. }
  373. }
  374. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  375. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  376. _telnetSetupClient(i, client);
  377. _telnetNotifyConnected(i);
  378. return;
  379. }
  380. }
  381. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  382. client->onDisconnect([](void *s, AsyncClient *c) {
  383. delete c;
  384. });
  385. client->close(true);
  386. }
  387. #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER
  388. // -----------------------------------------------------------------------------
  389. // Public API
  390. // -----------------------------------------------------------------------------
  391. bool telnetConnected() {
  392. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  393. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  394. }
  395. return false;
  396. }
  397. unsigned char telnetWrite(unsigned char ch) {
  398. char data[1] = {ch};
  399. return _telnetWrite(data, 1);
  400. }
  401. void _telnetConfigure() {
  402. _telnetAuth = getSetting("telnetAuth", 1 == TELNET_AUTHENTICATION);
  403. }
  404. void telnetSetup() {
  405. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  406. espurnaRegisterLoop(_telnetLoop);
  407. _telnetServer.setNoDelay(true);
  408. _telnetServer.begin();
  409. #else
  410. _telnetServer.onClient([](void *s, AsyncClient* c) {
  411. _telnetNewClient(c);
  412. }, 0);
  413. _telnetServer.begin();
  414. #endif
  415. #if WEB_SUPPORT
  416. wsRegister()
  417. .onVisible([](JsonObject& root) { root["telnetVisible"] = 1; })
  418. .onConnected(_telnetWebSocketOnConnected)
  419. .onKeyCheck(_telnetWebSocketOnKeyCheck);
  420. #endif
  421. #if TELNET_REVERSE_SUPPORT
  422. #if MQTT_SUPPORT
  423. mqttRegister(_telnetReverseMQTTCallback);
  424. #endif
  425. #if TERMINAL_SUPPORT
  426. terminalRegisterCommand(F("TELNET.REVERSE"), [](Embedis* e) {
  427. if (e->argc < 3) {
  428. terminalError(F("Wrong arguments. Usage: TELNET.REVERSE <host> <port>"));
  429. return;
  430. }
  431. String host = String(e->argv[1]);
  432. uint16_t port = String(e->argv[2]).toInt();
  433. terminalOK();
  434. _telnetReverse(host.c_str(), port);
  435. });
  436. #endif
  437. #endif
  438. espurnaRegisterReload(_telnetConfigure);
  439. _telnetConfigure();
  440. DEBUG_MSG_P(PSTR("[TELNET] %s server, Listening on port %d\n"),
  441. (TELNET_SERVER == TELNET_SERVER_WIFISERVER) ? "Sync" : "Async",
  442. TELNET_PORT);
  443. }
  444. #endif // TELNET_SUPPORT