fx-torrent 0.10.3

FX Torrent is a feature rich Bittorrent protocol implementation written in rust supporting Linux, MacOS and Windows.
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
use crate::peer::PeerId;
use crate::tracker::HttpServer;
use crate::tracker::UdpServer;
use crate::tracker::{
    AnnounceEvent, Announcement, AnnouncementResponse, ConnectionMetrics, Result,
    ScrapeFileMetrics, ScrapeResult, TrackerError, TrackerHandle,
};
use crate::InfoHash;
use async_trait::async_trait;
use derive_more::Display;
use futures::stream::FuturesUnordered;
use futures::StreamExt;
use log::debug;
use std::collections::{HashMap, VecDeque};
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{oneshot, Mutex, Notify};
use tokio::{select, time};
use tokio_util::sync::CancellationToken;
use url::Url;

/// A request received by a tracker server connection.
///
/// This represents either an announce or scrape request made by a tracker client.
#[derive(Debug)]
pub enum ServerRequest {
    Announcement {
        addr: SocketAddr,
        request: Announcement,
        response: oneshot::Sender<AnnouncementResponse>,
    },
    Scrape {
        request: Vec<InfoHash>,
        response: oneshot::Sender<ScrapeResult>,
    },
}

/// The underlying listener which accepts incoming tracker requests.
///
/// Implementations are responsible for accepting incoming tracker
/// requests (announce/scrape) and exposing connection-related metrics.
#[async_trait]
pub trait TrackerListener: Send {
    /// Waits for the next incoming server request.
    ///
    /// Returns `None` when the connection is shutting down, and no further
    /// requests will be produced.
    async fn accept(&self) -> Option<ServerRequest>;

    /// Returns the local address on which this listener is listening.
    fn addr(&self) -> &SocketAddr;

    /// Returns the url of the tracker server.
    fn url(&self) -> &Url;

    /// Returns metrics for this listener.
    fn metrics(&self) -> &ConnectionMetrics;

    /// Closes the listener and stops accepting new connections.
    fn close(&self);
}

/// A simple tracker server that can handle announce and scrape requests.
///
/// The server can listen on one or more underlying [`TrackerListener`]s
/// (e.g. HTTP, UDP) and maintains in-memory state about known torrents and peers.
#[derive(Debug, Clone)]
pub struct TrackerServer {
    inner: Arc<InnerServer>,
}

impl TrackerServer {
    /// Creates a new tracker server listening on an ephemeral port.
    ///
    /// Internally, this delegates to [`TrackerServer::with_port`] with `0`.
    pub async fn new() -> Result<Self> {
        Self::with_port(0).await
    }

    /// Creates a new tracker server listening on the specified `port`.
    pub async fn with_port(port: u16) -> Result<Self> {
        let http_server = HttpServer::with_port(port).await?;
        let udp_server = UdpServer::with_port(http_server.addr().port()).await?;
        Self::with_listeners(vec![Box::new(http_server), Box::new(udp_server)])
    }

    /// Creates a new tracker server backed by the provided listeners.
    ///
    /// # Errors
    ///
    /// Returns [`TrackerError::Unavailable`] if `listeners` is empty.
    pub fn with_listeners(listeners: Vec<Box<dyn TrackerListener>>) -> Result<Self> {
        let (addr, url) = match listeners.get(0) {
            Some(conn) => (conn.addr().clone(), conn.url().clone()),
            None => return Err(TrackerError::Unavailable),
        };
        let inner = Arc::new(InnerServer {
            handle: Default::default(),
            addr,
            url,
            announce_interval: Duration::from_secs(5 * 60),
            torrents: Default::default(),
            cancellation: Default::default(),
        });

        let main_inner = inner.clone();
        spawn!("InnerServer::run", async move {
            main_inner.run(listeners).await;
        });

        Ok(Self { inner })
    }

    /// Returns the socket address this server is listening on.
    pub fn addr(&self) -> &SocketAddr {
        &self.inner.addr
    }

    /// Returns the url of the tracker server.
    pub fn url(&self) -> &Url {
        &self.inner.url
    }

    /// Adds or updates a peer in the tracker state for the given torrent.
    pub async fn add_peer(
        &self,
        info_hash: InfoHash,
        addr: SocketAddr,
        peer_id: PeerId,
        peer_port: u16,
        completed: bool,
    ) {
        let event = if completed {
            AnnounceEvent::Completed
        } else {
            AnnounceEvent::None
        };

        let _ = self
            .inner
            .handle_announcement(
                addr,
                Announcement {
                    info_hash,
                    peer_id,
                    peer_port,
                    event,
                    bytes_completed: 0,
                    bytes_remaining: 0,
                },
            )
            .await;
    }
}

#[derive(Debug, Display)]
#[display("{}", handle)]
struct InnerServer {
    handle: TrackerHandle,
    addr: SocketAddr,
    url: Url,
    announce_interval: Duration,
    torrents: Mutex<HashMap<InfoHash, HashMap<PeerEntry, TorrentPeer>>>,
    cancellation: CancellationToken,
}

impl InnerServer {
    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
    async fn run(&self, listeners: Vec<Box<dyn TrackerListener>>) {
        loop {
            let mut futures =
                FuturesUnordered::from_iter(listeners.iter().map(|e| e.accept())).fuse();
            select! {
                _ = self.cancellation.cancelled() => break,
                Some(Some(request)) = futures.next() => self.on_request(request).await,
            }
        }

        listeners.into_iter().for_each(|e| e.close());
        debug!("Tracker server {} main loop ended", self);
    }

    async fn on_request(&self, request: ServerRequest) {
        match request {
            ServerRequest::Announcement {
                addr,
                request,
                response,
            } => {
                let _ = response.send(self.handle_announcement(addr, request).await);
            }
            ServerRequest::Scrape {
                request, response, ..
            } => {
                let _ = response.send(self.handle_scrape(request).await);
            }
        }
    }

    async fn handle_announcement(
        &self,
        addr: SocketAddr,
        announcement: Announcement,
    ) -> AnnouncementResponse {
        let mut torrents = self.torrents.lock().await;
        let torrent_peers = torrents.entry(announcement.info_hash.clone()).or_default();
        let entry = PeerEntry::new(announcement.peer_id, addr.ip());

        // update or remove the entry within the tracked torrents
        if announcement.event == AnnounceEvent::Stopped {
            torrent_peers.remove(&entry);
        } else {
            let torrent_peer = torrent_peers.entry(entry).or_insert(TorrentPeer {
                peer_port: announcement.peer_port,
                bytes_completed: announcement.bytes_completed,
                bytes_remaining: announcement.bytes_remaining,
                completed: false,
            });

            torrent_peer.completed = Self::is_torrent_peer_completed(&announcement);
            torrent_peer.bytes_completed = announcement.bytes_completed;
            torrent_peer.bytes_remaining = announcement.bytes_remaining;
        }

        // retrieve all peers except for the addr
        let mut leechers = 0;
        let mut seeders = 0;
        let mut peers = Vec::new();

        for (entry, torrent_peer) in torrent_peers
            .iter()
            .filter(|(entry, _)| entry.id != announcement.peer_id)
        {
            if torrent_peer.completed {
                seeders += 1;
            } else {
                leechers += 1;
            }

            peers.push((entry.ip, torrent_peer.peer_port).into());
        }

        AnnouncementResponse {
            interval_seconds: self.announce_interval.as_secs(),
            external_ip: None,
            leechers,
            seeders,
            peers,
        }
    }

    async fn handle_scrape(&self, hashes: Vec<InfoHash>) -> ScrapeResult {
        let torrents = self.torrents.lock().await;

        ScrapeResult {
            files: hashes
                .into_iter()
                .map(|hash| {
                    if let Some(peers) = torrents.get(&hash) {
                        let mut metrics = ScrapeFileMetrics::default();

                        for (_, peer) in peers {
                            if peer.completed {
                                metrics.complete += 1;
                                metrics.downloaded += 1;
                            } else {
                                metrics.incomplete += 1;
                            }
                        }

                        (hash, metrics)
                    } else {
                        (hash, ScrapeFileMetrics::default())
                    }
                })
                .collect(),
        }
    }

    fn is_torrent_peer_completed(announcement: &Announcement) -> bool {
        announcement.event == AnnounceEvent::Completed
            || (announcement.bytes_remaining == 0 && announcement.bytes_completed != 0)
    }
}

/// The common basic functionality of a tracker server.
#[derive(Debug)]
pub(crate) struct BaseServer {
    pub handle: TrackerHandle,
    pub queue: Mutex<VecDeque<ServerRequest>>,
    pub waker: Notify,
    pub timeout: Duration,
    pub metrics: ConnectionMetrics,
    pub cancellation_token: CancellationToken,
}

impl BaseServer {
    /// Create a new base with the specified operation timeout duration.
    pub fn new(handle: TrackerHandle, timeout: Duration) -> Self {
        Self {
            handle,
            queue: Default::default(),
            waker: Default::default(),
            timeout,
            metrics: Default::default(),
            cancellation_token: Default::default(),
        }
    }

    /// Waits for the next incoming server request.
    ///
    /// Returns `None` when the connection is shutting down, and no further
    /// requests will be produced.
    pub async fn accept(&self) -> Option<ServerRequest> {
        loop {
            if self.cancellation_token.is_cancelled() {
                return None;
            }
            if let Some(request) = self.queue.lock().await.pop_front() {
                return Some(request);
            }

            self.waker.notified().await;
        }
    }

    /// Queue a new pending request.
    pub async fn request(&self, request: ServerRequest) {
        let mut queue = self.queue.lock().await;
        queue.push_back(request);
        self.waker.notify_waiters();
    }

    /// Closes the server and stops accepting new connections.
    pub fn close(&self) {
        self.cancellation_token.cancel();
        self.waker.notify_waiters();
    }

    /// Waits for a response from the tracker server that manages this listener.
    ///
    /// If the tracker server does not respond within the specified timeout,
    /// an error is returned.
    pub async fn await_response<T>(&self, rx: oneshot::Receiver<T>) -> Result<T> {
        self.waker.notify_waiters();

        select! {
            _ = time::sleep(self.timeout) =>
                Err(TrackerError::Io(io::Error::new(io::ErrorKind::TimedOut, "response timed out"))),
            response = rx => response
                .map_err(|e| TrackerError::Io(io::Error::new(io::ErrorKind::BrokenPipe, e))),
        }
    }
}

/// A unique peer entry within the torrent tracker server.
/// Each announcement is always indexed based on the peer and ip address to prevent spoofing of peers.
#[derive(Debug, PartialEq, Eq, Hash)]
struct PeerEntry {
    id: PeerId,
    ip: IpAddr,
}

impl PeerEntry {
    fn new(id: PeerId, ip: IpAddr) -> Self {
        Self { id, ip }
    }
}

#[derive(Debug)]
struct TorrentPeer {
    peer_port: u16,
    bytes_completed: u64,
    bytes_remaining: u64,
    completed: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::Ipv4Addr;
    use std::str::FromStr;

    #[tokio::test]
    async fn test_handle_announcement_started() {
        init_logger!();
        let info_hash = InfoHash::from_str("EADAF0EFEA39406914414D359E0EA16416409BD7").unwrap();
        let peer_id = PeerId::new();
        let peer_port = 6881;
        let ip = Ipv4Addr::LOCALHOST;
        let expected_peer_entry = PeerEntry::new(peer_id.clone(), IpAddr::V4(ip.clone()));
        let server = TrackerServer::new().await.unwrap();

        server
            .inner
            .handle_announcement(
                (Ipv4Addr::LOCALHOST, 8000).into(),
                Announcement {
                    info_hash: info_hash.clone(),
                    peer_id,
                    peer_port,
                    event: AnnounceEvent::Started,
                    bytes_completed: 0,
                    bytes_remaining: 0,
                },
            )
            .await;

        let torrents = server.inner.torrents.lock().await;
        let torrent = torrents
            .get(&info_hash)
            .expect("expected the torrent entry to have been stored");
        let result = torrent
            .get(&expected_peer_entry)
            .expect("expected the peer entry to have been stored");

        assert_eq!(
            peer_port, result.peer_port,
            "expected the peer port to match the announcement"
        );
        assert_eq!(
            false, result.completed,
            "expected the torrent to not have been completed"
        );
    }

    #[tokio::test]
    async fn test_handle_announcement_completed() {
        init_logger!();
        let info_hash = InfoHash::from_str("EADAF0EFEA39406914414D359E0EA16416409BD7").unwrap();
        let peer_id = PeerId::new();
        let ip = Ipv4Addr::LOCALHOST;
        let expected_peer_entry = PeerEntry::new(peer_id.clone(), IpAddr::V4(ip.clone()));
        let server = TrackerServer::new().await.unwrap();

        server
            .inner
            .handle_announcement(
                (Ipv4Addr::LOCALHOST, 8000).into(),
                Announcement {
                    info_hash: info_hash.clone(),
                    peer_id,
                    peer_port: 6881,
                    event: AnnounceEvent::Completed,
                    bytes_completed: 0,
                    bytes_remaining: 0,
                },
            )
            .await;

        let torrents = server.inner.torrents.lock().await;
        let torrent = torrents
            .get(&info_hash)
            .expect("expected the torrent entry to have been stored");
        let result = torrent
            .get(&expected_peer_entry)
            .expect("expected the peer entry to have been stored");

        assert_eq!(
            true, result.completed,
            "expected the torrent to not have been completed"
        );
    }

    #[tokio::test]
    async fn test_handle_scrape() {
        init_logger!();
        let info_hash = InfoHash::from_str("EADAF0EFEA39406914414D359E0EA16416409BD7").unwrap();
        let peer_id = PeerId::new();
        let expected_result = ScrapeResult {
            files: vec![(
                info_hash.clone(),
                ScrapeFileMetrics {
                    complete: 0,
                    incomplete: 1,
                    downloaded: 0,
                },
            )]
            .into_iter()
            .collect(),
        };
        let server = TrackerServer::new().await.unwrap();

        // add dummy
        server
            .add_peer(
                info_hash.clone(),
                (Ipv4Addr::LOCALHOST, 8000).into(),
                peer_id.clone(),
                6881,
                false,
            )
            .await;

        let result = server.inner.handle_scrape(vec![info_hash.clone()]).await;

        assert_eq!(expected_result, result);
    }
}