Save tokens for each machine

This commit is contained in:
Andrey Golovizin 2023-04-15 13:34:25 +02:00
parent 4e62839513
commit 6c6d5a51a8
3 changed files with 83 additions and 27 deletions

View file

@ -1,14 +1,10 @@
use base64::prelude::{Engine as _, BASE64_STANDARD};
use gethostname::gethostname;
use log::debug;
use x25519_dalek::{PublicKey, StaticSecret};
use std::{
io::Write,
path::{Path, PathBuf},
};
use std::{io::Write, path::Path};
use crate::dirs::get_data_dir;
use crate::dirs::MachineConfig;
const KEY_SIZE: usize = 32;
@ -46,24 +42,15 @@ pub(crate) struct WireguardKeyPair {
pub private_key: Key,
}
pub(crate) fn get_keys(machine: Option<&PathBuf>) -> Result<WireguardKeyPair, anyhow::Error> {
let hostname: PathBuf;
let machine_subdir: &PathBuf = if let Some(machine) = machine {
machine
} else {
hostname = PathBuf::from(gethostname());
&hostname
};
let key_path = get_data_dir().join("keys");
pub(crate) fn get_keys(machine_config: &MachineConfig) -> Result<WireguardKeyPair, anyhow::Error> {
let key_path = machine_config.key_path();
debug!("key path = {:?}", &key_path);
std::fs::create_dir_all(&key_path)?;
let private_key_path = key_path.join(machine_subdir);
let private_key = if private_key_path.is_file() {
Key::load(&private_key_path)?
let private_key = if key_path.is_file() {
Key::load(&key_path)?
} else {
let key = generate_private_key();
key.save(&private_key_path)?;
key.save(&key_path)?;
key
};