#[macro_use]
extern crate itertools;
#[macro_use]
extern crate lazy_static;
extern crate reqwest;
extern crate termcolor;
#[macro_use]
extern crate version;

use std::env;
use termcolor::{Color, ColorSpec};

mod constant;
mod term;
mod vulnerable;

fn main() {
    welcome();
    run();
}

fn run() {
    let term = term::TERM.clone();
    let mut nargs = 0;
    term.set_default_color(Some(Some(Color::Blue)), None, Some(true));
    for url in env::args() {
        nargs += 1;
        if nargs == 1 {
            continue;
        }
        match vulnerable::test(&url) {
            Ok(Some(url)) => {
                term.out(url);
                term.cout(" Vulnerable\n", &term::VULNER_SPEC);
            }
            Ok(None) => {
                term.out(format!("{} ", &url));
                term.cout("Not Vulnerable\n", &term::NO_VULNER_SPEC);
            }
            Err(e) => {
                term.out(format!("{} ", &url));
                term.cout(format!("{}\n", e), &term::ERR_SPEC);
            }
        };
    }
    term.set_default_color(Some(None), Some(None), Some(false));
    if nargs <= 1 {
        let mut spec = ColorSpec::new();
        spec.set_fg(Some(Color::Blue)).set_bold(true);
        term.cout("USAGE: ", &spec);
        term.out(constant::USAGE);
        term.cout("\n\nEXAMPLE:", &spec);
        term.out(constant::EXAMPLE);
    } else {
        term.out("\n")
    }
}

fn welcome() {
    let term = term::TERM.clone();
    term.set_default_color(None, None, Some(true));
    term.out(format!(
        "Thank you to use CVE-2017-10271 POC {}!\nTo help author, Donate ",
        version!()
    ));
    cout_donate();
    term.set_default_color(None, None, Some(false));
    term.out("\n\n");
}

fn cout_donate() {
    let term = term::TERM.clone();
    let mut spec = ColorSpec::new();
    spec.set_fg(Some(Color::Red))
        .set_bg(Some(Color::Yellow))
        .set_bold(true);
    term.cout(constant::DONATE, &spec);
}
