From c23db2a5e917d94ee9ae99f7ac1a84911fcff574 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Tue, 27 Jul 2021 23:49:53 -0400 Subject: [PATCH] add Confirmation struct, and improve confirmation parsing --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/confirmation.rs | 33 +++++++++++++++++++++++++++++++++ src/lib.rs | 27 +++++++++++++++++++-------- src/main.rs | 2 +- 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 src/confirmation.rs diff --git a/Cargo.lock b/Cargo.lock index e9c42dc..25b0ac6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "anyhow" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" + [[package]] name = "async-compression" version = "0.3.7" @@ -1285,6 +1291,7 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0" name = "steamguard-cli" version = "0.2.0" dependencies = [ + "anyhow", "base64", "clap", "cookie", diff --git a/Cargo.toml b/Cargo.toml index e0466f9..ea115bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "^1.0" hmac-sha1 = "^0.1" base64 = "0.13.0" text_io = "0.1.8" diff --git a/src/confirmation.rs b/src/confirmation.rs new file mode 100644 index 0000000..b5570a1 --- /dev/null +++ b/src/confirmation.rs @@ -0,0 +1,33 @@ +/// A mobile confirmation. There are multiple things that can be confirmed, like trade offers. +#[derive(Debug, Clone)] +pub struct Confirmation { + pub id: u64, + pub key: u64, + /// Comes from the `data-type` attribute in the HTML + pub int_type: i32, + /// Trade offer ID or market transaction ID + pub creator: u64, + pub conf_type: ConfirmationType, + pub description: String, +} + +#[derive(Debug, Clone, Copy)] +pub enum ConfirmationType { + Generic = 1, + Trade = 2, + MarketSell = 3, + AccountRecovery = 6, + Unknown +} + +impl From<&str> for ConfirmationType { + fn from(text: &str) -> Self { + match text { + "1" => ConfirmationType::Generic, + "2" => ConfirmationType::Trade, + "3" => ConfirmationType::MarketSell, + "6" => ConfirmationType::AccountRecovery, + _ => ConfirmationType::Unknown, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index e9dd82e..68daf53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ use std::{collections::HashMap, convert::TryInto, thread, time}; +use confirmation::{Confirmation, ConfirmationType}; use hmacsha1::hmac_sha1; use regex::Regex; use reqwest::{Url, cookie::CookieStore, header::{COOKIE, USER_AGENT}}; @@ -6,8 +7,11 @@ use serde::{Serialize, Deserialize}; use log::*; #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate anyhow; pub mod steamapi; +mod confirmation; // const STEAMAPI_BASE: String = "https://api.steampowered.com"; // const COMMUNITY_BASE: String = "https://steamcommunity.com"; @@ -130,7 +134,7 @@ impl SteamGuardAccount { return params; } - pub fn get_trade_confirmations(&self) -> Result, reqwest::Error> { + pub fn get_trade_confirmations(&self) -> Result, anyhow::Error> { // uri: "https://steamcommunity.com/mobileconf/conf" // confirmation details: let url = "https://steamcommunity.com".parse::().unwrap(); @@ -171,12 +175,19 @@ impl SteamGuardAccount { //
Nothing to confirm
match CONFIRMATION_REGEX.captures(text.as_str()) { Some(caps) => { - let conf_id = &caps[1]; - let conf_key = &caps[2]; - let conf_type = &caps[3]; - let conf_creator = &caps[4]; - debug!("conf_id={} conf_key={} conf_type={} conf_creator={}", conf_id, conf_key, conf_type, conf_creator); - return Ok(vec![format!("conf_id={} conf_key={} conf_type={} conf_creator={}", conf_id, conf_key, conf_type, conf_creator)]); + let conf_id = caps[1].parse()?; + let conf_key = caps[2].parse()?; + let conf_type = caps[3].try_into().unwrap_or(ConfirmationType::Unknown); + let conf_creator = caps[4].parse()?; + debug!("conf_id={} conf_key={} conf_type={:?} conf_creator={}", conf_id, conf_key, conf_type, conf_creator); + return Ok(vec![Confirmation { + id: conf_id, + key: conf_key, + conf_type: conf_type, + creator: conf_creator, + int_type: 0, + description: "".into(), + }]); } _ => { info!("No confirmations"); @@ -186,7 +197,7 @@ impl SteamGuardAccount { } Err(e) => { error!("error: {:?}", e); - return Err(e); + bail!(e); } } } diff --git a/src/main.rs b/src/main.rs index 04a925a..2ea030f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,7 +134,7 @@ fn main() { match account.get_trade_confirmations() { Ok(confs) => { for conf in confs { - println!("{}", conf); + println!("{:?}", conf); } break; }