Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
realTristan committed Jan 1, 2023
1 parent 30084a8 commit d48b5d5
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 66 deletions.
3 changes: 2 additions & 1 deletion program/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub mod zip;
pub mod system;
pub mod user;
pub mod discord;
pub mod auth;
pub mod auth;
pub mod thread;
91 changes: 91 additions & 0 deletions program/src/lib/thread.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = 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));
}
}
}
2 changes: 1 addition & 1 deletion program/src/lib/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
85 changes: 22 additions & 63 deletions program/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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<u8> = 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));
}
}
2 changes: 1 addition & 1 deletion program/target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -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":{}}
{"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":{}}

0 comments on commit d48b5d5

Please sign in to comment.