Skip to content

Commit 99dbbe4

Browse files
committed
refactor(http): [torrust#184] extract announce service in Axum tracker
1 parent 02e2516 commit 99dbbe4

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

src/http/axum_implementation/extractors/peer_ip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl From<ResolutionError> for responses::error::Error {
2929
///
3030
/// Will return an error if the peer IP cannot be obtained according to the configuration.
3131
/// For example, if the IP is extracted from an HTTP header which is missing in the request.
32-
pub fn peer_ip(on_reverse_proxy: bool, remote_client_ip: &RemoteClientIp) -> Result<IpAddr, Response> {
32+
pub fn assign_ip_address_to_peer(on_reverse_proxy: bool, remote_client_ip: &RemoteClientIp) -> Result<IpAddr, Response> {
3333
if on_reverse_proxy {
3434
if let Some(ip) = remote_client_ip.right_most_x_forwarded_for {
3535
Ok(ip)

src/http/axum_implementation/handlers/announce.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use axum::extract::State;
66
use axum::response::{IntoResponse, Response};
77
use log::debug;
88

9-
use crate::http::axum_implementation::extractors::peer_ip::peer_ip;
9+
use crate::http::axum_implementation::extractors::peer_ip::assign_ip_address_to_peer;
1010
use crate::http::axum_implementation::extractors::remote_client_ip::RemoteClientIp;
1111
use crate::http::axum_implementation::requests::announce::{Announce, Event, ExtractAnnounceRequest};
12-
use crate::http::axum_implementation::responses;
12+
use crate::http::axum_implementation::{responses, services};
1313
use crate::protocol::clock::{Current, Time};
1414
use crate::tracker::peer::Peer;
15-
use crate::tracker::{statistics, Tracker};
15+
use crate::tracker::Tracker;
1616

1717
#[allow(clippy::unused_async)]
1818
pub async fn handle(
@@ -22,31 +22,19 @@ pub async fn handle(
2222
) -> Response {
2323
debug!("http announce request: {:#?}", announce_request);
2424

25-
let info_hash = announce_request.info_hash;
26-
27-
let peer_ip = peer_ip(tracker.config.on_reverse_proxy, &remote_client_ip);
28-
29-
let peer_ip = match peer_ip {
25+
let peer_ip = match assign_ip_address_to_peer(tracker.config.on_reverse_proxy, &remote_client_ip) {
3026
Ok(peer_ip) => peer_ip,
3127
Err(err) => return err,
3228
};
3329

3430
let mut peer = peer_from_request(&announce_request, &peer_ip);
3531

36-
let response = tracker.announce(&info_hash, &mut peer, &peer_ip).await;
37-
38-
match peer_ip {
39-
IpAddr::V4(_) => {
40-
tracker.send_stats_event(statistics::Event::Tcp4Announce).await;
41-
}
42-
IpAddr::V6(_) => {
43-
tracker.send_stats_event(statistics::Event::Tcp6Announce).await;
44-
}
45-
}
32+
let response = services::announce::invoke(tracker.clone(), announce_request.info_hash, &mut peer).await;
4633

4734
responses::announce::Announce::from(response).into_response()
4835
}
4936

37+
/// It ignores the peer address in the announce request params.
5038
#[must_use]
5139
fn peer_from_request(announce_request: &Announce, peer_ip: &IpAddr) -> Peer {
5240
Peer {

src/http/axum_implementation/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pub mod resources;
66
pub mod responses;
77
pub mod routes;
88
pub mod server;
9+
pub mod services;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::net::IpAddr;
2+
use std::sync::Arc;
3+
4+
use crate::protocol::info_hash::InfoHash;
5+
use crate::tracker::peer::Peer;
6+
use crate::tracker::{statistics, AnnounceResponse, Tracker};
7+
8+
pub async fn invoke(tracker: Arc<Tracker>, info_hash: InfoHash, peer: &mut Peer) -> AnnounceResponse {
9+
let original_peer_ip = peer.peer_addr.ip();
10+
11+
// The tracker could change the original peer ip
12+
let response = tracker.announce(&info_hash, peer, &original_peer_ip).await;
13+
14+
match original_peer_ip {
15+
IpAddr::V4(_) => {
16+
tracker.send_stats_event(statistics::Event::Tcp4Announce).await;
17+
}
18+
IpAddr::V6(_) => {
19+
tracker.send_stats_event(statistics::Event::Tcp6Announce).await;
20+
}
21+
}
22+
23+
response
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod announce;

src/http/warp_implementation/handlers.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pub async fn handle_announce(
4949

5050
let mut peer = peer_builder::from_request(&announce_request, &remote_client_ip);
5151

52+
// todo: we should be use the http::axum_implementation::services::announce::announce service,
53+
// but this Warp implementation is going to be removed.
54+
5255
let response = tracker.announce(&info_hash, &mut peer, &remote_client_ip).await;
5356

5457
match remote_client_ip {

0 commit comments

Comments
 (0)