🏁 build: fix Windows build for ptth_server

main
Trisha 2021-03-21 14:07:09 -05:00
parent 2253ae3391
commit 02d4362c3b
1 changed files with 32 additions and 17 deletions

View File

@ -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 <PathBuf>,
}
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)?
}