20
20
//! ```
21
21
//!
22
22
//! Depending on the tracker configuration.
23
- use std:: net:: IpAddr ;
23
+ use std:: net:: { IpAddr , SocketAddr } ;
24
24
use std:: panic:: Location ;
25
25
26
26
use serde:: { Deserialize , Serialize } ;
@@ -31,8 +31,9 @@ use thiserror::Error;
31
31
pub struct ClientIpSources {
32
32
/// The right most IP from the `X-Forwarded-For` HTTP header.
33
33
pub right_most_x_forwarded_for : Option < IpAddr > ,
34
- /// The IP from the connection info.
35
- pub connection_info_ip : Option < IpAddr > ,
34
+
35
+ /// The client's socket address from the connection info.
36
+ pub connection_info_socket_address : Option < SocketAddr > ,
36
37
}
37
38
38
39
/// The error that can occur when resolving the peer IP.
@@ -45,6 +46,7 @@ pub enum PeerIpResolutionError {
45
46
"missing or invalid the right most X-Forwarded-For IP (mandatory on reverse proxy tracker configuration) in {location}"
46
47
) ]
47
48
MissingRightMostXForwardedForIp { location : & ' static Location < ' static > } ,
49
+
48
50
/// The peer IP cannot be obtained because the tracker is not configured as
49
51
/// a reverse proxy but the connection info was not provided to the Axum
50
52
/// framework via a route extension.
@@ -71,7 +73,7 @@ pub enum PeerIpResolutionError {
71
73
/// on_reverse_proxy,
72
74
/// &ClientIpSources {
73
75
/// right_most_x_forwarded_for: Some(IpAddr::from_str("203.0.113.195").unwrap()),
74
- /// connection_info_ip : None,
76
+ /// connection_info_socket_address : None,
75
77
/// },
76
78
/// )
77
79
/// .unwrap();
@@ -93,7 +95,7 @@ pub enum PeerIpResolutionError {
93
95
/// on_reverse_proxy,
94
96
/// &ClientIpSources {
95
97
/// right_most_x_forwarded_for: None,
96
- /// connection_info_ip : Some(IpAddr::from_str(" 203.0. 113. 195").unwrap()),
98
+ /// connection_info_socket_address : Some(SocketAddr::new( IpAddr::V4(Ipv4Addr::new( 203, 0, 113, 195)), 8080))
97
99
/// },
98
100
/// )
99
101
/// .unwrap();
@@ -114,8 +116,8 @@ pub fn invoke(on_reverse_proxy: bool, client_ip_sources: &ClientIpSources) -> Re
114
116
}
115
117
116
118
fn resolve_peer_ip_without_reverse_proxy ( remote_client_ip : & ClientIpSources ) -> Result < IpAddr , PeerIpResolutionError > {
117
- if let Some ( ip ) = remote_client_ip. connection_info_ip {
118
- Ok ( ip )
119
+ if let Some ( socket_addr ) = remote_client_ip. connection_info_socket_address {
120
+ Ok ( socket_addr . ip ( ) )
119
121
} else {
120
122
Err ( PeerIpResolutionError :: MissingClientIp {
121
123
location : Location :: caller ( ) ,
@@ -138,7 +140,7 @@ mod tests {
138
140
use super :: invoke;
139
141
140
142
mod working_without_reverse_proxy {
141
- use std:: net:: IpAddr ;
143
+ use std:: net:: { IpAddr , Ipv4Addr , SocketAddr } ;
142
144
use std:: str:: FromStr ;
143
145
144
146
use super :: invoke;
@@ -152,7 +154,7 @@ mod tests {
152
154
on_reverse_proxy,
153
155
& ClientIpSources {
154
156
right_most_x_forwarded_for : None ,
155
- connection_info_ip : Some ( IpAddr :: from_str ( " 203.0. 113. 195" ) . unwrap ( ) ) ,
157
+ connection_info_socket_address : Some ( SocketAddr :: new ( IpAddr :: V4 ( Ipv4Addr :: new ( 203 , 0 , 113 , 195 ) ) , 8080 ) ) ,
156
158
} ,
157
159
)
158
160
. unwrap ( ) ;
@@ -168,7 +170,7 @@ mod tests {
168
170
on_reverse_proxy,
169
171
& ClientIpSources {
170
172
right_most_x_forwarded_for : None ,
171
- connection_info_ip : None ,
173
+ connection_info_socket_address : None ,
172
174
} ,
173
175
)
174
176
. unwrap_err ( ) ;
@@ -191,7 +193,7 @@ mod tests {
191
193
on_reverse_proxy,
192
194
& ClientIpSources {
193
195
right_most_x_forwarded_for : Some ( IpAddr :: from_str ( "203.0.113.195" ) . unwrap ( ) ) ,
194
- connection_info_ip : None ,
196
+ connection_info_socket_address : None ,
195
197
} ,
196
198
)
197
199
. unwrap ( ) ;
@@ -207,7 +209,7 @@ mod tests {
207
209
on_reverse_proxy,
208
210
& ClientIpSources {
209
211
right_most_x_forwarded_for : None ,
210
- connection_info_ip : None ,
212
+ connection_info_socket_address : None ,
211
213
} ,
212
214
)
213
215
. unwrap_err ( ) ;
0 commit comments