Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba6e9f9

Browse files
committedMar 17, 2025
refactor: [#1373] capture socket address from connection info in HTTP tracker
Instead of only the IP. The port will be abailable in evetns so we can build metrics also using the client's port.
1 parent 2de6c14 commit ba6e9f9

File tree

8 files changed

+31
-32
lines changed

8 files changed

+31
-32
lines changed
 

‎packages/axum-http-tracker-server/src/v1/extractors/client_ip_sources.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ where
6363
};
6464

6565
let connection_info_ip = match ConnectInfo::<SocketAddr>::from_request_parts(parts, state).await {
66-
Ok(connection_info_socket_addr) => Some(connection_info_socket_addr.0.ip()),
66+
Ok(connection_info_socket_addr) => Some(connection_info_socket_addr.0),
6767
Err(_) => None,
6868
};
6969

7070
Ok(Extract(ClientIpSources {
7171
right_most_x_forwarded_for,
72-
connection_info_ip,
72+
connection_info_socket_address: connection_info_ip,
7373
}))
7474
}
7575
}

‎packages/axum-http-tracker-server/src/v1/handlers/announce.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ mod tests {
194194
fn sample_client_ip_sources() -> ClientIpSources {
195195
ClientIpSources {
196196
right_most_x_forwarded_for: None,
197-
connection_info_ip: None,
197+
connection_info_socket_address: None,
198198
}
199199
}
200200

@@ -335,7 +335,7 @@ mod tests {
335335

336336
let client_ip_sources = ClientIpSources {
337337
right_most_x_forwarded_for: None,
338-
connection_info_ip: None,
338+
connection_info_socket_address: None,
339339
};
340340

341341
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -378,7 +378,7 @@ mod tests {
378378

379379
let client_ip_sources = ClientIpSources {
380380
right_most_x_forwarded_for: None,
381-
connection_info_ip: None,
381+
connection_info_socket_address: None,
382382
};
383383

384384
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

‎packages/axum-http-tracker-server/src/v1/handlers/scrape.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn build_response(scrape_data: ScrapeData) -> Response {
7979

8080
#[cfg(test)]
8181
mod tests {
82-
use std::net::IpAddr;
82+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
8383
use std::str::FromStr;
8484
use std::sync::Arc;
8585

@@ -155,7 +155,7 @@ mod tests {
155155
fn sample_client_ip_sources() -> ClientIpSources {
156156
ClientIpSources {
157157
right_most_x_forwarded_for: Some(IpAddr::from_str("203.0.113.195").unwrap()),
158-
connection_info_ip: Some(IpAddr::from_str("203.0.113.196").unwrap()),
158+
connection_info_socket_address: Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 196)), 8080)),
159159
}
160160
}
161161

@@ -282,7 +282,7 @@ mod tests {
282282

283283
let client_ip_sources = ClientIpSources {
284284
right_most_x_forwarded_for: None,
285-
connection_info_ip: None,
285+
connection_info_socket_address: None,
286286
};
287287

288288
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -327,7 +327,7 @@ mod tests {
327327

328328
let client_ip_sources = ClientIpSources {
329329
right_most_x_forwarded_for: None,
330-
connection_info_ip: None,
330+
connection_info_socket_address: None,
331331
};
332332

333333
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

‎packages/http-protocol/src/v1/services/peer_ip_resolver.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! ```
2121
//!
2222
//! Depending on the tracker configuration.
23-
use std::net::IpAddr;
23+
use std::net::{IpAddr, SocketAddr};
2424
use std::panic::Location;
2525

2626
use serde::{Deserialize, Serialize};
@@ -31,8 +31,9 @@ use thiserror::Error;
3131
pub struct ClientIpSources {
3232
/// The right most IP from the `X-Forwarded-For` HTTP header.
3333
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>,
3637
}
3738

3839
/// The error that can occur when resolving the peer IP.
@@ -45,6 +46,7 @@ pub enum PeerIpResolutionError {
4546
"missing or invalid the right most X-Forwarded-For IP (mandatory on reverse proxy tracker configuration) in {location}"
4647
)]
4748
MissingRightMostXForwardedForIp { location: &'static Location<'static> },
49+
4850
/// The peer IP cannot be obtained because the tracker is not configured as
4951
/// a reverse proxy but the connection info was not provided to the Axum
5052
/// framework via a route extension.
@@ -71,7 +73,7 @@ pub enum PeerIpResolutionError {
7173
/// on_reverse_proxy,
7274
/// &ClientIpSources {
7375
/// 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,
7577
/// },
7678
/// )
7779
/// .unwrap();
@@ -93,7 +95,7 @@ pub enum PeerIpResolutionError {
9395
/// on_reverse_proxy,
9496
/// &ClientIpSources {
9597
/// 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))
9799
/// },
98100
/// )
99101
/// .unwrap();
@@ -114,8 +116,8 @@ pub fn invoke(on_reverse_proxy: bool, client_ip_sources: &ClientIpSources) -> Re
114116
}
115117

116118
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())
119121
} else {
120122
Err(PeerIpResolutionError::MissingClientIp {
121123
location: Location::caller(),
@@ -138,7 +140,7 @@ mod tests {
138140
use super::invoke;
139141

140142
mod working_without_reverse_proxy {
141-
use std::net::IpAddr;
143+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
142144
use std::str::FromStr;
143145

144146
use super::invoke;
@@ -152,7 +154,7 @@ mod tests {
152154
on_reverse_proxy,
153155
&ClientIpSources {
154156
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)),
156158
},
157159
)
158160
.unwrap();
@@ -168,7 +170,7 @@ mod tests {
168170
on_reverse_proxy,
169171
&ClientIpSources {
170172
right_most_x_forwarded_for: None,
171-
connection_info_ip: None,
173+
connection_info_socket_address: None,
172174
},
173175
)
174176
.unwrap_err();
@@ -191,7 +193,7 @@ mod tests {
191193
on_reverse_proxy,
192194
&ClientIpSources {
193195
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,
195197
},
196198
)
197199
.unwrap();
@@ -207,7 +209,7 @@ mod tests {
207209
on_reverse_proxy,
208210
&ClientIpSources {
209211
right_most_x_forwarded_for: None,
210-
connection_info_ip: None,
212+
connection_info_socket_address: None,
211213
},
212214
)
213215
.unwrap_err();

‎packages/http-tracker-core/benches/helpers/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn sample_announce_request_for_peer(peer: Peer) -> (Announce, ClientIpSource
9393

9494
let client_ip_sources = ClientIpSources {
9595
right_most_x_forwarded_for: None,
96-
connection_info_ip: Some(peer.peer_addr.ip()),
96+
connection_info_socket_address: Some(SocketAddr::new(peer.peer_addr.ip(), 8080)),
9797
};
9898

9999
(announce_request, client_ip_sources)

‎packages/http-tracker-core/src/services/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ mod tests {
296296

297297
let client_ip_sources = ClientIpSources {
298298
right_most_x_forwarded_for: None,
299-
connection_info_ip: Some(peer.peer_addr.ip()),
299+
connection_info_socket_address: Some(SocketAddr::new(peer.peer_addr.ip(), 8080)),
300300
};
301301

302302
(announce_request, client_ip_sources)

‎packages/http-tracker-core/src/services/scrape.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ mod tests {
298298

299299
let client_ip_sources = ClientIpSources {
300300
right_most_x_forwarded_for: None,
301-
connection_info_ip: Some(original_peer_ip),
301+
connection_info_socket_address: Some(SocketAddr::new(original_peer_ip, 8080)),
302302
};
303303

304304
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -356,7 +356,7 @@ mod tests {
356356

357357
let client_ip_sources = ClientIpSources {
358358
right_most_x_forwarded_for: None,
359-
connection_info_ip: Some(peer_ip),
359+
connection_info_socket_address: Some(SocketAddr::new(peer_ip, 8080)),
360360
};
361361

362362
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -404,7 +404,7 @@ mod tests {
404404

405405
let client_ip_sources = ClientIpSources {
406406
right_most_x_forwarded_for: None,
407-
connection_info_ip: Some(peer_ip),
407+
connection_info_socket_address: Some(SocketAddr::new(peer_ip, 8080)),
408408
};
409409

410410
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -472,7 +472,7 @@ mod tests {
472472

473473
let client_ip_sources = ClientIpSources {
474474
right_most_x_forwarded_for: None,
475-
connection_info_ip: Some(original_peer_ip),
475+
connection_info_socket_address: Some(SocketAddr::new(original_peer_ip, 8080)),
476476
};
477477

478478
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -522,7 +522,7 @@ mod tests {
522522

523523
let client_ip_sources = ClientIpSources {
524524
right_most_x_forwarded_for: None,
525-
connection_info_ip: Some(peer_ip),
525+
connection_info_socket_address: Some(SocketAddr::new(peer_ip, 8080)),
526526
};
527527

528528
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);
@@ -570,7 +570,7 @@ mod tests {
570570

571571
let client_ip_sources = ClientIpSources {
572572
right_most_x_forwarded_for: None,
573-
connection_info_ip: Some(peer_ip),
573+
connection_info_socket_address: Some(SocketAddr::new(peer_ip, 8080)),
574574
};
575575

576576
let server_socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7070);

‎packages/http-tracker-core/src/statistics/event/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ pub mod listener;
55
pub mod sender;
66

77
/// An statistics event. It is used to collect tracker metrics.
8-
///
9-
/// - `Tcp` prefix means the event was triggered by the HTTP tracker.
10-
/// - The event suffix is the type of request: `announce` or `scrape`.
118
#[derive(Debug, PartialEq, Eq)]
129
pub enum Event {
1310
TcpAnnounce { connection: ConnectionContext },

0 commit comments

Comments
 (0)
Please sign in to comment.