From 8d8a918b88cc0fce86590a17042fd1e5d9d82e68 Mon Sep 17 00:00:00 2001 From: lbl8603 <49143209+lbl8603@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E5=8F=AF=E6=89=A7=E8=A1=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=A0=B9=E7=9B=AE=E5=BD=95=E5=88=9B=E5=BB=BAlog?= =?UTF-8?q?=E5=92=8Ckey=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cipher/rsa_cipher.rs | 27 ++++++++++++--------------- src/main.rs | 26 ++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/cipher/rsa_cipher.rs b/src/cipher/rsa_cipher.rs index 629a7b4..4052fce 100644 --- a/src/cipher/rsa_cipher.rs +++ b/src/cipher/rsa_cipher.rs @@ -21,8 +21,9 @@ struct Inner { } impl RsaCipher { - pub fn new() -> io::Result { - let priv_key_path = PathBuf::from("key/private_key.pem"); + pub fn new(root_path: PathBuf) -> io::Result { + let priv_key_path = root_path.join("key/private_key.pem"); + let pub_key_path = root_path.join("key/public_key.pem"); let private_key = if priv_key_path.exists() { let key = std::fs::read_to_string(priv_key_path)?; @@ -36,10 +37,6 @@ impl RsaCipher { } } } else { - let path = PathBuf::from("key"); - if !path.exists() { - std::fs::create_dir(path)?; - } let mut rng = rand::thread_rng(); let bits = 2048; let private_key = match RsaPrivateKey::new(&mut rng, bits) { @@ -51,25 +48,25 @@ impl RsaCipher { )); } }; + let path = root_path.join("key"); + if !path.exists() { + if let Err(e) = std::fs::create_dir(path) { + log::warn!("创建密钥目录失败:{}", e); + } + } match private_key.write_pkcs8_pem_file(priv_key_path, LineEnding::CRLF) { Ok(_) => {} Err(e) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("failed to write to file 'key/private_key.pem' {}", e), - )); + log::warn!("保存私钥文件失败:{}", e); } }; private_key }; let public_key = RsaPublicKey::from(&private_key); - match public_key.write_public_key_pem_file("key/public_key.pem", LineEnding::CRLF) { + match public_key.write_public_key_pem_file(pub_key_path, LineEnding::CRLF) { Ok(_) => {} Err(e) => { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("failed to write to file 'key/public_key.pem' {}", e), - )); + log::warn!("保存公钥文件失败:{}", e); } }; let public_key_der = match public_key.to_public_key_der() { diff --git a/src/main.rs b/src/main.rs index bdb4723..78c96e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,9 +70,9 @@ pub struct ConfigInfo { pub password: String, } -fn log_init(log_path: Option) { +fn log_init(root_path: PathBuf, log_path: Option) { let log_path = match log_path { - None => PathBuf::from("log"), + None => root_path.join("log"), Some(log_path) => { if &log_path == "/dev/null" { return; @@ -120,10 +120,28 @@ root: let _ = log4rs::init_file(log_config, Default::default()); } +pub fn app_root() -> PathBuf { + match std::env::current_exe() { + Ok(path) => { + if let Some(v) = path.as_path().parent() { + v.to_path_buf() + } else { + log::warn!("current_exe parent none:{:?}", path); + PathBuf::new() + } + } + Err(e) => { + log::warn!("current_exe err:{:?}", e); + PathBuf::new() + } + } +} + #[tokio::main] async fn main() { let args = StartArgs::parse(); - log_init(args.log_path); + let root_path = app_root(); + log_init(root_path.clone(), args.log_path); let port = args.port.unwrap_or(29872); #[cfg(feature = "web")] let web_port = { @@ -221,7 +239,7 @@ async fn main() { #[cfg(feature = "web")] password: args.password.unwrap_or_else(|| "admin".into()), }; - let rsa = match RsaCipher::new() { + let rsa = match RsaCipher::new(root_path) { Ok(rsa) => { println!("密钥指纹: {}", rsa.finger()); Some(rsa)