extract client ip from connection state

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-07 01:01:30 +00:00
parent 0bade5317f
commit 3ada847570
5 changed files with 36 additions and 0 deletions

29
Cargo.lock generated
View file

@ -227,6 +227,17 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "axum-client-ip"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72188bed20deb981f3a4a9fe674e5980fd9e9c2bd880baa94715ad5d60d64c67"
dependencies = [
"axum 0.7.5",
"forwarded-header-value",
"serde",
]
[[package]] [[package]]
name = "axum-core" name = "axum-core"
version = "0.3.4" version = "0.3.4"
@ -625,6 +636,7 @@ name = "conduit_api"
version = "0.4.1" version = "0.4.1"
dependencies = [ dependencies = [
"axum 0.7.5", "axum 0.7.5",
"axum-client-ip",
"axum-extra", "axum-extra",
"base64 0.22.1", "base64 0.22.1",
"bytes", "bytes",
@ -715,6 +727,7 @@ name = "conduit_router"
version = "0.4.1" version = "0.4.1"
dependencies = [ dependencies = [
"axum 0.7.5", "axum 0.7.5",
"axum-client-ip",
"axum-server", "axum-server",
"axum-server-dual-protocol", "axum-server-dual-protocol",
"bytes", "bytes",
@ -1100,6 +1113,16 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "forwarded-header-value"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835f84f38484cc86f110a805655697908257fb9a7af005234060891557198e9"
dependencies = [
"nonempty",
"thiserror",
]
[[package]] [[package]]
name = "futf" name = "futf"
version = "0.1.5" version = "0.1.5"
@ -2044,6 +2067,12 @@ dependencies = [
"minimal-lexical", "minimal-lexical",
] ]
[[package]]
name = "nonempty"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9e591e719385e6ebaeb5ce5d3887f7d5676fceca6411d1925ccc95745f3d6f7"
[[package]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" version = "0.46.0"

View file

@ -83,6 +83,9 @@ features = ["typed-header"]
version = "0.6.0" version = "0.6.0"
features = ["tls-rustls"] features = ["tls-rustls"]
[workspace.dependencies.axum-client-ip]
version = "0.6.0"
[workspace.dependencies.tower] [workspace.dependencies.tower]
version = "0.4.13" version = "0.4.13"
features = ["util"] features = ["util"]

View file

@ -33,6 +33,7 @@ brotli_compression = [
] ]
[dependencies] [dependencies]
axum-client-ip.workspace = true
axum-extra.workspace = true axum-extra.workspace = true
axum.workspace = true axum.workspace = true
base64.workspace = true base64.workspace = true

View file

@ -46,6 +46,7 @@ axum_dual_protocol = [
] ]
[dependencies] [dependencies]
axum-client-ip.workspace = true
axum-server-dual-protocol.optional = true axum-server-dual-protocol.optional = true
axum-server-dual-protocol.workspace = true axum-server-dual-protocol.workspace = true
axum-server.workspace = true axum-server.workspace = true

View file

@ -4,6 +4,7 @@ use axum::{
extract::{DefaultBodyLimit, MatchedPath}, extract::{DefaultBodyLimit, MatchedPath},
Router, Router,
}; };
use axum_client_ip::SecureClientIpSource;
use conduit::Server; use conduit::Server;
use http::{ use http::{
header::{self, HeaderName}, header::{self, HeaderName},
@ -46,6 +47,7 @@ pub(crate) fn build(server: &Arc<Server>) -> io::Result<Router> {
.on_response(DefaultOnResponse::new().level(Level::DEBUG)), .on_response(DefaultOnResponse::new().level(Level::DEBUG)),
) )
.layer(axum::middleware::from_fn_with_state(Arc::clone(server), request::handle)) .layer(axum::middleware::from_fn_with_state(Arc::clone(server), request::handle))
.layer(SecureClientIpSource::ConnectInfo.into_extension())
.layer(SetResponseHeaderLayer::if_not_present( .layer(SetResponseHeaderLayer::if_not_present(
HeaderName::from_static("origin-agent-cluster"), // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster HeaderName::from_static("origin-agent-cluster"), // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin-Agent-Cluster
HeaderValue::from_static("?1"), HeaderValue::from_static("?1"),