Skip to content

Commit

Permalink
fix(http): expose connecting ip via extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Sep 8, 2024
1 parent 89c54d1 commit 5f095a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions foundations/src/http/server/stream/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ impl<S: ServiceHandler> Connection<S> {
}
};

let ip_addr = connection.remote_address().ip();

let mut connection = match self
.builder
.build(h3_quinn::Connection::new(connection))
Expand Down Expand Up @@ -277,10 +279,9 @@ impl<S: ServiceHandler> Connection<S> {
hijack_conn_tx: hijack_conn_tx.clone(),
stream: stream.clone(),
});

request.extensions_mut().insert(SocketKind::Quic);

request.extensions_mut().insert(ctx.clone());
request.extensions_mut().insert(ip_addr);

let connection_context = connection_handle.context();

Expand Down
5 changes: 5 additions & 0 deletions foundations/src/http/server/stream/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl Backend for TcpBackend {
builder: self.builder.clone(),
service,
parent_ctx: ctx,
peer_addr: addr,
keep_alive_timeout: self.keep_alive_timeout,
}
.serve()
Expand All @@ -130,6 +131,7 @@ struct Connection<S: ServiceHandler> {
connection: TcpStream,
builder: Arc<Builder<TokioExecutor>>,
service: S,
peer_addr: std::net::SocketAddr,
parent_ctx: crate::context::Context,
keep_alive_timeout: Option<std::time::Duration>,
}
Expand All @@ -150,6 +152,8 @@ impl<S: ServiceHandler> Connection<S> {

let active_requests = Arc::new(std::sync::atomic::AtomicUsize::new(0));

let ip_addr = self.peer_addr.ip();

let service_fn = {
let service = self.service.clone();
let span = tracing::Span::current();
Expand All @@ -163,6 +167,7 @@ impl<S: ServiceHandler> Connection<S> {
let ctx = make_ctx();
req.extensions_mut().insert(ctx.clone());
req.extensions_mut().insert(SocketKind::Tcp);
req.extensions_mut().insert(ip_addr);
let resp = service.on_request(req.map(Body::new)).await.into_response();
drop(ctx);
drop(guard);
Expand Down
5 changes: 5 additions & 0 deletions foundations/src/http/server/stream/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl Backend for TlsBackend {
acceptor: self.acceptor.clone(),
service,
parent_ctx: ctx,
peer_addr: addr,
keep_alive_timeout: self.keep_alive_timeout,
}
.serve()
Expand All @@ -141,6 +142,7 @@ struct Connection<S: ServiceHandler> {
builder: Arc<Builder<TokioExecutor>>,
acceptor: Arc<TlsAcceptor>,
service: S,
peer_addr: std::net::SocketAddr,
keep_alive_timeout: Option<std::time::Duration>,
parent_ctx: crate::context::Context,
}
Expand Down Expand Up @@ -174,6 +176,8 @@ impl<S: ServiceHandler> Connection<S> {
Arc::new(move || handle.context())
};

let ip_addr = self.peer_addr.ip();

let active_requests = Arc::new(std::sync::atomic::AtomicUsize::new(0));

let service_fn = {
Expand All @@ -190,6 +194,7 @@ impl<S: ServiceHandler> Connection<S> {
let ctx = make_ctx();
req.extensions_mut().insert(ctx.clone());
req.extensions_mut().insert(SocketKind::TlsTcp);
req.extensions_mut().insert(ip_addr);
let resp = service.on_request(req.map(Body::new)).await.into_response();
drop(ctx);
drop(guard);
Expand Down

0 comments on commit 5f095a4

Please sign in to comment.