Skip to content

Commit

Permalink
dev: remove announce_request wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Jul 13, 2024
1 parent 2aef245 commit f71ca54
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 47 deletions.
11 changes: 4 additions & 7 deletions src/servers/udp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::core::{statistics, ScrapeData, Tracker};
use crate::servers::udp::error::Error;
use crate::servers::udp::logging::{log_bad_request, log_error_response, log_request, log_response};
use crate::servers::udp::peer_builder;
use crate::servers::udp::request::AnnounceWrapper;
use crate::shared::bit_torrent::common::MAX_SCRAPE_TORRENTS;

/// It handles the incoming UDP packets.
Expand Down Expand Up @@ -152,17 +151,15 @@ pub async fn handle_announce(

check(&remote_addr, &from_connection_id(&announce_request.connection_id))?;

let wrapped_announce_request = AnnounceWrapper::new(announce_request);

let info_hash = wrapped_announce_request.info_hash;
let info_hash = InfoHash(announce_request.info_hash.0);
let remote_client_ip = remote_addr.ip();

// Authorization
tracker.authorize(&info_hash).await.map_err(|e| Error::TrackerError {
source: (Arc::new(e) as Arc<dyn std::error::Error + Send + Sync>).into(),
})?;

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

let response = tracker.announce(&info_hash, &mut peer, &remote_client_ip);

Expand All @@ -179,7 +176,7 @@ pub async fn handle_announce(
if remote_addr.is_ipv4() {
let announce_response = AnnounceResponse {
fixed: AnnounceResponseFixedData {
transaction_id: wrapped_announce_request.announce_request.transaction_id,
transaction_id: announce_request.transaction_id,
announce_interval: AnnounceInterval(I32::new(i64::from(tracker.get_announce_policy().interval) as i32)),
leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)),
seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)),
Expand All @@ -206,7 +203,7 @@ pub async fn handle_announce(
} else {
let announce_response = AnnounceResponse {
fixed: AnnounceResponseFixedData {
transaction_id: wrapped_announce_request.announce_request.transaction_id,
transaction_id: announce_request.transaction_id,
announce_interval: AnnounceInterval(I32::new(i64::from(tracker.get_announce_policy().interval) as i32)),
leechers: NumberOfPeers(I32::new(i64::from(response.stats.incomplete) as i32)),
seeders: NumberOfPeers(I32::new(i64::from(response.stats.complete) as i32)),
Expand Down
4 changes: 0 additions & 4 deletions src/servers/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@
//! UDP packet -> Aquatic Struct Request -> [Torrust Struct Request] -> Tracker -> Aquatic Struct Response -> UDP packet
//! ```
//!
//! For the `Announce` request there is a wrapper struct [`AnnounceWrapper`](crate::servers::udp::request::AnnounceWrapper).
//! It was added to add an extra field with the internal [`InfoHash`](torrust_tracker_primitives::info_hash::InfoHash) struct.
//!
//! ### Connect
//!
//! `Connect` requests are used to get a connection ID which must be provided on
Expand Down Expand Up @@ -646,7 +643,6 @@ pub mod error;
pub mod handlers;
pub mod logging;
pub mod peer_builder;
pub mod request;
pub mod server;

pub const UDP_TRACKER_LOG_TARGET: &str = "UDP TRACKER";
Expand Down
15 changes: 7 additions & 8 deletions src/servers/udp/peer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use torrust_tracker_clock::clock::Time;
use torrust_tracker_primitives::announce_event::AnnounceEvent;
use torrust_tracker_primitives::{peer, NumberOfBytes};

use super::request::AnnounceWrapper;
use crate::CurrentClock;

/// Extracts the [`peer::Peer`] info from the
Expand All @@ -16,21 +15,21 @@ use crate::CurrentClock;
/// * `announce_wrapper` - The announce request to extract the peer info from.
/// * `peer_ip` - The real IP address of the peer, not the one in the announce request.
#[must_use]
pub fn from_request(announce_wrapper: &AnnounceWrapper, peer_ip: &IpAddr) -> peer::Peer {
let announce_event = match aquatic_udp_protocol::AnnounceEvent::from(announce_wrapper.announce_request.event) {
pub fn from_request(announce_request: &aquatic_udp_protocol::AnnounceRequest, peer_ip: &IpAddr) -> peer::Peer {
let announce_event = match aquatic_udp_protocol::AnnounceEvent::from(announce_request.event) {
aquatic_udp_protocol::AnnounceEvent::Started => AnnounceEvent::Started,
aquatic_udp_protocol::AnnounceEvent::Stopped => AnnounceEvent::Stopped,
aquatic_udp_protocol::AnnounceEvent::Completed => AnnounceEvent::Completed,
aquatic_udp_protocol::AnnounceEvent::None => AnnounceEvent::None,
};

peer::Peer {
peer_id: peer::Id(announce_wrapper.announce_request.peer_id.0),
peer_addr: SocketAddr::new(*peer_ip, announce_wrapper.announce_request.port.0.into()),
peer_id: peer::Id(announce_request.peer_id.0),
peer_addr: SocketAddr::new(*peer_ip, announce_request.port.0.into()),
updated: CurrentClock::now(),
uploaded: NumberOfBytes(announce_wrapper.announce_request.bytes_uploaded.0.into()),
downloaded: NumberOfBytes(announce_wrapper.announce_request.bytes_downloaded.0.into()),
left: NumberOfBytes(announce_wrapper.announce_request.bytes_left.0.into()),
uploaded: NumberOfBytes(announce_request.bytes_uploaded.0.into()),
downloaded: NumberOfBytes(announce_request.bytes_downloaded.0.into()),
left: NumberOfBytes(announce_request.bytes_left.0.into()),
event: announce_event,
}
}
28 changes: 0 additions & 28 deletions src/servers/udp/request.rs

This file was deleted.

0 comments on commit f71ca54

Please sign in to comment.