From e8e8f535c2caac0ef7150c3a8d17eac481211bb0 Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Thu, 29 Apr 2021 18:39:11 +0200 Subject: [PATCH] cli: Take cookies by file instead of by arg --- cli/src/daemon.rs | 22 ++++++++++++---------- cli/src/main.rs | 4 ++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/cli/src/daemon.rs b/cli/src/daemon.rs index e9c903c..ed2ca9a 100644 --- a/cli/src/daemon.rs +++ b/cli/src/daemon.rs @@ -16,6 +16,8 @@ use tokio::task::{spawn, JoinHandle}; pub fn run(opt: DaemonOpt) -> Result<(), Error> { let opt: &'static DaemonOpt = Box::leak(Box::new(opt)); + let cookie: &'static str = + Box::leak(std::fs::read_to_string(&opt.cookie_file)?.into_boxed_str()); let rt = Runtime::new()?; rt.block_on(async { @@ -29,10 +31,10 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> { .watch(&opt.file, RecursiveMode::NonRecursive) .expect("failed to watch file"); - update_category_list(opt).await?; + update_category_list(opt, cookie).await?; let mut last_update = Instant::now(); - let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt)); + let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt, cookie)); let mut rx = proxy_channel(rx); @@ -79,12 +81,12 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> { update_categories.unwrap_or_else(|| elapsed > Duration::from_secs(opt.poll_delay)); if update_categories { - update_category_list(opt).await?; + update_category_list(opt, cookie).await?; last_update = Instant::now(); } if update_categories || restart_wfe { - wfe = spawn(wait_for_event(opt)); + wfe = spawn(wait_for_event(opt, cookie)); } } }) @@ -96,7 +98,7 @@ struct WaitForEvent { timeout: bool, } -async fn wait_for_event(opt: &DaemonOpt) -> Result { +async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result { info!("calling wait_for_event"); let client = reqwest::Client::new(); let wfe: WaitForEvent = client @@ -106,7 +108,7 @@ async fn wait_for_event(opt: &DaemonOpt) -> Result { url::WAIT_FOR_EVENT, opt.poll_delay )) - .header("Cookie", &opt.cookie) + .header("Cookie", cookie) .send() .await? .json() @@ -115,8 +117,8 @@ async fn wait_for_event(opt: &DaemonOpt) -> Result { Ok(wfe) } -async fn update_category_list(opt: &DaemonOpt) -> Result<(), Error> { - let categories = get_active_categories(opt).await?; +async fn update_category_list(opt: &DaemonOpt, cookie: &str) -> Result<(), Error> { + let categories = get_active_categories(opt, cookie).await?; info!("writing output file"); let data = categories.join("|"); @@ -131,12 +133,12 @@ async fn update_category_list(opt: &DaemonOpt) -> Result<(), Error> { Ok(()) } -async fn get_active_categories(opt: &DaemonOpt) -> Result, Error> { +async fn get_active_categories(opt: &DaemonOpt, cookie: &str) -> Result, Error> { info!("downloading category list"); let client = reqwest::Client::new(); let response = client .get(format!("{}{}", &opt.api_uri, url::SESSIONS)) - .header("Cookie", &opt.cookie) + .header("Cookie", cookie) .send() .await?; diff --git a/cli/src/main.rs b/cli/src/main.rs index b2a5d43..7c5cda2 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -48,9 +48,9 @@ pub struct DaemonOpt { #[structopt(short = "d", long, default_value = "30")] poll_delay: u64, - /// The authorization cookie for the server + /// File containing the authorization cookie for the server #[structopt(short, long)] - cookie: String, + cookie_file: PathBuf, } #[derive(StructOpt)]