Przeglądaj źródła

Script launcher seems to be working

o7-machinehum 9 miesięcy temu
rodzic
commit
ac36e292a6

+ 21 - 0
blackhat_app.c

@@ -60,6 +60,14 @@ BlackhatApp* blackhat_app_alloc()
         variable_item_list_get_view(app->var_item_list)
     );
 
+    // Var item list for script
+    app->script_item_list = variable_item_list_alloc();
+    view_dispatcher_add_view(
+        app->view_dispatcher,
+        BlackhatAppViewScriptItemList,
+        variable_item_list_get_view(app->script_item_list)
+    );
+
     app->text_input = text_input_alloc();
     view_dispatcher_add_view(
         app->view_dispatcher,
@@ -72,11 +80,16 @@ BlackhatApp* blackhat_app_alloc()
     }
 
     app->text_box = text_box_alloc();
+
     view_dispatcher_add_view(
         app->view_dispatcher,
         BlackhatAppViewConsoleOutput,
         text_box_get_view(app->text_box)
     );
+
+    app->script_text = malloc(BLACKHAT_TEXT_BOX_STORE_SIZE);
+    app->script_text_ptr = 0;
+
     app->text_box_store = furi_string_alloc();
     furi_string_reserve(app->text_box_store, BLACKHAT_TEXT_BOX_STORE_SIZE);
 
@@ -93,7 +106,14 @@ void blackhat_app_free(BlackhatApp* app)
     view_dispatcher_remove_view(
         app->view_dispatcher, BlackhatAppViewVarItemList
     );
+
+    view_dispatcher_remove_view(
+        app->view_dispatcher, BlackhatAppViewScriptItemList
+    );
+
+    variable_item_list_free(app->script_item_list);
     variable_item_list_free(app->var_item_list);
+
     view_dispatcher_remove_view(app->view_dispatcher, BlackhatAppViewTextInput);
     text_input_free(app->text_input);
     view_dispatcher_remove_view(
@@ -125,6 +145,7 @@ int32_t blackhat_app(void* p)
     expansion_disable(expansion);
 
     BlackhatApp* blackhat_app = blackhat_app_alloc();
+    blackhat_app->scanned = false;
 
     bool otg_was_enabled = furi_hal_power_is_otg_enabled();
     // turn off 5v, so it gets reset on startup

+ 20 - 1
blackhat_app_i.h

@@ -1,6 +1,8 @@
 #pragma once
 
 #include <dialogs/dialogs.h>
+#include <dolphin/dolphin.h>
+#include <furi.h>
 #include <gui/gui.h>
 #include <gui/modules/loading.h>
 #include <gui/modules/text_box.h>
@@ -9,18 +11,24 @@
 #include <gui/scene_manager.h>
 #include <gui/view_dispatcher.h>
 #include <gui/view_stack.h>
+#include <input/input.h>
+#include <notification/notification.h>
+#include <notification/notification_messages.h>
+#include <stdio.h>
 
 #include "blackhat_app.h"
 #include "blackhat_custom_event.h"
 #include "blackhat_uart.h"
 #include "scenes/blackhat_scene.h"
 
-#define NUM_MENU_ITEMS (16)
+#define NUM_MENU_ITEMS (18)
 
 #define BLACKHAT_TEXT_BOX_STORE_SIZE (4096)
 #define UART_CH FuriHalSerialIdUsart
 
 #define SHELL_CMD "whoami"
+#define SCAN_CMD "bh scripts"
+#define RUN_CMD "bh scripts run"
 #define WIFI_CON_CMD "bh wifi connect"
 #define SET_INET_SSID_CMD "bh set SSID"
 #define SET_INET_PWD_CMD "bh set PASS"
@@ -64,6 +72,15 @@ struct BlackhatApp {
     SceneManager* scene_manager;
 
     FuriString* text_box_store;
+
+    // For custom scripts
+    char* script_text;
+    size_t script_text_ptr;
+    int num_scripts;
+    char* cmd[64];
+    bool scanned;
+    VariableItemList* script_item_list;
+
     size_t text_box_store_strlen;
     TextBox* text_box;
 
@@ -79,10 +96,12 @@ struct BlackhatApp {
     char text_store[128];
     char text_input_ch[ENTER_NAME_LENGTH];
     bool text_input_req;
+    bool is_script_scan;
 };
 
 typedef enum {
     BlackhatAppViewVarItemList,
+    BlackhatAppViewScriptItemList,
     BlackhatAppViewConsoleOutput,
     BlackhatAppViewStartPortal,
     BlackhatAppViewTextInput,

+ 2 - 1
blackhat_uart.c

@@ -1,6 +1,7 @@
-#include "blackhat_app_i.h"
 #include "blackhat_uart.h"
 
+#include "blackhat_app_i.h"
+
 struct BlackhatUart {
     BlackhatApp* app;
     FuriThread* rx_thread;

+ 1 - 0
scenes/blackhat_scene_config.h

@@ -1,3 +1,4 @@
 ADD_SCENE(blackhat, start, Start)
+ADD_SCENE(blackhat, scripts, Scripts)
 ADD_SCENE(blackhat, console_output, ConsoleOutput)
 ADD_SCENE(blackhat, rename, Rename)

+ 25 - 1
scenes/blackhat_scene_console_output.c

@@ -15,10 +15,15 @@ void blackhat_console_output_handle_rx_data_cb(
             furi_string_size(app->text_box_store) + len;
     }
 
+    // We gotta parse the output
+    if (app->is_script_scan) {
+        memcpy(&app->script_text[app->script_text_ptr], buf, len);
+        app->script_text_ptr += len;
+    }
+
     // Null-terminate buf and append to text box store
     buf[len] = '\0';
     furi_string_cat_printf(app->text_box_store, "%s", buf);
-
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
 }
 
@@ -40,6 +45,23 @@ void blackhat_scene_console_output_on_enter(void* context)
         return;
     }
 
+    app->is_script_scan = false;
+    if (!strcmp(app->selected_tx_string, SCAN_CMD)) {
+        app->is_script_scan = true;
+        app->script_text_ptr = 0;
+        app->scanned = true;
+    }
+
+    if (!strcmp(app->selected_tx_string, RUN_CMD)) {
+        if (app->scanned) {
+            app->script_text_ptr++;
+            app->script_text[app->script_text_ptr] = 0x00;
+            scene_manager_next_scene(app->scene_manager, BlackhatSceneScripts);
+        }
+        return;
+    }
+
+    FURI_LOG_I("selected_tx_string", "%s", app->selected_tx_string);
     snprintf(
         app->text_store,
         sizeof(app->text_store),
@@ -51,9 +73,11 @@ void blackhat_scene_console_output_on_enter(void* context)
     FURI_LOG_I("tag/app name", "%s", app->text_store);
 
     text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
+
     scene_manager_set_scene_state(
         app->scene_manager, BlackhatSceneConsoleOutput, 0
     );
+
     view_dispatcher_switch_to_view(
         app->view_dispatcher, BlackhatAppViewConsoleOutput
     );

+ 90 - 0
scenes/blackhat_scene_scripts.c

@@ -0,0 +1,90 @@
+#include "../blackhat_app_i.h"
+
+static void blackhat_scene_script_list_enter_callback(
+    void* context, uint32_t index
+)
+{
+    furi_assert(context);
+    BlackhatApp* app = context;
+
+    app->selected_tx_string = "bh script run";
+    app->selected_option_item_text = app->cmd[index + 1];
+    app->text_input_req = false;
+    app->selected_menu_index = index;
+
+    FURI_LOG_I("tag/app name", "%s", app->selected_tx_string);
+
+    scene_manager_set_scene_state(
+        app->scene_manager, BlackhatSceneStart, app->selected_menu_index
+    );
+
+    scene_manager_next_scene(app->scene_manager, BlackhatAppViewConsoleOutput);
+}
+
+static void blackhat_scene_script_list_change_callback(VariableItem* item)
+{
+    furi_assert(item);  // REMOVE
+}
+
+void blackhat_scene_scripts_on_enter(void* context)
+{
+    BlackhatApp* app = context;
+    VariableItemList* var_item_list = app->script_item_list;
+    size_t i = 0;
+    int start = 0;
+    memset(app->cmd, 0x00, sizeof(app->cmd));
+    app->num_scripts = 0;
+
+    while (app->script_text[i++]) {
+        if (app->script_text[i] == '\n') {
+            i++;
+            app->cmd[app->num_scripts] = malloc(i - start);
+            memcpy(
+                app->cmd[app->num_scripts], &app->script_text[start], i - start
+            );
+            start = i;
+            app->num_scripts++;
+        }
+    }
+
+    variable_item_list_set_enter_callback(
+        var_item_list, blackhat_scene_script_list_enter_callback, app
+    );
+
+    for (int i = 1; i < app->num_scripts; i++) {
+        variable_item_list_add(
+            var_item_list,
+            app->cmd[i],
+            1,
+            blackhat_scene_script_list_change_callback,
+            app
+        );
+    }
+
+    view_dispatcher_switch_to_view(
+        app->view_dispatcher, BlackhatAppViewScriptItemList
+    );
+}
+
+bool blackhat_scene_scripts_on_event(void* context, SceneManagerEvent event)
+{
+    BlackhatApp* app = context;
+    furi_assert(app);    // REMOVE
+    furi_assert(event);  // REMOVE
+    return false;
+}
+
+void blackhat_scene_scripts_on_exit(void* context)
+{
+    BlackhatApp* app = context;
+
+    // for(int i = 0 ; i < app->num_scripts ; i++) {
+    //     free(app->cmd[i]);
+    // }
+
+    variable_item_list_reset(app->script_item_list);
+
+    scene_manager_search_and_switch_to_previous_scene(
+        app->scene_manager, BlackhatSceneStart
+    );
+}

+ 2 - 0
scenes/blackhat_scene_start.c

@@ -2,6 +2,8 @@
 
 BlackhatItem items[] = {
     {"Shell", {""}, 1, NULL, SHELL_CMD, false},
+    {"Scan for Scripts", {""}, 1, NULL, SCAN_CMD, false},
+    {"Run Script", {""}, 1, NULL, RUN_CMD, false},
     {"Connect WiFi", {""}, 1, NULL, WIFI_CON_CMD, FOCUS_CONSOLE_END},
     {"Set inet SSID", {""}, 1, NULL, SET_INET_SSID_CMD, true},
     {"Set inet Password", {""}, 1, NULL, SET_INET_PWD_CMD, true},