change accounts to type Vec<Arc<Mutex<SteamGuardAccount>>> to make sure state stays in one place in memory

This commit is contained in:
Carson McManus 2021-08-01 12:34:13 -04:00
parent 31cc0f884e
commit b5ed878659
2 changed files with 15 additions and 13 deletions

View file

@ -1,8 +1,9 @@
use log::*; use log::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{BufReader, Write}; use std::io::{BufReader, Write};
use std::path::Path; use std::path::Path;
use std::sync::{Arc, Mutex};
use std::{cell::Cell, fs::File};
use steamguard::SteamGuardAccount; use steamguard::SteamGuardAccount;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -17,7 +18,7 @@ pub struct Manifest {
pub auto_confirm_trades: bool, pub auto_confirm_trades: bool,
#[serde(skip)] #[serde(skip)]
pub accounts: Vec<SteamGuardAccount>, pub accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
#[serde(skip)] #[serde(skip)]
folder: String, // I wanted to use a Path here, but it was too hard to make it work... folder: String, // I wanted to use a Path here, but it was too hard to make it work...
} }
@ -48,21 +49,21 @@ impl Manifest {
let file = File::open(path)?; let file = File::open(path)?;
let reader = BufReader::new(file); let reader = BufReader::new(file);
let account: SteamGuardAccount = serde_json::from_reader(reader)?; let account: SteamGuardAccount = serde_json::from_reader(reader)?;
self.accounts.push(account); self.accounts.push(Arc::new(Mutex::new(account)));
} }
Ok(()) Ok(())
} }
pub fn add_account(&mut self, account: &SteamGuardAccount) { pub fn add_account(&mut self, account: SteamGuardAccount) {
debug!("adding account to manifest: {}", account.account_name); debug!("adding account to manifest: {}", account.account_name);
let steamid = account.session.clone().unwrap().steam_id; let steamid = account.session.clone().unwrap().steam_id;
self.accounts.push(account.clone());
self.entries.push(ManifestEntry { self.entries.push(ManifestEntry {
filename: format!("{}.maFile", account.account_name), filename: format!("{}.maFile", &account.account_name),
steam_id: steamid, steam_id: steamid,
encryption_iv: None, encryption_iv: None,
encryption_salt: None, encryption_salt: None,
}); });
self.accounts.push(Arc::new(Mutex::new(account)));
} }
pub fn save(&self) -> anyhow::Result<()> { pub fn save(&self) -> anyhow::Result<()> {
@ -72,7 +73,7 @@ impl Manifest {
); );
for (entry, account) in self.entries.iter().zip(&self.accounts) { for (entry, account) in self.entries.iter().zip(&self.accounts) {
debug!("saving {}", entry.filename); debug!("saving {}", entry.filename);
let serialized = serde_json::to_string(&account)?; let serialized = serde_json::to_string(account.as_ref())?;
ensure!( ensure!(
serialized.len() > 2, serialized.len() > 2,
"Something extra weird happened and the account was serialized into nothing." "Something extra weird happened and the account was serialized into nothing."

View file

@ -6,6 +6,7 @@ use std::collections::HashSet;
use std::{ use std::{
io::{stdin, stdout, Write}, io::{stdin, stdout, Write},
path::Path, path::Path,
sync::{Arc, Mutex},
}; };
use steamguard::{steamapi, Confirmation, ConfirmationType, SteamGuardAccount}; use steamguard::{steamapi, Confirmation, ConfirmationType, SteamGuardAccount};
use termion::{ use termion::{
@ -122,12 +123,12 @@ fn main() {
if matches.is_present("setup") { if matches.is_present("setup") {
info!("setup"); info!("setup");
let mut linker = accountlinker::AccountLinker::new(); let mut linker = accountlinker::AccountLinker::new();
do_login(&mut linker.account); // do_login(&mut linker.account);
// linker.link(linker.account.session.expect("no login session")); // linker.link(linker.account.session.expect("no login session"));
return; return;
} }
let mut selected_accounts: Vec<SteamGuardAccount> = vec![]; let mut selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>> = vec![];
if matches.is_present("all") { if matches.is_present("all") {
// manifest.accounts.iter().map(|a| selected_accounts.push(a.b)); // manifest.accounts.iter().map(|a| selected_accounts.push(a.b));
for account in &manifest.accounts { for account in &manifest.accounts {
@ -139,7 +140,7 @@ fn main() {
selected_accounts.push(account.clone()); selected_accounts.push(account.clone());
break; break;
} }
if matches.value_of("username").unwrap() == account.account_name { if matches.value_of("username").unwrap() == account.lock().unwrap().account_name {
selected_accounts.push(account.clone()); selected_accounts.push(account.clone());
break; break;
} }
@ -150,14 +151,14 @@ fn main() {
"selected accounts: {:?}", "selected accounts: {:?}",
selected_accounts selected_accounts
.iter() .iter()
.map(|a| a.account_name.clone()) .map(|a| a.lock().unwrap().account_name.clone())
.collect::<Vec<String>>() .collect::<Vec<String>>()
); );
if let Some(trade_matches) = matches.subcommand_matches("trade") { if let Some(trade_matches) = matches.subcommand_matches("trade") {
info!("trade"); info!("trade");
for a in selected_accounts.iter_mut() { for a in selected_accounts.iter_mut() {
let mut account = a; // why is this necessary? let mut account = a.lock().unwrap();
info!("Checking for trade confirmations"); info!("Checking for trade confirmations");
let confirmations: Vec<Confirmation>; let confirmations: Vec<Confirmation>;
@ -205,7 +206,7 @@ fn main() {
let server_time = steamapi::get_server_time(); let server_time = steamapi::get_server_time();
for account in selected_accounts { for account in selected_accounts {
trace!("{:?}", account); trace!("{:?}", account);
let code = account.generate_code(server_time); let code = account.lock().unwrap().generate_code(server_time);
println!("{}", code); println!("{}", code);
} }
} }