diff --git a/src-tauri/src/data/auth.rs b/src-tauri/src/data/auth.rs index 12db041..ddefdd5 100644 --- a/src-tauri/src/data/auth.rs +++ b/src-tauri/src/data/auth.rs @@ -3,7 +3,10 @@ use crate::fs::encryption::{aes_encrypt, aes_decrypt, aes_try_decrypt}; use crate::data::json::data_bytes; use std::fs::{create_dir, read_dir, read_to_string, File, OpenOptions}; use std::io::{Read, Write}; +#[cfg(target_os = "windows")] use std::os::windows::ffi::OsStrExt; +#[cfg(target_os = "linux")] +use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; use crate::init_user_data; use base64::{decode, encode}; @@ -12,8 +15,8 @@ use tauri::{Manager, Pixel, Runtime}; use std::ffi::OsStr; pub fn init_dir() -> Result<(), std::io::Error>{if dir().exists() {Ok(())}else{create_dir(dir())}} -#[tauri::command] -pub fn update(app: tauri::AppHandle) {app.trigger_global("rust_event", None)} +trait Encodable{fn to_bytes(&self) -> Vec;} +#[cfg(target_os = "windows")] pub trait Encodable{fn to_bytes(&self) -> Vec;} impl Encodable for OsStr{ fn to_bytes(&self) -> Vec { @@ -28,6 +31,13 @@ impl Encodable for OsStr{ bytes } } +#[cfg(target_os = "linux")] +impl Encodable for OsStr { + fn to_bytes(&self) -> Vec { + self.as_encoded_bytes().to_vec() + } +} + #[tauri::command] pub fn authenticate_user(name: &str, password: &str) -> bool{ @@ -62,6 +72,7 @@ pub fn authenticate_user(name: &str, password: &str) -> bool{ #[tauri::command] pub fn load_user(name: &str, password: &str){ + let location = encode(aes_encrypt(name, password, name.as_bytes())); let location: &[u8] = &aes_encrypt(name, password, name.as_bytes()); let location = dir().join(format!("{:?}", location)); @@ -72,18 +83,33 @@ pub fn load_user(name: &str, password: &str){ for entry in read_dir(location).expect("failed to read directory"){ let entry = entry.expect("failed to read file"); let path = entry.path(); + let entry = decode(entry.file_name().to_bytes()).unwrap(); + println!("ni: {:?}", path); + let entry = aes_decrypt(name, password, &entry); + let entry = String::from_utf8(entry).unwrap(); + println!("file name: {}", entry); + match entry.as_str(){ + "user data" => { + let file_content = read_to_string(&path).unwrap(); + let content = decode(file_content).unwrap(); + let original_content = aes_decrypt(name, password, &content); + + }, + "system data" => { + let file_content = read_to_string(&path).unwrap(); + let content = decode(file_content).unwrap(); + let original_content = aes_decrypt(name, password, &content); + }, + _ => {} + + } + + let encrypted_content = read_to_string(&path).expect("failed to read file content"); + let decrypted_content = aes_decrypt(name, password, &decode(encrypted_content).expect("failed to decode encrypted file content")); } } -#[tauri::command] -pub fn user_exists(name: &str, password: &str) -> bool { - let location: &[u8] = &aes_encrypt(name, password, name.as_bytes()); - let location = dir().join(format!("{:?}", location)); - if location.exists(){true} - else{false} -} - #[tauri::command] pub fn save_user(name: &str, password: &str) { let location = encode(aes_encrypt(name, password, name.as_bytes())).replace('/', "_"); @@ -95,40 +121,41 @@ pub fn save_user(name: &str, password: &str) { let encrypted_user_data = aes_encrypt(name, password, data_0); let encrypted_user_name = format!("{:?}", aes_encrypt(name, password, b"user data")); let encrypted_system_data = aes_encrypt(name, password, data_1); - let encrypted_user_data_base64 = base64::encode(&encrypted_user_data); - let encrypted_system_data_base64 = base64::encode(&encrypted_system_data); let encrypted_system_name = format!("{:?}", aes_encrypt(name, password, b"system data")); println!("{:?}", location); if !location.exists(){create_dir(&location).expect("could not create a directory");} - if location.join(&encrypted_user_name).exists(){ + let encrypted_user_name_base64 = encode(&encrypted_user_name); + let encrypted_system_name_base64 = encode(&encrypted_system_name); + println!("us: {}",encrypted_user_name); + if location.join(&encrypted_user_name_base64).exists(){ OpenOptions::new() .write(true) .append(true) - .open(location.join(encrypted_user_name)) + .open(location.join(encrypted_user_name_base64)) .expect("failed to open an existing file") - .write_all(&encrypted_user_data_base64.as_bytes()) + .write_all(&encrypted_user_data) .expect("failed to write data"); } else{ - File::create(location.join(encrypted_user_name)) + File::create(location.join(encrypted_user_name_base64)) .expect("failed to create a file") - .write_all(&encrypted_user_data_base64.as_bytes()) + .write_all(&encrypted_user_data) .expect("failed to write data into file"); } - if location.join(&encrypted_system_name).exists(){ + if location.join(&encrypted_system_name_base64).exists(){ OpenOptions::new() .write(true) .append(true) - .open(location.join(encrypted_system_name)) + .open(location.join(encrypted_system_name_base64)) .expect("failed to open an existing file") - .write_all(&encrypted_system_data_base64.as_bytes()) + .write_all(&encrypted_system_data) .expect("failed to write data"); } else{ - File::create(location.join(encrypted_system_name)) + File::create(location.join(encrypted_system_name_base64)) .expect("failed to create a file") - .write_all(&encrypted_system_data_base64.as_bytes()) + .write_all(&encrypted_system_data) .expect("failed to write data into file"); } @@ -139,8 +166,9 @@ pub fn save_user(name: &str, password: &str) { fn test_authentication(){ init_user_data(); init_dir().expect("failed to create the main directory"); - let name = "non existed user"; - let password = "non existed user"; + let name = "non oe user"; + let password = "non oe user"; save_user(name, password); authenticate_user(name, password); + load_user(name, password); } diff --git a/src-tauri/src/fs/commands.rs b/src-tauri/src/fs/commands.rs index 3227533..3b61148 100644 --- a/src-tauri/src/fs/commands.rs +++ b/src-tauri/src/fs/commands.rs @@ -1,9 +1,10 @@ -use std::{fs::{self, read}, io::Write, path::PathBuf}; -use std::error::Error; +use std::{fs::{self, read}, io::{Read, Write}, path::PathBuf}; use crate::dir; use serde::{Deserialize, Serialize}; use serde_json::to_vec; use base64::{self, decode, encode}; +use std::error::Error; +use tauri::api::file; use crate::fs::encryption::{aes_decrypt, xor_encrypt}; @@ -24,6 +25,24 @@ pub fn save_fs(name: String, password: String) -> Result<(), Box>{ println!("{:?}", dir.as_path()); Ok(()) } +pub fn load_fs(name: String, password: String) -> Result<(), String>{ + let dir = dir(); + let fs_bytes = unsafe{to_vec(&FS)}; + if fs_bytes.is_err(){return Err(format!("{}", fs_bytes.unwrap_err()));} + let fs_bytes = fs_bytes.unwrap(); + let file_path = dir.join(encode(xor_encrypt(b"encrypt".to_vec(), &fs_bytes))); + let mut file = fs::File::open(&file_path).map_err(|e| format!("failed to open file: {}", e))?; + let mut content = Vec::new(); + file.read_to_end(&mut content).map_err(|e|format!("failed to read file: {}", e))?; + + let decoded = decode(&content).map_err(|e| format!("failed to decode: {}", e))?; + let decrypted = xor_encrypt(decoded, &password.as_bytes()); + + let home: Home = serde_json::from_slice(&decrypted).map_err(|e| format!("failed to deserialize data: {}", e))?; + unsafe{FS = home;} + println!("Loaded file system state from: {:?}", dir.as_path()); + Ok(()) +} #[tauri::command] pub fn pwd() -> String{return unsafe { diff --git a/src-tauri/src/fs/encryption.rs b/src-tauri/src/fs/encryption.rs index 89771fa..934fc24 100644 --- a/src-tauri/src/fs/encryption.rs +++ b/src-tauri/src/fs/encryption.rs @@ -23,7 +23,7 @@ pub fn aes_encrypt(username: &str, password: &str, data: &[u8]) -> Vec { Ok(BufferResult::BufferOverflow) => { final_result.extend(write_buffer.take_read_buffer().take_remaining()); } - Err(e) => panic!("{:?}", e), + Err(e) => println!("{:?}", e), } } final_result.extend(write_buffer.take_read_buffer().take_remaining()); @@ -54,7 +54,7 @@ pub fn aes_decrypt(username: &str, password: &str, data: &[u8]) -> Vec { Ok(BufferResult::BufferOverflow) => { final_result.extend(write_buffer.take_read_buffer().take_remaining()); } - Err(e) => panic!("{:?}", e), + Err(e) => println!("{:?}", e), } }