Skip to content

Commit

Permalink
More verbosity about which token will receive Nitro and safer main to…
Browse files Browse the repository at this point in the history
…ken handling
  • Loading branch information
melnary committed Aug 6, 2020
1 parent 2a5e439 commit ffbc7ad
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-nitro-sniper"
version = "2.0.0"
version = "2.1.0"
authors = ["Mel"]
edition = "2018"

Expand Down
84 changes: 81 additions & 3 deletions src/handler.rs → src/discord.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{pretty_error, pretty_info, pretty_warn};
use crate::{log_error_and_exit, pretty_error, pretty_info, pretty_warn};
use colored::*;
use hyper::client::HttpConnector;
use hyper::{Body, Client, Method, Request, StatusCode};
Expand All @@ -8,10 +8,13 @@ use serenity::async_trait;
use serenity::model::channel::Message;
use serenity::model::gateway::Ready;
use serenity::prelude::{Context, EventHandler};
use std::fmt;

type HttpsClient = Client<HttpsConnector<HttpConnector>>;

#[derive(Clone)]
pub struct Handler {
pub client: Client<HttpsConnector<HttpConnector>>,
pub client: HttpsClient,
pub main_token: String,
}

Expand All @@ -24,7 +27,7 @@ impl Handler {
gift_token
))
.header("Content-Type", "application/json")
.header("Authorization", self.main_token.clone())
.header("Authorization", &self.main_token)
.body(Body::from(format!("{{\"channel_id\":{}}}", channel_id)))
.unwrap();

Expand Down Expand Up @@ -78,3 +81,78 @@ impl EventHandler for Handler {
);
}
}

#[derive(Deserialize, Clone)]
pub struct Profile {
username: String,
discriminator: String,
}

impl fmt::Display for Profile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}#{}", self.username, self.discriminator)
}
}

pub enum ProfileError {
Unauthorized,
RateLimited,
ConnectionError,
Other,
}

impl ProfileError {
pub fn handle(&self) {
match self {
ProfileError::Unauthorized => {
log_error_and_exit!(
"┐(¯ω¯;)┌",
"I couldn't verify your main token. Is it correct?"
);
}
ProfileError::RateLimited => {
log_error_and_exit!("(x_x)", "Your're rate-limited. Try again later...");
}
ProfileError::ConnectionError => {
log_error_and_exit!("┐(¯ω¯;)┌", "Requesting failed. Check your connection!");
}
ProfileError::Other => {
log_error_and_exit!("┐(¯ω¯;)┌", "Received unknown response for Discord...");
}
}
}
}

pub async fn get_profile_for_token(
token: &str,
client: &HttpsClient,
) -> Result<Profile, ProfileError> {
let request = Request::builder()
.method(Method::GET)
.uri("https://discordapp.com/api/v6/users/@me")
.header("Authorization", token)
.body(Body::empty())
.unwrap();

let response_result = client.request(request).await;

if let Ok(response) = response_result {
match response.status() {
StatusCode::OK => {
let streamed_bytes = hyper::body::to_bytes(response.into_body()).await;
if let Ok(bytes) = streamed_bytes {
let body = String::from_utf8(bytes.to_vec()).expect("Received bad stream.");
let profile = serde_json::from_str(&body).expect("Malformed response.");
Ok(profile)
} else {
Err(ProfileError::Other)
}
}
StatusCode::UNAUTHORIZED => Err(ProfileError::Unauthorized),
StatusCode::TOO_MANY_REQUESTS => Err(ProfileError::RateLimited),
_ => Err(ProfileError::Other),
}
} else {
Err(ProfileError::ConnectionError)
}
}
4 changes: 2 additions & 2 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ pub fn set_up_logger() -> Result<(), SetLoggerError> {

pub fn pause_exit() {
let mut stdout = stdout();
stdout.write(b"Press the enter key to exit...").unwrap();
stdout.write_all(b"Press the enter key to exit...").unwrap();
stdout.flush().unwrap();
stdin().read(&mut [0]).unwrap();
stdin().read_exact(&mut [0]).unwrap();
std::process::exit(1);
}

Expand Down
33 changes: 24 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod config;
mod handler;
mod discord;
#[macro_use]
mod logging;

Expand All @@ -21,7 +21,6 @@ extern crate serde_json;
extern crate serenity;
extern crate tokio;

use crate::handler::Handler;
use colored::*;
use hyper::{Body, Client};
use hyper_tls::HttpsConnector;
Expand All @@ -31,33 +30,48 @@ use serenity::Client as DiscordClient;
async fn main() {
logging::set_up_logger().expect("(o_O) Failed setting up logger. (HOW?)");

let https = HttpsConnector::new();
let client = Client::builder().build::<_, Body>(https);

let config = config::try_read_config().map_err(|e| e.handle()).unwrap();
let main_token = config.main_token();
let main_profile = discord::get_profile_for_token(&main_token, &client)
.await
.map_err(|e| e.handle())
.unwrap();

let https = HttpsConnector::new();
let client = Client::builder().build::<_, Body>(https);
pretty_info!(
"o(»ω«)o",
"If we're lucky you'll get Nitro on {}!",
main_profile
);

let sniping_tokens = config.get_all_sniping_tokens();
let mut sniping_tokens = config.get_all_sniping_tokens();
sniping_tokens.sort();
sniping_tokens.dedup();

if sniping_tokens.is_empty() {
log_error_and_exit!("┐(¯ω¯;)┌", "I need at least one token to snipe with...");
log_error_and_exit!(
"┐(¯ω¯;)┌",
"...but I need at least one token to snipe with..."
);
}

pretty_info!(
"(o·ω·o)",
"Connecting to {} account(s).",
"I'll be sniping on {} account(s)!",
sniping_tokens.len()
);

let handler = Handler {
let handler = discord::Handler {
client,
main_token: main_token.clone(),
};

let mut tasks = Vec::new();

for (index, token) in sniping_tokens.iter().enumerate() {
let discord_client_result = DiscordClient::new(&token)
let discord_client_result = DiscordClient::new(token)
.event_handler(handler.clone())
.await;

Expand All @@ -82,4 +96,5 @@ async fn main() {
}

futures::future::join_all(tasks).await;
log_error_and_exit!("(x_x)", "Lost all connections.");
}

0 comments on commit ffbc7ad

Please sign in to comment.