Update 'h3' and 's2n_quic' dependencies.

This commit is contained in:
Sergio Benitez 2024-08-16 16:09:54 -07:00
parent faa0543c3d
commit 51d4ed4394
4 changed files with 22 additions and 32 deletions

View File

@ -33,7 +33,7 @@ uuid = ["uuid_", "rocket_http/uuid"]
tls = ["rustls", "tokio-rustls", "rustls-pemfile"]
mtls = ["tls", "x509-parser"]
tokio-macros = ["tokio/macros"]
trace = ["tracing-subscriber", "tinyvec", "thread_local", "rustls?/logging", "tokio-rustls?/logging", "multer/log"]
trace = ["tracing-subscriber", "tinyvec", "thread_local", "rustls?/logging", "tokio-rustls?/logging", "multer/log", "s2n-quic-h3?/tracing"]
[dependencies]
# Optional serialization dependencies.
@ -128,7 +128,7 @@ optional = true
[dependencies.s2n-quic-h3]
git = "https://github.com/SergioBenitez/s2n-quic-h3.git"
rev = "865fd25"
rev = "7aa3be0"
optional = true
[target.'cfg(unix)'.dependencies]

View File

@ -48,10 +48,10 @@ pub struct QuicListener {
tls: TlsConfig,
}
pub struct H3Stream(H3Conn);
pub struct H3Stream(H3Conn, quic::connection::Result<SocketAddr>);
pub struct H3Connection {
pub(crate) handle: quic::connection::Handle,
pub(crate) remote: quic::connection::Result<SocketAddr>,
pub(crate) parts: http::request::Parts,
pub(crate) tx: QuicTx,
pub(crate) rx: QuicRx,
@ -104,9 +104,10 @@ impl QuicListener {
}
pub async fn connect(&self, accept: quic::Connection) -> io::Result<H3Stream> {
let remote = accept.remote_addr();
let quic_conn = quic_h3::Connection::new(accept);
let conn = H3Conn::new(quic_conn).await.map_err(io::Error::other)?;
Ok(H3Stream(conn))
Ok(H3Stream(conn, remote))
}
pub fn endpoint(&self) -> io::Result<Endpoint> {
@ -116,7 +117,7 @@ impl QuicListener {
impl H3Stream {
pub async fn accept(&mut self) -> io::Result<Option<H3Connection>> {
let handle = self.0.inner.conn.handle().clone();
let remote = self.1.clone();
let ((parts, _), (tx, rx)) = match self.0.accept().await {
Ok(Some((req, stream))) => (req.into_parts(), stream.split()),
Ok(None) => return Ok(None),
@ -129,7 +130,7 @@ impl H3Stream {
}
};
Ok(Some(H3Connection { handle, parts, tx: QuicTx(tx), rx: QuicRx(rx) }))
Ok(Some(H3Connection { remote, parts, tx: QuicTx(tx), rx: QuicRx(rx) }))
}
}
@ -158,8 +159,7 @@ impl QuicTx {
// FIXME: Expose certificates when possible.
impl H3Connection {
pub fn endpoint(&self) -> io::Result<Endpoint> {
let addr = self.handle.remote_addr()?;
Ok(Endpoint::Quic(addr).assume_tls())
Ok(Endpoint::Quic(self.remote?).assume_tls())
}
}

View File

@ -5,6 +5,9 @@
# directly for your browser to show connections as secure. You should NEVER use
# these certificate/key pairs. They are here for DEMONSTRATION PURPOSES ONLY.
[default]
log_format = "compact"
[default.tls]
certs = "private/rsa_sha256_cert.pem"
key = "private/rsa_sha256_key.pem"

View File

@ -3,7 +3,7 @@
use std::net::SocketAddr;
use rocket::http::Status;
use rocket::tracing::Level;
use rocket::tracing::{self, Instrument};
use rocket::{route, Error, Request, Data, Route, Orbit, Rocket, Ignite};
use rocket::fairing::{Fairing, Info, Kind};
use rocket::response::Redirect;
@ -45,16 +45,13 @@ impl Redirector {
pub async fn try_launch(self, config: Config) -> Result<Rocket<Ignite>, Error> {
use rocket::http::Method::*;
rocket::span_info!("HTTP -> HTTPS Redirector" => {
info!(from = self.0, to = config.tls_addr.port(), "redirecting");
});
// Build a vector of routes to `redirect` on `<path..>` for each method.
let redirects = [Get, Put, Post, Delete, Options, Head, Trace, Connect, Patch]
.into_iter()
.map(|m| Route::new(m, "/<path..>", Self::redirect))
.collect::<Vec<_>>();
info!(from = self.0, to = config.tls_addr.port(), "redirecting");
let addr = SocketAddr::new(config.tls_addr.ip(), self.0);
rocket::custom(&config.server)
.manage(config)
@ -73,35 +70,25 @@ impl Fairing for Redirector {
}
}
#[tracing::instrument(name = "HTTP -> HTTPS Redirector", skip_all)]
async fn on_liftoff(&self, rocket: &Rocket<Orbit>) {
let Some(tls_addr) = rocket.endpoints().find_map(|e| e.tls()?.tcp()) else {
rocket::span_warn!("HTTP -> HTTPS Redirector" => {
warn!("Main instance is not being served over TLS/TCP.\n\
Redirector refusing to start.");
});
return;
};
let config = Config {
tls_addr,
server: rocket::Config {
log_level: Some(Level::ERROR),
..rocket.config().clone()
},
};
let this = *self;
let shutdown = rocket.shutdown();
let span = tracing::info_span!("HTTP -> HTTPS Redirector");
let config = Config { tls_addr, server: rocket.config().clone() };
rocket::tokio::spawn(async move {
if let Err(e) = this.try_launch(config).await {
span_error!("HTTP -> HTTPS Redirector", "failed to start" => {
e.trace_error();
info!("shutting down main instance");
});
shutdown.notify();
}
});
}.instrument(span));
}
}