Skip to content
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["serde"]
default = ["serde", "connect-info"]
# Enables `ConnectInfo` extractor
connect-info = ["axum/tokio"]
# Enables `RightmostForwarded` extractor
forwarded-header = ["client-ip/forwarded-header"]
# Enables `ClientIpSource` serde compatibility
serde = ["dep:serde"]

[dependencies]
axum = { version = "0.8", default-features = false, features = ["tokio"] }
axum = { version = "0.8", default-features = false }
client-ip = "0.1"
serde = { version = "1", features = ["derive"], optional = true }

Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ use std::{
error::Error,
fmt,
marker::Sync,
net::{IpAddr, SocketAddr},
net::IpAddr,
str::FromStr,
};

use axum::{
extract::{ConnectInfo, Extension, FromRequestParts},
extract::{Extension, FromRequestParts},
http::{StatusCode, request::Parts},
response::{IntoResponse, Response},
};

#[cfg(feature = "connect-info")]
use std::net::SocketAddr;

#[cfg(feature = "connect-info")]
use axum::extract::ConnectInfo;

/// Defines an extractor
macro_rules! define_extractor {
(
Expand Down Expand Up @@ -116,6 +122,7 @@ pub enum ClientIpSource {
CfConnectingIp,
/// IP from the `CloudFront-Viewer-Address` header
CloudFrontViewerAddress,
#[cfg(feature = "connect-info")]
/// IP from the [`axum::extract::ConnectInfo`]
ConnectInfo,
/// IP from the `Fly-Client-IP` header
Expand Down Expand Up @@ -158,6 +165,7 @@ impl FromStr for ClientIpSource {
Ok(match s {
"CfConnectingIp" => Self::CfConnectingIp,
"CloudFrontViewerAddress" => Self::CloudFrontViewerAddress,
#[cfg(feature = "connect-info")]
"ConnectInfo" => Self::ConnectInfo,
"FlyClientIp" => Self::FlyClientIp,
#[cfg(feature = "forwarded-header")]
Expand All @@ -176,6 +184,7 @@ impl fmt::Display for ClientIpSource {
f.write_str(match self {
ClientIpSource::CfConnectingIp => "CfConnectingIp",
ClientIpSource::CloudFrontViewerAddress => "CloudFrontViewerAddress",
#[cfg(feature = "connect-info")]
ClientIpSource::ConnectInfo => "ConnectInfo",
ClientIpSource::FlyClientIp => "FlyClientIp",
#[cfg(feature = "forwarded-header")]
Expand Down Expand Up @@ -203,6 +212,7 @@ where
ClientIpSource::CloudFrontViewerAddress => {
CloudFrontViewerAddress::ip_from_headers(&parts.headers)
}
#[cfg(feature = "connect-info")]
ClientIpSource::ConnectInfo => parts
.extensions
.get::<ConnectInfo<SocketAddr>>()
Expand All @@ -227,6 +237,7 @@ where
#[non_exhaustive]
#[derive(Debug, PartialEq)]
pub enum Rejection {
#[cfg(feature = "connect-info")]
/// No [`axum::extract::ConnectInfo`] in extensions
NoConnectInfo,
/// No [`ClientIpSource`] in extensions
Expand All @@ -244,6 +255,7 @@ impl From<client_ip::Error> for Rejection {
impl fmt::Display for Rejection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "connect-info")]
Rejection::NoConnectInfo => {
write!(f, "Add `axum::extract::ConnectInfo` to request extensions")
}
Expand All @@ -261,7 +273,9 @@ impl std::error::Error for Rejection {}
impl IntoResponse for Rejection {
fn into_response(self) -> Response {
let title = match self {
Self::NoConnectInfo | Self::NoClientIpSource => "500 Axum Misconfiguration",
#[cfg(feature = "connect-info")]
Self::NoConnectInfo => "500 Axum Misconfiguration",
Self::NoClientIpSource => "500 Axum Misconfiguration",
Self::ClientIp { .. } => "500 Proxy Server Misconfiguration",
};
let footer = "(the request is rejected by axum-client-ip)";
Expand Down Expand Up @@ -526,6 +540,7 @@ mod tests {

assert_match(ClientIpSource::CfConnectingIp);
assert_match(ClientIpSource::CloudFrontViewerAddress);
#[cfg(feature = "connect-info")]
assert_match(ClientIpSource::ConnectInfo);
assert_match(ClientIpSource::FlyClientIp);
#[cfg(feature = "forwarded-header")]
Expand Down
Loading