add connection info to router

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-06 22:31:52 +00:00
parent c2267d4c03
commit 0bade5317f
5 changed files with 27 additions and 18 deletions

View file

@ -25,7 +25,7 @@ const CONDUWUIT_CSP: &str = "sandbox; default-src 'none'; font-src 'none'; scrip
form-action 'none'; base-uri 'none';"; form-action 'none'; base-uri 'none';";
const CONDUWUIT_PERMISSIONS_POLICY: &str = "interest-cohort=(),browsing-topics=()"; const CONDUWUIT_PERMISSIONS_POLICY: &str = "interest-cohort=(),browsing-topics=()";
pub(crate) fn build(server: &Arc<Server>) -> io::Result<axum::routing::IntoMakeService<Router>> { pub(crate) fn build(server: &Arc<Server>) -> io::Result<Router> {
let layers = ServiceBuilder::new(); let layers = ServiceBuilder::new();
#[cfg(feature = "sentry_telemetry")] #[cfg(feature = "sentry_telemetry")]
@ -74,10 +74,7 @@ pub(crate) fn build(server: &Arc<Server>) -> io::Result<axum::routing::IntoMakeS
.layer(body_limit_layer(server)) .layer(body_limit_layer(server))
.layer(CatchPanicLayer::custom(catch_panic)); .layer(CatchPanicLayer::custom(catch_panic));
let routes = router::build(server); Ok(router::build(server).layer(layers))
let layers = routes.layer(layers);
Ok(layers.into_make_service())
} }
#[cfg(any(feature = "zstd_compression", feature = "gzip_compression", feature = "brotli_compression"))] #[cfg(any(feature = "zstd_compression", feature = "gzip_compression", feature = "brotli_compression"))]

View file

@ -4,14 +4,14 @@ mod unix;
use std::sync::Arc; use std::sync::Arc;
use axum::{routing::IntoMakeService, Router}; use axum::Router;
use axum_server::Handle as ServerHandle; use axum_server::Handle as ServerHandle;
use conduit::{Error, Result, Server}; use conduit::{Error, Result, Server};
use tokio::sync::broadcast; use tokio::sync::broadcast;
/// Serve clients /// Serve clients
pub(super) async fn serve( pub(super) async fn serve(
server: &Arc<Server>, app: IntoMakeService<Router>, handle: ServerHandle, shutdown: broadcast::Receiver<()>, server: &Arc<Server>, app: Router, handle: ServerHandle, shutdown: broadcast::Receiver<()>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let config = &server.config; let config = &server.config;
let addrs = config.get_bind_addrs(); let addrs = config.get_bind_addrs();

View file

@ -3,15 +3,16 @@ use std::{
sync::{atomic::Ordering, Arc}, sync::{atomic::Ordering, Arc},
}; };
use axum::{routing::IntoMakeService, Router}; use axum::Router;
use axum_server::{bind, Handle as ServerHandle}; use axum_server::{bind, Handle as ServerHandle};
use conduit::{debug_info, Result, Server}; use conduit::{debug_info, Result, Server};
use tokio::task::JoinSet; use tokio::task::JoinSet;
use tracing::info; use tracing::info;
pub(super) async fn serve( pub(super) async fn serve(
server: &Arc<Server>, app: IntoMakeService<Router>, handle: ServerHandle, addrs: Vec<SocketAddr>, server: &Arc<Server>, app: Router, handle: ServerHandle, addrs: Vec<SocketAddr>,
) -> Result<()> { ) -> Result<()> {
let app = app.into_make_service_with_connect_info::<SocketAddr>();
let mut join_set = JoinSet::new(); let mut join_set = JoinSet::new();
for addr in &addrs { for addr in &addrs {
join_set.spawn_on(bind(*addr).handle(handle.clone()).serve(app.clone()), server.runtime()); join_set.spawn_on(bind(*addr).handle(handle.clone()).serve(app.clone()), server.runtime());

View file

@ -1,6 +1,6 @@
use std::{net::SocketAddr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use axum::{routing::IntoMakeService, Router}; use axum::Router;
use axum_server::{bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle}; use axum_server::{bind_rustls, tls_rustls::RustlsConfig, Handle as ServerHandle};
#[cfg(feature = "axum_dual_protocol")] #[cfg(feature = "axum_dual_protocol")]
use axum_server_dual_protocol::ServerExt; use axum_server_dual_protocol::ServerExt;
@ -9,7 +9,7 @@ use tokio::task::JoinSet;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
pub(super) async fn serve( pub(super) async fn serve(
server: &Arc<Server>, app: IntoMakeService<Router>, handle: ServerHandle, addrs: Vec<SocketAddr>, server: &Arc<Server>, app: Router, handle: ServerHandle, addrs: Vec<SocketAddr>,
) -> Result<()> { ) -> Result<()> {
let config = &server.config; let config = &server.config;
let tls = config.tls.as_ref().expect("TLS configuration"); let tls = config.tls.as_ref().expect("TLS configuration");
@ -31,6 +31,7 @@ pub(super) async fn serve(
} }
let mut join_set = JoinSet::new(); let mut join_set = JoinSet::new();
let app = app.into_make_service_with_connect_info::<SocketAddr>();
if cfg!(feature = "axum_dual_protocol") && tls.dual_protocol { if cfg!(feature = "axum_dual_protocol") && tls.dual_protocol {
#[cfg(feature = "axum_dual_protocol")] #[cfg(feature = "axum_dual_protocol")]
for addr in &addrs { for addr in &addrs {

View file

@ -1,8 +1,15 @@
#![cfg(unix)] #![cfg(unix)]
use std::{path::Path, sync::Arc}; use std::{
net::{self, IpAddr, Ipv4Addr},
path::Path,
sync::Arc,
};
use axum::{extract::Request, routing::IntoMakeService, Router}; use axum::{
extract::{connect_info::IntoMakeServiceWithConnectInfo, Request},
Router,
};
use conduit::{debug_error, trace, utils, Error, Result, Server}; use conduit::{debug_error, trace, utils, Error, Result, Server};
use hyper::{body::Incoming, service::service_fn}; use hyper::{body::Incoming, service::service_fn};
use hyper_util::{ use hyper_util::{
@ -19,12 +26,15 @@ use tower::{Service, ServiceExt};
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use utils::unwrap_infallible; use utils::unwrap_infallible;
type MakeService = IntoMakeServiceWithConnectInfo<Router, net::SocketAddr>;
static NULL_ADDR: net::SocketAddr = net::SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub(super) async fn serve( pub(super) async fn serve(server: &Arc<Server>, app: Router, mut shutdown: broadcast::Receiver<()>) -> Result<()> {
server: &Arc<Server>, app: IntoMakeService<Router>, mut shutdown: broadcast::Receiver<()>,
) -> Result<()> {
let mut tasks = JoinSet::<()>::new(); let mut tasks = JoinSet::<()>::new();
let executor = TokioExecutor::new(); let executor = TokioExecutor::new();
let app = app.into_make_service_with_connect_info::<net::SocketAddr>();
let builder = server::conn::auto::Builder::new(executor); let builder = server::conn::auto::Builder::new(executor);
let listener = init(server).await?; let listener = init(server).await?;
loop { loop {
@ -46,14 +56,14 @@ pub(super) async fn serve(
#[allow(clippy::let_underscore_must_use)] #[allow(clippy::let_underscore_must_use)]
async fn accept( async fn accept(
server: &Arc<Server>, listener: &UnixListener, tasks: &mut JoinSet<()>, mut app: IntoMakeService<Router>, server: &Arc<Server>, listener: &UnixListener, tasks: &mut JoinSet<()>, mut app: MakeService,
builder: server::conn::auto::Builder<TokioExecutor>, conn: (UnixStream, SocketAddr), builder: server::conn::auto::Builder<TokioExecutor>, conn: (UnixStream, SocketAddr),
) { ) {
let (socket, remote) = conn; let (socket, remote) = conn;
let socket = TokioIo::new(socket); let socket = TokioIo::new(socket);
trace!(?listener, ?socket, ?remote, "accepted"); trace!(?listener, ?socket, ?remote, "accepted");
let called = unwrap_infallible(app.call(()).await); let called = unwrap_infallible(app.call(NULL_ADDR).await);
let handler = service_fn(move |req: Request<Incoming>| called.clone().oneshot(req)); let handler = service_fn(move |req: Request<Incoming>| called.clone().oneshot(req));
let task = async move { let task = async move {