diff options
| author | Fuwn <[email protected]> | 2021-05-20 01:58:02 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-20 01:58:02 +0000 |
| commit | 1bd7e65a94289e444f067f4da28935df0baca9be (patch) | |
| tree | 9d54c9e91ac6a79f060ad069b9c1de34c34a844f /src | |
| download | nitrous-1bd7e65a94289e444f067f4da28935df0baca9be.tar.xz nitrous-1bd7e65a94289e444f067f4da28935df0baca9be.zip | |
feat(nitrous): :star:
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 60 | ||||
| -rw-r--r-- | src/main.rs | 24 | ||||
| -rw-r--r-- | src/nitrous.rs | 72 |
3 files changed, 156 insertions, 0 deletions
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..fe43439 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,60 @@ +// Copyleft (ɔ) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use structopt::{ + clap, + clap::{App, Arg, SubCommand}, +}; + +use crate::nitrous::Nitrous; + +pub struct Cli; +impl Cli { + pub async fn execute() { + let matches = Self::cli().get_matches(); + + if matches.is_present("generate") { + Nitrous::generate( + matches + .subcommand_matches("generate") + .unwrap() + .value_of("amount") + .unwrap() + .to_string() + .parse::<usize>() + .unwrap(), + ); + } else if matches.is_present("check") { + Nitrous::check( + matches + .subcommand_matches("check") + .unwrap() + .value_of("file") + .unwrap(), + ) + .await; + } + } + + fn cli() -> App<'static, 'static> { + App::new(env!("CARGO_PKG_NAME")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommands(vec![ + SubCommand::with_name("generate").alias("gen").arg( + Arg::with_name("amount") + .required(true) + .index(1) + .takes_value(true), + ), + SubCommand::with_name("check").arg( + Arg::with_name("file") + .required(true) + .takes_value(true) + .index(1), + ), + ]) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..37c0fcc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,24 @@ +// Copyleft (ɔ) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +#![warn(rust_2018_idioms)] +#![recursion_limit = "128"] + +#[macro_use] +extern crate log; + +#[cfg(target_family = "windows")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(target_family = "unix")] +#[global_allocator] +static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; + +mod cli; +mod nitrous; + +use crate::nitrous::Nitrous; + +#[tokio::main] +async fn main() { Nitrous::execute().await; } diff --git a/src/nitrous.rs b/src/nitrous.rs new file mode 100644 index 0000000..429cd2e --- /dev/null +++ b/src/nitrous.rs @@ -0,0 +1,72 @@ +// Copyleft (ɔ) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +use std::{ + fs::{create_dir, File}, + io::{BufRead, Write}, +}; + +use rand::Rng; + +pub struct Nitrous; +impl Nitrous { + pub async fn execute() { + // Environment + dotenv::dotenv().ok(); + std::env::set_var("RUST_LOG", "nitrous=trace"); + + // Logging + pretty_env_logger::init(); + + crate::cli::Cli::execute().await; + } + + fn initialize() { let _ = create_dir("nitrous"); } + + pub fn generate(amount: usize) { + Self::initialize(); + + let mut codes = File::create("nitrous/codes.txt").unwrap(); + + for _ in 0..amount { + let code = rand::thread_rng() + .sample_iter(rand::distributions::Alphanumeric) + .take(16) + .map(char::from) + .collect::<String>(); + + writeln!(codes, "{}", code).unwrap(); + info!("{}", code,) + } + } + + pub async fn check(codes_file_name: &str) { + Self::initialize(); + + let _ = create_dir("nitrous/check/"); + let codes = File::open(codes_file_name).unwrap(); + let mut invalid = File::create("nitrous/check/invalid.txt").unwrap(); + let mut valid = File::create("nitrous/check/valid.txt").unwrap(); + + for code in std::io::BufReader::new(codes).lines() { + let code = code.unwrap(); + let status = reqwest::get(format!( + "https://discordapp.com/api/v6/entitlements/gift-codes/{}?with_applica\ + tion=false&with_subscription_plan=true", + code + )) + .await + .unwrap() + .status() + .as_u16(); + + if status == 200 { + writeln!(valid, "{}", code).unwrap(); + info!("{}", code); + } else { + writeln!(invalid, "{}", code).unwrap(); + error!("{}", code); + } + } + } +} |