Skip to content

Commit 9326da3

Browse files
committed
refactor: [#1338] clean bittorrent_udp_tracker_core::services::announce::AnnunceService
1 parent 0fce925 commit 9326da3

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

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

+35-27
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::ops::Range;
1212
use std::sync::Arc;
1313

1414
use aquatic_udp_protocol::AnnounceRequest;
15+
use bittorrent_primitives::info_hash::InfoHash;
1516
use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
1617
use bittorrent_tracker_core::error::{AnnounceError, WhitelistError};
1718
use bittorrent_tracker_core::whitelist;
@@ -60,47 +61,54 @@ impl AnnounceService {
6061
request: &AnnounceRequest,
6162
cookie_valid_range: Range<f64>,
6263
) -> Result<AnnounceData, UdpAnnounceError> {
63-
// Authentication
64-
check(
65-
&request.connection_id,
66-
gen_remote_fingerprint(&remote_addr),
67-
cookie_valid_range,
68-
)?;
64+
Self::authenticate(remote_addr, request, cookie_valid_range)?;
6965

7066
let info_hash = request.info_hash.into();
71-
let remote_client_ip = remote_addr.ip();
7267

73-
// Authorization
74-
self.whitelist_authorization.authorize(&info_hash).await?;
68+
self.authorize(&info_hash).await?;
69+
70+
let remote_client_ip = remote_addr.ip();
7571

7672
let mut peer = peer_builder::from_request(request, &remote_client_ip);
77-
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
7873

79-
let original_peer_ip = peer.peer_addr.ip();
74+
let peers_wanted: PeersWanted = i32::from(request.peers_wanted.0).into();
8075

81-
// The tracker could change the original peer ip
8276
let announce_data = self
8377
.announce_handler
84-
.announce(&info_hash, &mut peer, &original_peer_ip, &peers_wanted)
78+
.announce(&info_hash, &mut peer, &remote_client_ip, &peers_wanted)
8579
.await?;
8680

87-
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
88-
match original_peer_ip {
89-
IpAddr::V4(_) => {
90-
udp_stats_event_sender
91-
.send_event(statistics::event::Event::Udp4Announce)
92-
.await;
93-
}
94-
IpAddr::V6(_) => {
95-
udp_stats_event_sender
96-
.send_event(statistics::event::Event::Udp6Announce)
97-
.await;
98-
}
99-
}
100-
}
81+
self.send_stats_event(remote_client_ip).await;
10182

10283
Ok(announce_data)
10384
}
85+
86+
fn authenticate(
87+
remote_addr: SocketAddr,
88+
request: &AnnounceRequest,
89+
cookie_valid_range: Range<f64>,
90+
) -> Result<f64, ConnectionCookieError> {
91+
check(
92+
&request.connection_id,
93+
gen_remote_fingerprint(&remote_addr),
94+
cookie_valid_range,
95+
)
96+
}
97+
98+
async fn authorize(&self, info_hash: &InfoHash) -> Result<(), WhitelistError> {
99+
self.whitelist_authorization.authorize(info_hash).await
100+
}
101+
102+
async fn send_stats_event(&self, peer_ip: IpAddr) {
103+
if let Some(udp_stats_event_sender) = self.opt_udp_core_stats_event_sender.as_deref() {
104+
let event = match peer_ip {
105+
IpAddr::V4(_) => statistics::event::Event::Udp4Announce,
106+
IpAddr::V6(_) => statistics::event::Event::Udp6Announce,
107+
};
108+
109+
udp_stats_event_sender.send_event(event).await;
110+
}
111+
}
104112
}
105113

106114
/// Errors related to announce requests.

0 commit comments

Comments
 (0)