Skip to content

Commit bd6dc99

Browse files
committed
docs: [#1261] review docs for tracker-core package
1 parent 74d0d28 commit bd6dc99

File tree

5 files changed

+365
-308
lines changed

5 files changed

+365
-308
lines changed

packages/tracker-core/src/announce_handler.rs

+84
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,87 @@
1+
//! Announce handler.
2+
//!
3+
//! Handling `announce` requests is the most important task for a `BitTorrent` tracker.
4+
//!
5+
//! A `BitTorrent` swarm is a network of peers that are all trying to download the same torrent.
6+
//! When a peer wants to find other peers it announces itself to the swarm via the tracker.
7+
//! The peer sends its data to the tracker so that the tracker can add it to the swarm.
8+
//! The tracker responds to the peer with the list of other peers in the swarm so that
9+
//! the peer can contact them to start downloading pieces of the file from them.
10+
//!
11+
//! Once you have instantiated the `AnnounceHandler` you can `announce` a new [`peer::Peer`](torrust_tracker_primitives) with:
12+
//!
13+
//! ```rust,no_run
14+
//! use std::net::SocketAddr;
15+
//! use std::net::IpAddr;
16+
//! use std::net::Ipv4Addr;
17+
//! use std::str::FromStr;
18+
//!
19+
//! use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
20+
//! use torrust_tracker_primitives::DurationSinceUnixEpoch;
21+
//! use torrust_tracker_primitives::peer;
22+
//! use bittorrent_primitives::info_hash::InfoHash;
23+
//!
24+
//! let info_hash = InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap();
25+
//!
26+
//! let peer = peer::Peer {
27+
//! peer_id: PeerId(*b"-qB00000000000000001"),
28+
//! peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8081),
29+
//! updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
30+
//! uploaded: NumberOfBytes::new(0),
31+
//! downloaded: NumberOfBytes::new(0),
32+
//! left: NumberOfBytes::new(0),
33+
//! event: AnnounceEvent::Completed,
34+
//! };
35+
//!
36+
//! let peer_ip = IpAddr::V4(Ipv4Addr::from_str("126.0.0.1").unwrap());
37+
//! ```
38+
//!
39+
//! ```text
40+
//! let announce_data = announce_handler.announce(&info_hash, &mut peer, &peer_ip).await;
41+
//! ```
42+
//!
43+
//! The `Tracker` returns the list of peers for the torrent with the infohash `3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0`,
44+
//! filtering out the peer that is making the `announce` request.
45+
//!
46+
//! > **NOTICE**: that the peer argument is mutable because the `Tracker` can change the peer IP if the peer is using a loopback IP.
47+
//!
48+
//! The `peer_ip` argument is the resolved peer ip. It's a common practice that trackers ignore the peer ip in the `announce` request params,
49+
//! and resolve the peer ip using the IP of the client making the request. As the tracker is a domain service, the peer IP must be provided
50+
//! for the `Tracker` user, which is usually a higher component with access the the request metadata, for example, connection data, proxy headers,
51+
//! etcetera.
52+
//!
53+
//! The returned struct is:
54+
//!
55+
//! ```rust,no_run
56+
//! use torrust_tracker_primitives::peer;
57+
//! use torrust_tracker_configuration::AnnouncePolicy;
58+
//!
59+
//! pub struct AnnounceData {
60+
//! pub peers: Vec<peer::Peer>,
61+
//! pub swarm_stats: SwarmMetadata,
62+
//! pub policy: AnnouncePolicy, // the tracker announce policy.
63+
//! }
64+
//!
65+
//! pub struct SwarmMetadata {
66+
//! pub completed: u32, // The number of peers that have ever completed downloading
67+
//! pub seeders: u32, // The number of active peers that have completed downloading (seeders)
68+
//! pub leechers: u32, // The number of active peers that have not completed downloading (leechers)
69+
//! }
70+
//!
71+
//! // Core tracker configuration
72+
//! pub struct AnnounceInterval {
73+
//! // ...
74+
//! pub interval: u32, // Interval in seconds that the client should wait between sending regular announce requests to the tracker
75+
//! pub interval_min: u32, // Minimum announce interval. Clients must not reannounce more frequently than this
76+
//! // ...
77+
//! }
78+
//! ```
79+
//!
80+
//! Refer to `BitTorrent` BEPs and other sites for more information about the `announce` request:
81+
//!
82+
//! - [BEP 3. The `BitTorrent` Protocol Specification](https://www.bittorrent.org/beps/bep_0003.html)
83+
//! - [BEP 23. Tracker Returns Compact Peer Lists](https://www.bittorrent.org/beps/bep_0023.html)
84+
//! - [Vuze docs](https://wiki.vuze.com/w/Announce)
185
use std::net::IpAddr;
286
use std::sync::Arc;
387

packages/tracker-core/src/authentication/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//! One of the crate responsibilities is to create and keep authentication keys.
2+
//! Auth keys are used by HTTP trackers when the tracker is running in `private`
3+
//! mode.
4+
//!
5+
//! HTTP tracker's clients need to obtain an authentication key before starting
6+
//! requesting the tracker. Once they get one they have to include a `PATH`
7+
//! param with the key in all the HTTP requests. For example, when a peer wants
8+
//! to `announce` itself it has to use the HTTP tracker endpoint:
9+
//!
10+
//! `GET /announce/:key`
11+
//!
12+
//! The common way to obtain the keys is by using the tracker API directly or
13+
//! via other applications like the [Torrust Index](https://github.com/torrust/torrust-index).
114
use crate::CurrentClock;
215

316
pub mod handler;

packages/tracker-core/src/error.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
//! Errors returned by the core tracker.
1+
//! Core tracker errors.
2+
//!
3+
//! This module defines the error types used internally by the BitTorrent
4+
//! tracker core.
5+
//!
6+
//! These errors encapsulate issues such as whitelisting violations, invalid
7+
//! peer key data, and database persistence failures. Each error variant
8+
//! includes contextual information (such as source code location) to facilitate
9+
//! debugging.
210
use std::panic::Location;
311

412
use bittorrent_primitives::info_hash::InfoHash;
@@ -7,29 +15,41 @@ use torrust_tracker_located_error::LocatedError;
715
use super::authentication::key::ParseKeyError;
816
use super::databases;
917

10-
/// Whitelist errors returned by the core tracker.
18+
/// Errors related to torrent whitelisting.
19+
///
20+
/// This error is returned when an operation involves a torrent that is not
21+
/// present in the whitelist.
1122
#[derive(thiserror::Error, Debug, Clone)]
1223
pub enum WhitelistError {
24+
/// Indicates that the torrent identified by `info_hash` is not whitelisted.
1325
#[error("The torrent: {info_hash}, is not whitelisted, {location}")]
1426
TorrentNotWhitelisted {
1527
info_hash: InfoHash,
1628
location: &'static Location<'static>,
1729
},
1830
}
1931

20-
/// Peers keys errors returned by the core tracker.
32+
/// Errors related to peer key operations.
33+
///
34+
/// This error type covers issues encountered during the handling of peer keys,
35+
/// including validation of key durations, parsing errors, and database
36+
/// persistence problems.
2137
#[allow(clippy::module_name_repetitions)]
2238
#[derive(thiserror::Error, Debug, Clone)]
2339
pub enum PeerKeyError {
40+
/// Returned when the duration specified for the peer key exceeds the
41+
/// maximum.
2442
#[error("Invalid peer key duration: {seconds_valid:?}, is not valid")]
2543
DurationOverflow { seconds_valid: u64 },
2644

45+
/// Returned when the provided peer key is invalid.
2746
#[error("Invalid key: {key}")]
2847
InvalidKey {
2948
key: String,
3049
source: LocatedError<'static, ParseKeyError>,
3150
},
3251

52+
/// Returned when persisting the peer key to the database fails.
3353
#[error("Can't persist key: {source}")]
3454
DatabaseError {
3555
source: LocatedError<'static, databases::error::Error>,

0 commit comments

Comments
 (0)