Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cargo.lock
#	Cargo.toml
#	src/httphandler.rs
#	src/main.rs
  • Loading branch information
Loudbooks committed Apr 15, 2024
2 parents 00b4e8a + cdacd52 commit 50daa37
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 57 deletions.
9 changes: 7 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ pub struct App {
}

impl App {
pub fn to_string(&self, name_spaces: usize, version_spaces: usize, author_spaces: usize) -> String {
pub fn to_string(
&self,
name_spaces: usize,
version_spaces: usize,
author_spaces: usize,
) -> String {
let name = format!("{:width$}", self.name, width = name_spaces);
let version = format!("{:width$}", self.version, width = version_spaces);
let author = format!("{:width$}", self.author, width = author_spaces);

format!("{} │ v{} │ {}", name, version, author)
}
}
}
14 changes: 9 additions & 5 deletions src/datagatherers/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ pub fn gather_hosts() -> Vec<String> {
} else {
"/etc/hosts"
};

let hosts = std::fs::read_to_string(file);
if hosts.is_err() {
return Vec::new();
}

let hosts = hosts.unwrap();
let hosts = hosts.split('\n');

hosts.filter(|host| !host.starts_with('#')).filter(|host| !host.trim().is_empty()).map(|host| host.to_string()).collect()
}

hosts
.filter(|host| !host.starts_with('#'))
.filter(|host| !host.trim().is_empty())
.map(|host| host.to_string())
.collect()
}
6 changes: 3 additions & 3 deletions src/datagatherers/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub fn gather_installed() -> Vec<App> {

installed_apps.push(app);
}

installed_apps.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));

installed_apps
}
}
4 changes: 2 additions & 2 deletions src/datagatherers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod processes;
pub mod hosts;
pub mod installed;
pub mod hosts;
pub mod processes;
27 changes: 18 additions & 9 deletions src/datagatherers/processes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use sysinfo::System;
use crate::process::Process;
use sysinfo::System;

pub fn gather_processes(sys: &System) -> Vec<Process> {
let username = whoami::username();
Expand All @@ -10,29 +10,38 @@ pub fn gather_processes(sys: &System) -> Vec<Process> {
None => {}
Some(_) => {
let amount = if processes.iter().any(|p| p.name == process.name()) {
let new_process = processes.iter_mut().find(|p| p.name == process.name()).unwrap();
let new_process = processes
.iter_mut()
.find(|p| p.name == process.name())
.unwrap();
new_process.amount += 1;
new_process.memory += process.memory() as f64;

continue
continue;
} else {
1
};

let hashtags = '#'.to_string().repeat(username.len());

let path = if cfg!(target_os = "windows") {
process.cwd().unwrap().to_str().unwrap().replace(format!("\\{}\\", username).as_str(), format!("\\{}\\", hashtags.as_str()).as_str())
process.cwd().unwrap().to_str().unwrap().replace(
format!("\\{}\\", username).as_str(),
format!("\\{}\\", hashtags.as_str()).as_str(),
)
} else {
process.cwd().unwrap().to_str().unwrap().replace(format!("/{}/", username).as_str(), format!("/{}/", hashtags.as_str()).as_str())
process.cwd().unwrap().to_str().unwrap().replace(
format!("/{}/", username).as_str(),
format!("/{}/", hashtags.as_str()).as_str(),
)
};

let process = Process {
pid: *pid,
name: process.name().to_string(),
path,
memory: process.memory() as f64,
amount
amount,
};

processes.push(process);
Expand All @@ -41,6 +50,6 @@ pub fn gather_processes(sys: &System) -> Vec<Process> {
}
processes.sort_by_key(|p| p.memory as i64);
processes.reverse();

processes
}
}
17 changes: 9 additions & 8 deletions src/httphandler.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use ureq::post;
use std::io::Cursor;

pub struct HttpHandler {
pub url: String,
pub lines: Vec<String>
pub lines: Vec<String>,
}

impl HttpHandler {
pub fn new(url: String) -> Self {
Self {
url,
lines: Vec::new()
lines: Vec::new(),
}
}

Expand All @@ -21,14 +21,15 @@ impl HttpHandler {
println!("Uploading your paste...");
let title = format!("{}'s ReportBook", name);

let result = post(&self.url)
let result = ureq::post(&self.url)
.set("content-type", "text/plain")
.set("title", title.as_str())
.send_string(&self.lines.join("\n"))
.send(Cursor::new(self.lines.join("\n")))
.unwrap()
.into_string();

println!("View your report at: {}", result.unwrap_or("".to_string()));
.into_string()
.unwrap();

println!("View your report at: {}", result);
println!("Share this link with whoever asked you to run this report!")
}
}
98 changes: 75 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io::{stdin, stdout, Write};
use crate::process::prettify_memory;
use machine_info::Machine;
use std::io::{stdin, stdout, Write};
use sysinfo::System;
use crate::process::prettify_memory;

mod process;
mod app;
mod datagatherers;
mod httphandler;
mod process;

fn main() {
let intro = r#"
Expand All @@ -22,7 +22,8 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
println!("{}", intro);
wait_for_enter("continue");

let mut http_handler = httphandler::HttpHandler::new("https://pastebook.dev/upload".to_string());
let mut http_handler =
httphandler::HttpHandler::new("https://pastebook.dev/upload".to_string());

let mut sys = System::new_all();
sys.refresh_all();
Expand All @@ -39,7 +40,11 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
let username = whoami::username();
let is_alphanumeric_username = username.chars().all(char::is_alphanumeric);

let os_name = format!("{}, {}", System::name().unwrap_or("Unknown".to_string()), System::os_version().unwrap_or("Unknown".to_string()));
let os_name = format!(
"{}, {}",
System::name().unwrap_or("Unknown".to_string()),
System::os_version().unwrap_or("Unknown".to_string())
);

const INFORMATION_SPACES: usize = 20;

Expand All @@ -54,28 +59,48 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
let id = "Total Memory: ";

let spaces = INFORMATION_SPACES - id.len();
format!("{}{}{}", id, " ".repeat(spaces), prettify_memory(total_memory as f64))
format!(
"{}{}{}",
id,
" ".repeat(spaces),
prettify_memory(total_memory as f64)
)
};

let total_swap_str = {
let id = "Total Swap: ";

let spaces = INFORMATION_SPACES - id.len();
format!("{}{}{}", id, " ".repeat(spaces), prettify_memory(total_swap as f64))
format!(
"{}{}{}",
id,
" ".repeat(spaces),
prettify_memory(total_swap as f64)
)
};

let total_memory_used_str = {
let id = "Total Memory Used: ";

let spaces = INFORMATION_SPACES - id.len();
format!("{}{}{}", id, " ".repeat(spaces), prettify_memory(total_memory_used as f64))
format!(
"{}{}{}",
id,
" ".repeat(spaces),
prettify_memory(total_memory_used as f64)
)
};

let total_swap_used_str = {
let id = "Total Swap Used: ";

let spaces = INFORMATION_SPACES - id.len();
format!("{}{}{}", id, " ".repeat(spaces), prettify_memory(total_swap_used as f64))
format!(
"{}{}{}",
id,
" ".repeat(spaces),
prettify_memory(total_swap_used as f64)
)
};

let cpu = {
Expand All @@ -100,15 +125,22 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
for graphics in gpus {
let str = graphics.name.to_string();

gpu_str.push_str(format!("{}{}{}\n", id, " ".repeat(spaces).as_str(), str.as_str()).as_str())
gpu_str.push_str(
format!("{}{}{}\n", id, " ".repeat(spaces).as_str(), str.as_str()).as_str(),
)
}

gpu_str
};

let bad_chars = username.chars().filter(|c| !c.is_alphanumeric()).map(|c| c.to_string()).collect::<Vec<String>>().join(", ");

let bad_chars = username
.chars()
.filter(|c| !c.is_alphanumeric())
.map(|c| c.to_string())
.collect::<Vec<String>>()
.join(", ");
let non_alphanumeric = format!("Contains {}", bad_chars.clone());

let alphanumeric_username = {
let id = "Username: ";

Expand All @@ -121,7 +153,7 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
let spaces = INFORMATION_SPACES - id.len();
format!("{}{}{}", id, " ".repeat(spaces), value)
};

http_handler.add_line(os_name_str.to_string());
http_handler.add_line(total_memory_str);
if total_swap > 0 {
Expand All @@ -142,11 +174,17 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
let processes = datagatherers::processes::gather_processes(&sys);
let installed_apps = datagatherers::installed::gather_installed();

http_handler.add_line(format!("Total Processes: {}", processes.iter().clone().map(|p| p.amount).sum::<u16>()));
http_handler.add_line(format!(
"Total Processes: {}",
processes.iter().clone().map(|p| p.amount).sum::<u16>()
));

const MEMORY_SPACES: usize = 9;
const AMOUNT_SPACES: usize = 3;
let path_spaces: usize = MEMORY_SPACES + AMOUNT_SPACES + 7 + processes.iter().map(|p| p.name.len()).max().unwrap_or(0);
let path_spaces: usize = MEMORY_SPACES
+ AMOUNT_SPACES
+ 7
+ processes.iter().map(|p| p.name.len()).max().unwrap_or(0);

for process in processes {
http_handler.add_line(process.to_string(MEMORY_SPACES, AMOUNT_SPACES, path_spaces));
Expand All @@ -155,9 +193,21 @@ Reports will be uploaded to a pastebin, to expire after nine hours.
http_handler.add_line("".to_string());
http_handler.add_line("Installed Apps:".to_string());

let name_spaces = installed_apps.iter().map(|a| a.name.len()).max().unwrap_or(0);
let version_spaces = installed_apps.iter().map(|a| a.version.len()).max().unwrap_or(0);
let author_spaces = installed_apps.iter().map(|a| a.author.len()).max().unwrap_or(0);
let name_spaces = installed_apps
.iter()
.map(|a| a.name.len())
.max()
.unwrap_or(0);
let version_spaces = installed_apps
.iter()
.map(|a| a.version.len())
.max()
.unwrap_or(0);
let author_spaces = installed_apps
.iter()
.map(|a| a.author.len())
.max()
.unwrap_or(0);

for app in installed_apps {
http_handler.add_line(app.to_string(name_spaces, version_spaces, author_spaces));
Expand All @@ -176,17 +226,19 @@ Reports will be uploaded to a pastebin, to expire after nine hours.

println!("Enter your username: ");
let username = user_input();

http_handler.submit(username.as_str());

wait_for_enter("exit")
}

fn user_input() -> String {
let mut input= String::new();
let mut input = String::new();

stdout().flush().expect("Failed to flush");
stdin().read_line(&mut input).expect("Did not enter a correct string");
stdin()
.read_line(&mut input)
.expect("Did not enter a correct string");

input = input.trim().to_string();

Expand Down
Loading

0 comments on commit 50daa37

Please sign in to comment.