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
use crate::{InfoHash, Result, TorrentError};
use derive_more::Display;
use fx_callback::{Callback, MultiThreadedCallback, Subscription};
use itertools::Itertools;
use log::{debug, trace, warn};
use rand::{rng, RngExt};
use socket2::{Domain, Protocol, Socket, Type};
use std::io;
use std::io::Write;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::str::FromStr;
use std::sync::Arc;
use tokio::net::UdpSocket;
use tokio::select;
use tokio_util::sync::CancellationToken;

const MULTICAST_IPV4: Ipv4Addr = Ipv4Addr::new(239, 192, 152, 143);
const MULTICAST_IPV6: &str = "ff15::efc0:988f";
const LSD_METHOD: &str = "BT-SEARCH";
const LSD_PORT: u16 = 6771;

#[derive(Debug)]
pub enum LocalServiceDiscoveryEvent {
    /// Invoked when a peer is discovered for the given info hash.
    PeerDiscovered(InfoHash, SocketAddr),
    /// Invoked when the local service discovery is closed.
    Closed,
}

/// The local service discovery (BEP14) used for finding peers.
#[derive(Debug, Clone)]
pub struct LocalServiceDiscovery {
    inner: Arc<InnerLocalServiceDiscovery>,
}

impl LocalServiceDiscovery {
    /// Create a new local service discovery instance.
    /// The instance will join the multicast group of `listen_address`.
    pub async fn new(listen_address: IpAddr) -> Result<Self> {
        let socket = Self::bind_socket(listen_address).await?;
        let inner = Arc::new(InnerLocalServiceDiscovery {
            socket,
            listen_address,
            cookie: rng().random(),
            callbacks: MultiThreadedCallback::new(),
            cancellation_token: Default::default(),
        });

        let inner_main = inner.clone();
        spawn!("InnerLocalServiceDiscovery::run", async move {
            inner_main.run().await
        });

        Ok(Self { inner })
    }

    /// Announce a torrent to the local network.
    pub async fn announce(&self, info_hash: &InfoHash, port: u16) {
        self.inner.announce(info_hash, port).await;
    }

    /// Returns `true` if the local service discovery is closed and no longer accepts any messages.
    pub fn is_closed(&self) -> bool {
        self.inner.cancellation_token.is_cancelled()
    }

    /// Close the local service discovery.
    pub fn close(&self) {
        self.inner.cancellation_token.cancel();
    }

    /// Try to bind a new socket for the local service discovery.
    /// The socket will try to join the broadcast group of `listen_address`.
    async fn bind_socket(listen_address: IpAddr) -> Result<UdpSocket> {
        let (domain, bind_addr): (Domain, IpAddr) = match listen_address {
            IpAddr::V4(_) => (Domain::IPV4, Ipv4Addr::UNSPECIFIED.into()),
            IpAddr::V6(_) => (Domain::IPV6, Ipv6Addr::UNSPECIFIED.into()),
        };
        let socket = Socket::new(domain, Type::DGRAM, Some(Protocol::UDP))?;
        socket.set_reuse_address(true)?;
        #[cfg(not(any(
            target_os = "windows",
            target_os = "solaris",
            target_os = "illumos",
            target_os = "cygwin",
            target_os = "wasi"
        )))]
        socket.set_reuse_port(true)?;
        socket.set_nonblocking(true)?;
        socket.set_broadcast(true)?;
        socket.bind(&SocketAddr::from((bind_addr, LSD_PORT)).into())?;
        let socket = UdpSocket::from_std(Into::<std::net::UdpSocket>::into(socket))?;
        trace!("Local service discovery bound to {}", socket.local_addr()?);

        // setup loopback
        let result = match listen_address {
            IpAddr::V4(_) => socket.set_multicast_loop_v4(true),
            IpAddr::V6(_) => socket.set_multicast_loop_v6(true),
        };
        match result {
            Ok(_) => (),
            Err(e) => debug!("Local service discovery failed to set loopback, {}", e),
        }

        // setup multicast
        match listen_address {
            IpAddr::V4(addr) => socket.join_multicast_v4(MULTICAST_IPV4, addr)?,
            IpAddr::V6(_) => socket.join_multicast_v6(
                &MULTICAST_IPV6
                    .parse::<Ipv6Addr>()
                    .map_err(|e| TorrentError::AddressParse(e.to_string()))?,
                0,
            )?,
        };

        Ok(socket)
    }
}

impl Callback<LocalServiceDiscoveryEvent> for LocalServiceDiscovery {
    fn subscribe(&self) -> Subscription<LocalServiceDiscoveryEvent> {
        self.inner.callbacks.subscribe()
    }
}

impl Drop for LocalServiceDiscovery {
    fn drop(&mut self) {
        if Arc::strong_count(&self.inner) <= 2 {
            self.inner.cancellation_token.cancel();
        }
    }
}

#[derive(Debug, Display)]
#[display("{}", self.listen_address)]
struct InnerLocalServiceDiscovery {
    socket: UdpSocket,
    listen_address: IpAddr,
    cookie: u32,
    callbacks: MultiThreadedCallback<LocalServiceDiscoveryEvent>,
    cancellation_token: CancellationToken,
}

impl InnerLocalServiceDiscovery {
    /// Run the main loop of the local service discovery.
    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all))]
    async fn run(&self) {
        loop {
            let mut buffer = [0u8; 1500];
            select! {
                _ = self.cancellation_token.cancelled() => break,
                Ok((len, addr)) = self.socket.recv_from(&mut buffer) => self.on_packet(&buffer[..len], addr),
            }
        }

        self.callbacks.invoke(LocalServiceDiscoveryEvent::Closed);
        debug!("Local service discovery ({}) main loop ended", self);
    }

    /// Handle a received packet.
    fn on_packet(&self, buffer: &[u8], addr: SocketAddr) {
        trace!(
            "Local service discovery ({}) received packet from {}",
            self,
            addr
        );
        let msg = match str::from_utf8(buffer) {
            Ok(msg) => msg,
            Err(e) => {
                debug!(
                    "Local service discovery ({}) failed to parse packet, {}",
                    self, e
                );
                return;
            }
        };
        // check if the received packet is a bittorrent search
        // if not, ignore the packet
        if !msg.lines().next().map_or(false, |l| l.contains(LSD_METHOD)) {
            return;
        }

        // try to parse the message
        trace!(
            "Local service discovery ({}) is parsing message from {}",
            self,
            addr
        );
        let message = match Message::from_str(msg) {
            Ok(message) => message,
            Err(e) => {
                debug!(
                    "Local service discovery ({}) failed to parse message, {}",
                    self, e
                );
                return;
            }
        };

        // check if the received packet is our own packet
        if message.cookie == self.cookie {
            trace!(
                "Local service discovery ({}) received its own packet, skipping",
                self
            );
            return;
        }

        debug!(
            "Local service discovery ({}) received {} announce(s) from {}",
            self,
            message.info_hashes.len(),
            addr
        );
        let peer_addr = SocketAddr::new(addr.ip(), message.port);
        for info_hash in message.info_hashes {
            self.callbacks
                .invoke(LocalServiceDiscoveryEvent::PeerDiscovered(
                    info_hash,
                    peer_addr.clone(),
                ));
        }
    }

    async fn announce(&self, info_hash: &InfoHash, port: u16) {
        let target_addr = match self.listen_address {
            IpAddr::V4(_) => SocketAddr::new(MULTICAST_IPV4.into(), LSD_PORT),
            IpAddr::V6(_) => match MULTICAST_IPV6.parse::<Ipv6Addr>() {
                Ok(addr) => SocketAddr::new(addr.into(), LSD_PORT),
                Err(_) => unreachable!(),
            },
        };
        let message = Message {
            host: target_addr,
            port,
            info_hashes: vec![info_hash.clone()],
            cookie: self.cookie,
        };
        let bytes: Vec<u8> = message.into();

        match self.socket.send_to(bytes.as_ref(), target_addr).await {
            Ok(_) => debug!(
                "Local service discovery ({}) sent announce for {}",
                self, info_hash
            ),
            Err(e) => warn!(
                "Local service discovery ({}) failed to send message, {}",
                self, e
            ),
        }
    }
}

/// The local service discovery message used to make announcements.
#[derive(Debug)]
struct Message {
    host: SocketAddr,
    port: u16,
    info_hashes: Vec<InfoHash>,
    cookie: u32,
}

impl FromStr for Message {
    type Err = TorrentError;

    fn from_str(msg: &str) -> Result<Self> {
        let mut host: Option<SocketAddr> = None;
        let mut port: Option<u16> = None;
        let mut cookie: Option<u32> = None;
        let mut info_hashes: Vec<InfoHash> = vec![];

        // parse the packet lines
        for line in msg.lines() {
            if line.is_empty() {
                break;
            }
            // try to split the line into key and value
            let parts = line.splitn(2, ':').collect::<Vec<_>>();
            if parts.len() != 2 {
                continue;
            }
            let key = parts[0].trim().to_lowercase();
            let value = parts[1].trim();

            match key.as_str() {
                "host" => host = value.parse().ok(),
                "port" => port = value.parse().ok(),
                "cookie" => cookie = u32::from_str_radix(value, 16).ok(),
                "infohash" => {
                    match hex::decode(value.as_bytes())
                        .map_err(|e| TorrentError::InvalidInfoHash(e.to_string()))
                        .and_then(|bytes| InfoHash::try_from_bytes(&bytes))
                    {
                        Ok(hash) => info_hashes.push(hash),
                        Err(e) => {
                            debug!("Local service discovery failed to parse info hash: {}", e)
                        }
                    }
                }
                _ => continue,
            }
        }

        let port = port.ok_or(TorrentError::Io(io::Error::new(
            io::ErrorKind::InvalidData,
            "port field is missing",
        )))?;

        Ok(Message {
            host: host.unwrap_or(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), LSD_PORT)),
            port,
            info_hashes,
            cookie: cookie.unwrap_or(0),
        })
    }
}

impl From<Message> for Vec<u8> {
    fn from(value: Message) -> Self {
        let mut buffer = vec![];
        let encoded_info_hashes = value
            .info_hashes
            .iter()
            .map(|info_hash| hex::encode(info_hash.short_info_hash_bytes()))
            .collect_vec();

        // write the header
        let _ = buffer.write_all(format!("{} * HTTP/1.1\r\n", LSD_METHOD).as_bytes());
        // write the host
        let _ = buffer.write_all(format!("Host: {}\r\n", value.host).as_bytes());
        // write the port
        let _ = buffer.write_all(format!("Port: {}\r\n", value.port).as_bytes());
        // write the info hashes
        for info_hash in encoded_info_hashes {
            let _ = buffer.write_all(format!("Infohash: {}\r\n", info_hash).as_bytes());
        }
        // write the cookie
        let _ = buffer.write_all(format!("cookie: {}\r\n\r\n\r\n", value.cookie).as_bytes());

        buffer
    }
}

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

    mod new {
        use super::*;

        #[tokio::test]
        async fn test_broadcasting() {
            init_logger!();
            let service = LocalServiceDiscovery::new(Ipv4Addr::LOCALHOST.into())
                .await
                .unwrap();

            let result = service.inner.socket.broadcast().unwrap();

            assert_eq!(true, result, "expected the socket to be broadcasting");
        }
    }

    mod close {
        use super::*;

        #[tokio::test]
        async fn test_invoke_close_event() {
            init_logger!();
            let service = LocalServiceDiscovery::new(Ipv4Addr::LOCALHOST.into())
                .await
                .unwrap();

            let mut receiver = service.subscribe();

            service.close();

            let result = timeout!(Duration::from_millis(250), receiver.recv())
                .expect("expected to receive an event");
            match &*result {
                LocalServiceDiscoveryEvent::Closed => (),
                _ => assert!(
                    false,
                    "expected LocalServiceDiscoveryEvent::Closed, but got {:?}",
                    result
                ),
            }
        }
    }

    mod announce {
        use super::*;

        #[tokio::test]
        async fn test_announce() {
            init_logger!();
            let info_hash = InfoHash::from_str("A1DFEFEC1A9DD7FA8A041EBEEEA271DB55126D2F").unwrap();
            let source = LocalServiceDiscovery::new(Ipv4Addr::LOCALHOST.into())
                .await
                .unwrap();

            // announce the torrent
            source.announce(&info_hash, 6881).await;

            // receiving broadcast messages on the loopback address is OS specific
            // so we can't test it here
        }
    }

    mod on_packet {
        use super::*;

        #[tokio::test]
        async fn test_on_announce_packet() {
            init_logger!();
            let expected_info_hash =
                InfoHash::from_str("A1DFEFEC1A9DD7FA8A041EBEEEA271DB55126D2F").unwrap();
            let cookie: u32 = rng().random();
            let packet = format!(
                r#"BT-SEARCH * HTTP/1.1
Host: 239.192.152.143:6771
Port: 9900
Infohash: {}
cookie: {}


"#,
                expected_info_hash.v1_as_str().unwrap(),
                cookie
            );
            let service = LocalServiceDiscovery::new(Ipv4Addr::LOCALHOST.into())
                .await
                .unwrap();

            // subscribe to the target events
            let mut receiver = service.subscribe();

            // process the packet
            service.inner.on_packet(
                packet.as_bytes(),
                SocketAddr::from((Ipv4Addr::LOCALHOST, 6771)),
            );

            // wait for the packet event to be received
            let result = timeout!(Duration::from_millis(250), receiver.recv())
                .expect("expected to receive an event");
            match &*result {
                LocalServiceDiscoveryEvent::PeerDiscovered(info_hash, addr) => {
                    assert_eq!(
                        &expected_info_hash, info_hash,
                        "expected the info hash to match"
                    );
                    assert_eq!(
                        SocketAddr::from((Ipv4Addr::LOCALHOST, 9900)),
                        *addr,
                        "expected the address to match"
                    );
                }
                _ => assert!(
                    false,
                    "expected LocalServiceDiscoveryEvent::PeerDiscovered, but got {:?}",
                    result
                ),
            }
        }
    }
}