Skip to content

Commit ec0e437

Browse files
committed
refactor: [torrust#1338] clean bittorrent_http_tracker_core::services::announce::AnnounceService
1 parent 5178aba commit ec0e437

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

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

+46-36
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::sync::Arc;
1313

1414
use bittorrent_http_tracker_protocol::v1::requests::announce::{peer_from_request, Announce};
1515
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::{self, ClientIpSources, PeerIpResolutionError};
16+
use bittorrent_primitives::info_hash::InfoHash;
1617
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
1718
use bittorrent_tracker_core::authentication::service::AuthenticationService;
1819
use bittorrent_tracker_core::authentication::{self, Key};
@@ -73,50 +74,61 @@ impl AnnounceService {
7374
client_ip_sources: &ClientIpSources,
7475
maybe_key: Option<Key>,
7576
) -> Result<AnnounceData, HttpAnnounceError> {
76-
// Authentication
77-
if self.core_config.private {
78-
match maybe_key {
79-
Some(key) => match self.authentication_service.authenticate(&key).await {
80-
Ok(()) => (),
81-
Err(error) => return Err(error.into()),
82-
},
83-
None => {
84-
return Err(authentication::key::Error::MissingAuthKey {
85-
location: Location::caller(),
86-
}
87-
.into())
88-
}
89-
}
90-
}
77+
self.authenticate(maybe_key).await?;
9178

92-
// Authorization
93-
match self.whitelist_authorization.authorize(&announce_request.info_hash).await {
94-
Ok(()) => (),
95-
Err(error) => return Err(error.into()),
96-
}
79+
self.authorize(announce_request.info_hash).await?;
9780

98-
let peer_ip = match peer_ip_resolver::invoke(self.core_config.net.on_reverse_proxy, client_ip_sources) {
99-
Ok(peer_ip) => peer_ip,
100-
Err(error) => return Err(error.into()),
101-
};
81+
let remote_client_ip = self.resolve_remote_client_ip(client_ip_sources)?;
10282

103-
let mut peer = peer_from_request(announce_request, &peer_ip);
83+
let mut peer = peer_from_request(announce_request, &remote_client_ip);
10484

105-
let peers_wanted = match announce_request.numwant {
106-
Some(numwant) => PeersWanted::only(numwant),
107-
None => PeersWanted::AsManyAsPossible,
108-
};
109-
110-
let original_peer_ip = peer.peer_addr.ip();
85+
let peers_wanted = Self::peers_wanted(announce_request);
11186

112-
// The tracker could change the original peer ip
11387
let announce_data = self
11488
.announce_handler
115-
.announce(&announce_request.info_hash, &mut peer, &original_peer_ip, &peers_wanted)
89+
.announce(&announce_request.info_hash, &mut peer, &remote_client_ip, &peers_wanted)
11690
.await?;
11791

92+
self.send_stats_event(remote_client_ip).await;
93+
94+
Ok(announce_data)
95+
}
96+
97+
async fn authenticate(&self, maybe_key: Option<Key>) -> Result<(), authentication::key::Error> {
98+
if self.core_config.private {
99+
let key = maybe_key.ok_or(authentication::key::Error::MissingAuthKey {
100+
location: Location::caller(),
101+
})?;
102+
103+
self.authentication_service.authenticate(&key).await?;
104+
}
105+
106+
Ok(())
107+
}
108+
109+
async fn authorize(&self, info_hash: InfoHash) -> Result<(), WhitelistError> {
110+
self.whitelist_authorization.authorize(&info_hash).await
111+
}
112+
113+
/// Resolves the client's real IP address considering proxy headers
114+
fn resolve_remote_client_ip(&self, client_ip_sources: &ClientIpSources) -> Result<IpAddr, PeerIpResolutionError> {
115+
match peer_ip_resolver::invoke(self.core_config.net.on_reverse_proxy, client_ip_sources) {
116+
Ok(peer_ip) => Ok(peer_ip),
117+
Err(error) => Err(error),
118+
}
119+
}
120+
121+
/// Determines how many peers the client wants in the response
122+
fn peers_wanted(announce_request: &Announce) -> PeersWanted {
123+
match announce_request.numwant {
124+
Some(numwant) => PeersWanted::only(numwant),
125+
None => PeersWanted::AsManyAsPossible,
126+
}
127+
}
128+
129+
async fn send_stats_event(&self, peer_ip: IpAddr) {
118130
if let Some(http_stats_event_sender) = self.opt_http_stats_event_sender.as_deref() {
119-
match original_peer_ip {
131+
match peer_ip {
120132
IpAddr::V4(_) => {
121133
http_stats_event_sender
122134
.send_event(statistics::event::Event::Tcp4Announce)
@@ -129,8 +141,6 @@ impl AnnounceService {
129141
}
130142
}
131143
}
132-
133-
Ok(announce_data)
134144
}
135145
}
136146

0 commit comments

Comments
 (0)