aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-06-02 04:05:45 +0000
committerFuwn <[email protected]>2021-06-02 04:05:45 +0000
commite4a3d642a64b0c15d127f040419d805e2f0dff34 (patch)
treee00cbfea71068a113fe8aaca672d8cd4fa133b15 /src
parentrefactor(clippy): strictor clippy constraints (diff)
downloadnitrous-e4a3d642a64b0c15d127f040419d805e2f0dff34.tar.xz
nitrous-e4a3d642a64b0c15d127f040419d805e2f0dff34.zip
feat(check): proxy support
Diffstat (limited to 'src')
-rw-r--r--src/cli.rs55
-rw-r--r--src/nitrous.rs63
2 files changed, 100 insertions, 18 deletions
diff --git a/src/cli.rs b/src/cli.rs
index d3525d5..f5cc080 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,6 +1,8 @@
// Copyleft (ɔ) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only
+use std::str::FromStr;
+
use structopt::{
clap,
clap::{App, Arg, SubCommand},
@@ -8,6 +10,26 @@ use structopt::{
use crate::nitrous::Nitrous;
+pub enum ProxyType {
+ Http,
+ Socks4,
+ Socks5,
+ Tor,
+}
+impl FromStr for ProxyType {
+ type Err = &'static str;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "http" => Ok(Self::Http),
+ "socks4" => Ok(Self::Socks4),
+ "socks5" => Ok(Self::Socks5),
+ "tor" => Ok(Self::Tor),
+ _ => Err("no match"),
+ }
+ }
+}
+
pub struct Cli;
impl Cli {
pub async fn execute() {
@@ -43,6 +65,19 @@ impl Cli {
}
},
debug,
+ ProxyType::from_str(
+ matches
+ .subcommand_matches("check")
+ .unwrap()
+ .value_of("proxy_type")
+ .unwrap(),
+ )
+ .unwrap(),
+ matches
+ .subcommand_matches("check")
+ .unwrap()
+ .value_of("proxy_list")
+ .unwrap_or("null"),
)
.await;
}
@@ -76,8 +111,24 @@ impl Cli {
Arg::with_name("file")
.required(false)
.takes_value(true)
- .index(1),
- ),
+ .long("file")
+ .short("f"),
+ )
+ .args(&[
+ Arg::with_name("proxy_type")
+ .required(true)
+ .takes_value(true)
+ .index(1)
+ .possible_values(&["http", "socks4", "socks5", "tor"]),
+ Arg::with_name("proxy_list")
+ .required_ifs(&[
+ ("proxy_type", "http"),
+ ("proxy_type", "socks4"),
+ ("proxy_type", "socks5"),
+ ])
+ .takes_value(true)
+ .index(2),
+ ]),
])
.arg(
Arg::with_name("debug")
diff --git a/src/nitrous.rs b/src/nitrous.rs
index 9459eb4..3c2be32 100644
--- a/src/nitrous.rs
+++ b/src/nitrous.rs
@@ -3,10 +3,12 @@
use std::{
fs::{create_dir, File},
- io::{BufRead, Write},
+ io::{BufRead, BufReader, Write},
};
-use rand::Rng;
+use rand::{seq::IteratorRandom, Rng};
+
+use crate::cli::ProxyType;
pub struct Nitrous;
impl Nitrous {
@@ -22,7 +24,8 @@ impl Nitrous {
crate::cli::Cli::execute().await;
}
- fn initialize() { create_dir("nitrous").unwrap(); }
+ #[allow(clippy::let_underscore_drop)]
+ fn initialize() { let _ = create_dir("nitrous"); }
pub fn generate(amount: usize, debug: bool) {
Self::initialize();
@@ -43,35 +46,63 @@ impl Nitrous {
}
}
- pub async fn check(codes_file_name: &str, debug: bool) {
+ pub async fn check(
+ codes_file_name: &str,
+ debug: bool,
+ proxy_type: crate::cli::ProxyType,
+ proxy_file: &str,
+ ) {
Self::initialize();
- create_dir("nitrous/check/").unwrap();
+ #[allow(clippy::let_underscore_drop)]
+ 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 proxy_addr = if matches!(&proxy_type, ProxyType::Tor) {
+ "127.0.0.1:9050".to_string()
+ } else {
+ BufReader::new(
+ File::open(proxy_file).unwrap_or_else(|e| panic!("unable to open file: {}", e)),
+ )
+ .lines()
+ .map(|l| l.expect("couldn't read line"))
+ .choose(&mut rand::thread_rng())
+ .expect("file had no 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();
+ let status = reqwest::Client::builder()
+ .proxy(reqwest::Proxy::all(format!("{}://{}", {
+ match proxy_type {
+ ProxyType::Http => "http",
+ ProxyType::Socks4 => "socks4h",
+ ProxyType::Socks5 | ProxyType::Tor => "socks5h",
+ }
+ }, proxy_addr)).unwrap())
+ .build()
+ .unwrap()
+ .get(
+ format!("https://discordapp.com/api/v6/entitlements/gift-codes/{}?with_application=false&\
+ with_subscription_plan=true", code),
+ )
+ .send()
+ .await
+ .unwrap()
+ .status()
+ .as_u16();
if status == 200 {
writeln!(valid, "{}", code).unwrap();
if debug {
- info!("{}", code);
+ info!("{}: {}", proxy_addr, code);
}
} else {
writeln!(invalid, "{}", code).unwrap();
if debug {
- error!("{}", code);
+ error!("{}: {}", proxy_addr, code);
}
}
}