Skip to content

Commit c0fc390

Browse files
committed
refactor: [torrust#1268] move announce logic from udp server to udp_tracker_core package
1 parent 37a142e commit c0fc390

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

src/packages/udp_tracker_core/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod peer_builder;
12
pub mod services;
23
pub mod statistics;
File renamed without changes.

src/packages/udp_tracker_core/services/announce.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,55 @@
77
//!
88
//! It also sends an [`udp_tracker_core::statistics::event::Event`]
99
//! because events are specific for the HTTP tracker.
10-
use std::net::IpAddr;
10+
use std::net::{IpAddr, SocketAddr};
1111
use std::sync::Arc;
1212

13+
use aquatic_udp_protocol::AnnounceRequest;
1314
use bittorrent_primitives::info_hash::InfoHash;
1415
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
16+
use bittorrent_tracker_core::error::WhitelistError;
17+
use bittorrent_tracker_core::whitelist;
1518
use torrust_tracker_primitives::core::AnnounceData;
1619
use torrust_tracker_primitives::peer;
1720

18-
use crate::packages::udp_tracker_core;
21+
use crate::packages::udp_tracker_core::{self, peer_builder};
22+
23+
/// It handles the `Announce` request.
24+
///
25+
/// # Errors
26+
///
27+
/// It will return an error if:
28+
///
29+
/// - The tracker is running in listed mode and the torrent is not in the
30+
/// whitelist.
31+
#[allow(clippy::too_many_arguments)]
32+
pub async fn handle_announce(
33+
remote_addr: SocketAddr,
34+
request: &AnnounceRequest,
35+
announce_handler: &Arc<AnnounceHandler>,
36+
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
37+
opt_udp_stats_event_sender: &Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>,
38+
) -> Result<AnnounceData, WhitelistError> {
39+
let info_hash = request.info_hash.into();
40+
let remote_client_ip = remote_addr.ip();
41+
42+
// Authorization
43+
whitelist_authorization.authorize(&info_hash).await?;
44+
45+
let mut peer = peer_builder::from_request(request, &remote_client_ip);
46+
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
47+
48+
let announce_data = invoke(
49+
announce_handler.clone(),
50+
opt_udp_stats_event_sender.clone(),
51+
info_hash,
52+
&mut peer,
53+
&peers_wanted,
54+
)
55+
.await;
56+
57+
Ok(announce_data)
58+
}
1959

2060
pub async fn invoke(
2161
announce_handler: Arc<AnnounceHandler>,

src/servers/udp/handlers/announce.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ use aquatic_udp_protocol::{
88
Port, Response, ResponsePeer, TransactionId,
99
};
1010
use bittorrent_primitives::info_hash::InfoHash;
11-
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
11+
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
1212
use bittorrent_tracker_core::whitelist;
1313
use torrust_tracker_configuration::Core;
1414
use tracing::{instrument, Level};
1515
use zerocopy::network_endian::I32;
1616

17-
use crate::packages::udp_tracker_core::{self, services};
17+
use crate::packages::udp_tracker_core::{self};
1818
use crate::servers::udp::connection_cookie::check;
1919
use crate::servers::udp::error::Error;
2020
use crate::servers::udp::handlers::gen_remote_fingerprint;
21-
use crate::servers::udp::peer_builder;
2221

2322
/// It handles the `Announce` request. Refer to [`Announce`](crate::servers::udp#announce)
2423
/// request for more information.
@@ -44,36 +43,29 @@ pub async fn handle_announce(
4443

4544
tracing::trace!("handle announce");
4645

46+
// todo: move authentication to `udp_tracker_core::services::announce::handle_announce`
47+
4748
check(
4849
&request.connection_id,
4950
gen_remote_fingerprint(&remote_addr),
5051
cookie_valid_range,
5152
)
5253
.map_err(|e| (e, request.transaction_id))?;
5354

54-
let info_hash = request.info_hash.into();
55-
let remote_client_ip = remote_addr.ip();
56-
57-
// Authorization
58-
whitelist_authorization
59-
.authorize(&info_hash)
60-
.await
61-
.map_err(|e| Error::TrackerError {
62-
source: (Arc::new(e) as Arc<dyn std::error::Error + Send + Sync>).into(),
63-
})
64-
.map_err(|e| (e, request.transaction_id))?;
65-
66-
let mut peer = peer_builder::from_request(request, &remote_client_ip);
67-
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
68-
69-
let response = services::announce::invoke(
70-
announce_handler.clone(),
71-
opt_udp_stats_event_sender.clone(),
72-
info_hash,
73-
&mut peer,
74-
&peers_wanted,
55+
let response = udp_tracker_core::services::announce::handle_announce(
56+
remote_addr,
57+
request,
58+
announce_handler,
59+
whitelist_authorization,
60+
opt_udp_stats_event_sender,
7561
)
76-
.await;
62+
.await
63+
.map_err(|e| Error::TrackerError {
64+
source: (Arc::new(e) as Arc<dyn std::error::Error + Send + Sync>).into(),
65+
})
66+
.map_err(|e| (e, request.transaction_id))?;
67+
68+
// todo: extract `build_response` function.
7769

7870
#[allow(clippy::cast_possible_truncation)]
7971
if remote_addr.is_ipv4() {

src/servers/udp/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ use std::net::SocketAddr;
640640
pub mod connection_cookie;
641641
pub mod error;
642642
pub mod handlers;
643-
pub mod peer_builder;
644643
pub mod server;
645644

646645
pub const UDP_TRACKER_LOG_TARGET: &str = "UDP TRACKER";

0 commit comments

Comments
 (0)