feat: v0.2.0-rc, add macro ecc_private_key_convert
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use rcgen::{BasicConstraints, Certificate, CertificateParams, DistinguishedName, DnType, IsCa, KeyPair, PKCS_ECDSA_P256_SHA256};
|
use rcgen::{
|
||||||
|
BasicConstraints, Certificate, CertificateParams,
|
||||||
|
DistinguishedName, DnType, IsCa, KeyPair, PKCS_ECDSA_P256_SHA256,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let key_pair = KeyPair::generate(&PKCS_ECDSA_P256_SHA256).expect("Generate key pair failed");
|
let key_pair = KeyPair::generate(&PKCS_ECDSA_P256_SHA256).expect("Generate key pair failed");
|
||||||
|
|||||||
11
src/app.rs
11
src/app.rs
@@ -20,13 +20,13 @@ pub struct ProxyApp {
|
|||||||
tls: bool,
|
tls: bool,
|
||||||
lookup_dns: bool,
|
lookup_dns: bool,
|
||||||
host_configs: Vec<HostConfig>,
|
host_configs: Vec<HostConfig>,
|
||||||
tokio_async_resolver: TokioAsyncResolver,
|
dns_resolver: TokioAsyncResolver,
|
||||||
dns_resolver_cache_map: RwLock<HashMap<String, String>>,
|
dns_resolver_cache_map: RwLock<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProxyApp {
|
impl ProxyApp {
|
||||||
pub fn new(tls: bool, lookup_dns: bool, host_configs: Vec<HostConfig>) -> Self {
|
pub fn new(tls: bool, lookup_dns: bool, host_configs: Vec<HostConfig>) -> Self {
|
||||||
let tokio_async_resolver = TokioAsyncResolver::tokio(
|
let dns_resolver = TokioAsyncResolver::tokio(
|
||||||
ResolverConfig::default(),
|
ResolverConfig::default(),
|
||||||
ResolverOpts::default(),
|
ResolverOpts::default(),
|
||||||
);
|
);
|
||||||
@@ -34,7 +34,7 @@ impl ProxyApp {
|
|||||||
tls,
|
tls,
|
||||||
lookup_dns,
|
lookup_dns,
|
||||||
host_configs,
|
host_configs,
|
||||||
tokio_async_resolver,
|
dns_resolver,
|
||||||
dns_resolver_cache_map: Default::default(),
|
dns_resolver_cache_map: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,12 +47,11 @@ impl ProxyApp {
|
|||||||
return Some(ipv4_address.to_string());
|
return Some(ipv4_address.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let ips = self.tokio_async_resolver.ipv4_lookup(hostname).await;
|
let ips = self.dns_resolver.ipv4_lookup(hostname).await;
|
||||||
log::debug!("DNS lookup {} --> {:#?}", hostname, ips);
|
log::debug!("DNS lookup {} --> {:#?}", hostname, ips);
|
||||||
match ips {
|
match ips {
|
||||||
Ok(ips) => {
|
Ok(ips) => {
|
||||||
let records = ips.as_lookup().records();
|
for record in ips.as_lookup().records() {
|
||||||
for record in records {
|
|
||||||
if let Some(RData::A(a)) = record.data() {
|
if let Some(RData::A(a)) = record.data() {
|
||||||
let ipv4_address = a.0.to_string();
|
let ipv4_address = a.0.to_string();
|
||||||
{
|
{
|
||||||
|
|||||||
42
src/cert.rs
42
src/cert.rs
@@ -36,35 +36,23 @@ pub fn issue_certificate(intermediate_certificate: &Certificate, domain: &str) -
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! ecc_private_key_convert {
|
||||||
|
($base_crate:ident, $pem:expr) => {{
|
||||||
|
use $base_crate::{pkcs8::{DecodePrivateKey, EncodePrivateKey, LineEnding}, SecretKey};
|
||||||
|
let secret_key = SecretKey::from_pkcs8_pem($pem);
|
||||||
|
if let Ok(secret_key) = secret_key {
|
||||||
|
if let Ok(pem) = secret_key.to_pkcs8_pem(LineEnding::CR) {
|
||||||
|
return pem.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_pkcs8(pem: &str) -> String {
|
fn parse_pkcs8(pem: &str) -> String {
|
||||||
// KeyPair only support PKCS#8 private key with public key, though public key is optional
|
// KeyPair only support PKCS#8 private key with public key, though public key is optional
|
||||||
{
|
ecc_private_key_convert!(p256, pem);
|
||||||
use p256::{pkcs8::{DecodePrivateKey, EncodePrivateKey, LineEnding}, SecretKey};
|
ecc_private_key_convert!(p384, pem);
|
||||||
let secret_key = SecretKey::from_pkcs8_pem(pem);
|
ecc_private_key_convert!(p521, pem);
|
||||||
if let Ok(secret_key) = secret_key {
|
|
||||||
if let Ok(pem) = secret_key.to_pkcs8_pem(LineEnding::CR) {
|
|
||||||
return pem.to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
use p384::{pkcs8::{DecodePrivateKey, EncodePrivateKey, LineEnding}, SecretKey};
|
|
||||||
let secret_key = SecretKey::from_pkcs8_pem(pem);
|
|
||||||
if let Ok(secret_key) = secret_key {
|
|
||||||
if let Ok(pem) = secret_key.to_pkcs8_pem(LineEnding::CR) {
|
|
||||||
return pem.to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
use p521::{pkcs8::{DecodePrivateKey, EncodePrivateKey, LineEnding}, SecretKey};
|
|
||||||
let secret_key = SecretKey::from_pkcs8_pem(pem);
|
|
||||||
if let Ok(secret_key) = secret_key {
|
|
||||||
if let Ok(pem) = secret_key.to_pkcs8_pem(LineEnding::CR) {
|
|
||||||
return pem.to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pem.to_string()
|
pem.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user