add Confirmation struct, and improve confirmation parsing

This commit is contained in:
Carson McManus 2021-07-27 23:49:53 -04:00
parent d2bf5478ba
commit c23db2a5e9
5 changed files with 61 additions and 9 deletions

7
Cargo.lock generated
View file

@ -24,6 +24,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "anyhow"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486"
[[package]] [[package]]
name = "async-compression" name = "async-compression"
version = "0.3.7" version = "0.3.7"
@ -1285,6 +1291,7 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
name = "steamguard-cli" name = "steamguard-cli"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"anyhow",
"base64", "base64",
"clap", "clap",
"cookie", "cookie",

View file

@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "^1.0"
hmac-sha1 = "^0.1" hmac-sha1 = "^0.1"
base64 = "0.13.0" base64 = "0.13.0"
text_io = "0.1.8" text_io = "0.1.8"

33
src/confirmation.rs Normal file
View file

@ -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,
}
}
}

View file

@ -1,4 +1,5 @@
use std::{collections::HashMap, convert::TryInto, thread, time}; use std::{collections::HashMap, convert::TryInto, thread, time};
use confirmation::{Confirmation, ConfirmationType};
use hmacsha1::hmac_sha1; use hmacsha1::hmac_sha1;
use regex::Regex; use regex::Regex;
use reqwest::{Url, cookie::CookieStore, header::{COOKIE, USER_AGENT}}; use reqwest::{Url, cookie::CookieStore, header::{COOKIE, USER_AGENT}};
@ -6,8 +7,11 @@ use serde::{Serialize, Deserialize};
use log::*; use log::*;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use]
extern crate anyhow;
pub mod steamapi; pub mod steamapi;
mod confirmation;
// const STEAMAPI_BASE: String = "https://api.steampowered.com"; // const STEAMAPI_BASE: String = "https://api.steampowered.com";
// const COMMUNITY_BASE: String = "https://steamcommunity.com"; // const COMMUNITY_BASE: String = "https://steamcommunity.com";
@ -130,7 +134,7 @@ impl SteamGuardAccount {
return params; return params;
} }
pub fn get_trade_confirmations(&self) -> Result<Vec<String>, reqwest::Error> { pub fn get_trade_confirmations(&self) -> Result<Vec<Confirmation>, anyhow::Error> {
// uri: "https://steamcommunity.com/mobileconf/conf" // uri: "https://steamcommunity.com/mobileconf/conf"
// confirmation details: // confirmation details:
let url = "https://steamcommunity.com".parse::<Url>().unwrap(); let url = "https://steamcommunity.com".parse::<Url>().unwrap();
@ -171,12 +175,19 @@ impl SteamGuardAccount {
// <div>Nothing to confirm</div> // <div>Nothing to confirm</div>
match CONFIRMATION_REGEX.captures(text.as_str()) { match CONFIRMATION_REGEX.captures(text.as_str()) {
Some(caps) => { Some(caps) => {
let conf_id = &caps[1]; let conf_id = caps[1].parse()?;
let conf_key = &caps[2]; let conf_key = caps[2].parse()?;
let conf_type = &caps[3]; let conf_type = caps[3].try_into().unwrap_or(ConfirmationType::Unknown);
let conf_creator = &caps[4]; let conf_creator = caps[4].parse()?;
debug!("conf_id={} conf_key={} conf_type={} conf_creator={}", conf_id, conf_key, conf_type, conf_creator); 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)]); 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"); info!("No confirmations");
@ -186,7 +197,7 @@ impl SteamGuardAccount {
} }
Err(e) => { Err(e) => {
error!("error: {:?}", e); error!("error: {:?}", e);
return Err(e); bail!(e);
} }
} }
} }

View file

@ -134,7 +134,7 @@ fn main() {
match account.get_trade_confirmations() { match account.get_trade_confirmations() {
Ok(confs) => { Ok(confs) => {
for conf in confs { for conf in confs {
println!("{}", conf); println!("{:?}", conf);
} }
break; break;
} }