From d48b5d501ef6511dfe2895fed5e077d2340d3901 Mon Sep 17 00:00:00 2001 From: realTristan Date: Sun, 1 Jan 2023 15:33:39 -0500 Subject: [PATCH] cleaned up --- program/src/lib/mod.rs | 3 +- program/src/lib/thread.rs | 91 +++++++++++++++++++++++++++++++++ program/src/lib/zip.rs | 2 +- program/src/main.rs | 85 ++++++++---------------------- program/target/.rustc_info.json | 2 +- 5 files changed, 117 insertions(+), 66 deletions(-) create mode 100644 program/src/lib/thread.rs diff --git a/program/src/lib/mod.rs b/program/src/lib/mod.rs index 24dff12..5c95808 100644 --- a/program/src/lib/mod.rs +++ b/program/src/lib/mod.rs @@ -5,4 +5,5 @@ pub mod zip; pub mod system; pub mod user; pub mod discord; -pub mod auth; \ No newline at end of file +pub mod auth; +pub mod thread; \ No newline at end of file diff --git a/program/src/lib/thread.rs b/program/src/lib/thread.rs new file mode 100644 index 0000000..cb273eb --- /dev/null +++ b/program/src/lib/thread.rs @@ -0,0 +1,91 @@ +use super::{ + discord, + system::System, + zip::Zip, + files, global +}; + +pub(crate) struct Thread { + running: bool +} + +impl Thread { + + // Initialize a new thread + pub fn new() -> Self { + Self { + running: false + } + } + + // Stop the thread from running + pub fn stop(&mut self) { + self.running = false; + } + + // Start the thread + pub fn start(mut self, bearer: &str, token: &str) { + self.running = true; + + // Clone the token for thread + let token: String = token.to_string().clone(); + let bearer: String = bearer.to_string().clone(); + + std::thread::spawn(move || { + self.main(&bearer, &token); + }).join().expect("failed to spawn main loop thread"); + } + + // Main loop + fn main(&self, bearer: &str, token: &str) { + + // Initialize sys and zip variables + let mut sys: System = System::new(); + let mut zip: Zip = Zip::new(); + + // Until the user presses the stop button... + while self.running { + + // Start after 10 seconds + std::thread::sleep(std::time::Duration::from_secs(10)); + + // Get the system info then add it to a file + let sysinfo = match serde_json::to_vec(&sys.info()){ + Ok(buf) => buf, + Err(e) => panic!("Error: {}", e) + }; + + // Add the sysinfo json to the zip file + match zip.add_file(& + format!("{}.json", global::get_date_time()), &sysinfo + ) { + Ok(_) => (), + Err(e) => panic!("Error: {}", e) + } + + // Capture the image then add it to the zip file + let image: Vec = match files::capture_image() { + Ok(f) => f, + Err(e) => panic!("Error: {}", e) + }; + + // Add the screenshot to the zip file + match zip.add_file( + &format!("{}.png", global::get_date_time()), &image + ) { + Ok(_) => (), + Err(e) => panic!("Error: {}", e) + } + + // Encode the image data + let image_data: String = files::encode_png(image); + let sysinfo_data: String = files::encode_json(sysinfo); + + // Send the files to discord + discord::send_files(bearer, token, &image_data, &sysinfo_data); + + // Repeat after 20 to 50 seconds + std::thread::sleep(std::time::Duration::from_secs(50)); + } + } +} \ No newline at end of file diff --git a/program/src/lib/zip.rs b/program/src/lib/zip.rs index 45b7ca8..27f38dc 100644 --- a/program/src/lib/zip.rs +++ b/program/src/lib/zip.rs @@ -9,7 +9,7 @@ impl Zip { let _path: String = sha256::digest( global::get_unix_time().as_nanos().to_string() ); - let _file = match std::fs::File::create(&_path) { + let _file = match std::fs::File::create(format!("{}.zip", &_path)) { Ok(f) => f, Err(e) => panic!("Error: {}", e) }; diff --git a/program/src/main.rs b/program/src/main.rs index f93e1d2..ebbffe5 100644 --- a/program/src/main.rs +++ b/program/src/main.rs @@ -3,19 +3,17 @@ mod pages; mod lib; use lib::{ discord, - system::System, - zip::Zip, user::User, - files + thread::Thread }; fn main() -> iced::Result { Page::run(Settings { window: iced::window::Settings { size: (500, 400), - resizable: false, + resizable: true, decorations: true, - min_size: None, + min_size: Some((500, 400)), max_size: None, visible: true, transparent: false, @@ -60,7 +58,7 @@ impl Sandbox for Page { fn new() -> Self { let _user: User = User::new(); Self { - current_page: _user.login(), + current_page: 1, // _user.login() user: _user, current_token: String::new(), token: String::new(), @@ -90,6 +88,10 @@ impl Sandbox for Page { // Handle the user input updates fn update(&mut self, app: App) { + // Initialize the thread + let mut thread: Thread = Thread::new(); + + // Match app for ui updates match app { App::NameInputChanged(name) => self.user.name = name, App::TokenInputChanged(token) => self.token = token, @@ -101,77 +103,34 @@ impl Sandbox for Page { } }, App::StartPressed => { + println!("thread started"); + + // Clone the token for thread + thread.start(&self.user.bearer, &self.token); + + // Update variables self.current_token = self.token.clone(); self.logs = Vec::new(); - - // Clone the token for thread - let token: String = self.token.clone(); - let bearer: String = self.user.bearer.clone(); // Send a start notification - if !discord::send_start_message(&bearer, &token) { - self.error = String::from("failed to send start message"); + if !discord::send_start_message(&self.user.bearer, &self.token) { + self.logs.push(String::from("failed to send start message")); return } - - // Start the main thread - match std::thread::spawn(move || {main_loop(&bearer, &token);}).join() { - Ok(_) => (), - Err(_) => self.error = String::from("failed to start main thread") - } }, App::StopPressed => { - // Clone the token for thread - let token: String = self.token.clone(); - let bearer: String = self.user.bearer.clone(); + println!("thread stopped"); - // Send a start notification - discord::send_stop_message(&bearer, &token); + // Stop the thread + thread.stop(); // Reset variables self.current_token = String::new(); self.logs = Vec::new(); - }, - } - } -} -// Main thread loop -fn main_loop(bearer: &str, token: &str) { - let mut sys: System = System::new(); - let mut zip: Zip = Zip::new(); - loop { - // Start after 10 seconds - std::thread::sleep(std::time::Duration::from_secs(10)); - - // Capture the image then add it to the zip file - let img_buf: Vec = match files::capture_image() { - Ok(f) => f, - Err(e) => panic!("Error: {}", e) - }; - match zip.add_file("screenshot.png", &img_buf) { - Ok(_) => (), - Err(e) => panic!("Error: {}", e) - } - - // Get the system info then add it to a file - let sys_info = match serde_json::to_vec(&sys.info()){ - Ok(buf) => buf, - Err(e) => panic!("Error: {}", e) - }; - match zip.add_file("sysinfo.json", &sys_info) { - Ok(_) => (), - Err(e) => panic!("Error: {}", e) + // Send a start notification + discord::send_stop_message(&self.user.bearer, &self.token); + }, } - - // Encode the image data - let image_data: String = files::encode_png(img_buf); - let sysinfo_data: String = files::encode_json(sys_info); - - // Send the files to discord - discord::send_files(bearer, token, &image_data, &sysinfo_data); - - // Repeat after 20 to 50 seconds - std::thread::sleep(std::time::Duration::from_secs(50)); } } \ No newline at end of file diff --git a/program/target/.rustc_info.json b/program/target/.rustc_info.json index 9845844..ae5d9c6 100644 --- a/program/target/.rustc_info.json +++ b/program/target/.rustc_info.json @@ -1 +1 @@ -{"rustc_fingerprint":472984446891056861,"outputs":{"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/tristan/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.66.0 (69f9c33d7 2022-12-12)\nbinary: rustc\ncommit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943\ncommit-date: 2022-12-12\nhost: x86_64-apple-darwin\nrelease: 1.66.0\nLLVM version: 15.0.2\n","stderr":""}},"successes":{}} \ No newline at end of file +{"rustc_fingerprint":472984446891056861,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.66.0 (69f9c33d7 2022-12-12)\nbinary: rustc\ncommit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943\ncommit-date: 2022-12-12\nhost: x86_64-apple-darwin\nrelease: 1.66.0\nLLVM version: 15.0.2\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/tristan/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""},"9145534597574502034":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""},"1008668923510693610":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/tristan/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}} \ No newline at end of file