Skip to content

Commit

Permalink
chore(iroh-dns-server): cleanup some code
Browse files Browse the repository at this point in the history
- avoid spawn-blocking
- remove unused imports
  • Loading branch information
dignifiedquire committed Nov 16, 2024
1 parent 38bfe5e commit e81f88e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
23 changes: 10 additions & 13 deletions iroh-dns-server/src/http/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ impl TlsAcceptor {
let cert_path = dir.join(format!("{keyname}.crt"));
let key_path = dir.join(format!("{keyname}.key"));

let (certs, secret_key) = tokio::task::spawn_blocking(move || {
let certs = load_certs(cert_path)?;
let key = load_secret_key(key_path)?;
anyhow::Ok((certs, key))
})
.await??;
let certs = load_certs(cert_path).await?;
let secret_key = load_secret_key(key_path).await?;

let config = config.with_single_cert(certs, secret_key)?;
let config = RustlsConfig::from_config(Arc::new(config));
Expand Down Expand Up @@ -136,23 +132,24 @@ impl TlsAcceptor {
}
}

fn load_certs(
async fn load_certs(
filename: impl AsRef<Path>,
) -> Result<Vec<rustls::pki_types::CertificateDer<'static>>> {
let certfile = std::fs::File::open(filename).context("cannot open certificate file")?;
let mut reader = std::io::BufReader::new(certfile);

let certfile = tokio::fs::read(filename)
.await
.context("cannot open certificate file")?;
let mut reader = std::io::Cursor::new(certfile);
let certs: Result<Vec<_>, std::io::Error> = rustls_pemfile::certs(&mut reader).collect();
let certs = certs?;

Ok(certs)
}

fn load_secret_key(
async fn load_secret_key(
filename: impl AsRef<Path>,
) -> Result<rustls::pki_types::PrivateKeyDer<'static>> {
let keyfile = std::fs::File::open(filename.as_ref()).context("cannot open secret key file")?;
let mut reader = std::io::BufReader::new(keyfile);
let keyfile = std::fs::read(filename.as_ref()).context("cannot open secret key file")?;
let mut reader = std::io::Cursor::new(keyfile);

loop {
match rustls_pemfile::read_one(&mut reader).context("cannot parse secret key .pem file")? {
Expand Down
14 changes: 2 additions & 12 deletions iroh-dns-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
#![allow(unused_imports)]

use std::{
future::Future,
net::{Ipv4Addr, SocketAddr},
path::PathBuf,
};
use std::path::PathBuf;

use anyhow::Result;
use axum::{routing::get, Router};
use clap::Parser;
use futures_lite::FutureExt;
use iroh_dns_server::{
config::Config, metrics::init_metrics, server::run_with_config_until_ctrl_c,
};
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
use tracing::{debug, debug_span, error, error_span, Instrument, Span};
use tracing::debug;

#[derive(Parser, Debug)]
struct Cli {
Expand Down

0 comments on commit e81f88e

Please sign in to comment.