Skip to content

Commit

Permalink
JSON endpoint & CLI reporters
Browse files Browse the repository at this point in the history
Basic JSON endpoint (no features yet).

Choice of two CLI reporters "ticker" and "spinner".
  • Loading branch information
danielpclark committed Dec 19, 2017
1 parent a89ecb9 commit 924be44
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "abrute"
version = "0.1.6"
version = "0.1.7"
authors = ["Daniel P. Clark <[email protected]>"]
description = "AESCrypt Brute force attempter."
documentation = "http:https://danielpclark.github.io/abrute/index.html"
Expand Down
20 changes: 12 additions & 8 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use resume::{ResumeKey,ResumeFile};
use self::tempdir::TempDir;
use std::{fs,path,env};
use std::time::{Duration, Instant};
use ::{WorkLoad,ITERATIONS};
use ::{WorkLoad,ITERATIONS,SUCCESS};
use std::sync::{Arc, Mutex};
use reporter::prelude::*;
use reporter::CliReporter;
use std::sync::atomic::Ordering;

fn has_five_minutes_passed(t: Instant) -> bool {
Expand Down Expand Up @@ -80,8 +80,8 @@ fn unzip_command(value: &str, target: &str) -> Output {
unwrap()
}

fn progress_report<'a>(sequencer: &Digits) {
TickerTape::report(sequencer);
fn progress_report<'a>(reporter: &CliReporter, sequencer: &Digits) {
reporter.report(sequencer);
}

fn has_reached_end<'a>(sequencer: &Digits, max: usize) -> Result<(), Error> {
Expand All @@ -101,13 +101,14 @@ pub fn aescrypt_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
adj,
chunk_size,
cluster_step,
reporter_handler
reporter_handler,
cli_reporter
) = work_load;
let mut time_keeper = Instant::now();
let mut five_minute_iterations: usize = 0;
loop {
has_reached_end(&sequencer, max)?;
progress_report(&sequencer);
progress_report(&cli_reporter, &sequencer);

let chunk = chunk_sequence(
&mut sequencer,
Expand All @@ -126,6 +127,7 @@ pub fn aescrypt_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
if output.status.success() {
let mut code_mutex = code.lock().unwrap();
code_mutex.push(value.clone().to_string());
SUCCESS.store(true, Ordering::SeqCst);
println!("Success!\nPassword is: {}", value);
}
}
Expand Down Expand Up @@ -190,7 +192,8 @@ pub fn unzip_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
adj,
chunk_size,
cluster_step,
reporter_handler
reporter_handler,
cli_reporter
) = work_load;
let mut time_keeper = Instant::now();
let mut five_minute_iterations: usize = 0;
Expand All @@ -203,7 +206,7 @@ pub fn unzip_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {

loop {
has_reached_end(&sequencer, max)?;
progress_report(&sequencer);
progress_report(&cli_reporter, &sequencer);

let chunk = chunk_sequence(
&mut sequencer,
Expand Down Expand Up @@ -234,6 +237,7 @@ pub fn unzip_core_loop<'a>(work_load: WorkLoad) -> Result<(), Error> {
});
let mut code_mutex = code.lock().unwrap();
code_mutex.push(Ok(()));
SUCCESS.store(true, Ordering::SeqCst);
println!("Success!\nPassword is: {}", value);
}
}
Expand Down
33 changes: 28 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// copied, modified, or distributed except according to those terms.

#![feature(try_from)]
#![feature(conservative_impl_trait)]
extern crate digits;
extern crate rayon;
use digits::Digits;
Expand All @@ -24,17 +25,19 @@ use core::*;
#[macro_use]
extern crate clap;
use clap::{Arg, App};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT};
use reporter::ReportData;
use std::sync::atomic::{AtomicUsize, AtomicBool, ATOMIC_USIZE_INIT, ATOMIC_BOOL_INIT};
extern crate serde_json;
use std::time::SystemTime;
use std::sync::{Arc, Mutex};
extern crate num_cpus;
extern crate tiny_http;
mod web;
use web::ReportData;
use std::thread;
use reporter::CliReporter;

static ITERATIONS: AtomicUsize = ATOMIC_USIZE_INIT;
static SUCCESS: AtomicBool = ATOMIC_BOOL_INIT;

pub struct WorkLoad(
pub String, // characters: String,
Expand All @@ -45,6 +48,7 @@ pub struct WorkLoad(
pub Option<String>, // chunk: Option<String>
pub Option<usize>, // cluster_step: Option<(usize,usize)>
pub ReportData, // cloned ReportData for web JSON results and other reporters
pub CliReporter, // cli Reporter chosen
);

fn run_app() -> Result<(), Error> {
Expand Down Expand Up @@ -85,6 +89,11 @@ fn run_app() -> Result<(), Error> {
long("cluster").
takes_value(true)
).
arg(Arg::with_name("reporter").
short("r").
long("reporter").
takes_value(true)
).
arg(Arg::with_name("TARGET").
required(true).
last(true)
Expand Down Expand Up @@ -112,6 +121,7 @@ fn run_app() -> Result<(), Error> {
--cluster Takes an offset and cluster size such as 1:4 for the
first system in a cluster of 4. Helps different systems
split the workload without trying the same passwords.
-r, --reporter Use `spinner` for different command line reporter.
<TARGET> Target file to decrypt. The target must be preceeded
by a double dash: -- target.aes
-h, --help Prints help information.
Expand Down Expand Up @@ -155,6 +165,18 @@ USE OF THIS BINARY FALLS UNDER THE MIT LICENSE (c) 2017").
sequencer.mut_add(additive);
}

let reporter =
verify_reporter_name(
matches.
value_of("reporter").
unwrap_or("ticker").
to_string()
);

// JSON URI
println!("JSON endpoint available on Port 3838");
// END JSON URI

// Begin Resume Feature
let starting = sequencer.to_s();
use ::resume::{ResumeKey,ResumeFile};
Expand All @@ -174,7 +196,7 @@ USE OF THIS BINARY FALLS UNDER THE MIT LICENSE (c) 2017").
// DATA for JSON web end point
let reporter_handler = ReportData {
cores: num_cpus::get() as u8,
chunk: chunk.clone().unwrap().parse::<usize>().unwrap_or(32),
chunk: chunk.clone().unwrap_or("").parse::<usize>().unwrap_or(32),
cluster: {
if matches.is_present("cluster") {
Some(derive_cluster(matches.value_of("cluster").unwrap()).ok().unwrap())
Expand All @@ -201,7 +223,8 @@ USE OF THIS BINARY FALLS UNDER THE MIT LICENSE (c) 2017").
adjacent.map(str::to_string),
chunk.map(str::to_string),
cluster_step,
reporter_handler
reporter_handler,
reporter
);

let mtchs = matches.clone();
Expand All @@ -214,7 +237,7 @@ USE OF THIS BINARY FALLS UNDER THE MIT LICENSE (c) 2017").
return aescrypt_core_loop(work_load);
});

let _a = web_runner.join().unwrap();
let _ = web_runner.join().unwrap();
crypt_runner.join().unwrap()
}

Expand Down
9 changes: 9 additions & 0 deletions src/process_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

use digits::BaseCustom;
use super::result::Error;
use reporter::CliReporter;

pub fn verify_reporter_name(rn: String) -> CliReporter {
match &rn[..] {
"spinner" => CliReporter::Spinner,
"ticker" => CliReporter::TickerTape,
_ => CliReporter::TickerTape,
}
}

pub fn derive_min_max(range: &str) -> Result<(usize, usize), Error> {
let rvals = range.split(':').collect::<Vec<&str>>();
Expand Down
64 changes: 10 additions & 54 deletions src/reporter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,19 @@
use std::time::SystemTime;
extern crate serde;
use self::serde::ser::{Serialize, Serializer, SerializeStruct};
extern crate serde_json;
use std::sync::{Arc, Mutex};
pub mod ticker_tape;
pub mod spinner;
use ::ITERATIONS;
use std::sync::atomic::Ordering;
use ::Digits;
use std::ops::Deref;

pub mod prelude {
pub use reporter::ticker_tape::*;
pub use reporter::spinner::*;
pub use reporter::Reporter;
#[derive(Debug)]
pub enum CliReporter {
TickerTape,
Spinner,
}

#[derive(Clone)]
pub struct ReportData {
pub cores: u8,
pub chunk: usize,
pub cluster: Option<(usize,usize)>,
pub character_set: String,
pub start_time: SystemTime,
pub start_at: String,
pub adjacent_limit: Option<u8>,
pub five_min_progress: Arc<Mutex<(usize, String)>>, // Mutex try_lock
}

impl Serialize for ReportData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {

let mut struct_fields = 8;
if self.cluster.is_some() { struct_fields += 2; }
if self.adjacent_limit.is_some() { struct_fields += 1; }

let mut state = serializer.serialize_struct("ReportData", struct_fields)?;
state.serialize_field("cores", &self.cores)?;
state.serialize_field("chunk", &self.chunk)?;
if let Some((node, cluster_size)) = self.cluster {
state.serialize_field("cluster_node", &node)?;
state.serialize_field("cluster_size", &cluster_size)?;
impl CliReporter {
#[inline]
pub fn report(&self, data: &Digits) {
match *self {
ref _thingy @ CliReporter::TickerTape => ticker_tape::report(data),
ref _thingy @ CliReporter::Spinner => spinner::report(data),
}
state.serialize_field("character_set", &self.character_set)?;
state.serialize_field("start_time", &self.start_time)?;
state.serialize_field("start_at", &self.start_at)?;
if let Some(adj) = self.adjacent_limit {
state.serialize_field("adjacent_limit", &adj)?;
}
state.serialize_field("iterations", &ITERATIONS.load(Ordering::SeqCst))?;
let getter = self.five_min_progress.lock().unwrap();
let &(five_min_iters, ref last_string) = getter.deref();
state.serialize_field("last_five_minute_iterations", &five_min_iters)?;
state.serialize_field("last_attempt", &last_string)?;
state.end()
}
}

pub trait Reporter {
fn report(data: &Digits);
}
18 changes: 10 additions & 8 deletions src/reporter/spinner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use ::reporter::Reporter;
use ::{Digits,ITERATIONS};
use std::sync::atomic::Ordering;
use std::io::{self, Write};

const SPINNER: [char; 10] = ['⠋','⠙','⠹','⠸','⠼','⠴','⠦','⠧','⠇','⠏'];

struct Spinner;

impl Reporter for Spinner {
fn report(_data: &Digits) {
print!(":");
io::stdout().flush().unwrap();
}
pub fn report(data: &Digits) {
let global_iterations = ITERATIONS.load(Ordering::SeqCst);
print!(
"\x1b[1000D {} Iterations: {} String: {}",
SPINNER[global_iterations % 10],
global_iterations,
data.to_s()
);
io::stdout().flush().unwrap();
}
10 changes: 3 additions & 7 deletions src/reporter/ticker_tape.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::io::{self, Write};
use ::reporter::Reporter;
use ::Digits;
pub struct TickerTape;

impl Reporter for TickerTape {
fn report(data: &Digits) {
print!("{}..", data.to_s());
io::stdout().flush().unwrap();
}
pub fn report(data: &Digits) {
print!("{}..", data.to_s());
io::stdout().flush().unwrap();
}
8 changes: 4 additions & 4 deletions src/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn input_and_output_for_resume_works() {
let k = keys.rkeys.iter().next();
assert_eq!(Some(&r), k);

let _a = fs::remove_file(".example.res");
let _ = fs::remove_file(".example.res");
assert!(!Path::new(".example.res").exists(), "`.example.res` cleanup failed!");
}

Expand All @@ -213,7 +213,7 @@ impl ResumeFile {

if let Ok(mut f) = file {
f.write_all(stringify.as_bytes()).ok();
let _a = f.sync_data();
let _ = f.sync_data();
}

ResumeFile::backup()
Expand Down Expand Up @@ -243,8 +243,8 @@ impl ResumeFile {
}

pub(crate) fn purge() {
let _a = fs::remove_file(".abrute");
let _b = fs::remove_file(".abrute.bak");
let _ = fs::remove_file(".abrute");
let _ = fs::remove_file(".abrute.bak");
}
}

Expand Down
Loading

0 comments on commit 924be44

Please sign in to comment.