src files
This commit is contained in:
89
src/certs.rs
Normal file
89
src/certs.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
use crate::common::{is_file_exist, read_file, write_file, AppConfig};
|
||||
use crate::crypto::ICryptoProvider;
|
||||
|
||||
pub(crate) struct Certs<T>
|
||||
where
|
||||
T: ICryptoProvider,
|
||||
{
|
||||
pub(crate) encoding: String,
|
||||
pub(crate) ca_file: PathBuf,
|
||||
pub(crate) key_file: PathBuf,
|
||||
pub(crate) cert_file: PathBuf,
|
||||
pub(crate) config_file: PathBuf,
|
||||
pub(crate) template_file: PathBuf,
|
||||
pub(crate) provider: Arc<T>,
|
||||
}
|
||||
|
||||
impl<T> Certs<T>
|
||||
where
|
||||
T: ICryptoProvider,
|
||||
{
|
||||
pub(crate) fn new(cfg: &AppConfig, provider: T) -> Self {
|
||||
let base_dir = PathBuf::from(&cfg.base_directory);
|
||||
let keys_dir = base_dir.clone().join(cfg.keys_subdir.clone());
|
||||
let config_dir = base_dir.clone().join(cfg.config_subdir.clone());
|
||||
let name = cfg.name.clone();
|
||||
|
||||
Certs {
|
||||
encoding: cfg.encoding.clone(),
|
||||
ca_file: keys_dir.join(cfg.ca_filename.clone()),
|
||||
key_file: keys_dir.join(format!("{}.key", &name)),
|
||||
cert_file: keys_dir.join(format!("{}.crt", &name)),
|
||||
config_file: config_dir.join(format!("{}.ovpn", &name)),
|
||||
template_file: base_dir.clone().join(cfg.template_file.clone()),
|
||||
provider: Arc::new(provider),
|
||||
}
|
||||
}
|
||||
|
||||
async fn is_config_exists(&self) -> bool {
|
||||
is_file_exist(&self.config_file).await
|
||||
}
|
||||
|
||||
pub(crate) async fn request(&self) -> Result<()> {
|
||||
self.provider.request().await
|
||||
}
|
||||
|
||||
pub(crate) async fn sign(&self) -> Result<()> {
|
||||
self.provider.sign().await
|
||||
}
|
||||
|
||||
pub(crate) async fn build_client_config(&self) -> Result<bool> {
|
||||
if self.is_config_exists().await {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.request().await?;
|
||||
self.sign().await?;
|
||||
|
||||
let (template_file, ca_file, cert_file, key_file) = (
|
||||
self.template_file.clone(),
|
||||
self.ca_file.clone(),
|
||||
self.cert_file.clone(),
|
||||
self.key_file.clone(),
|
||||
);
|
||||
let enc = self.encoding.clone();
|
||||
let (enc1, enc2, enc3, enc4) = (enc.clone(), enc.clone(), enc.clone(), enc.clone());
|
||||
|
||||
if let (Ok(Ok(template)), Ok(Ok(ca)), Ok(Ok(cert)), Ok(Ok(key))) = tokio::join!(
|
||||
tokio::spawn(read_file(template_file, enc1)),
|
||||
tokio::spawn(read_file(ca_file, enc2)),
|
||||
tokio::spawn(read_file(cert_file, enc3)),
|
||||
tokio::spawn(read_file(key_file, enc4))
|
||||
) {
|
||||
let text = template
|
||||
.replace("{{ca}}", ca.trim())
|
||||
.replace("{{cert}}", cert.trim())
|
||||
.replace("{{key}}", key.trim());
|
||||
|
||||
write_file(&self.config_file, text, &self.encoding).await?;
|
||||
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(anyhow!("files read error"))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user