2024-05-30 22:38:39 +00:00
|
|
|
use std::{net::SocketAddr, sync::Arc};
|
|
|
|
|
2024-06-06 22:31:52 +00:00
|
|
|
use axum::Router;
|
2024-05-30 22:38:39 +00:00
|
|
|
use axum_server::{bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle};
|
|
|
|
#[cfg(feature = "axum_dual_protocol")]
|
|
|
|
use axum_server_dual_protocol::ServerExt;
|
|
|
|
use conduit::{Result, Server};
|
|
|
|
use tokio::task::JoinSet;
|
|
|
|
use tracing::{debug, info, warn};
|
|
|
|
|
|
|
|
pub(super) async fn serve(
|
2024-06-06 22:31:52 +00:00
|
|
|
server: &Arc<Server>, app: Router, handle: ServerHandle, addrs: Vec<SocketAddr>,
|
2024-05-30 22:38:39 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
let config = &server.config;
|
|
|
|
let tls = config.tls.as_ref().expect("TLS configuration");
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"Using direct TLS. Certificate path {} and certificate private key path {}",
|
|
|
|
&tls.certs, &tls.key
|
|
|
|
);
|
|
|
|
info!(
|
|
|
|
"Note: It is strongly recommended that you use a reverse proxy instead of running conduwuit directly with TLS."
|
|
|
|
);
|
|
|
|
let conf = RustlsConfig::from_pem_file(&tls.certs, &tls.key).await?;
|
|
|
|
|
|
|
|
if cfg!(feature = "axum_dual_protocol") {
|
|
|
|
info!(
|
|
|
|
"conduwuit was built with axum_dual_protocol feature to listen on both HTTP and HTTPS. This will only \
|
|
|
|
take effect if `dual_protocol` is enabled in `[global.tls]`"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut join_set = JoinSet::new();
|
2024-06-06 22:31:52 +00:00
|
|
|
let app = app.into_make_service_with_connect_info::<SocketAddr>();
|
2024-05-30 22:38:39 +00:00
|
|
|
if cfg!(feature = "axum_dual_protocol") && tls.dual_protocol {
|
|
|
|
#[cfg(feature = "axum_dual_protocol")]
|
|
|
|
for addr in &addrs {
|
|
|
|
join_set.spawn_on(
|
|
|
|
axum_server_dual_protocol::bind_dual_protocol(*addr, conf.clone())
|
|
|
|
.set_upgrade(false)
|
|
|
|
.handle(handle.clone())
|
|
|
|
.serve(app.clone()),
|
|
|
|
server.runtime(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for addr in &addrs {
|
|
|
|
join_set.spawn_on(
|
|
|
|
bind_rustls(*addr, conf.clone())
|
|
|
|
.handle(handle.clone())
|
|
|
|
.serve(app.clone()),
|
|
|
|
server.runtime(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg!(feature = "axum_dual_protocol") && tls.dual_protocol {
|
|
|
|
warn!(
|
|
|
|
"Listening on {:?} with TLS certificate {} and supporting plain text (HTTP) connections too (insecure!)",
|
|
|
|
addrs, &tls.certs
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!("Listening on {:?} with TLS certificate {}", addrs, &tls.certs);
|
|
|
|
}
|
|
|
|
|
|
|
|
while join_set.join_next().await.is_some() {}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|