|
| 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) |
1 | 85 | use std::net::IpAddr;
|
2 | 86 | use std::sync::Arc;
|
3 | 87 |
|
|
0 commit comments