add --offline flag for generating codes, closes #155

This commit is contained in:
Carson McManus 2022-06-20 20:43:53 -04:00
parent 610cda120e
commit fdc61e63d1
2 changed files with 37 additions and 3 deletions

View file

@ -32,6 +32,12 @@ pub(crate) struct Args {
#[clap(short, long, arg_enum, default_value_t=Verbosity::Info, help = "Set the log level. Be warned, trace is capable of printing sensitive data.")] #[clap(short, long, arg_enum, default_value_t=Verbosity::Info, help = "Set the log level. Be warned, trace is capable of printing sensitive data.")]
pub verbosity: Verbosity, pub verbosity: Verbosity,
#[clap(
long,
help = "Assume the computer's time is correct. Don't ask Steam for the time when generating codes."
)]
pub offline: bool,
#[clap(subcommand)] #[clap(subcommand)]
pub sub: Option<Subcommands>, pub sub: Option<Subcommands>,
} }
@ -143,3 +149,23 @@ pub(crate) struct ArgsEncrypt;
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
#[clap(about = "Decrypt all maFiles")] #[clap(about = "Decrypt all maFiles")]
pub(crate) struct ArgsDecrypt; pub(crate) struct ArgsDecrypt;
#[derive(Debug, Clone, Parser)]
#[clap(about = "Generate 2FA codes")]
pub(crate) struct ArgsCode {
#[clap(
long,
help = "Assume the computer's time is correct. Don't ask Steam for the time when generating codes."
)]
pub offline: bool,
}
// HACK: the derive API doesn't support default subcommands, so we are going to make it so that it'll be easier to switch over when it's implemented.
// See: https://github.com/clap-rs/clap/issues/3857
impl From<Args> for ArgsCode {
fn from(args: Args) -> Self {
ArgsCode {
offline: args.offline,
}
}
}

View file

@ -1,6 +1,7 @@
extern crate rpassword; extern crate rpassword;
use clap::{IntoApp, Parser}; use clap::{IntoApp, Parser};
use log::*; use log::*;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{ use std::{
io::{stdout, Write}, io::{stdout, Write},
path::Path, path::Path,
@ -180,7 +181,7 @@ fn run() -> anyhow::Result<()> {
} }
_ => { _ => {
debug!("No subcommand given, assuming user wants a 2fa code"); debug!("No subcommand given, assuming user wants a 2fa code");
return do_subcmd_code(selected_accounts); return do_subcmd_code(args.into(), selected_accounts);
} }
} }
} }
@ -662,8 +663,15 @@ fn do_subcmd_decrypt(
return Ok(()); return Ok(());
} }
fn do_subcmd_code(selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>) -> anyhow::Result<()> { fn do_subcmd_code(
let server_time = steamapi::get_server_time()?.server_time; args: cli::ArgsCode,
selected_accounts: Vec<Arc<Mutex<SteamGuardAccount>>>,
) -> anyhow::Result<()> {
let server_time = if args.offline {
SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs()
} else {
steamapi::get_server_time()?.server_time
};
debug!("Time used to generate codes: {}", server_time); debug!("Time used to generate codes: {}", server_time);
for account in selected_accounts { for account in selected_accounts {
info!( info!(