From 00d5dc9d0921ffb4c9488ebd246fb3cc4410881a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 5 May 2021 15:01:15 +0800 Subject: [PATCH] feat: email --- src/config.rs | 11 +++++++---- src/main.rs | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index eed972c..55bca69 100644 --- a/src/config.rs +++ b/src/config.rs @@ -150,11 +150,14 @@ impl CertConfigItem { } cert_dns_names.push(x509_certificate.common_name.to_lowercase()); - self.dns_names.as_ref().map(|dns_names| dns_names.iter().map(|n| n.to_lowercase()).map(|n| { - if !self_dns_names.contains(&n) { - self_dns_names.push(n); + if let Some(dns_names) = &self.dns_names { + for n in dns_names { + let n = n.to_lowercase(); + if !self_dns_names.contains(&n) { + self_dns_names.push(n); + } } - })); + } for n in &x509_certificate.alt_names { let n = n.to_lowercase(); if !cert_dns_names.contains(&n) { diff --git a/src/main.rs b/src/main.rs index b94d391..3c47b0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ use async_std::channel::Sender; use config::AcmeMode; use crate::config::{CertConfig, CERT_NAME, KEY_NAME}; use crate::x509::{X509PublicKeyAlgo, X509EcPublicKeyAlgo}; +use std::path::PathBuf; const NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -86,10 +87,37 @@ async fn main() -> tide::Result<()> { debugging!("Clap matches: {:?}", matches); - let email = matches.value_of("email").unwrap_or_else(|| { - failure!("Email is not assigned."); - exit(1); - }); + let account_dir = matches.value_of("dir").unwrap_or("acme_dir"); + + let mut account_email = PathBuf::from(account_dir); + account_email.push("account_email.conf"); + + let email = if account_email.exists() { + match fs::read_to_string(&account_email) { + Err(e) => { + failure!("Read from file: {:?}, failed: {}", account_email, e); + exit(1); + } + Ok(email) => { + if let Some(email_from_args) = matches.value_of("email") { + if &email != email_from_args { + warning!("Get email from account config: {}", email); + } + } + email.trim().to_string() + } + } + } else { + let email = matches.value_of("email").unwrap_or_else(|| { + failure!("Email is not assigned."); + exit(1); + }); + + information!("Write email to account config: {:?}", account_email); + fs::write(account_email, email); + + email.to_string() + }; match matches.value_of("type") { Some("http") => {} @@ -138,7 +166,6 @@ async fn main() -> tide::Result<()> { exit(1); } }; - let account_dir = matches.value_of("dir").unwrap_or("acme_dir"); let check = matches.is_present("check"); @@ -161,7 +188,7 @@ async fn main() -> tide::Result<()> { let alt_names: Vec<&str> = domains.into_iter().skip(1).collect(); information!("Domains, main: {}, alt: {:?}", primary_name, alt_names); let acme_request = AcmeRequest { - contract_email: email, + contract_email: &email, primary_name, alt_names: &alt_names, algo, @@ -197,7 +224,7 @@ async fn main() -> tide::Result<()> { information!("Domains, main: {}, alt: {:?}", common_name, dns_names); let alt_names: Vec<&str> = dns_names.iter().map(|n| n.as_str()).collect(); let acme_request = AcmeRequest { - contract_email: email, + contract_email: &email, primary_name: common_name, alt_names: &alt_names, algo,