2
\$\begingroup\$

I have created a rust program to create https://mail.tm accounts, but I think there are things I could have done better. Are there things I can do to improve performance? Are there any unnecessary things I have done? I am new to rust, please give examples of what you mean when you suggest improvements :) Thank you.

use json;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use reqwest;
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
use std::thread;
use std::time::Duration;
use std::{collections::HashMap, io, iter, vec::Vec};

fn gen_address() -> Vec<String> {
    let mut rng = thread_rng();
    let address: String = iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(10)
        .collect();

    let password: String = iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(5)
        .collect();
    let body = reqwest::blocking::get("https://api.mail.tm/domains")
        .unwrap()
        .text()
        .unwrap();
    let domains = json::parse(&body).expect("Failed to parse domain json.");

    let domain = domains["hydra:member"][0]["domain"].to_string();
    let email = format!("{}@{}", &address, &domain);
    vec![email, password]
}

fn gen_email() -> Vec<String> {
    let client = reqwest::blocking::Client::new();
    let address_info = gen_address();
    let address = &address_info[0];
    let password = &address_info[1];

    let mut data = HashMap::new();
    data.insert("address", &address);
    data.insert("password", &password);

    let mut headers = HeaderMap::new();
    headers.insert(ACCEPT, HeaderValue::from_static("application/ld+json"));
    headers.insert(
        CONTENT_TYPE,
        HeaderValue::from_static("application/ld+json"),
    );

    let res = client
        .post("https://api.mail.tm/accounts")
        .headers(headers)
        .json(&data)
        .send()
        .expect("Error while attempting to create acconut.");

    vec![
        res.status().to_string(),
        address.to_string(),
        password.to_string(),
    ]
}

fn main() {
    fn get_amount() -> i32 {
        let mut amount = String::new();

        loop {
            println!("How many emails do you want?");
            io::stdin()
                .read_line(&mut amount)
                .expect("Failed to read line.");

            let _amount: i32 = match amount.trim().parse() {
                Ok(num) => return num,
                Err(_) => {
                    println!("Please enter a number.");
                    continue;
                }
            };
        }
    }

    let amount = get_amount();
    let handle = thread::spawn(move || {
        let mut handles = vec![];
        for _gen in 0..amount {
            thread::sleep(Duration::from_secs_f32(0.5));
            let handle = thread::spawn(|| {
                let maildata = gen_email();
                println!(
                    "Status: {}, Address: {}, Password: {}",
                    maildata[0], maildata[1], maildata[2]
                );
            });
            handles.push(handle);
        }
        handles.into_iter().for_each(|h| h.join().unwrap());
    });
    handle.join().unwrap();
}
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.