Skip to content

Commit 67d62ef

Browse files
committed
refactor: [#1326] extract bittorrent_udp_tracker_core::services::announce::AnnounceService
1 parent e1d9aa4 commit 67d62ef

File tree

5 files changed

+145
-185
lines changed

5 files changed

+145
-185
lines changed

packages/udp-tracker-core/src/container.rs

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bittorrent_tracker_core::whitelist;
77
use tokio::sync::RwLock;
88
use torrust_tracker_configuration::{Core, UdpTracker};
99

10+
use crate::services::announce::AnnounceService;
1011
use crate::services::banning::BanService;
1112
use crate::services::connect::ConnectService;
1213
use crate::{statistics, MAX_CONNECTION_ID_ERRORS_PER_IP};
@@ -23,6 +24,7 @@ pub struct UdpTrackerCoreContainer {
2324
pub udp_core_stats_repository: Arc<statistics::repository::Repository>,
2425
pub ban_service: Arc<RwLock<BanService>>,
2526
pub connect_service: Arc<ConnectService>,
27+
pub announce_service: Arc<AnnounceService>,
2628
}
2729

2830
impl UdpTrackerCoreContainer {
@@ -43,6 +45,11 @@ impl UdpTrackerCoreContainer {
4345
let udp_core_stats_repository = Arc::new(udp_core_stats_repository);
4446
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
4547
let connect_service = Arc::new(ConnectService::new(udp_core_stats_event_sender.clone()));
48+
let announce_service = Arc::new(AnnounceService::new(
49+
tracker_core_container.announce_handler.clone(),
50+
tracker_core_container.whitelist_authorization.clone(),
51+
udp_core_stats_event_sender.clone(),
52+
));
4653

4754
Arc::new(UdpTrackerCoreContainer {
4855
core_config: tracker_core_container.core_config.clone(),
@@ -55,6 +62,7 @@ impl UdpTrackerCoreContainer {
5562
udp_core_stats_repository: udp_core_stats_repository.clone(),
5663
ban_service: ban_service.clone(),
5764
connect_service: connect_service.clone(),
65+
announce_service: announce_service.clone(),
5866
})
5967
}
6068
}

packages/udp-tracker-core/src/services/announce.rs

+71-87
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ use std::ops::Range;
1212
use std::sync::Arc;
1313

1414
use aquatic_udp_protocol::AnnounceRequest;
15-
use bittorrent_primitives::info_hash::InfoHash;
1615
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
1716
use bittorrent_tracker_core::error::{AnnounceError, WhitelistError};
1817
use bittorrent_tracker_core::whitelist;
1918
use bittorrent_udp_tracker_protocol::peer_builder;
2019
use torrust_tracker_primitives::core::AnnounceData;
21-
use torrust_tracker_primitives::peer;
2220

2321
use crate::connection_cookie::{check, gen_remote_fingerprint, ConnectionCookieError};
2422
use crate::statistics;
@@ -59,95 +57,81 @@ impl From<WhitelistError> for UdpAnnounceError {
5957
}
6058
}
6159

62-
/// It handles the `Announce` request.
63-
///
64-
/// # Errors
65-
///
66-
/// It will return an error if:
67-
///
68-
/// - The tracker is running in listed mode and the torrent is not in the
69-
/// whitelist.
70-
#[allow(clippy::too_many_arguments)]
71-
pub async fn handle_announce(
72-
remote_addr: SocketAddr,
73-
request: &AnnounceRequest,
74-
announce_handler: &Arc<AnnounceHandler>,
75-
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
76-
opt_udp_stats_event_sender: &Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
77-
cookie_valid_range: Range<f64>,
78-
) -> Result<AnnounceData, UdpAnnounceError> {
79-
// Authentication
80-
check(
81-
&request.connection_id,
82-
gen_remote_fingerprint(&remote_addr),
83-
cookie_valid_range,
84-
)?;
85-
86-
let info_hash = request.info_hash.into();
87-
let remote_client_ip = remote_addr.ip();
88-
89-
// Authorization
90-
whitelist_authorization.authorize(&info_hash).await?;
91-
92-
let mut peer = peer_builder::from_request(request, &remote_client_ip);
93-
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
94-
95-
let original_peer_ip = peer.peer_addr.ip();
96-
97-
// The tracker could change the original peer ip
98-
let announce_data = announce_handler
99-
.announce(&info_hash, &mut peer, &original_peer_ip, &peers_wanted)
100-
.await?;
101-
102-
if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
103-
match original_peer_ip {
104-
IpAddr::V4(_) => {
105-
udp_stats_event_sender
106-
.send_event(statistics::event::Event::Udp4Announce)
107-
.await;
108-
}
109-
IpAddr::V6(_) => {
110-
udp_stats_event_sender
111-
.send_event(statistics::event::Event::Udp6Announce)
112-
.await;
113-
}
60+
/// The `AnnounceService` is responsible for handling the `announce` requests.
61+
pub struct AnnounceService {
62+
pub announce_handler: Arc<AnnounceHandler>,
63+
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
64+
pub opt_udp_core_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
65+
}
66+
67+
impl AnnounceService {
68+
#[must_use]
69+
pub fn new(
70+
announce_handler: Arc<AnnounceHandler>,
71+
whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
72+
opt_udp_core_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
73+
) -> Self {
74+
Self {
75+
announce_handler,
76+
whitelist_authorization,
77+
opt_udp_core_stats_event_sender,
11478
}
11579
}
11680

117-
Ok(announce_data)
118-
}
119-
120-
/// # Errors
121-
///
122-
/// It will return an error if the announce request fails.
123-
pub async fn invoke(
124-
announce_handler: Arc<AnnounceHandler>,
125-
opt_udp_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
126-
info_hash: InfoHash,
127-
peer: &mut peer::Peer,
128-
peers_wanted: &PeersWanted,
129-
) -> Result<AnnounceData, AnnounceError> {
130-
let original_peer_ip = peer.peer_addr.ip();
131-
132-
// The tracker could change the original peer ip
133-
let announce_data = announce_handler
134-
.announce(&info_hash, peer, &original_peer_ip, peers_wanted)
135-
.await?;
136-
137-
if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() {
138-
match original_peer_ip {
139-
IpAddr::V4(_) => {
140-
udp_stats_event_sender
141-
.send_event(statistics::event::Event::Udp4Announce)
142-
.await;
143-
}
144-
IpAddr::V6(_) => {
145-
udp_stats_event_sender
146-
.send_event(statistics::event::Event::Udp6Announce)
147-
.await;
81+
/// It handles the `Announce` request.
82+
///
83+
/// # Errors
84+
///
85+
/// It will return an error if:
86+
///
87+
/// - The tracker is running in listed mode and the torrent is not in the
88+
/// whitelist.
89+
#[allow(clippy::too_many_arguments)]
90+
pub async fn handle_announce(
91+
&self,
92+
remote_addr: SocketAddr,
93+
request: &AnnounceRequest,
94+
cookie_valid_range: Range<f64>,
95+
) -> Result<AnnounceData, UdpAnnounceError> {
96+
// Authentication
97+
check(
98+
&request.connection_id,
99+
gen_remote_fingerprint(&remote_addr),
100+
cookie_valid_range,
101+
)?;
102+
103+
let info_hash = request.info_hash.into();
104+
let remote_client_ip = remote_addr.ip();
105+
106+
// Authorization
107+
self.whitelist_authorization.authorize(&info_hash).await?;
108+
109+
let mut peer = peer_builder::from_request(request, &remote_client_ip);
110+
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
111+
112+
let original_peer_ip = peer.peer_addr.ip();
113+
114+
// The tracker could change the original peer ip
115+
let announce_data = self
116+
.announce_handler
117+
.announce(&info_hash, &mut peer, &original_peer_ip, &peers_wanted)
118+
.await?;
119+
120+
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
121+
match original_peer_ip {
122+
IpAddr::V4(_) => {
123+
udp_stats_event_sender
124+
.send_event(statistics::event::Event::Udp4Announce)
125+
.await;
126+
}
127+
IpAddr::V6(_) => {
128+
udp_stats_event_sender
129+
.send_event(statistics::event::Event::Udp6Announce)
130+
.await;
131+
}
148132
}
149133
}
150-
}
151134

152-
Ok(announce_data)
135+
Ok(announce_data)
136+
}
153137
}

0 commit comments

Comments
 (0)