aria2-protocol 0.3.10

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
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
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use bytes::Bytes;
use futures::Stream;
use futures::stream::{self, StreamExt};
use tokio::sync::{Mutex, Notify, watch};
use tokio::time::sleep;
use tracing::{info, warn};

mod parse;
mod source_fetcher;

#[cfg(test)]
mod tests;

use parse::EMBEDDED_TRACKER_LIST;
use source_fetcher::{ReqwestTrackerSourceFetcher, fetch_tracker_source, tracker_source_origin};

pub const DEFAULT_TRACKER_SOURCE: &str = "https://cf.trackerslist.com/best.txt";
pub const DEFAULT_TRACKER_UPDATE_INTERVAL: Duration = Duration::from_secs(604_800);
pub const DEFAULT_TRACKER_SOURCE_TIMEOUT: Duration = Duration::from_secs(15);
/// Maximum decoded response body accepted from one public tracker source.
pub const MAX_TRACKER_SOURCE_BYTES: usize = 8 * 1024 * 1024;
/// Maximum number of unique valid trackers accepted from one source.
pub const MAX_TRACKERS_PER_SOURCE: usize = 4096;
/// Bound source downloads so a large user-provided source list cannot create
/// an unbounded burst of outbound HTTP requests.
pub const MAX_TRACKER_SOURCE_CONCURRENCY: usize = 4;
const MAX_BACKOFF: Duration = Duration::from_secs(3_600);
const REMOTE_TEMPORARY_BACKOFF: Duration = Duration::from_secs(30);

/// Read a tracker-source response stream with the shared byte limit.
///
/// Engine integrations use this helper for their policy-configured HTTP
/// transports so they enforce the same resource bound as the standalone
/// protocol fetcher.
pub async fn read_tracker_source_body<S, E>(mut chunks: S) -> Result<String, String>
where
    S: Stream<Item = Result<Bytes, E>> + Unpin,
    E: std::fmt::Display,
{
    let mut body = Vec::new();
    while let Some(chunk) = chunks.next().await {
        let chunk = chunk.map_err(|error| format!("Read body failed: {error}"))?;
        if chunk.len() > MAX_TRACKER_SOURCE_BYTES.saturating_sub(body.len()) {
            return Err(format!(
                "Tracker source response exceeds the {MAX_TRACKER_SOURCE_BYTES}-byte limit"
            ));
        }
        body.extend_from_slice(&chunk);
    }
    Ok(String::from_utf8_lossy(&body).into_owned())
}

/// Fetches the body of a configured public tracker-list source.
///
/// The catalog owns source scheduling and parsing. Callers embedding it in an
/// engine can provide the transport so process network policy is applied at
/// the layer that owns that policy.
pub trait TrackerSourceFetcher: Send + Sync {
    fn fetch<'a>(
        &'a self,
        source: &'a str,
        timeout: Duration,
    ) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send + 'a>>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrackerProtocol {
    Http,
    Https,
    Udp,
    Wss,
}

/// Failure categories used to keep local connectivity problems out of the
/// process-wide public tracker health state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrackerFailureKind {
    /// DNS, socket, proxy, or TLS failures where the tracker was not reached.
    Network,
    /// The request or response timed out without a tracker-level response.
    Timeout,
    /// The tracker responded with a temporary server-side status.
    RemoteTemporary,
    /// The tracker explicitly rejected the announce.
    TrackerRejected,
    /// The remote response was not a valid tracker response.
    MalformedResponse,
}

impl TrackerFailureKind {
    /// Stable lowercase name used by RPC and diagnostic output.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Network => "network",
            Self::Timeout => "timeout",
            Self::RemoteTemporary => "remoteTemporary",
            Self::TrackerRejected => "trackerRejected",
            Self::MalformedResponse => "malformedResponse",
        }
    }
}

impl TrackerProtocol {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Http => "http",
            Self::Https => "https",
            Self::Udp => "udp",
            Self::Wss => "wss",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrackerEntry {
    pub url: String,
    pub protocol: TrackerProtocol,
    pub host: String,
    pub port: u16,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrackerCatalogConfig {
    pub enabled: bool,
    pub sources: Vec<String>,
    pub update_interval: Duration,
}

impl Default for TrackerCatalogConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            sources: vec![DEFAULT_TRACKER_SOURCE.to_string()],
            update_interval: DEFAULT_TRACKER_UPDATE_INTERVAL,
        }
    }
}

pub struct PublicTrackerListStats {
    pub total_entries: usize,
    pub http_count: usize,
    pub udp_count: usize,
    pub wss_count: usize,
    pub is_embedded_fallback: bool,
    pub last_updated: Option<Duration>,
    pub sources: Vec<String>,
    pub disabled: bool,
}

#[derive(Debug, Clone, Default)]
struct TrackerHealth {
    consecutive_failures: u32,
    next_retry: Option<Instant>,
    successes: u64,
    failures: u64,
}

pub struct PublicTrackerList {
    entries: tokio::sync::RwLock<Arc<Vec<TrackerEntry>>>,
    source_entries: tokio::sync::RwLock<HashMap<String, Vec<TrackerEntry>>>,
    source_fetcher: tokio::sync::RwLock<Arc<dyn TrackerSourceFetcher>>,
    config: tokio::sync::RwLock<TrackerCatalogConfig>,
    source_request_timeout: tokio::sync::RwLock<Duration>,
    source_fetch_concurrency: AtomicUsize,
    health: Mutex<HashMap<String, TrackerHealth>>,
    last_updated: tokio::sync::RwLock<Option<Instant>>,
    last_refresh_error: tokio::sync::RwLock<Option<String>>,
    running: AtomicBool,
    update_started: AtomicBool,
    config_changed: Notify,
    updates: watch::Sender<u64>,
}

impl Default for PublicTrackerList {
    fn default() -> Self {
        Self::new()
    }
}

impl PublicTrackerList {
    pub fn new() -> Self {
        Self::new_with_config(TrackerCatalogConfig::default())
    }

    pub fn new_with_config(config: TrackerCatalogConfig) -> Self {
        Self::new_with_config_and_fetcher(config, Arc::new(ReqwestTrackerSourceFetcher::new()))
    }

    /// Construct a catalog using a caller-owned HTTP source transport.
    pub fn new_with_config_and_fetcher(
        config: TrackerCatalogConfig,
        source_fetcher: Arc<dyn TrackerSourceFetcher>,
    ) -> Self {
        let entries = Self::parse(EMBEDDED_TRACKER_LIST);
        let (updates, _) = watch::channel(0);
        Self {
            entries: tokio::sync::RwLock::new(Arc::new(entries)),
            source_entries: tokio::sync::RwLock::new(HashMap::new()),
            source_fetcher: tokio::sync::RwLock::new(source_fetcher),
            config: tokio::sync::RwLock::new(config),
            source_request_timeout: tokio::sync::RwLock::new(DEFAULT_TRACKER_SOURCE_TIMEOUT),
            source_fetch_concurrency: AtomicUsize::new(MAX_TRACKER_SOURCE_CONCURRENCY),
            health: Mutex::new(HashMap::new()),
            last_updated: tokio::sync::RwLock::new(Some(Instant::now())),
            last_refresh_error: tokio::sync::RwLock::new(None),
            running: AtomicBool::new(true),
            update_started: AtomicBool::new(false),
            config_changed: Notify::new(),
            updates,
        }
    }

    /// Subscribe to tracker catalog membership and configuration changes.
    /// Health/backoff changes are intentionally excluded; callers can query
    /// the current availability snapshot when this revision advances.
    pub fn subscribe_updates(&self) -> watch::Receiver<u64> {
        self.updates.subscribe()
    }

    fn notify_updated(&self) {
        self.updates
            .send_modify(|revision| *revision = revision.wrapping_add(1));
    }

    pub fn parse(text: &str) -> Vec<TrackerEntry> {
        parse::parse_tracker_list(text)
    }

    pub async fn config(&self) -> TrackerCatalogConfig {
        self.config.read().await.clone()
    }

    /// Return the timeout applied to each complete tracker-source HTTP fetch.
    pub async fn source_request_timeout(&self) -> Duration {
        *self.source_request_timeout.read().await
    }

    /// Set the maximum number of public tracker sources fetched concurrently.
    ///
    /// The value is clamped to `1..=MAX_TRACKER_SOURCE_CONCURRENCY`. A refresh
    /// already in progress keeps the limit captured when it began.
    pub fn set_source_fetch_concurrency_limit(&self, limit: usize) {
        self.source_fetch_concurrency.store(
            limit.clamp(1, MAX_TRACKER_SOURCE_CONCURRENCY),
            Ordering::Release,
        );
    }

    /// Return the maximum concurrency used by future refreshes.
    pub fn source_fetch_concurrency_limit(&self) -> usize {
        self.source_fetch_concurrency.load(Ordering::Acquire)
    }

    /// Set the timeout for future tracker-source fetches.
    ///
    /// An in-progress fetch keeps the timeout captured when it started. A
    /// zero duration restores the library default.
    pub async fn set_source_request_timeout(&self, timeout: Duration) {
        *self.source_request_timeout.write().await = if timeout.is_zero() {
            DEFAULT_TRACKER_SOURCE_TIMEOUT
        } else {
            timeout
        };
        self.notify_updated();
        self.config_changed.notify_one();
    }

    /// Set the source-fetch timeout synchronously before the update loop starts.
    pub fn set_source_request_timeout_now(&self, timeout: Duration) {
        *self
            .source_request_timeout
            .try_write()
            .expect("tracker source timeout must not be held during engine setup") =
            if timeout.is_zero() {
                DEFAULT_TRACKER_SOURCE_TIMEOUT
            } else {
                timeout
            };
        self.notify_updated();
        self.config_changed.notify_one();
    }

    pub async fn set_config(&self, mut config: TrackerCatalogConfig) {
        config.sources = normalize_sources(config.sources);
        if config.update_interval.is_zero() {
            config.update_interval = DEFAULT_TRACKER_UPDATE_INTERVAL;
        }
        *self.config.write().await = config;
        self.notify_updated();
        self.config_changed.notify_one();
    }

    /// Apply configuration synchronously before the engine starts.
    ///
    /// Engine construction happens before its event loop is spawned, so using
    /// a blocking task here would introduce a race with the first download.
    pub fn set_config_now(&self, mut config: TrackerCatalogConfig) {
        config.sources = normalize_sources(config.sources);
        if config.update_interval.is_zero() {
            config.update_interval = DEFAULT_TRACKER_UPDATE_INTERVAL;
        }
        *self
            .config
            .try_write()
            .expect("public tracker config must not be held during engine setup") = config;
        self.notify_updated();
        self.config_changed.notify_one();
    }

    pub async fn sources(&self) -> Vec<String> {
        self.config.read().await.sources.clone()
    }

    pub async fn contains(&self, url: &str) -> bool {
        self.snapshot().await.iter().any(|entry| entry.url == url)
    }

    pub async fn snapshot(&self) -> Arc<Vec<TrackerEntry>> {
        self.entries.read().await.clone()
    }

    pub async fn available_snapshot(&self) -> Arc<Vec<TrackerEntry>> {
        if !self.config.read().await.enabled {
            return Arc::new(Vec::new());
        }
        let entries = self.snapshot().await;
        let now = Instant::now();
        let health = self.health.lock().await;
        Arc::new(
            entries
                .iter()
                .filter(|entry| {
                    health
                        .get(&entry.url)
                        .and_then(|state| state.next_retry)
                        .is_none_or(|retry_at| retry_at <= now)
                })
                .cloned()
                .collect(),
        )
    }

    pub async fn get_http_trackers(&self) -> Vec<String> {
        let entries = self.available_snapshot().await;
        entries
            .iter()
            .filter(|e| e.protocol == TrackerProtocol::Http || e.protocol == TrackerProtocol::Https)
            .map(|e| e.url.clone())
            .collect()
    }

    pub async fn get_udp_trackers(&self) -> Vec<String> {
        let entries = self.available_snapshot().await;
        entries
            .iter()
            .filter(|e| e.protocol == TrackerProtocol::Udp)
            .map(|e| e.url.clone())
            .collect()
    }

    pub async fn get_all(&self) -> Vec<TrackerEntry> {
        self.snapshot().await.as_ref().clone()
    }

    pub async fn fetch_and_update(&self, url: &str) -> Result<usize, String> {
        let timeout = self.source_request_timeout().await;
        let fetcher = self.source_fetcher.read().await.clone();
        let entries = fetch_tracker_source(fetcher.as_ref(), url, timeout).await?;
        self.source_entries
            .write()
            .await
            .insert(url.to_string(), entries);
        self.merge_source_snapshots().await
    }

    pub fn start_auto_update(self: &Arc<Self>, url: String, interval: Duration) {
        self.set_config_now(TrackerCatalogConfig {
            enabled: true,
            sources: vec![url],
            update_interval: interval,
        });
        self.start_catalog_update();
    }

    pub fn start_catalog_update(self: &Arc<Self>) {
        if self.update_started.swap(true, Ordering::AcqRel) {
            return;
        }
        let catalog = Arc::clone(self);
        tokio::spawn(async move {
            while catalog.running.load(Ordering::Relaxed) {
                let config = catalog.config().await;
                if config.enabled
                    && let Err(error) = catalog.refresh().await
                {
                    warn!("Public tracker catalog refresh failed: {}", error);
                }
                let sleep = sleep(config.update_interval);
                tokio::pin!(sleep);
                tokio::select! {
                    _ = &mut sleep => {},
                    _ = catalog.config_changed.notified() => {},
                }
            }
            info!("Public tracker catalog update loop exited");
        });
    }

    pub async fn refresh(&self) -> Result<usize, String> {
        let config = self.config().await;
        if !config.enabled {
            return Ok(0);
        }
        let sources = normalize_sources(config.sources);
        if sources.is_empty() {
            *self.last_refresh_error.write().await = None;
            return Ok(0);
        }

        let fetcher = self.source_fetcher.read().await.clone();
        let request_timeout = self.source_request_timeout().await;
        let source_concurrency = self.source_fetch_concurrency.load(Ordering::Acquire);
        let results = stream::iter(sources.iter().cloned().map(|source| {
            let fetcher = Arc::clone(&fetcher);
            async move {
                let result = fetch_tracker_source(fetcher.as_ref(), &source, request_timeout).await;
                (source, result)
            }
        }))
        .buffer_unordered(source_concurrency)
        .collect::<Vec<_>>()
        .await;
        let mut errors = Vec::new();
        let mut source_entries = self.source_entries.write().await;
        source_entries.retain(|source, _| sources.contains(source));
        for (source, result) in results {
            match result {
                Ok(entries) => {
                    source_entries.insert(source, entries);
                }
                Err(error) => errors.push(format!("{}: {}", tracker_source_origin(&source), error)),
            }
        }
        let has_source_snapshot = !source_entries.is_empty();
        drop(source_entries);

        if !has_source_snapshot {
            let error = errors.join("; ");
            *self.last_refresh_error.write().await = Some(error.clone());
            return Err(if error.is_empty() {
                "All public tracker sources returned no valid trackers".to_string()
            } else {
                error
            });
        }

        let count = self.merge_source_snapshots().await?;
        *self.last_refresh_error.write().await = (!errors.is_empty()).then_some(errors.join("; "));
        Ok(count)
    }

    async fn merge_source_snapshots(&self) -> Result<usize, String> {
        let source_entries = self.source_entries.read().await;
        let mut merged = Vec::new();
        let mut seen = std::collections::HashSet::new();
        for entries in source_entries.values() {
            for entry in entries {
                if seen.insert(entry.url.clone()) {
                    merged.push(entry.clone());
                }
            }
        }
        drop(source_entries);
        self.replace_snapshot(merged).await
    }

    async fn replace_snapshot(&self, entries: Vec<TrackerEntry>) -> Result<usize, String> {
        if entries.is_empty() {
            return Err("Parsed 0 entries from response".to_string());
        }
        let count = entries.len();
        *self.entries.write().await = Arc::new(entries);
        let current_urls: std::collections::HashSet<_> = self
            .entries
            .read()
            .await
            .iter()
            .map(|entry| entry.url.clone())
            .collect();
        self.health
            .lock()
            .await
            .retain(|url, _| current_urls.contains(url));
        *self.last_updated.write().await = Some(Instant::now());
        self.notify_updated();
        info!("Public tracker catalog updated: {} trackers", count);
        Ok(count)
    }

    pub async fn record_success(&self, url: &str) {
        let mut health = self.health.lock().await;
        let state = health.entry(url.to_string()).or_default();
        state.consecutive_failures = 0;
        state.next_retry = None;
        state.successes = state.successes.saturating_add(1);
    }

    pub async fn record_failure(&self, url: &str) {
        self.record_failure_kind(url, TrackerFailureKind::RemoteTemporary)
            .await;
    }

    /// Record a classified announce failure.
    ///
    /// Local failures are deliberately telemetry-only at catalog scope. A
    /// caller's DNS outage, offline connection, or timeout must not hide a
    /// tracker from unrelated downloads in the same process.
    pub async fn record_failure_kind(&self, url: &str, kind: TrackerFailureKind) {
        let mut health = self.health.lock().await;
        let state = health.entry(url.to_string()).or_default();
        state.failures = state.failures.saturating_add(1);

        let (consecutive_failures, base_backoff) = match kind {
            TrackerFailureKind::Network | TrackerFailureKind::Timeout => return,
            TrackerFailureKind::RemoteTemporary => (
                state.consecutive_failures.saturating_add(1),
                REMOTE_TEMPORARY_BACKOFF,
            ),
            TrackerFailureKind::MalformedResponse => return,
            TrackerFailureKind::TrackerRejected => return,
        };

        state.consecutive_failures = consecutive_failures;
        let exponent = consecutive_failures.saturating_sub(1).min(10);
        let backoff = base_backoff
            .checked_mul(1u32 << exponent)
            .unwrap_or(MAX_BACKOFF)
            .min(MAX_BACKOFF);
        state.next_retry = Some(Instant::now() + backoff);
    }

    pub async fn last_refresh_error(&self) -> Option<String> {
        self.last_refresh_error.read().await.clone()
    }

    pub fn shutdown(&self) {
        self.running.store(false, Ordering::Relaxed);
        self.config_changed.notify_one();
    }

    pub async fn stats(&self) -> PublicTrackerListStats {
        let entries = self.snapshot().await;
        let http_count = entries
            .iter()
            .filter(|e| matches!(e.protocol, TrackerProtocol::Http | TrackerProtocol::Https))
            .count();
        let udp_count = entries
            .iter()
            .filter(|e| e.protocol == TrackerProtocol::Udp)
            .count();
        let wss_count = entries
            .iter()
            .filter(|e| e.protocol == TrackerProtocol::Wss)
            .count();

        let embedded = EMBEDDED_TRACKER_LIST
            .lines()
            .filter(|l| l.trim().contains("://"))
            .count();

        PublicTrackerListStats {
            total_entries: entries.len(),
            http_count,
            udp_count,
            wss_count,
            is_embedded_fallback: entries.len() == embedded,
            last_updated: self.last_updated.read().await.map(|t| t.elapsed()),
            sources: self.sources().await,
            disabled: !self.config.read().await.enabled,
        }
    }
}

fn normalize_sources(sources: Vec<String>) -> Vec<String> {
    let mut seen = std::collections::HashSet::new();
    sources
        .into_iter()
        .flat_map(|source| {
            source
                .split([',', '\n'])
                .map(str::trim)
                .map(str::to_string)
                .collect::<Vec<_>>()
        })
        .filter(|source| !source.is_empty() && seen.insert(source.clone()))
        .collect()
}