1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Tracker protocols ([BEP-3], [BEP-15], [BEP-23]).
//!
//! Trackers are servers that help peers find each other. This module implements
//! both HTTP and UDP tracker protocols.
//!
//! # Overview
//!
//! When downloading a torrent, clients "announce" to trackers to:
//! 1. Register themselves in the swarm
//! 2. Get a list of other peers to connect to
//! 3. Report download/upload statistics
//!
//! # HTTP Trackers
//!
//! HTTP trackers use simple GET requests with query parameters. The response
//! is a bencoded dictionary containing peer information.
//!
//! ```no_run
//! use rbit::tracker::{HttpTracker, TrackerEvent};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let tracker = HttpTracker::new("http://tracker.example.com/announce")?;
//!
//! let response = tracker.announce(
//! &[0u8; 20], // info_hash
//! &[0u8; 20], // peer_id
//! 6881, // port
//! 0, // uploaded
//! 0, // downloaded
//! 1000000, // left (bytes remaining)
//! TrackerEvent::Started,
//! ).await?;
//!
//! println!("Interval: {} seconds", response.interval);
//! println!("Seeders: {:?}", response.complete);
//! println!("Leechers: {:?}", response.incomplete);
//!
//! for peer in response.peers {
//! println!("Peer: {}", peer);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # UDP Trackers
//!
//! UDP trackers ([BEP-15]) are more efficient than HTTP, using a connection-based
//! protocol with binary messages.
//!
//! ```no_run
//! use rbit::tracker::{UdpTracker, TrackerEvent};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let mut tracker = UdpTracker::connect("udp://tracker.example.com:6969").await?;
//!
//! let response = tracker.announce(
//! &[0u8; 20], // info_hash
//! &[0u8; 20], // peer_id
//! 0, // downloaded
//! 1000000, // left
//! 0, // uploaded
//! TrackerEvent::Started,
//! 6881, // port
//! ).await?;
//!
//! for peer in response.peers {
//! println!("Peer: {}", peer);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Tracker Events
//!
//! Clients send different events during the torrent lifecycle:
//!
//! - **Started** - First announce when beginning the download
//! - **Completed** - Sent when download finishes (becomes a seeder)
//! - **Stopped** - Sent when removing the torrent
//! - **None** - Regular periodic announcement
//!
//! # Compact Peer Format
//!
//! [BEP-23] defines a compact format for peer lists that's more efficient than
//! the dictionary format. IPv4 peers are 6 bytes (4 IP + 2 port), IPv6 peers
//! are 18 bytes (16 IP + 2 port).
//!
//! [BEP-3]: http://bittorrent.org/beps/bep_0003.html
//! [BEP-15]: http://bittorrent.org/beps/bep_0015.html
//! [BEP-23]: http://bittorrent.org/beps/bep_0023.html
pub use TrackerError;
pub use HttpTracker;
pub use ;
pub use UdpTracker;
use crateInfoHash;
/// Parameters for a tracker announce request.
///
/// This struct groups all the parameters needed to announce to a tracker,
/// making it easier to pass them to [`TrackerClient::announce`].
///
/// # Example
///
/// ```no_run
/// use rbit::tracker::{AnnounceParams, TrackerClient, TrackerEvent};
/// use rbit::metainfo::InfoHash;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let info_hash = InfoHash::from_hex("c12fe1c06bba254a9dc9f519b335aa7c1367a88a")?;
/// let peer_id = [0u8; 20];
///
/// let params = AnnounceParams {
/// url: "http://tracker.example.com/announce",
/// info_hash: &info_hash,
/// peer_id: &peer_id,
/// port: 6881,
/// uploaded: 0,
/// downloaded: 0,
/// left: 1000000,
/// event: TrackerEvent::Started,
/// };
///
/// let client = TrackerClient::new();
/// let response = client.announce(params).await?;
/// # Ok(())
/// # }
/// ```
/// A unified tracker client supporting both HTTP and UDP trackers.
///
/// This client automatically selects the appropriate protocol based on the
/// tracker URL scheme.
///
/// # Example
///
/// ```no_run
/// use rbit::tracker::{AnnounceParams, TrackerClient, TrackerEvent};
/// use rbit::metainfo::InfoHash;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = TrackerClient::new();
/// let info_hash = InfoHash::from_hex("c12fe1c06bba254a9dc9f519b335aa7c1367a88a")?;
/// let peer_id = [0u8; 20];
///
/// // Works with HTTP trackers
/// let params = AnnounceParams {
/// url: "http://tracker.example.com/announce",
/// info_hash: &info_hash,
/// peer_id: &peer_id,
/// port: 6881,
/// uploaded: 0,
/// downloaded: 0,
/// left: 1000000,
/// event: TrackerEvent::Started,
/// };
/// let response = client.announce(params).await?;
///
/// // Also works with UDP trackers
/// let params = AnnounceParams {
/// url: "udp://tracker.example.com:6969",
/// info_hash: &info_hash,
/// peer_id: &peer_id,
/// port: 6881,
/// uploaded: 0,
/// downloaded: 0,
/// left: 1000000,
/// event: TrackerEvent::Started,
/// };
/// let response = client.announce(params).await?;
/// # Ok(())
/// # }
/// ```