diff --git a/src/cli.rs b/src/cli.rs index 687d904..2931fd7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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.")] 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)] pub sub: Option, } @@ -143,3 +149,23 @@ pub(crate) struct ArgsEncrypt; #[derive(Debug, Clone, Parser)] #[clap(about = "Decrypt all maFiles")] 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 for ArgsCode { + fn from(args: Args) -> Self { + ArgsCode { + offline: args.offline, + } + } +} diff --git a/src/main.rs b/src/main.rs index 34981a6..80cdf47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate rpassword; use clap::{IntoApp, Parser}; use log::*; +use std::time::{SystemTime, UNIX_EPOCH}; use std::{ io::{stdout, Write}, path::Path, @@ -180,7 +181,7 @@ fn run() -> anyhow::Result<()> { } _ => { 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(()); } -fn do_subcmd_code(selected_accounts: Vec>>) -> anyhow::Result<()> { - let server_time = steamapi::get_server_time()?.server_time; +fn do_subcmd_code( + args: cli::ArgsCode, + selected_accounts: Vec>>, +) -> 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); for account in selected_accounts { info!(