config from args + more args
This commit is contained in:
parent
bcd725583e
commit
21d6eb268a
139
src/main.rs
139
src/main.rs
@ -35,6 +35,26 @@ struct Args {
|
|||||||
/// files encoding
|
/// files encoding
|
||||||
#[arg(short = 'c', long)]
|
#[arg(short = 'c', long)]
|
||||||
encoding: Option<String>,
|
encoding: Option<String>,
|
||||||
|
|
||||||
|
/// keys subdir
|
||||||
|
#[arg(long, default_value = "keys")]
|
||||||
|
keys_dir: String,
|
||||||
|
|
||||||
|
/// config subdir
|
||||||
|
#[arg(long, default_value = "config")]
|
||||||
|
config_dir: String,
|
||||||
|
|
||||||
|
/// valid days
|
||||||
|
#[arg(long, default_value = "30650")]
|
||||||
|
days: u32,
|
||||||
|
|
||||||
|
/// openssl binary
|
||||||
|
#[arg(long, default_value = "openssl")]
|
||||||
|
openssl: String,
|
||||||
|
|
||||||
|
/// template file
|
||||||
|
#[arg(long, default_value = "template.ovpn")]
|
||||||
|
template_file: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VarsFile {
|
struct VarsFile {
|
||||||
@ -43,7 +63,7 @@ struct VarsFile {
|
|||||||
encoding: String,
|
encoding: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Config {
|
struct AppConfig {
|
||||||
encoding: String,
|
encoding: String,
|
||||||
req_days: u32,
|
req_days: u32,
|
||||||
keys_subdir: String,
|
keys_subdir: String,
|
||||||
@ -53,9 +73,13 @@ struct Config {
|
|||||||
openssl_cnf_env: String,
|
openssl_cnf_env: String,
|
||||||
ca_filename: String,
|
ca_filename: String,
|
||||||
default_email_domain: String,
|
default_email_domain: String,
|
||||||
|
openssl: String,
|
||||||
|
base_directory: String,
|
||||||
|
email: String,
|
||||||
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for AppConfig {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
encoding: "cp866".into(),
|
encoding: "cp866".into(),
|
||||||
@ -67,6 +91,51 @@ impl Default for Config {
|
|||||||
openssl_cnf_env: "KEY_CONFIG".into(),
|
openssl_cnf_env: "KEY_CONFIG".into(),
|
||||||
ca_filename: "ca.crt".into(),
|
ca_filename: "ca.crt".into(),
|
||||||
default_email_domain: "example.com".into(),
|
default_email_domain: "example.com".into(),
|
||||||
|
openssl: "openssl".into(),
|
||||||
|
base_directory: ".".into(),
|
||||||
|
email: "name@example.com".into(),
|
||||||
|
name: "user".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&Args> for AppConfig {
|
||||||
|
fn from(args: &Args) -> Self {
|
||||||
|
let defaults = Self::default();
|
||||||
|
|
||||||
|
let base_directory = args
|
||||||
|
.directory
|
||||||
|
.as_ref()
|
||||||
|
.unwrap_or(&defaults.base_directory)
|
||||||
|
.clone();
|
||||||
|
let email = args.email.clone().unwrap_or(format!(
|
||||||
|
"{}@{}",
|
||||||
|
&args.name,
|
||||||
|
defaults.default_email_domain.clone()
|
||||||
|
));
|
||||||
|
let encoding = if let Some(enc) = args.encoding.clone() {
|
||||||
|
enc.to_string()
|
||||||
|
} else {
|
||||||
|
defaults.encoding.clone()
|
||||||
|
};
|
||||||
|
let name = args.name.clone();
|
||||||
|
let openssl = args.openssl.clone();
|
||||||
|
let template_file = args.template_file.clone();
|
||||||
|
let req_days = args.days;
|
||||||
|
let keys_subdir = args.keys_dir.clone();
|
||||||
|
let config_subdir = args.config_dir.clone();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
base_directory,
|
||||||
|
email,
|
||||||
|
encoding,
|
||||||
|
name,
|
||||||
|
openssl,
|
||||||
|
template_file,
|
||||||
|
req_days,
|
||||||
|
keys_subdir,
|
||||||
|
config_subdir,
|
||||||
|
..defaults
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,6 +237,14 @@ impl VarsFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn from_config(config: &AppConfig) -> Result<Self> {
|
||||||
|
Self::from_dir(
|
||||||
|
PathBuf::from(&config.base_directory),
|
||||||
|
config.encoding.clone(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
async fn parse(&mut self) -> Result<()> {
|
async fn parse(&mut self) -> Result<()> {
|
||||||
let mut result = BTreeMap::new();
|
let mut result = BTreeMap::new();
|
||||||
let lines = read_file_by_lines(&self.filepath, &self.encoding).await?;
|
let lines = read_file_by_lines(&self.filepath, &self.encoding).await?;
|
||||||
@ -222,26 +299,22 @@ struct Certs {
|
|||||||
config_file: PathBuf,
|
config_file: PathBuf,
|
||||||
template_file: PathBuf,
|
template_file: PathBuf,
|
||||||
openssl_cnf: PathBuf,
|
openssl_cnf: PathBuf,
|
||||||
|
openssl: String,
|
||||||
vars: BTreeMap<String, String>,
|
vars: BTreeMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Certs {
|
impl Certs {
|
||||||
fn new(
|
fn new(cfg: &AppConfig, vars: BTreeMap<String, String>) -> Self {
|
||||||
name: String,
|
let base_dir = PathBuf::from(&cfg.base_directory);
|
||||||
email: String,
|
|
||||||
dir: PathBuf,
|
|
||||||
vars: BTreeMap<String, String>,
|
|
||||||
cfg: &Config,
|
|
||||||
) -> Self {
|
|
||||||
let base_dir = dir;
|
|
||||||
let keys_dir = base_dir.clone().join(cfg.keys_subdir.clone());
|
let keys_dir = base_dir.clone().join(cfg.keys_subdir.clone());
|
||||||
let config_dir = base_dir.clone().join(cfg.config_subdir.clone());
|
let config_dir = base_dir.clone().join(cfg.config_subdir.clone());
|
||||||
|
|
||||||
let mut vars = vars;
|
let mut vars = vars;
|
||||||
|
let name = cfg.name.clone();
|
||||||
|
|
||||||
vars.insert("KEY_CN".into(), name.clone());
|
vars.insert("KEY_CN".into(), name.clone());
|
||||||
vars.insert("KEY_NAME".into(), name.clone());
|
vars.insert("KEY_NAME".into(), name.clone());
|
||||||
vars.insert("KEY_EMAIL".into(), email);
|
vars.insert("KEY_EMAIL".into(), cfg.email.clone());
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
base_dir: base_dir.clone(),
|
base_dir: base_dir.clone(),
|
||||||
@ -257,6 +330,7 @@ impl Certs {
|
|||||||
std::env::var(cfg.openssl_cnf_env.clone())
|
std::env::var(cfg.openssl_cnf_env.clone())
|
||||||
.unwrap_or(cfg.openssl_default_cnf.clone()),
|
.unwrap_or(cfg.openssl_default_cnf.clone()),
|
||||||
),
|
),
|
||||||
|
openssl: cfg.openssl.clone(),
|
||||||
vars,
|
vars,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -289,17 +363,17 @@ impl Certs {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = Command::new("openssl")
|
let status = Command::new(&self.openssl)
|
||||||
.args(&[
|
.args([
|
||||||
"req",
|
"req",
|
||||||
"-nodes",
|
"-nodes",
|
||||||
"-new",
|
"-new",
|
||||||
"-keyout",
|
"-keyout",
|
||||||
&self.key_file.to_str().unwrap(),
|
self.key_file.to_str().unwrap(),
|
||||||
"-out",
|
"-out",
|
||||||
&self.req_file.to_str().unwrap(),
|
self.req_file.to_str().unwrap(),
|
||||||
"-config",
|
"-config",
|
||||||
&self.openssl_cnf.to_str().unwrap(),
|
self.openssl_cnf.to_str().unwrap(),
|
||||||
"-batch",
|
"-batch",
|
||||||
])
|
])
|
||||||
.current_dir(&self.base_dir)
|
.current_dir(&self.base_dir)
|
||||||
@ -325,17 +399,17 @@ impl Certs {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = Command::new("openssl")
|
let status = Command::new(&self.openssl)
|
||||||
.args(&[
|
.args([
|
||||||
"ca",
|
"ca",
|
||||||
"-days",
|
"-days",
|
||||||
format!("{}", self.req_days).as_str(),
|
format!("{}", self.req_days).as_str(),
|
||||||
"-out",
|
"-out",
|
||||||
&self.cert_file.to_str().unwrap(),
|
self.cert_file.to_str().unwrap(),
|
||||||
"-in",
|
"-in",
|
||||||
&self.req_file.to_str().unwrap(),
|
self.req_file.to_str().unwrap(),
|
||||||
"-config",
|
"-config",
|
||||||
&self.openssl_cnf.to_str().unwrap(),
|
self.openssl_cnf.to_str().unwrap(),
|
||||||
"-batch",
|
"-batch",
|
||||||
])
|
])
|
||||||
.current_dir(&self.base_dir)
|
.current_dir(&self.base_dir)
|
||||||
@ -389,31 +463,14 @@ impl Certs {
|
|||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let default_directory = ".".to_string();
|
let config = AppConfig::from(&args);
|
||||||
let directory = args.directory.as_ref().unwrap_or(&default_directory);
|
let mut vars = VarsFile::from_config(&config).await?;
|
||||||
let mut config = Config::default();
|
|
||||||
let email = args.email.clone().unwrap_or(format!(
|
|
||||||
"{}@{}",
|
|
||||||
&args.name,
|
|
||||||
config.default_email_domain.clone()
|
|
||||||
));
|
|
||||||
if let Some(encoding) = args.encoding.clone() {
|
|
||||||
config.encoding = encoding.to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut vars = VarsFile::from_dir(PathBuf::from(directory), config.encoding.clone()).await?;
|
|
||||||
vars.parse().await?;
|
vars.parse().await?;
|
||||||
|
|
||||||
println!("found vars: {}", vars.filepath.to_str().expect("fff"));
|
println!("found vars: {}", vars.filepath.to_str().expect("fff"));
|
||||||
println!("loaded: {:#?}", &vars.vars);
|
println!("loaded: {:#?}", &vars.vars);
|
||||||
|
|
||||||
let certs = Certs::new(
|
let certs = Certs::new(&config, vars.vars.unwrap());
|
||||||
args.name,
|
|
||||||
email,
|
|
||||||
directory.into(),
|
|
||||||
vars.vars.unwrap(),
|
|
||||||
&config,
|
|
||||||
);
|
|
||||||
let created = certs.build_client_config().await?;
|
let created = certs.build_client_config().await?;
|
||||||
|
|
||||||
let result_file = certs.config_file.to_str().unwrap();
|
let result_file = certs.config_file.to_str().unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user