-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #1269: Overhaul core Tracker: review whitelist functionality
eca5c59 refactor: [#1268] move scrape logic from udp server to udp_tracker_core package (Jose Celano) c0fc390 refactor: [#1268] move announce logic from udp server to udp_tracker_core package (Jose Celano) 37a142e refactor: [#1268] move scrape logic from axum to http_tracker_core package (Jose Celano) 74815ab refactor: [#1268] move announce logic from axum to http_tracker_core package (Jose Celano) e48aaf5 [#1268] move udp services to udp_tracker_core package (Jose Celano) 73753e3 [#1268] move http services to http_tracker_core package (Jose Celano) dec742e refactor: [#1268] extract servers::udp::services::scrape service (Jose Celano) 3c07b26 refactor: [#1268] extract servers::udp::services::announce service (Jose Celano) 81825c9 refactor: [#1268] separate UDP handlers into diferent modules (Jose Celano) Pull request description: Overhaul core Tracker: review whitelist functionality. ### Sub-tasks - [x] Introduce submodules for handlers in UDP: `servers::udp::handlers::{announce, scrape}`. - [x] Create the missing services (app layer) in the UDP tracker. There is no intermediary level between handlers and the core tracker. It will moved to its own package `udp-tracker-core` later. - [x] Move the service `services::announce::invoke()` in the HTTP tracker to the `http-tracker-core` package. - [x] Move the service `services::announce::invoke()` in the UDP tracker to the `udp-tracker-core` package. - [x] Move logic from the handler (in the framework level - delivery layer) to the application service in the `http-tracker-core` package. - [x] For the `announce` request - [x] For the `scrape` request - [x] Move logic from the handler (controller level - delivery layer) to the application service in the `udp-tracker-core` package. - [x] For the `announce` request - [x] For the `scrape` request - [ ] ~~Add version module also for the UDP tracker. I don't see any reason to use `v1` in the http tracker but not in the UDP tracker.~~ I will leave this until we introduce a new major version. ### Sub-tasks for a new PR I've left these tasks for a new [issue](#1270). This PR is just moving things and the new tasks imply changing function signatures. - [ ] In the tracker-core announce handler return a `Result<AnnounceData, AnnounceError>` when the torrent is not included in the whitelist. - [ ] In the tracker-core scrape handler return a `Result<ScrapeData, ScrapeError>` so we are able to return errors in the future without breaking the public API. ACKs for top commit: josecelano: ACK eca5c59 Tree-SHA512: d3ee37ffa806e8a86813fe564e3840fab7bfc44d2072f85bc2eba84ac3402c95c0f6a5beb2725071cb0498415f55915431b656e98c71b3a6bf469de961c37f03
- Loading branch information
Showing
23 changed files
with
2,248 additions
and
1,974 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
//! Primitive types and function for `BitTorrent` HTTP trackers. | ||
pub mod percent_encoding; | ||
pub mod v1; | ||
|
||
use torrust_tracker_clock::clock; | ||
|
||
/// This code needs to be copied into each crate. | ||
/// Working version, for production. | ||
#[cfg(not(test))] | ||
#[allow(dead_code)] | ||
pub(crate) type CurrentClock = clock::Working; | ||
|
||
/// Stopped version, for testing. | ||
#[cfg(test)] | ||
#[allow(dead_code)] | ||
pub(crate) type CurrentClock = clock::Stopped; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod services; | ||
pub mod statistics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod peer_builder; | ||
pub mod services; | ||
pub mod statistics; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! The `announce` service. | ||
//! | ||
//! The service is responsible for handling the `announce` requests. | ||
//! | ||
//! It delegates the `announce` logic to the [`AnnounceHandler`] and it returns | ||
//! the [`AnnounceData`]. | ||
//! | ||
//! It also sends an [`udp_tracker_core::statistics::event::Event`] | ||
//! because events are specific for the HTTP tracker. | ||
use std::net::{IpAddr, SocketAddr}; | ||
use std::sync::Arc; | ||
|
||
use aquatic_udp_protocol::AnnounceRequest; | ||
use bittorrent_primitives::info_hash::InfoHash; | ||
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted}; | ||
use bittorrent_tracker_core::error::WhitelistError; | ||
use bittorrent_tracker_core::whitelist; | ||
use torrust_tracker_primitives::core::AnnounceData; | ||
use torrust_tracker_primitives::peer; | ||
|
||
use crate::packages::udp_tracker_core::{self, peer_builder}; | ||
|
||
/// It handles the `Announce` request. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It will return an error if: | ||
/// | ||
/// - The tracker is running in listed mode and the torrent is not in the | ||
/// whitelist. | ||
#[allow(clippy::too_many_arguments)] | ||
pub async fn handle_announce( | ||
remote_addr: SocketAddr, | ||
request: &AnnounceRequest, | ||
announce_handler: &Arc<AnnounceHandler>, | ||
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>, | ||
opt_udp_stats_event_sender: &Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>, | ||
) -> Result<AnnounceData, WhitelistError> { | ||
let info_hash = request.info_hash.into(); | ||
let remote_client_ip = remote_addr.ip(); | ||
|
||
// Authorization | ||
whitelist_authorization.authorize(&info_hash).await?; | ||
|
||
let mut peer = peer_builder::from_request(request, &remote_client_ip); | ||
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into(); | ||
|
||
let announce_data = invoke( | ||
announce_handler.clone(), | ||
opt_udp_stats_event_sender.clone(), | ||
info_hash, | ||
&mut peer, | ||
&peers_wanted, | ||
) | ||
.await; | ||
|
||
Ok(announce_data) | ||
} | ||
|
||
pub async fn invoke( | ||
announce_handler: Arc<AnnounceHandler>, | ||
opt_udp_stats_event_sender: Arc<Option<Box<dyn udp_tracker_core::statistics::event::sender::Sender>>>, | ||
info_hash: InfoHash, | ||
peer: &mut peer::Peer, | ||
peers_wanted: &PeersWanted, | ||
) -> AnnounceData { | ||
let original_peer_ip = peer.peer_addr.ip(); | ||
|
||
// The tracker could change the original peer ip | ||
let announce_data = announce_handler.announce(&info_hash, peer, &original_peer_ip, peers_wanted); | ||
|
||
if let Some(udp_stats_event_sender) = opt_udp_stats_event_sender.as_deref() { | ||
match original_peer_ip { | ||
IpAddr::V4(_) => { | ||
udp_stats_event_sender | ||
.send_event(udp_tracker_core::statistics::event::Event::Udp4Announce) | ||
.await; | ||
} | ||
IpAddr::V6(_) => { | ||
udp_stats_event_sender | ||
.send_event(udp_tracker_core::statistics::event::Event::Udp6Announce) | ||
.await; | ||
} | ||
} | ||
} | ||
|
||
announce_data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod announce; | ||
pub mod scrape; |
Oops, something went wrong.