certs: cleanup logic and add new util functions
This commit is contained in:
@@ -14,6 +14,7 @@ openssl = "0.10.73"
|
||||
rand = "0.9.1"
|
||||
reqwest = { version = "0.12.20", features = [
|
||||
"rustls-tls",
|
||||
"native-tls",
|
||||
], default-features = false }
|
||||
salvo = { version = "0.79.0", features = ["oapi"] }
|
||||
serde = { version = "1.0.219", features = ["serde_derive"] }
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::Result;
|
||||
use openssl::hash::MessageDigest;
|
||||
@@ -7,6 +8,17 @@ use openssl::pkey::{PKey, Private};
|
||||
use openssl::rsa::Rsa;
|
||||
use openssl::x509::X509;
|
||||
|
||||
pub fn get_and_create_cert_dir() -> Result<PathBuf> {
|
||||
let project_dirs =
|
||||
directories::ProjectDirs::from("xyz", "ohea", "gamestream-webtransport-proxy")
|
||||
.ok_or(anyhow::anyhow!("Could not get project dirs"))?;
|
||||
let data_dir = project_dirs.data_dir();
|
||||
let cert_dir = data_dir.join("certs");
|
||||
fs::create_dir_all(&cert_dir)?;
|
||||
|
||||
Ok(cert_dir)
|
||||
}
|
||||
|
||||
pub fn get_cert_and_key() -> Result<(X509, PKey<Private>)> {
|
||||
if let Ok((cert, key)) = load_cert_and_key_from_disk() {
|
||||
Ok((cert, key))
|
||||
@@ -16,12 +28,7 @@ pub fn get_cert_and_key() -> Result<(X509, PKey<Private>)> {
|
||||
}
|
||||
|
||||
pub fn load_cert_and_key_from_disk() -> Result<(X509, PKey<Private>)> {
|
||||
let project_dirs =
|
||||
directories::ProjectDirs::from("xyz", "ohea", "gamestream-webtransport-proxy")
|
||||
.ok_or(anyhow::anyhow!("Could not get project dirs"))?;
|
||||
let data_dir = project_dirs.data_dir();
|
||||
let cert_dir = data_dir.join("certs");
|
||||
fs::create_dir_all(&cert_dir)?;
|
||||
let cert_dir = get_and_create_cert_dir()?;
|
||||
|
||||
let cert_filepath = cert_dir.join("cert");
|
||||
let key_filepath = cert_dir.join("key");
|
||||
@@ -72,13 +79,7 @@ pub fn generate_cert_and_key() -> Result<(X509, PKey<Private>)> {
|
||||
}
|
||||
|
||||
pub fn save_cert_and_key_to_disk(cert: &X509, key: &PKey<Private>) -> Result<()> {
|
||||
let project_dirs =
|
||||
directories::ProjectDirs::from("xyz", "ohea", "gamestream-webtransport-proxy")
|
||||
.ok_or(anyhow::anyhow!("Could not get project dirs"))?;
|
||||
let data_dir = project_dirs.data_dir();
|
||||
let cert_dir = data_dir.join("certs");
|
||||
fs::create_dir_all(&cert_dir)?;
|
||||
|
||||
let cert_dir = get_and_create_cert_dir()?;
|
||||
let cert_filepath = cert_dir.join("cert");
|
||||
let key_filepath = cert_dir.join("key");
|
||||
|
||||
@@ -109,4 +110,15 @@ pub fn save_cert_and_key_to_disk(cert: &X509, key: &PKey<Private>) -> Result<()>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn http_client_with_identity() {}
|
||||
pub fn http_client_with_identity() -> Result<reqwest::Client> {
|
||||
let cert_dir = get_and_create_cert_dir()?;
|
||||
let cert_filepath = cert_dir.join("cert");
|
||||
let key_filepath = cert_dir.join("key");
|
||||
|
||||
let cert_bytes = fs::read(cert_filepath)?;
|
||||
let key_bytes = fs::read(key_filepath)?;
|
||||
|
||||
let identity = reqwest::tls::Identity::from_pkcs8_pem(&cert_bytes, &key_bytes)?;
|
||||
|
||||
Ok(reqwest::Client::builder().identity(identity).build()?)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user