Skip to content

Commit

Permalink
在可执行文件根目录创建log和key文件
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Apr 28, 2024
1 parent 898bc45 commit 8d8a918
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
27 changes: 12 additions & 15 deletions src/cipher/rsa_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ struct Inner {
}

impl RsaCipher {
pub fn new() -> io::Result<Self> {
let priv_key_path = PathBuf::from("key/private_key.pem");
pub fn new(root_path: PathBuf) -> io::Result<Self> {
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)?;

Expand All @@ -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) {
Expand All @@ -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() {
Expand Down
26 changes: 22 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub struct ConfigInfo {
pub password: String,
}

fn log_init(log_path: Option<String>) {
fn log_init(root_path: PathBuf, log_path: Option<String>) {
let log_path = match log_path {
None => PathBuf::from("log"),
None => root_path.join("log"),
Some(log_path) => {
if &log_path == "/dev/null" {
return;
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8d8a918

Please sign in to comment.