cli: Take cookies by file instead of by arg

This commit is contained in:
2021-04-29 18:39:11 +02:00
parent 429615dea7
commit e8e8f535c2
2 changed files with 14 additions and 12 deletions

View File

@ -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<WaitForEvent, Error> {
async fn wait_for_event(opt: &DaemonOpt, cookie: &str) -> Result<WaitForEvent, Error> {
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<WaitForEvent, Error> {
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<WaitForEvent, Error> {
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<Vec<String>, Error> {
async fn get_active_categories(opt: &DaemonOpt, cookie: &str) -> Result<Vec<String>, 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?;

View File

@ -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)]