From 57c1dd78ebf4bdd6282ac3632dcab8a3a98c511e Mon Sep 17 00:00:00 2001 From: shenjack-5600u <3695888@qq.com> Date: Sun, 11 Feb 2024 00:02:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0sign=E7=9A=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cluster.rs | 22 +++++++++++++++++++++- src/utils.rs | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/cluster.rs b/src/cluster.rs index 4c6a893..72b5f77 100644 --- a/src/cluster.rs +++ b/src/cluster.rs @@ -68,7 +68,6 @@ impl Cluster { .expect("Failed to disconnect"); } - /// /// public async requestCert(): Promise { /// const cert = await new Promise<{cert: string; key: string}>((resolve, reject) => { /// this.socket?.emit('request-cert', ([err, cert]: [unknown, {cert: string; key: string}]) => { @@ -98,6 +97,27 @@ impl Cluster { let key = &data["key"]; let cert_file = tmp_dir.clone().join("cert.pem"); let key_file = tmp_dir.clone().join("key.pem"); + if cert_file.exists() { + if tokio::fs::remove_file(cert_file.clone()).await.is_err() { + warn!("remove cert file error"); + } + + } else { + if let Some(parent) = cert_file.parent() { + if !parent.exists() { + if tokio::fs::create_dir_all(parent).await.is_err() { + warn!("create cert file parent dir error"); + } + } + } + } + if key_file.exists() { + if tokio::fs::remove_file(key_file.clone()).await.is_err() { + warn!("remove key file error"); + } + } + + }, _ => (), diff --git a/src/utils.rs b/src/utils.rs index 8347842..b420b1b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,6 +8,7 @@ use apache_avro::{from_avro_datum, from_value, types::Value}; use base64::Engine; use md5::{Digest, Md5}; use sha1::Sha1; +use tokio::io::AsyncWriteExt; use tracing::{info, warn}; /// import {join} from 'path' @@ -132,6 +133,17 @@ pub fn avro_data_to_file_list(data: Vec) -> Option> { } } +pub async fn safe_write_file(path: &PathBuf, data: &[u8]) -> Result<(), std::io::Error> { + let mut file = tokio::fs::OpenOptions::new() + .write(true) + .create(true) + .open(path) + .await?; + file.write_all(data).await?; + file.sync_all().await?; + Ok(()) +} + /// FATAL 级 Log /// 这个宏会输出一条 error 级的日志, 并且 panic! /// 这个宏应当接收两个参数, 分别定义为 arg1 和 arg2, 其应当均为 String 类型 @@ -184,3 +196,15 @@ fn test_validate_file() { false ); } + +#[test] +fn test_check_sign() { + let mut query = HashMap::new(); + // 使用 21000101 00:00:00 UTC+8 作为日期 + let long_long_after = 4102416000_i64; + query.insert("s".to_string(), "QiMQm3c-nXiUKu0Cmmp14hXnVk0=".to_string()); + query.insert("e".to_string(), long_long_after.to_string()); + let secret = "abcd"; + let hash = "1234567890abcdef"; + assert!(check_sign(hash, secret, &query)); +}