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.

561 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", TELNET_STA).toInt() == 1;
  62. root["telnetAuth"] = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
  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 TERMINAL_SUPPORT == 0
  270. info();
  271. wifiDebug();
  272. crashDump();
  273. crashClear();
  274. #endif
  275. #ifdef ESPURNA_CORE
  276. _telnetClientsAuth[i] = true;
  277. #else
  278. _telnetClientsAuth[i] = !_telnetAuth;
  279. if (_telnetAuth) {
  280. if (getAdminPass().length()) {
  281. _telnetWrite(i, "Password: ");
  282. } else {
  283. _telnetClientsAuth[i] = true;
  284. }
  285. }
  286. #endif
  287. wifiReconnectCheck();
  288. }
  289. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  290. void _telnetLoop() {
  291. if (_telnetServer.hasClient()) {
  292. int i;
  293. for (i = 0; i < TELNET_MAX_CLIENTS; i++) {
  294. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  295. _telnetClients[i] = std::make_unique<TTelnetClient>(_telnetServer.available());
  296. if (_telnetClients[i]->localIP() != WiFi.softAPIP()) {
  297. // Telnet is always available for the ESPurna Core image
  298. #ifdef ESPURNA_CORE
  299. bool telnetSTA = true;
  300. #else
  301. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  302. #endif
  303. if (!telnetSTA) {
  304. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  305. _telnetDisconnect(i);
  306. return;
  307. }
  308. }
  309. _telnetNotifyConnected(i);
  310. break;
  311. }
  312. }
  313. //no free/disconnected spot so reject
  314. if (i == TELNET_MAX_CLIENTS) {
  315. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  316. _telnetServer.available().stop();
  317. return;
  318. }
  319. }
  320. for (int i = 0; i < TELNET_MAX_CLIENTS; i++) {
  321. if (_telnetClients[i]) {
  322. // Handle client timeouts
  323. if (!_telnetClients[i]->connected()) {
  324. _telnetDisconnect(i);
  325. } else {
  326. // Read data from clients
  327. while (_telnetClients[i] && _telnetClients[i]->available()) {
  328. char data[TERMINAL_BUFFER_SIZE];
  329. size_t len = _telnetClients[i]->available();
  330. unsigned int r = _telnetClients[i]->readBytes(data, min(sizeof(data), len));
  331. _telnetData(i, data, r);
  332. }
  333. }
  334. }
  335. }
  336. }
  337. #elif TELNET_SERVER == TELNET_SERVER_ASYNC
  338. void _telnetSetupClient(unsigned char i, AsyncClient *client) {
  339. client->onError([i](void *s, AsyncClient *client, int8_t error) {
  340. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), client->errorToString(error), error, i);
  341. });
  342. client->onData([i](void*, AsyncClient*, void *data, size_t len){
  343. _telnetData(i, reinterpret_cast<char*>(data), len);
  344. });
  345. client->onDisconnect([i](void*, AsyncClient*) {
  346. _telnetCleanUp();
  347. });
  348. // XXX: AsyncClient does not have copy ctor
  349. #if TELNET_SERVER_ASYNC_BUFFERED
  350. _telnetClients[i] = std::make_unique<TTelnetClient>(client);
  351. #else
  352. _telnetClients[i] = std::unique_ptr<TTelnetClient>(client);
  353. #endif // TELNET_SERVER_ASYNC_BUFFERED
  354. }
  355. void _telnetNewClient(AsyncClient* client) {
  356. if (client->localIP() != WiFi.softAPIP()) {
  357. // Telnet is always available for the ESPurna Core image
  358. #ifdef ESPURNA_CORE
  359. bool telnetSTA = true;
  360. #else
  361. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  362. #endif
  363. if (!telnetSTA) {
  364. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  365. client->onDisconnect([](void *s, AsyncClient *c) {
  366. delete c;
  367. });
  368. client->close(true);
  369. return;
  370. }
  371. }
  372. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  373. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  374. _telnetSetupClient(i, client);
  375. _telnetNotifyConnected(i);
  376. return;
  377. }
  378. }
  379. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  380. client->onDisconnect([](void *s, AsyncClient *c) {
  381. delete c;
  382. });
  383. client->close(true);
  384. }
  385. #endif // TELNET_SERVER == TELNET_SERVER_WIFISERVER
  386. // -----------------------------------------------------------------------------
  387. // Public API
  388. // -----------------------------------------------------------------------------
  389. bool telnetConnected() {
  390. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  391. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  392. }
  393. return false;
  394. }
  395. unsigned char telnetWrite(unsigned char ch) {
  396. char data[1] = {ch};
  397. return _telnetWrite(data, 1);
  398. }
  399. void _telnetConfigure() {
  400. _telnetAuth = getSetting("telnetAuth", TELNET_AUTHENTICATION).toInt() == 1;
  401. }
  402. void telnetSetup() {
  403. #if TELNET_SERVER == TELNET_SERVER_WIFISERVER
  404. espurnaRegisterLoop(_telnetLoop);
  405. _telnetServer.setNoDelay(true);
  406. _telnetServer.begin();
  407. #else
  408. _telnetServer.onClient([](void *s, AsyncClient* c) {
  409. _telnetNewClient(c);
  410. }, 0);
  411. _telnetServer.begin();
  412. #endif
  413. #if WEB_SUPPORT
  414. wsRegister()
  415. .onVisible([](JsonObject& root) { root["telnetVisible"] = 1; })
  416. .onConnected(_telnetWebSocketOnConnected)
  417. .onKeyCheck(_telnetWebSocketOnKeyCheck);
  418. #endif
  419. #if TELNET_REVERSE_SUPPORT
  420. #if MQTT_SUPPORT
  421. mqttRegister(_telnetReverseMQTTCallback);
  422. #endif
  423. #if TERMINAL_SUPPORT
  424. terminalRegisterCommand(F("TELNET.REVERSE"), [](Embedis* e) {
  425. if (e->argc < 3) {
  426. terminalError(F("Wrong arguments. Usage: TELNET.REVERSE <host> <port>"));
  427. return;
  428. }
  429. String host = String(e->argv[1]);
  430. uint16_t port = String(e->argv[2]).toInt();
  431. terminalOK();
  432. _telnetReverse(host.c_str(), port);
  433. });
  434. #endif
  435. #endif
  436. espurnaRegisterReload(_telnetConfigure);
  437. _telnetConfigure();
  438. DEBUG_MSG_P(PSTR("[TELNET] %s server, Listening on port %d\n"),
  439. (TELNET_SERVER == TELNET_SERVER_WIFISERVER) ? "Sync" : "Async",
  440. TELNET_PORT);
  441. }
  442. #endif // TELNET_SUPPORT