From 02d4362c3b84144846f950e143b12c9fd5792105 Mon Sep 17 00:00:00 2001 From: Trisha Date: Sun, 21 Mar 2021 14:07:09 -0500 Subject: [PATCH] :checkered_flag: build: fix Windows build for ptth_server --- crates/ptth_server/src/bin/ptth_server.rs | 49 +++++++++++++++-------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/crates/ptth_server/src/bin/ptth_server.rs b/crates/ptth_server/src/bin/ptth_server.rs index fa61f20..f1efafb 100644 --- a/crates/ptth_server/src/bin/ptth_server.rs +++ b/crates/ptth_server/src/bin/ptth_server.rs @@ -3,8 +3,7 @@ use std::{ fs::File, io::Write, - path::PathBuf, - os::unix::fs::PermissionsExt, + path::{Path, PathBuf}, }; use structopt::StructOpt; @@ -49,6 +48,36 @@ pub struct ConfigFile { pub file_server_root: Option , } +fn gen_key (path: &Path) -> anyhow::Result <()> { + use rand::RngCore; + let mut buffer = vec! [0_u8; 64]; + rand::thread_rng ().fill_bytes (&mut buffer); + + let api_key = base64::encode (&buffer); + + { + let mut f = File::create (path)?; + + #[cfg (unix)] + { + use std::os::unix::fs::PermissionsExt; + + let metadata = f.metadata ()?; + let mut permissions = metadata.permissions (); + permissions.set_mode (0o600); + f.set_permissions (permissions)?; + } + #[cfg (not (unix))] + { + tracing::warn! ("API keys aren't protected from clients on non-Unix OSes yet"); + } + + f.write_all (format! ("api_key = \"{}\"\n", api_key).as_bytes ())?; + } + + Ok (()) +} + #[tokio::main] async fn main () -> Result <(), anyhow::Error> { tracing_subscriber::fmt::init (); @@ -60,21 +89,7 @@ async fn main () -> Result <(), anyhow::Error> { 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! [0_u8; 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_all (format! ("api_key = \"{}\"\n", api_key).as_bytes ())?; - } + gen_key (&path)?; load_toml::load (&path)? }