Skip to content
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

fix: don't crash the derper #1110

Merged
merged 4 commits into from
Jun 16, 2023
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
14 changes: 6 additions & 8 deletions src/hp/derp/client_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,16 @@ where
loop {
trace!("tick");
tokio::select! {
biased;

_ = done.cancelled() => {
trace!("cancelled");
// final flush
self.io.flush().await?;
return Ok(());
}
read_res = read_frame(&mut self.io, MAX_FRAME_SIZE, &mut read_buf) => {
trace!("handle read");
self.handle_read(read_res, &mut read_buf).await?;
}
peer = self.peer_gone.recv() => {
Expand Down Expand Up @@ -496,14 +501,7 @@ where
}
Ok(())
}
Err(err) => {
if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
return Ok(());
}
}
Err(err)
}
Err(err) => Err(err),
}
}

Expand Down
49 changes: 31 additions & 18 deletions src/hp/derp/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,21 +365,26 @@ where
// Note: This can't possibly be fulfilled until the 101 response
// is returned below, so it's better to spawn this future instead
// waiting for it to complete to then return a response.
tokio::task::spawn(async move {
match hyper::upgrade::on(&mut req).await {
Ok(upgraded) => {
if let Err(e) =
derp_connection_handler(&closure_conn_handler, upgraded).await
{
tracing::warn!(
"server \"{HTTP_UPGRADE_PROTOCOL}\" io error: {:?}",
e
)
};
tokio::task::spawn(
async move {
match hyper::upgrade::on(&mut req).await {
Ok(upgraded) => {
if let Err(e) =
derp_connection_handler(&closure_conn_handler, upgraded).await
{
tracing::warn!(
"server \"{HTTP_UPGRADE_PROTOCOL}\" io error: {:?}",
e
);
} else {
tracing::info!("server \"{HTTP_UPGRADE_PROTOCOL}\" success");
};
}
Err(e) => tracing::warn!("upgrade error: {:?}", e),
}
Err(e) => tracing::warn!("upgrade error: {:?}", e),
}
});
.instrument(tracing::debug_span!("derp_connection_handler")),
);

// Now return a 101 Response saying we agree to the upgrade to the
// HTTP_UPGRADE_PROTOCOL
Expand Down Expand Up @@ -514,6 +519,7 @@ impl DerpService {
match tls_config {
Some(tls_config) => self.tls_serve_connection(stream, tls_config).await,
None => {
debug!("HTTP: serve connection");
self.serve_connection(MaybeTlsStreamServer::Plain(stream))
.await
}
Expand All @@ -526,18 +532,25 @@ impl DerpService {
match acceptor {
TlsAcceptor::LetsEncrypt(a) => match a.accept(stream).await? {
None => {
info!("received TLS-ALPN-01 validation request");
info!("TLS[acme]: received TLS-ALPN-01 validation request");
}
Some(start_handshake) => {
let tls_stream = start_handshake.into_stream(config).await?;
debug!("TLS[acme]: start handshake");
let tls_stream = start_handshake
.into_stream(config)
.await
.context("TLS[acme] handshake")?;
self.serve_connection(MaybeTlsStreamServer::Tls(tls_stream))
.await?;
.await
.context("TLS[acme] serve connection")?;
}
},
TlsAcceptor::Manual(a) => {
let tls_stream = a.accept(stream).await?;
debug!("TLS[manual]: accept");
let tls_stream = a.accept(stream).await.context("TLS[manual] accept")?;
self.serve_connection(MaybeTlsStreamServer::Tls(tls_stream))
.await?;
.await
.context("TLS[manual] serve connection")?;
}
}
Ok(())
Expand Down