From 456ab04f786142a9680ba93e6ae819a39827a40b Mon Sep 17 00:00:00 2001 From: _ <> Date: Mon, 15 Mar 2021 20:34:32 +0000 Subject: [PATCH] impl auto-gen-key --- crates/ptth_server/Cargo.toml | 1 + crates/ptth_server/src/bin/ptth_server.rs | 54 +++++++++++++++++------ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/crates/ptth_server/Cargo.toml b/crates/ptth_server/Cargo.toml index 9711ca8..33437e0 100644 --- a/crates/ptth_server/Cargo.toml +++ b/crates/ptth_server/Cargo.toml @@ -21,6 +21,7 @@ http = "0.2.1" lazy_static = "1.4.0" percent-encoding = "2.1.0" pulldown-cmark = "0.8.0" +rand = "0.8.3" regex = "1.4.1" reqwest = { version = "0.11.1", features = [] } rmp-serde = "0.14.4" diff --git a/crates/ptth_server/src/bin/ptth_server.rs b/crates/ptth_server/src/bin/ptth_server.rs index f364a4f..53ba1f2 100644 --- a/crates/ptth_server/src/bin/ptth_server.rs +++ b/crates/ptth_server/src/bin/ptth_server.rs @@ -1,11 +1,13 @@ #![warn (clippy::pedantic)] use std::{ + fs::File, + io::Write, path::PathBuf, + os::unix::fs::PermissionsExt, }; use structopt::StructOpt; -use tokio::runtime; use ptth_server::{ load_toml, @@ -14,6 +16,9 @@ use ptth_server::{ #[derive (Debug, StructOpt)] struct Opt { + #[structopt (long)] + auto_gen_key: bool, + #[structopt (long)] file_server_root: Option , @@ -41,14 +46,41 @@ pub struct ConfigFile { pub file_server_root: Option , } -fn main () -> Result <(), anyhow::Error> { +#[tokio::main] +async fn main () -> Result <(), anyhow::Error> { tracing_subscriber::fmt::init (); let opt = Opt::from_args (); let asset_root = opt.asset_root; let path = opt.config_path.clone ().unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml")); - let config_file: ConfigFile = load_toml::load (&path)?; + + let config_file: ConfigFile = match load_toml::load (&path) { + Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key { + use rand::RngCore; + let mut buffer = vec! [0u8; 64]; + rand::thread_rng ().fill_bytes (&mut buffer); + + let api_key = base64::encode (&buffer); + + { + let mut f = File::create (&path)?; + let metadata = f.metadata ()?; + let mut permissions = metadata.permissions (); + permissions.set_mode (0o600); + f.set_permissions (permissions)?; + + f.write (format! ("api_key = \"{}\"\n", api_key).as_bytes ())?; + } + + load_toml::load (&path)? + } + else { + panic! ("API key not provided in config file and auto-gen-key not provided"); + }, + Ok (x) => x, + Err (e) => return Err (e.into ()), + }; let config_file = ptth_server::ConfigFile { name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"), @@ -63,16 +95,12 @@ fn main () -> Result <(), anyhow::Error> { return Ok (()); } - let rt = runtime::Runtime::new ()?; - - rt.block_on (async move { - run_server ( - config_file, - ptth_core::graceful_shutdown::init (), - Some (path), - asset_root - ).await - })?; + run_server ( + config_file, + ptth_core::graceful_shutdown::init (), + Some (path), + asset_root + ).await?; Ok (()) }