torrent 0.1.1

High-level async BitTorrent library — session management, HTTP/UDP tracker communication, DHT networking, peer connections, and file storage. Built on torrent-core with tokio.
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Async tracker implementations — HTTP (plain + TLS) and UDP announce.
//!
//! Re-exports data types from `torrent_core::tracker` and provides
//! async HTTP and UDP tracker clients.
//!
//! # Key Types
//!
//! - [`Tracker`] — unified announce client (single or multi-tracker)
//! - [`HttpTracker`] — HTTP/HTTPS GET announce (BEP 3/23)
//! - [`UdpTracker`] — UDP announce (BEP 15)
//!
//! # Constructors
//!
//! - [`Tracker::single`] — create from a single URL
//! - [`Tracker::multi`] — create from multiple URLs
//! - [`Tracker::from_metainfo`] — create from parsed torrent metadata
//!
//! # Tracker Management
//!
//! - [`Tracker::add`] / [`Tracker::add_all`] — add trackers at runtime
//! - [`Tracker::remove`] — remove a tracker by URL
//! - [`Tracker::clear`] — remove all trackers
//! - [`Tracker::len`] / [`Tracker::is_empty`] / [`Tracker::urls`] — introspection
//!
//! # Announce Methods
//!
//! | Method                         | Behavior                                             |
//! | ------------------------------ | ---------------------------------------------------- |
//! | [`Tracker::announce`]          | single-tracker; for multi acts like `announce_first` |
//! | [`Tracker::announce_first`]    | race all trackers, return first success              |
//! | [`Tracker::announce_all`]      | collect all successful responses                     |
//! | [`Tracker::announce_into_set`] | return a [`JoinSet`] for caller to drive             |

mod http;
mod into_url;
mod udp;

pub use torrent_core::tracker::{
    AnnounceEvent, AnnounceRequest, AnnounceResponse, parse_compact_peers_ipv4,
};
pub use url::Url;

pub use self::http::HttpTracker;
pub use self::into_url::IntoUrl;
pub use self::udp::UdpTracker;

use std::collections::HashSet;
use std::sync::Arc;

use tokio::task::JoinSet;
use torrent_core::metainfo::Metainfo;

use crate::error::{Error, ErrorKind};

/// Unified tracker client that auto-detects HTTP vs UDP from the URL scheme.
///
/// Can represent a single tracker or a collection of trackers.
///
/// # Examples
///
/// ```no_run
/// # use torrent::tracker::{Tracker, AnnounceRequest, AnnounceEvent};
/// # use torrent::peer::PeerId;
/// # async fn example() {
/// let Some(tracker) = Tracker::single("http://tracker.example.com:6969/announce") else {
///     return;
/// };
/// let mut req = AnnounceRequest::new([0u8; 20], PeerId::random(), 6881);
/// req.event = AnnounceEvent::Started;
/// let _resp = tracker.announce(&req).await;
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Tracker {
    trackers: Vec<Inner>,
}

impl Tracker {
    /// Create a single `Tracker` from a URL.
    ///
    /// The scheme determines which backend is used:
    /// - `http://` → [`HttpTracker`] (plain TCP)
    /// - `https://` → [`HttpTracker`] with TLS
    /// - `udp://` → [`UdpTracker`]
    ///
    /// Accepts `&str`, `String`, `&String`, or [`Url`].
    ///
    /// Returns `None` if the URL is invalid or uses an unsupported scheme.
    pub fn single(url: impl IntoUrl) -> Option<Self> {
        let inner = Inner::from_url(url.into_url().ok()?).ok()?;
        Some(Tracker {
            trackers: vec![inner],
        })
    }

    /// Create a `Tracker` from multiple tracker URLs.
    ///
    /// Invalid or unsupported URLs are silently skipped.
    /// Duplicate URLs are silently skipped (the first occurrence is kept).
    ///
    /// Returns `None` if **all** URLs are invalid.
    pub fn multi<I: IntoIterator>(urls: I) -> Option<Self>
    where
        I::Item: IntoUrl,
    {
        let mut seen: HashSet<String> = HashSet::new();
        let mut trackers: Vec<Inner> = Vec::new();

        for url in urls {
            if let Ok(url) = url.into_url() {
                // Keep first occurrence; skip duplicates
                if seen.insert(url.as_str().into())
                    && let Ok(inner) = Inner::from_url(url)
                {
                    trackers.push(inner);
                }
            }
        }

        if trackers.is_empty() {
            None
        } else {
            Some(Tracker { trackers })
        }
    }

    /// Create a `Tracker` from a parsed [`Metainfo`].
    ///
    /// Collects all tracker URLs from `announce` and `announce_list` (BEP 12),
    /// deduplicates them (duplicates across announce and announce_list are
    /// deduplicated), and returns a single-tracker or multi-tracker as
    /// appropriate. Invalid or unsupported URLs are silently skipped.
    ///
    /// Returns `None` if no valid tracker URLs were found.
    pub fn from_metainfo(meta: &Metainfo) -> Option<Self> {
        Self::multi(std::iter::once(&meta.announce).chain(meta.announce_list.iter().flatten()))
    }

    /// Returns the number of trackers.
    pub fn len(&self) -> usize {
        self.trackers.len()
    }

    /// Returns `true` if there are no trackers.
    pub fn is_empty(&self) -> bool {
        self.trackers.is_empty()
    }

    /// Add a single tracker URL.
    ///
    /// If the URL is already registered, it is silently skipped.
    /// Returns an error if the URL is invalid or has an unsupported scheme.
    pub fn add(&mut self, url: impl IntoUrl) -> Result<(), Error> {
        let url = url.into_url()?;
        // Silently skip if the URL is already registered
        if self.trackers.iter().any(|t| t.url() == url.as_str()) {
            return Ok(());
        }
        self.trackers.push(Inner::from_url(url)?);
        Ok(())
    }

    /// Add multiple tracker URLs.
    ///
    /// Duplicate and already-registered URLs are silently skipped.
    /// Returns an error if any URL is invalid.
    pub fn add_all<I: IntoIterator>(&mut self, urls: I) -> Result<(), Error>
    where
        I::Item: IntoUrl,
    {
        let mut seen: HashSet<String> = self.trackers.iter().map(|t| t.url().into()).collect();

        for url in urls {
            let url = url.into_url()?;
            if seen.insert(url.as_str().into()) {
                self.trackers.push(Inner::from_url(url)?);
            }
        }

        Ok(())
    }

    /// Remove a tracker by its URL.
    ///
    /// Returns `true` if a tracker was found and removed.
    pub fn remove(&mut self, url: &str) -> bool {
        let len_before = self.trackers.len();
        self.trackers.retain(|inner| inner.url() != url);
        self.trackers.len() < len_before
    }

    /// Remove all trackers.
    pub fn clear(&mut self) {
        self.trackers.clear();
    }

    /// Return the URLs of all trackers (for logging / debugging).
    pub fn urls(&self) -> Vec<&str> {
        self.trackers.iter().map(|inner| inner.url()).collect()
    }

    /// Announce to the tracker(s).
    ///
    /// For a single tracker, delegates directly.
    /// For multiple trackers, acts as [`announce_first`](Self::announce_first).
    pub async fn announce(&self, req: &AnnounceRequest) -> Result<AnnounceResponse, Error> {
        self.announce_first(req).await
    }

    /// Race all trackers and return the first successful response.
    ///
    /// If all trackers fail, the last error is returned.
    pub async fn announce_first(&self, req: &AnnounceRequest) -> Result<AnnounceResponse, Error> {
        let mut set = self.announce_into_set(req);

        let mut last_err = None;
        while let Some(result) = set.join_next().await {
            match result {
                Ok(Ok(resp)) => return Ok(resp),
                Ok(Err(e)) => last_err = Some(e),
                Err(_) => last_err = Some(Error::new(ErrorKind::TrackerRequestFailed)),
            }
        }

        Err(last_err.unwrap_or_else(|| Error::new(ErrorKind::TrackerRequestFailed)))
    }

    /// Announce to all trackers and collect all successful responses.
    ///
    /// Errors are silently ignored.
    pub async fn announce_all(&self, req: &AnnounceRequest) -> Vec<AnnounceResponse> {
        let mut set = self.announce_into_set(req);

        let mut results = Vec::new();
        while let Some(result) = set.join_next().await {
            if let Ok(Ok(resp)) = result {
                results.push(resp);
            }
        }
        results
    }

    /// Spawn all trackers into a new [`JoinSet`] for the caller to drive.
    ///
    /// Each task returns `Ok(AnnounceResponse)` on success or `Err(Error)` on failure.
    pub fn announce_into_set(
        &self,
        req: &AnnounceRequest,
    ) -> JoinSet<Result<AnnounceResponse, Error>> {
        let mut set = JoinSet::new();
        let req = Arc::new(req.clone());
        for inner in &self.trackers {
            let inner = inner.clone();
            let req = Arc::clone(&req);
            set.spawn(async move { inner.announce(&req).await });
        }
        set
    }
}

/// Internal tracker variant: HTTP or UDP.
#[derive(Debug, Clone)]
enum Inner {
    Http(HttpTracker),
    Udp(UdpTracker),
}

impl Inner {
    fn url(&self) -> &str {
        match self {
            Inner::Http(t) => t.url().as_str(),
            Inner::Udp(t) => t.url().as_str(),
        }
    }

    fn from_url(url: Url) -> Result<Self, Error> {
        match url.scheme() {
            "http" | "https" => Ok(Inner::Http(HttpTracker::new(url)?)),
            "udp" => Ok(Inner::Udp(UdpTracker::new(url)?)),
            _ => Err(Error::new(ErrorKind::InvalidInput)),
        }
    }

    async fn announce(&self, req: &AnnounceRequest) -> Result<AnnounceResponse, Error> {
        match self {
            Inner::Http(t) => t.announce(req).await,
            Inner::Udp(t) => t.announce(req).await,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::peer::PeerId;

    #[test]
    fn test_tracker_single_http() {
        let t = Tracker::single("http://tracker.example.com:6969/announce").unwrap();
        assert_eq!(t.trackers.len(), 1);
    }

    #[test]
    fn test_tracker_single_udp() {
        let t = Tracker::single("udp://tracker.example.com:6969").unwrap();
        assert_eq!(t.trackers.len(), 1);
    }

    #[test]
    fn test_tracker_single_https() {
        // HTTPS is now supported via TLS wrapping
        let t = Tracker::single("https://tracker.example.com/announce").unwrap();
        assert_eq!(t.trackers.len(), 1);
    }

    #[test]
    fn test_tracker_multi_mixed_http_https() {
        let result = Tracker::multi([
            "http://tracker.a.com/announce",
            "https://tracker.b.com/announce",
        ]);
        let t = result.unwrap();
        assert_eq!(t.trackers.len(), 2);
    }

    #[test]
    fn test_tracker_single_invalid_scheme() {
        assert!(Tracker::single("ftp://tracker.example.com").is_none());
    }

    #[test]
    fn test_tracker_single_invalid_url() {
        assert!(Tracker::single("not a url").is_none());
    }

    #[test]
    fn test_tracker_multi_valid() {
        let t =
            Tracker::multi(["http://tracker.a.com/announce", "udp://tracker.b.com:6969"]).unwrap();
        assert_eq!(t.trackers.len(), 2);
    }

    #[test]
    fn test_tracker_multi_skips_invalid() {
        // Invalid URLs are silently skipped; valid ones remain.
        let t = Tracker::multi(["http://tracker.a.com/announce", "not a url"]).unwrap();
        assert_eq!(t.trackers.len(), 1);
    }

    #[test]
    fn test_tracker_multi_all_invalid_scheme() {
        assert!(Tracker::multi(["ftp://tracker.example.com"]).is_none());
    }

    #[tokio::test]
    async fn test_tracker_multi_empty() {
        let urls: Vec<&str> = Vec::new();
        assert!(Tracker::multi(urls).is_none());
    }

    #[tokio::test]
    async fn test_tracker_announce_into_set_type() {
        // Verify the method compiles and returns the right type
        let t = Tracker::single("http://tracker.example.com/announce").unwrap();
        let mut req = AnnounceRequest::new([0u8; 20], PeerId::random(), 6881);
        req.compact = false;
        req.numwant = None;
        let set: JoinSet<Result<AnnounceResponse, Error>> = t.announce_into_set(&req);
        // Just verify it's not empty when there are trackers
        assert!(!set.is_empty());
    }

    #[test]
    fn test_tracker_multi_from_vec_string() {
        let urls = vec![
            "http://tracker.a.com/announce".to_string(),
            "udp://tracker.b.com:6969".to_string(),
        ];
        let t = Tracker::multi(urls).unwrap();
        assert_eq!(t.trackers.len(), 2);
    }

    #[test]
    fn test_tracker_multi_from_urls() {
        let url1 = Url::parse("http://tracker.a.com/announce").unwrap();
        let url2 = Url::parse("udp://tracker.b.com:6969").unwrap();
        let t = Tracker::multi([url1, url2]).unwrap();
        assert_eq!(t.trackers.len(), 2);
    }

    #[test]
    fn test_tracker_from_metainfo() {
        use torrent_core::metainfo::{Info, Metainfo, Mode};

        let info = Info {
            piece_length: 262144,
            pieces: vec![[0u8; 20]],
            mode: Mode::Single {
                name: "test.txt".into(),
                length: 1024,
            },
            raw_info: bytes::Bytes::new(),
        };
        let meta = Metainfo {
            announce: "http://tracker.a.com/announce".into(),
            announce_list: vec![vec!["udp://tracker.b.com:6969".into()]],
            info,
            creation_date: None,
            comment: None,
            created_by: None,
            encoding: None,
        };

        let t = Tracker::from_metainfo(&meta).unwrap();
        assert_eq!(t.trackers.len(), 2);
    }

    #[test]
    fn test_tracker_from_metainfo_skip_invalid() {
        use torrent_core::metainfo::{Info, Metainfo, Mode};

        let info = Info {
            piece_length: 262144,
            pieces: vec![[0u8; 20]],
            mode: Mode::Single {
                name: "test.txt".into(),
                length: 1024,
            },
            raw_info: bytes::Bytes::new(),
        };
        // announce has invalid scheme; announce_list is valid
        let meta = Metainfo {
            announce: "ftp://tracker.a.com/announce".into(),
            announce_list: vec![vec!["http://tracker.b.com/announce".into()]],
            info,
            creation_date: None,
            comment: None,
            created_by: None,
            encoding: None,
        };

        let t = Tracker::from_metainfo(&meta).unwrap();
        assert_eq!(t.trackers.len(), 1);
        assert_eq!(t.urls(), &["http://tracker.b.com/announce"]);
    }

    #[test]
    fn test_tracker_add_and_remove() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();
        assert_eq!(t.len(), 1);
        assert!(!t.is_empty());
        assert_eq!(t.urls(), &["http://tracker.a.com/announce"]);

        t.add("udp://tracker.b.com:6969").unwrap();
        assert_eq!(t.len(), 2);
        assert_eq!(
            t.urls(),
            &["http://tracker.a.com/announce", "udp://tracker.b.com:6969"]
        );

        assert!(t.remove("http://tracker.a.com/announce"));
        assert_eq!(t.len(), 1);
        assert!(!t.remove("http://tracker.a.com/announce")); // already gone

        t.clear();
        assert!(t.is_empty());
        assert!(t.urls().is_empty());
    }

    #[test]
    fn test_tracker_multi_dedup() {
        // Duplicate URLs are deduplicated (keep first occurrence)
        let t = Tracker::multi([
            "http://tracker.a.com/announce",
            "udp://tracker.b.com:6969",
            "http://tracker.a.com/announce", // duplicate
        ])
        .unwrap();
        assert_eq!(t.trackers.len(), 2);
        assert_eq!(
            t.urls(),
            &["http://tracker.a.com/announce", "udp://tracker.b.com:6969"]
        );
    }

    #[test]
    fn test_tracker_multi_dedup_all_same() {
        // All URLs identical → single tracker
        let t = Tracker::multi([
            "http://tracker.a.com/announce",
            "http://tracker.a.com/announce",
            "http://tracker.a.com/announce",
        ])
        .unwrap();
        assert_eq!(t.trackers.len(), 1);
    }

    #[test]
    fn test_tracker_add_dedup() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();
        assert_eq!(t.len(), 1);

        // Adding the same URL again should silently succeed without increasing count
        t.add("http://tracker.a.com/announce").unwrap();
        assert_eq!(t.len(), 1);
    }

    #[test]
    fn test_tracker_from_metainfo_dedup_across_tiers() {
        use torrent_core::metainfo::{Info, Metainfo, Mode};

        let info = Info {
            piece_length: 262144,
            pieces: vec![[0u8; 20]],
            mode: Mode::Single {
                name: "test.txt".into(),
                length: 1024,
            },
            raw_info: bytes::Bytes::new(),
        };
        // announce and announce_list[0] share the same URL
        let meta = Metainfo {
            announce: "http://tracker.a.com/announce".into(),
            announce_list: vec![
                vec!["http://tracker.a.com/announce".into()], // duplicate
                vec!["udp://tracker.b.com:6969".into()],
            ],
            info,
            creation_date: None,
            comment: None,
            created_by: None,
            encoding: None,
        };

        let t = Tracker::from_metainfo(&meta).unwrap();
        assert_eq!(t.trackers.len(), 2);
        // announce (top-level) is kept first; duplicate from announce_list is skipped
        assert_eq!(
            t.urls(),
            &["http://tracker.a.com/announce", "udp://tracker.b.com:6969"]
        );
    }

    #[test]
    fn test_tracker_add_all_dedup() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();

        t.add_all([
            "udp://tracker.b.com:6969",
            "http://tracker.c.com/announce",
            "udp://tracker.b.com:6969", // duplicate in the same batch
        ])
        .unwrap();

        assert_eq!(t.trackers.len(), 3);
        assert_eq!(
            t.urls(),
            &[
                "http://tracker.a.com/announce",
                "udp://tracker.b.com:6969",
                "http://tracker.c.com/announce"
            ]
        );
    }

    #[test]
    fn test_tracker_add_all_dedup_with_existing() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();

        // Some URLs already exist, some are new, some are duplicates within the batch
        t.add_all([
            "http://tracker.a.com/announce", // already exists
            "udp://tracker.b.com:6969",      // new
            "http://tracker.a.com/announce", // duplicate (existing + same batch)
            "udp://tracker.b.com:6969",      // duplicate
            "http://tracker.c.com/announce", // new
        ])
        .unwrap();

        assert_eq!(t.trackers.len(), 3);
    }

    #[test]
    fn test_tracker_add_all_invalid_url() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();

        // Invalid URL should cause early Err (short-circuit)
        let result = t.add_all([
            "udp://tracker.b.com:6969",
            "not a url",
            "http://tracker.c.com/announce",
        ]);

        assert!(result.is_err());
        // The valid URL before the invalid one should NOT have been added
        // (short-circuit after the invalid parse failure)
    }

    #[test]
    fn test_tracker_add_all_empty() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();
        let urls: Vec<&str> = Vec::new();

        t.add_all(urls).unwrap();
        assert_eq!(t.trackers.len(), 1); // unchanged
    }

    #[test]
    fn test_tracker_from_metainfo_preserves_order() {
        use torrent_core::metainfo::{Info, Metainfo, Mode};

        let info = Info {
            piece_length: 262144,
            pieces: vec![[0u8; 20]],
            mode: Mode::Single {
                name: "test.txt".into(),
                length: 1024,
            },
            raw_info: bytes::Bytes::new(),
        };
        // Multiple tiers with unique URLs; order should be preserved
        let meta = Metainfo {
            announce: "http://tracker.a.com/announce".into(),
            announce_list: vec![
                vec!["udp://tracker.b.com:6969".into()],
                vec!["http://tracker.c.com/announce".into()],
            ],
            info,
            creation_date: None,
            comment: None,
            created_by: None,
            encoding: None,
        };

        let t = Tracker::from_metainfo(&meta).unwrap();
        assert_eq!(t.trackers.len(), 3);
        assert_eq!(
            t.urls(),
            &[
                "http://tracker.a.com/announce",
                "udp://tracker.b.com:6969",
                "http://tracker.c.com/announce"
            ]
        );
    }

    #[test]
    fn test_tracker_remove_nonexistent() {
        let mut t = Tracker::single("http://tracker.a.com/announce").unwrap();

        // Remove existing
        assert!(t.remove("http://tracker.a.com/announce"));
        assert!(t.is_empty());

        // Remove already-removed URL — should be idempotent
        assert!(!t.remove("http://tracker.a.com/announce"));
        assert!(t.is_empty());

        // Remove URL that was never added
        assert!(!t.remove("udp://tracker.b.com:6969"));
        assert!(t.is_empty());
    }
}