Skip to content

chore(iroh-dns-server): cleanup some code #2941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what makes this async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should use tokio::fs::read but I screwed it up..thanks for noticing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new pr: #2941

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
Loading