torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
use std::collections::{HashSet, VecDeque};
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Instant;

use tokio::sync::{watch, RwLock};
use crate::layout::TorrentLayout;
use crate::session::engine::DownloadProgress;

use crate::client::Client;
use crate::config::Config;
use crate::error::{Error, Result};
use crate::metadata::fetch_metadata_with_bytes;
use crate::peer::PeerId;
use crate::piece::PiecePicker;
use crate::session::{PeerLedger, ResumeCheckpoint};
use crate::store::{StagedStore, StagedStoreConfig};
use crate::torrent::{InfoHash, MagnetLink, TorrentFile, TorrentInfo, pieces_for_selected_files};
use crate::tracker::{announce, AnnounceEvent, ScrapeStats};

pub struct Session {
    client: Arc<Client>,
    cfg: Config,
    info: TorrentInfo,
    /// Exact bencoded info dict for BEP 9 ut_metadata serve.
    info_dict_bytes: Arc<[u8]>,
    store: StagedStore,
    peer_id: PeerId,
    picker: Arc<RwLock<PiecePicker>>,
    announce: Option<String>,
    trackers: Vec<String>,
    web_seeds: Vec<String>,
    static_peers: RwLock<Vec<SocketAddr>>,
    peer_ledger: Arc<RwLock<PeerLedger>>,
    checkpoint_path: std::path::PathBuf,
    resumed_bytes: AtomicU64,
    bytes_uploaded: AtomicU64,
    /// Last tracker `interval` / `min interval` hint (seconds). 0 = unset.
    tracker_interval_secs: AtomicU64,
    /// Wall clock of the last successful tracker announce (heal throttle).
    last_tracker_announce: RwLock<Option<Instant>>,
    scrape_stats: RwLock<Option<ScrapeStats>>,
    download_files: Vec<String>,
    /// Host-observable download progress (updated by the swarm engine).
    progress_tx: watch::Sender<DownloadProgress>,
    /// Cooperative cancel for download/seed loops (`stop` / `remove` RPC).
    stop_requested: AtomicBool,
}

impl Session {
    /// Single-torrent convenience: opens a dedicated mTLS [`Client`] for one swarm.
    ///
    /// Prefer [`Client::add_torrent`] on a shared host client for multi-torrent
    /// embeddings — each call here binds its own listen port and DHT node.
    pub async fn from_torrent(cfg: Config, torrent: TorrentFile) -> Result<Arc<Self>> {
        let client = Client::open(cfg).await?;
        client.add_torrent(torrent).await
    }

    /// Single-torrent magnet convenience (same dedicated-client caveat as `from_torrent`).
    /// Prefer [`Client::add_magnet`] on a shared host client.
    pub async fn from_magnet(cfg: Config, magnet: MagnetLink) -> Result<Arc<Self>> {
        let client = Client::open(cfg).await?;
        client.add_magnet(magnet).await
    }

    /// Single-torrent convenience with explicit TLS/policy (tests and host overrides).
    /// Prefer a shared [`Client`] for production multi-torrent hosts.
    pub async fn from_torrent_with_tls(
        cfg: Config,
        torrent: TorrentFile,
        policy: Arc<dyn crate::secure::SecurePolicy>,
        tls: crate::secure::TlsMaterial,
        handshake_key: Vec<u8>,
    ) -> Result<Arc<Self>> {
        let client =
            Client::open_secure_with_handshake(cfg, policy, tls, handshake_key).await?;
        client.add_torrent(torrent).await
    }

    pub(crate) async fn from_torrent_with_client(
        client: Arc<Client>,
        torrent: TorrentFile,
    ) -> Result<Self> {
        let trackers = torrent.tracker_urls();
        let announce = torrent.announce.clone();
        let web_seeds = torrent.web_seeds.clone();
        let info_dict_bytes = if torrent.info_dict_bytes.is_empty() {
            crate::bencode::encode(&crate::torrent::info_to_bencode(&torrent.info)?)
        } else {
            torrent.info_dict_bytes
        };
        Self::from_info_with_client(
            client,
            torrent.info,
            Arc::from(info_dict_bytes.into_boxed_slice()),
            announce,
            trackers,
            web_seeds,
        )
        .await
    }

    pub(crate) async fn from_magnet_with_client(
        client: Arc<Client>,
        magnet: MagnetLink,
    ) -> Result<Self> {
        let peer_id = client.peer_id();
        let cfg = client.config().clone();
        let use_tracker = cfg.peer_discovery.uses_tracker();

        let gate = crate::net::DialGate::from_block_private(cfg.tracker_block_private);
        let magnet_peers = gate.filter(magnet.peers.iter().copied());

        if let Some(dht) = client.dht() {
            dht.bootstrap(&magnet_peers).await?;
        }

        let mut peer_addrs = magnet_peers;
        // DHT peer discovery for metadata is safe before knowing private: we only query,
        // and never announce until the torrent is loaded and non-private.
        if cfg.peer_discovery.uses_dht() {
            if let Some(dht) = client.dht() {
                match dht.get_peers(magnet.info_hash.as_bytes()).await {
                    Ok(peers) => peer_addrs.extend(peers),
                    Err(err) => tracing::debug!(%err, "dht get_peers for magnet metadata"),
                }
            }
        }
        if use_tracker {
            let ipv6 = Self::tracker_ipv6_compact_from_cfg(&cfg);
            for tracker in &magnet.trackers {
                if let Ok(response) = announce(
                    tracker,
                    magnet.info_hash.as_bytes(),
                    &peer_id,
                    cfg.listen_port,
                    0,
                    0,
                    u64::MAX,
                    AnnounceEvent::Started,
                    cfg.tracker_block_private,
                    ipv6.as_ref(),
                    cfg.announce_ip,
                )
                .await
                {
                    peer_addrs.extend(response.peers);
                }
            }
        }

        peer_addrs.sort();
        peer_addrs.dedup();

        let fetched = fetch_metadata_from_peers(
            &client,
            &peer_addrs,
            magnet.info_hash,
            peer_id,
        )
        .await?;
        if magnet.has_btih && fetched.info.info_hash != magnet.info_hash {
            return Err(Error::Metadata(
                "fetched metadata v1 info-hash mismatch".into(),
            ));
        }
        if let Some(expected_v2) = magnet.info_hash_v2 {
            let Some(got_v2) = fetched.info.info_hash_v2 else {
                return Err(Error::Metadata(
                    "magnet has urn:btmh but fetched metadata is not v2/hybrid".into(),
                ));
            };
            if got_v2 != expected_v2 {
                return Err(Error::Metadata(
                    "fetched metadata v2 info-hash mismatch".into(),
                ));
            }
            if !magnet.has_btih && fetched.info.info_hash != magnet.info_hash {
                return Err(Error::Metadata(
                    "fetched metadata truncated v2 info-hash mismatch".into(),
                ));
            }
        } else if fetched.info.info_hash != magnet.info_hash {
            return Err(Error::Metadata("fetched metadata info hash mismatch".into()));
        }

        let announce = magnet.trackers.first().cloned();
        let trackers = magnet.trackers.clone();
        Self::from_info_with_client(
            client,
            fetched.info,
            Arc::from(fetched.info_dict_bytes.into_boxed_slice()),
            announce,
            trackers,
            Vec::new(),
        )
        .await
    }

    async fn from_info_with_client(
        client: Arc<Client>,
        info: TorrentInfo,
        info_dict_bytes: Arc<[u8]>,
        announce: Option<String>,
        trackers: Vec<String>,
        web_seeds: Vec<String>,
    ) -> Result<Self> {
        let cfg = client.config().clone();
        let store_cfg = StagedStoreConfig::from_app_config(&cfg, info.info_hash.as_bytes())?;
        let layout = cfg.torrent_layout(&info.info_hash.to_hex());
        let store = StagedStore::open_with_paths(
            &cfg.download_dir,
            &layout.state_root,
            info.clone(),
            store_cfg,
        )
        .await?;
        let peer_id = client.peer_id();
        let mut picker = PiecePicker::with_end_game(
            info.piece_count(),
            cfg.block_size,
            cfg.end_game_threshold,
        );
        let download_files = cfg.download_files.clone();
        if !download_files.is_empty() {
            let wanted = pieces_for_selected_files(&info, &download_files);
            picker.set_wanted_pieces(&wanted);
        }
        let checkpoint_path = ResumeCheckpoint::path_for(store.state_path());
        let piece_count = info.piece_count();
        let (progress_tx, _) = watch::channel(DownloadProgress {
            pieces_complete: 0,
            piece_count,
            bytes_downloaded: 0,
        });

        let mut session = Self {
            client,
            cfg,
            info,
            info_dict_bytes,
            store,
            peer_id,
            picker: Arc::new(RwLock::new(picker)),
            announce,
            trackers,
            web_seeds,
            static_peers: RwLock::new(Vec::new()),
            peer_ledger: Arc::new(RwLock::new(PeerLedger::default())),
            checkpoint_path,
            resumed_bytes: AtomicU64::new(0),
            bytes_uploaded: AtomicU64::new(0),
            tracker_interval_secs: AtomicU64::new(0),
            last_tracker_announce: RwLock::new(None),
            scrape_stats: RwLock::new(None),
            download_files,
            progress_tx,
            stop_requested: AtomicBool::new(false),
        };

        session.restore_from_disk().await?;
        session.publish_progress().await;
        Ok(session)
    }

    async fn restore_from_disk(&mut self) -> Result<()> {
        let checkpoint = ResumeCheckpoint::load(&self.checkpoint_path, self.store.integrity()).await?;
        // Only trust pieces that verify on disk. Checkpoint completed_pieces is advisory
        // metadata under host integrity ACL — never a substitute for hash proof.
        let verified = self.store.scan_verified_pieces().await?;
        let completed: HashSet<u32> = verified.into_iter().collect();

        {
            let mut picker = self.picker.write().await;
            for index in &completed {
                picker.mark_have(*index);
            }
        }

        let complete_count = self.picker.read().await.complete_count();
        let bytes = self
            .info
            .piece_length
            .saturating_mul(complete_count as u32) as u64;
        self.resumed_bytes
            .store(bytes.max(checkpoint.bytes_downloaded), Ordering::Relaxed);

        *self.peer_ledger.write().await = PeerLedger::load(checkpoint.peers);

        if self.picker.read().await.is_complete() {
            tracing::info!(
                hash = %self.info.info_hash.to_hex(),
                name = %self.info.name,
                "torrent already complete on disk"
            );
        } else if complete_count > 0 {
            tracing::info!(
                hash = %self.info.info_hash.to_hex(),
                pieces = complete_count,
                total = self.info.piece_count(),
                "resumed partial download from disk"
            );
        }

        Ok(())
    }

    pub fn peer_ledger(&self) -> Arc<RwLock<PeerLedger>> {
        Arc::clone(&self.peer_ledger)
    }

    pub fn resilience(&self) -> &crate::config::ResilienceConfig {
        &self.cfg.resilience
    }

    pub async fn save_checkpoint(&self, bytes_downloaded: u64, completed: &HashSet<u32>) -> Result<()> {
        let checkpoint = ResumeCheckpoint {
            info_hash_hex: self.info.info_hash.to_hex(),
            bytes_downloaded,
            completed_pieces: completed.clone(),
            peers: self
                .peer_ledger
                .read()
                .await
                .iter()
                .map(|(addr, record)| (*addr, record.clone()))
                .collect(),
            saved_at: crate::session::resume::unix_now(),
        };
        checkpoint
            .save(&self.checkpoint_path, self.store.integrity())
            .await
    }

    pub async fn heal_discovery(&self) {
        tracing::info!(
            hash = %self.info_hash().to_hex(),
            "self-heal: re-bootstrap peer discovery"
        );
        self.peer_ledger
            .write()
            .await
            .prune_expired_bans(self.resilience());
        if let Err(err) = self.bootstrap_dht().await {
            tracing::debug!(%err, "self-heal dht bootstrap failed");
        }
        self.announce_dht().await;
        if self.cfg.peer_discovery.uses_tracker() && self.tracker_announce_due().await {
            self.tracker_announce(AnnounceEvent::Started).await;
        }
    }

    pub fn record_peer_success(&self, addr: SocketAddr) {
        let ledger = Arc::clone(&self.peer_ledger);
        tokio::spawn(async move {
            ledger.write().await.record_success(addr);
        });
    }

    pub fn record_peer_failure(&self, addr: SocketAddr) {
        let ledger = Arc::clone(&self.peer_ledger);
        let cfg = *self.resilience();
        tokio::spawn(async move {
            ledger.write().await.record_failure(addr, &cfg);
        });
    }

    pub fn resumed_bytes(&self) -> u64 {
        self.resumed_bytes.load(Ordering::Relaxed)
    }

    pub fn bytes_uploaded(&self) -> u64 {
        self.bytes_uploaded.load(Ordering::Relaxed)
    }

    pub fn record_downloaded(&self, n: u64) {
        // Single cumulative counter (resume baseline + live); avoid resume+live double-count.
        self.resumed_bytes.fetch_add(n, Ordering::Relaxed);
    }

    /// Cumulative downloaded reported to trackers (resume baseline + live).
    pub fn announce_downloaded(&self) -> u64 {
        self.resumed_bytes.load(Ordering::Relaxed)
    }

    pub fn record_uploaded(&self, n: u64) {
        self.bytes_uploaded.fetch_add(n, Ordering::Relaxed);
    }

    pub fn tracker_interval_secs(&self) -> Option<u64> {
        let v = self.tracker_interval_secs.load(Ordering::Relaxed);
        (v > 0).then_some(v)
    }

    async fn tracker_announce_due(&self) -> bool {
        let Some(secs) = self.tracker_interval_secs() else {
            return true;
        };
        match *self.last_tracker_announce.read().await {
            None => true,
            Some(at) => at.elapsed() >= std::time::Duration::from_secs(secs),
        }
    }

    fn note_tracker_interval(&self, response: &crate::tracker::AnnounceResponse) {
        let secs = response
            .min_interval_secs
            .or(response.interval_secs)
            .map(u64::from)
            .unwrap_or(0);
        if secs > 0 {
            self.tracker_interval_secs.store(secs, Ordering::Relaxed);
        }
    }

    async fn note_tracker_announce_time(&self) {
        *self.last_tracker_announce.write().await = Some(Instant::now());
    }

    pub fn client(&self) -> &Arc<Client> {
        &self.client
    }

    pub async fn add_static_peers(&self, peers: Vec<SocketAddr>) {
        let mut static_peers = self.static_peers.write().await;
        static_peers.extend(peers);
        static_peers.sort();
        static_peers.dedup();
    }

    pub async fn is_static_peer(&self, addr: SocketAddr) -> bool {
        self.static_peers.read().await.binary_search(&addr).is_ok()
    }

    pub fn info_hash(&self) -> InfoHash {
        self.info.info_hash
    }

    /// Host content + ledger paths for this torrent.
    pub fn layout(&self) -> TorrentLayout {
        self.cfg.torrent_layout(&self.info.info_hash.to_hex())
    }

    pub fn checkpoint_path(&self) -> &std::path::Path {
        &self.checkpoint_path
    }

    pub fn content_path(&self) -> &std::path::Path {
        self.store.download_path()
    }

    pub fn state_path(&self) -> &std::path::Path {
        self.store.state_path()
    }

    /// Subscribe to download progress updates (piece completion / byte counters).
    pub fn subscribe_progress(&self) -> watch::Receiver<DownloadProgress> {
        self.progress_tx.subscribe()
    }

    /// Snapshot current progress from the piece picker + byte counters.
    pub async fn current_progress(&self) -> DownloadProgress {
        let pieces_complete = self.picker.read().await.complete_count();
        DownloadProgress {
            pieces_complete: pieces_complete as u32,
            piece_count: self.info.piece_count(),
            bytes_downloaded: self.resumed_bytes(),
        }
    }

    pub(crate) async fn publish_progress(&self) {
        let progress = self.current_progress().await;
        self.progress_tx.send_replace(progress);
    }

    pub(crate) fn publish_progress_snapshot(&self, progress: DownloadProgress) {
        self.progress_tx.send_replace(progress);
    }

    pub fn request_stop(&self) {
        self.stop_requested.store(true, Ordering::SeqCst);
    }

    pub fn is_stop_requested(&self) -> bool {
        self.stop_requested.load(Ordering::SeqCst)
    }

    pub fn clear_stop_request(&self) {
        self.stop_requested.store(false, Ordering::SeqCst);
    }

    pub fn info(&self) -> &TorrentInfo {
        &self.info
    }

    /// Exact bencoded info dict bytes for BEP 9 ut_metadata.
    pub fn info_dict_bytes(&self) -> &[u8] {
        &self.info_dict_bytes
    }

    pub fn metadata_size(&self) -> Option<u32> {
        let len = self.info_dict_bytes.len();
        if len == 0 || len > u32::MAX as usize {
            None
        } else {
            Some(len as u32)
        }
    }

    pub fn peer_id(&self) -> PeerId {
        self.peer_id
    }

    pub fn store(&self) -> &StagedStore {
        &self.store
    }

    pub fn config(&self) -> &Config {
        &self.cfg
    }

    pub fn web_seeds(&self) -> &[String] {
        &self.web_seeds
    }

    pub fn picker(&self) -> Arc<RwLock<PiecePicker>> {
        Arc::clone(&self.picker)
    }

    pub async fn bootstrap_dht(&self) -> Result<()> {
        if let Some(dht) = self.client.dht() {
            dht.bootstrap(&[]).await?;
        }
        Ok(())
    }

    fn tracker_ipv6_compact(&self) -> Option<[u8; 18]> {
        Self::tracker_ipv6_compact_from_cfg(&self.cfg)
    }

    fn tracker_ipv6_compact_from_cfg(cfg: &Config) -> Option<[u8; 18]> {
        cfg.announce_ipv6
            .or_else(|| {
                if cfg.listen_dual_stack {
                    crate::net::detect_local_ipv6()
                } else {
                    None
                }
            })
            .map(|ip| crate::net::compact_ipv6(ip, cfg.listen_port))
    }

    pub fn scrape_stats(&self) -> Option<ScrapeStats> {
        self.scrape_stats.try_read().ok().and_then(|g| *g)
    }

    pub fn download_files(&self) -> &[String] {
        &self.download_files
    }

    pub async fn discover_peers(&self) -> Result<Vec<SocketAddr>> {
        let mut found = HashSet::new();
        let left = self.remaining_bytes();
        let downloaded = self.announce_downloaded();
        let uploaded = self.bytes_uploaded();

        if self.cfg.peer_discovery.uses_tracker() {
            let ipv6 = self.tracker_ipv6_compact();
            for url in self.all_tracker_urls() {
                match announce(
                    &url,
                    self.info.info_hash.as_bytes(),
                    &self.peer_id,
                    self.cfg.listen_port,
                    uploaded,
                    downloaded,
                    left,
                    AnnounceEvent::None,
                    self.cfg.tracker_block_private,
                    ipv6.as_ref(),
                    self.cfg.announce_ip,
                )
                .await
                {
                    Ok(response) => {
                        self.note_tracker_interval(&response);
                        found.extend(response.peers);
                    }
                    Err(err) => tracing::debug!(%err, tracker = %url, "tracker announce failed"),
                }
            }

            if let Some(url) = self.all_tracker_urls().first() {
                let hash = *self.info.info_hash.as_bytes();
                if let Ok(stats) = crate::tracker::scrape_http(
                    url,
                    std::slice::from_ref(&hash),
                    self.cfg.tracker_block_private,
                )
                .await
                {
                    if let Some(seeders) = stats.get(&hash) {
                        *self.scrape_stats.write().await = Some(*seeders);
                        tracing::debug!(
                            complete = seeders.complete,
                            incomplete = seeders.incomplete,
                            downloaded = seeders.downloaded,
                            "tracker scrape"
                        );
                        if seeders.complete == 0 && seeders.incomplete == 0 {
                            tracing::debug!("scrape reports empty swarm; prioritizing DHT/static peers");
                        }
                    }
                }
            }
        }

        if self.swarm_uses_dht() {
            if let Some(dht) = self.client.dht() {
                match dht.get_peers(self.info.info_hash.as_bytes()).await {
                    Ok(peers) => found.extend(crate::tracker::filter_peers_for_dial(
                        peers,
                        self.cfg.tracker_block_private,
                    )),
                    Err(err) => tracing::debug!(%err, "dht get_peers failed"),
                }
            }
        }

        // Host-configured static peers are trusted and not filtered.
        found.extend(self.static_peers.read().await.iter().copied());

        if found.is_empty() {
            return Err(Error::Session("no peers discovered".into()));
        }

        let mut out: Vec<_> = found.into_iter().collect();
        out.sort();
        self.peer_ledger.write().await.note_discovered(&out);
        Ok(out)
    }

    pub fn swarm_uses_dht(&self) -> bool {
        !self.info.private && self.cfg.dht_enabled && self.cfg.peer_discovery.uses_dht()
    }

    pub async fn announce_dht(&self) {
        if !self.swarm_uses_dht() {
            return;
        }
        if let Some(dht) = self.client.dht() {
            if let Err(err) = dht
                .announce_peer(self.info.info_hash.as_bytes(), self.cfg.listen_port)
                .await
            {
                tracing::debug!(%err, "dht announce_peer failed");
            }
        }
    }

    pub async fn announce_started(&self) {
        if self.cfg.peer_discovery.uses_tracker() {
            self.tracker_announce(AnnounceEvent::Started).await;
        }
        self.announce_dht().await;
    }

    pub async fn announce_completed(&self) {
        if !self.picker.read().await.is_fully_complete() {
            tracing::debug!(
                hash = %self.info_hash().to_hex(),
                "skipping completed announce: not all torrent pieces present"
            );
            return;
        }
        self.tracker_announce(AnnounceEvent::Completed).await;
    }

    pub async fn announce_stopped(&self) {
        if self.cfg.peer_discovery.uses_tracker() {
            self.tracker_announce(AnnounceEvent::Stopped).await;
        }
    }

    async fn tracker_announce(&self, event: AnnounceEvent) {
        let left = match event {
            AnnounceEvent::Completed => 0,
            _ => self.remaining_bytes(),
        };
        let downloaded = self.announce_downloaded();
        let uploaded = self.bytes_uploaded();
        let ipv6 = self.tracker_ipv6_compact();
        let mut any_ok = false;
        for url in self.all_tracker_urls() {
            match announce(
                &url,
                self.info.info_hash.as_bytes(),
                &self.peer_id,
                self.cfg.listen_port,
                uploaded,
                downloaded,
                left,
                event,
                self.cfg.tracker_block_private,
                ipv6.as_ref(),
                self.cfg.announce_ip,
            )
            .await
            {
                Ok(response) => {
                    self.note_tracker_interval(&response);
                    any_ok = true;
                    // Apply discovered peers via existing path if present in call sites.
                    let _ = response;
                }
                Err(err) => tracing::debug!(%err, %url, "tracker announce failed"),
            }
        }
        if any_ok {
            self.note_tracker_announce_time().await;
        }
    }

    fn all_tracker_urls(&self) -> Vec<String> {
        let mut urls = VecDeque::new();
        if let Some(url) = &self.announce {
            urls.push_back(url.clone());
        }
        for url in &self.trackers {
            if self.announce.as_ref() != Some(url) {
                urls.push_back(url.clone());
            }
        }
        urls.into()
    }

    fn remaining_bytes(&self) -> u64 {
        let Ok(picker) = self.picker.try_read() else {
            return self.info.total_len;
        };
        let mut left = 0u64;
        for index in 0..self.info.piece_count() {
            if picker.is_wanted(index) && !picker.has_piece(index) {
                left = left.saturating_add(u64::from(self.info.piece_size(index)));
            }
        }
        left
    }

    pub async fn download(self: Arc<Self>) -> Result<super::engine::DownloadProgress> {
        super::engine::run_download(self).await
    }

    /// Durable download loop: resumes from disk, reconnects, and self-heals until complete.
    pub async fn run(self: Arc<Self>) -> Result<super::engine::DownloadProgress> {
        use std::time::Duration;

        let mut backoff = Duration::from_secs(self.cfg.resilience.run_retry_initial_secs.max(1));
        let max_backoff = Duration::from_secs(self.cfg.resilience.run_retry_max_secs.max(1));
        loop {
            if self.is_stop_requested() {
                return Err(Error::Session("session stopped".into()));
            }
            match self.clone().download().await {
                Ok(progress) => return Ok(progress),
                Err(err) => {
                    if self.is_stop_requested() {
                        return Err(Error::Session("session stopped".into()));
                    }
                    if self.picker.read().await.is_complete() {
                        return Err(err);
                    }
                    tracing::warn!(
                        %err,
                        hash = %self.info_hash().to_hex(),
                        retry_secs = backoff.as_secs(),
                        "download interrupted; restarting session"
                    );
                    tokio::time::sleep(backoff).await;
                    backoff = (backoff * 2).min(max_backoff);
                }
            }
        }
    }

    pub async fn seed(self: Arc<Self>) -> Result<()> {
        super::seed::run_seed(self).await
    }

    /// Dial a remote peer and upload pieces (used when the leech listens on a shared port).
    pub async fn serve_outbound_to(self: Arc<Self>, addr: SocketAddr) -> Result<()> {
        crate::session::seed_inbound::serve_outbound_to(self, addr).await
    }
}

async fn fetch_metadata_from_peers(
    client: &Arc<Client>,
    peers: &[SocketAddr],
    info_hash: InfoHash,
    peer_id: PeerId,
) -> Result<crate::metadata::FetchedMetadata> {
    let _ = peer_id;
    let mut last_err = Error::Metadata("no peers available for metadata".into());
    for addr in peers {
        match async {
            let conn = client.dial_torrent_peer(*addr, info_hash, true).await?;
            fetch_metadata_with_bytes(conn).await
        }
        .await
        {
            Ok(fetched) => return Ok(fetched),
            Err(err) => {
                tracing::debug!(%err, %addr, "metadata fetch failed");
                last_err = err;
            }
        }
    }
    Err(last_err)
}