evil_portal_scene_console_output.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "../evil_portal_app_i.h"
  2. #include "../helpers/evil_portal_storage.h"
  3. #include <m-string.h>
  4. void evil_portal_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  5. furi_assert(context);
  6. Evil_PortalApp* app = context;
  7. // If text box store gets too big, then truncate it
  8. app->text_box_store_strlen += len;
  9. if(app->text_box_store_strlen >= EVIL_PORTAL_TEXT_BOX_STORE_SIZE - 1) {
  10. furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
  11. app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
  12. }
  13. // Null-terminate buf and append to text box store
  14. buf[len] = '\0';
  15. furi_string_cat_printf(app->text_box_store, "%s", buf);
  16. if(app->capture_line) {
  17. furi_string_cat_printf(app->captured_line, "%s", buf);
  18. }
  19. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  20. }
  21. static inline bool captured(Evil_PortalApp* app, const char* str) {
  22. return furi_string_search_str(app->captured_line, str) != STRING_FAILURE;
  23. }
  24. void evil_portal_scene_console_output_on_enter(void* context) {
  25. Evil_PortalApp* app = context;
  26. TextBox* text_box = app->text_box;
  27. text_box_reset(app->text_box);
  28. text_box_set_font(text_box, TextBoxFontText);
  29. if(app->focus_console_start) {
  30. text_box_set_focus(text_box, TextBoxFocusStart);
  31. } else {
  32. text_box_set_focus(text_box, TextBoxFocusEnd);
  33. }
  34. if(app->is_command) {
  35. furi_string_reset(app->text_box_store);
  36. app->text_box_store_strlen = 0;
  37. app->sent_reset = false;
  38. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  39. const char* help_msg = "BLUE = Waiting\nGREEN = Good\nRED = Bad\n\nThis project is a "
  40. "WIP.\ngithub.com/bigbrodude6119/flipper-zero-evil-portal\n\n"
  41. "Version 0.0.2\n\n";
  42. furi_string_cat_str(app->text_box_store, help_msg);
  43. app->text_box_store_strlen += strlen(help_msg);
  44. if(app->show_stopscan_tip) {
  45. const char* msg = "Press BACK to return\n";
  46. furi_string_cat_str(app->text_box_store, msg);
  47. app->text_box_store_strlen += strlen(msg);
  48. }
  49. }
  50. if(0 == strncmp("savelogs", app->selected_tx_string, strlen("savelogs"))) {
  51. const char* help_msg = "Logs saved.\n\n";
  52. furi_string_cat_str(app->text_box_store, help_msg);
  53. app->text_box_store_strlen += strlen(help_msg);
  54. furi_mutex_acquire(app->portal_logs_mutex, FuriWaitForever);
  55. write_logs(app->portal_logs);
  56. furi_string_reset(app->portal_logs);
  57. furi_mutex_release(app->portal_logs_mutex);
  58. if(app->show_stopscan_tip) {
  59. const char* msg = "Press BACK to return\n";
  60. furi_string_cat_str(app->text_box_store, msg);
  61. app->text_box_store_strlen += strlen(msg);
  62. }
  63. }
  64. if(0 == strncmp("setapname", app->selected_tx_string, strlen("setapname"))) {
  65. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneRename);
  66. return;
  67. }
  68. if(0 == strncmp("selecthtml", app->selected_tx_string, strlen("selecthtml"))) {
  69. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneSelectHtml);
  70. return;
  71. }
  72. if(0 == strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  73. if(app->show_stopscan_tip) {
  74. const char* msg =
  75. "Starting portal\nMarauder takes a few secs to start\nPress BACK to return\n";
  76. furi_string_cat_str(app->text_box_store, msg);
  77. app->text_box_store_strlen += strlen(msg);
  78. }
  79. }
  80. if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  81. app->sent_reset = true;
  82. if(app->show_stopscan_tip) {
  83. const char* msg = "Reseting portal\nPress BACK to return\n\n\n\n";
  84. furi_string_cat_str(app->text_box_store, msg);
  85. app->text_box_store_strlen += strlen(msg);
  86. }
  87. }
  88. }
  89. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  90. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneConsoleOutput, 0);
  91. view_dispatcher_switch_to_view(app->view_dispatcher, Evil_PortalAppViewConsoleOutput);
  92. // Register callback to receive data
  93. evil_portal_uart_set_handle_rx_data_cb(
  94. app->uart, evil_portal_console_output_handle_rx_data_cb);
  95. if(app->is_command && app->selected_tx_string) {
  96. if(0 == strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  97. FuriString* data = furi_string_alloc();
  98. app->capture_line = true;
  99. evil_portal_read_ap_name(context);
  100. // Test evil portal syntax and response, marauder ignores it
  101. furi_string_printf(data, "setap=%s\n", (char*)app->ap_name);
  102. furi_string_reset(app->captured_line);
  103. evil_portal_uart_tx(
  104. app->uart, (uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
  105. // TODO: move timeouts and commands elsewhere, can't block input cycle
  106. for(uint8_t t = 0; t < 69 && !captured(app, "ap set") && !captured(app, "\n>"); t++)
  107. furi_delay_ms(100);
  108. bool icanhazmarauder = !captured(app, "ap set") && // Evil portal didn't respond
  109. captured(app, "\n>"); // Marauder did respond
  110. // Not evil portal, set up marauder
  111. if(icanhazmarauder) {
  112. furi_string_printf(data, "clearlist -a -s -c\nssid -a -n '%s'\n", app->ap_name);
  113. furi_string_reset(app->captured_line);
  114. evil_portal_uart_tx(
  115. app->uart, (uint8_t*)(furi_string_get_cstr(data)), furi_string_size(data));
  116. // Marauder echoes the command, maybe still init so wait a while for echo
  117. for(uint8_t t = 0; t < 10 && !captured(app, (char*)app->ap_name); t++)
  118. furi_delay_ms(100);
  119. }
  120. free(app->ap_name);
  121. evil_portal_read_index_html(context);
  122. if(icanhazmarauder) {
  123. furi_string_reset(app->captured_line);
  124. evil_portal_uart_tx(
  125. app->uart,
  126. (uint8_t*)("evilportal -c sethtmlstr\n"),
  127. strlen("evilportal -c sethtmlstr\n"));
  128. for(uint8_t t = 0; t < 10 && !captured(app, "\n>") &&
  129. !captured(app, "Setting HTML from serial...");
  130. t++)
  131. furi_delay_ms(100);
  132. // Check for active attack
  133. if(!(captured(app, "\n>") && !captured(app, "Setting HTML from serial..."))) {
  134. furi_string_reset(app->captured_line);
  135. evil_portal_uart_tx(
  136. app->uart, app->index_html, strlen((char*)app->index_html));
  137. evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
  138. for(uint8_t t = 0; t < 20 && !captured(app, "html set"); t++)
  139. furi_delay_ms(100);
  140. evil_portal_uart_tx(
  141. app->uart,
  142. (uint8_t*)("evilportal -c start\n"),
  143. strlen("evilportal -c start\n"));
  144. }
  145. } else {
  146. furi_string_set(data, "sethtml=");
  147. furi_string_cat(data, (char*)app->index_html);
  148. evil_portal_uart_tx(
  149. app->uart,
  150. (uint8_t*)(furi_string_get_cstr(data)),
  151. strlen(furi_string_get_cstr(data)));
  152. evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
  153. }
  154. free(app->index_html);
  155. app->capture_line = false;
  156. furi_string_reset(app->captured_line);
  157. furi_string_free(data);
  158. } else if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  159. evil_portal_uart_tx(
  160. app->uart, (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  161. evil_portal_uart_tx(app->uart, (uint8_t*)("\nstopscan\n"), strlen("\nstopscan\n"));
  162. } else if(1 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  163. evil_portal_uart_tx(
  164. app->uart, (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  165. evil_portal_uart_tx(app->uart, (uint8_t*)("\n"), 1);
  166. }
  167. }
  168. }
  169. bool evil_portal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  170. UNUSED(context);
  171. bool consumed = false;
  172. if(event.type == SceneManagerEventTypeTick) {
  173. consumed = true;
  174. }
  175. return consumed;
  176. }
  177. void evil_portal_scene_console_output_on_exit(void* context) {
  178. Evil_PortalApp* app = context;
  179. // Unregister rx callback
  180. evil_portal_uart_set_handle_rx_data_cb(app->uart, NULL);
  181. }