🏁 build: fix Windows build for ptth_server
parent
2253ae3391
commit
02d4362c3b
|
@ -3,8 +3,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::Write,
|
io::Write,
|
||||||
path::PathBuf,
|
path::{Path, PathBuf},
|
||||||
os::unix::fs::PermissionsExt,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
@ -49,6 +48,36 @@ pub struct ConfigFile {
|
||||||
pub file_server_root: Option <PathBuf>,
|
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]
|
#[tokio::main]
|
||||||
async fn main () -> Result <(), anyhow::Error> {
|
async fn main () -> Result <(), anyhow::Error> {
|
||||||
tracing_subscriber::fmt::init ();
|
tracing_subscriber::fmt::init ();
|
||||||
|
@ -60,21 +89,7 @@ async fn main () -> Result <(), anyhow::Error> {
|
||||||
|
|
||||||
let config_file: ConfigFile = match load_toml::load (&path) {
|
let config_file: ConfigFile = match load_toml::load (&path) {
|
||||||
Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key {
|
Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key {
|
||||||
use rand::RngCore;
|
gen_key (&path)?;
|
||||||
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 ())?;
|
|
||||||
}
|
|
||||||
|
|
||||||
load_toml::load (&path)?
|
load_toml::load (&path)?
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue