Skip to content

Commit

Permalink
wip: message passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ps1dr3x committed May 18, 2019
1 parent ad849f4 commit 78bab53
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
30 changes: 24 additions & 6 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
use hyper::rt::{self, lazy, Future};
use hyper::{Client, StatusCode};
use hyper::{
Client, StatusCode,
rt::{self, Future}
};
use futures::Stream;
use std::sync::mpsc::Sender;

#[derive(Debug)]
pub enum FetcherMessage {
Response(String),
Log(String)
}

fn _fetch_url(
tx: Sender<FetcherMessage>,
client: &hyper::Client<hyper::client::HttpConnector>,
url: hyper::Uri,
) -> impl Future<Item = (), Error = ()> {
client
// Fetch the url...
.get(url)
// And then, if we get a response back...
.and_then(|res| {
.and_then(move |res| {
if res.status() == StatusCode::OK {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());

tx.send(
FetcherMessage::Log(String::from("Status != 200"))
).unwrap();
}

tx.send(
FetcherMessage::Response(String::from("Response received!"))
).unwrap();

Ok(())
})
// If there was an error, let the user know...
Expand All @@ -24,13 +42,13 @@ fn _fetch_url(
})
}

pub fn _run(urls: Vec<hyper::Uri>) {
pub fn _run(tx: Sender<FetcherMessage>, urls: Vec<hyper::Uri>) {
let client = Client::new();

let stream = futures::stream::iter_ok(urls)
.map(move |url| _fetch_url(&client, url))
.map(move |url| _fetch_url(tx.clone(), &client, url))
.buffer_unordered(1)
.for_each(|_| Ok(()))
.for_each(Ok)
.map_err(|_| eprintln!("Err"));

rt::run(stream);
Expand Down
39 changes: 30 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate clap;
extern crate hyper;
extern crate pretty_env_logger;

#[macro_use]
extern crate log;
#[macro_use] extern crate log;

use clap::{App, Arg};
use std::fs;
use std::str;

use std::{
fs, str, thread,
sync::mpsc::channel
};

use crate::fetcher::FetcherMessage;

mod fetcher;

Expand Down Expand Up @@ -96,7 +96,28 @@ fn main() {
debug!("using mode: dir");
let urls = load_wordlist_and_build_urls(wordlist_path, url, extensions);
debug!("urls: {:#?}", urls);
fetcher::_run(urls);

let (tx, rx) = channel();

thread::spawn(move || {
fetcher::_run(tx, urls);
});

loop {
let msg = match rx.recv() {
Ok(msg) => msg,
Err(_err) => continue
};

match msg {
FetcherMessage::Response(res) => {
println!("{:?}", res);
}
FetcherMessage::Log(log) => {
println!("{:?}", log);
}
}
}
}
_ => (),
}
Expand Down

0 comments on commit 78bab53

Please sign in to comment.