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
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
use crate::dht::{Error, NodeId, NodeMetrics, Result};
use rand::{rng, RngExt};
use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha1::{Digest, Sha1};
use std::fmt::{Display, Formatter};
use std::net::{IpAddr, SocketAddr};
use std::result;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock};

const TOKEN_SECRET_SIZE: usize = 20;
const TOKEN_SIZE: usize = 8;
const TOKEN_MAX_LEN: usize = 64;
const QUESTIONABLE_NODE_AFTER: Duration = Duration::from_secs(15 * 60); // 15 mins.
const BAD_NODE_AFTER_TIMEOUTS: usize = 5;
const BAD_NODE_ERROR_RATE_THRESHOLD: usize = 2;
const TOKEN_SECRET_REFRESH: Duration = Duration::from_secs(60 * 5); // 5 mins.

/// The opaque token secret value used within the hashing process.
type TokenSecretValue = [u8; TOKEN_SECRET_SIZE];

/// The unique identifier key of a node.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct NodeKey {
    /// The unique ID of the node
    pub id: NodeId,
    /// The address of the node within the DHT network
    pub addr: SocketAddr,
}

/// The announce token for a node.
/// This is derived from token secret.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeToken(Vec<u8>);

impl NodeToken {
    /// Create a new [NodeToken] from the given byte slice.
    /// The length of this new token is limited to [TOKEN_SIZE].
    ///
    /// If you want a longer token, use [NodeToken::try_from] instead.
    fn new<T: AsRef<[u8]>>(value: T) -> Self {
        let len = value.as_ref().len().min(TOKEN_SIZE);
        Self(value.as_ref()[0..len].to_vec())
    }

    /// Returns the underlying byte slice of the token.
    pub fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }

    /// Returns an owned vector containing the bytes of the token.
    pub fn to_vec(&self) -> Vec<u8> {
        self.0.to_vec()
    }

    /// Returns the length of the token in bytes.
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl Serialize for NodeToken {
    fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(&self.0)
    }
}

impl<'de> Deserialize<'de> for NodeToken {
    fn deserialize<D>(deserializer: D) -> result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct NodeTokenVisitor;
        impl<'de> Visitor<'de> for NodeTokenVisitor {
            type Value = NodeToken;

            fn expecting(&self, f: &mut Formatter) -> std::fmt::Result {
                write!(f, "expected a string or byte array as node token")
            }

            fn visit_str<E>(self, value: &str) -> result::Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                NodeToken::try_from(value.as_bytes()).map_err(E::custom)
            }

            fn visit_bytes<E>(self, value: &[u8]) -> result::Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                NodeToken::try_from(value).map_err(E::custom)
            }
        }

        deserializer.deserialize_any(NodeTokenVisitor)
    }
}

impl Display for NodeToken {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(&self.0))
    }
}

impl TryFrom<&[u8]> for NodeToken {
    type Error = Error;

    fn try_from(value: &[u8]) -> Result<Self> {
        // as token should always be short binary strings,
        // we limit the length to `TOKEN_MAX_LEN` bytes to prevent token abuse
        if value.len() > TOKEN_MAX_LEN {
            return Err(Error::InvalidToken);
        }

        Ok(Self(value.to_vec()))
    }
}

/// The node information within the DHT network
#[derive(Debug, Clone)]
pub struct Node {
    inner: Arc<InnerNode>,
}

impl Node {
    /// Create a new node for the given ID and address.
    pub fn new(id: NodeId, addr: SocketAddr) -> Self {
        Self::new_with_opts(id, addr, NodeState::Good)
    }

    /// Create a new node for the given ID and address.
    /// This fn allow settings additional properties of the node.
    pub(crate) fn new_with_opts(id: NodeId, addr: SocketAddr, state: NodeState) -> Self {
        Self {
            inner: Arc::new(InnerNode {
                key: NodeKey { id, addr },
                token: RwLock::new(TokenSecret::new()),
                announce_token: Default::default(),
                state: Mutex::new(state),
                last_seen: Mutex::new(Instant::now()),
                last_indexed: Default::default(),
                indexing_interval: Default::default(),
                metrics: Default::default(),
            }),
        }
    }

    /// Returns a reference to the id of this node.
    pub fn id(&self) -> &NodeId {
        &self.inner.key.id
    }

    /// Returns a reference to the address of this node.
    pub fn addr(&self) -> &SocketAddr {
        &self.inner.key.addr
    }

    /// Returns a reference to the key of this node.
    pub fn key(&self) -> &NodeKey {
        &self.inner.key
    }

    /// Returns the metrics of this node.
    pub fn metrics(&self) -> &NodeMetrics {
        &self.inner.metrics
    }

    /// Returns the current state of this node.
    pub async fn state(&self) -> NodeState {
        *self.inner.state.lock().await
    }

    /// Returns the last time we received a message from this node.
    pub async fn last_seen(&self) -> Instant {
        *self.inner.last_seen.lock().await
    }

    /// Verify that the given token is valid for this node.
    pub(crate) async fn verify_token(&self, token: &NodeToken, ip: &IpAddr) -> bool {
        self.inner.token.read().await.verify(token, ip)
    }

    /// Generate a new secret token for announcing peers.
    /// This token is always based on the ip of the node.
    pub(crate) async fn generate_token(&self) -> NodeToken {
        self.inner
            .token
            .read()
            .await
            .generate(&self.inner.key.addr.ip())
    }

    /// Rotate the token secret for this node, if needed.
    /// This is done every 5 minutes
    pub(crate) async fn rotate_token_secret(&self) {
        let mut token_secret = self.inner.token.write().await;

        if token_secret.needs_rotation() {
            token_secret.rotate();
        }
    }

    /// Returns the opaque token for this node, if available.
    pub(crate) async fn announce_token(&self) -> Option<NodeToken> {
        self.inner.announce_token.lock().await.clone()
    }

    /// Update the opaque token for this node.
    pub(crate) async fn update_announce_token(&self, token: NodeToken) {
        self.inner.update_announce_token(token).await;
    }

    /// Returns the interval announced by the node for scraping info hashes.
    pub(crate) async fn indexing_interval(&self) -> Option<Duration> {
        self.inner.indexing_interval.lock().await.clone()
    }

    /// Update the interval announced by the node for scraping info hashes.
    pub(crate) async fn update_indexing_interval(&self, interval: Duration) {
        *self.inner.indexing_interval.lock().await = Some(interval);
        self.indexed().await;
    }

    /// Returns the last time we indexed the node.
    pub(crate) async fn last_indexed(&self) -> Option<Instant> {
        self.inner.last_indexed.lock().await.clone()
    }

    /// The node has successfully responded to a query.
    pub(crate) async fn confirmed(&self) {
        self.inner.confirmed().await;
    }

    /// The node has sent a query message.
    pub(crate) async fn seen(&self) {
        self.inner.update_last_seen().await;
    }

    /// Increase the number of times the node failed to respond to a query.
    pub(crate) async fn failed(&self) {
        self.inner.failed().await;
    }

    /// Mark the node as successfully indexed.
    pub(crate) async fn indexed(&self) {
        *self.inner.last_indexed.lock().await = Some(Instant::now());
    }

    /// Get the distance between this node and the target node.
    /// See [NodeId::distance] for more information.
    pub fn distance(&self, node: &Node) -> u8 {
        self.inner.key.id.distance(&node.inner.key.id)
    }

    /// Check if the [NodeId] is valid for its own ip address.
    /// See BEP42 for more info.
    pub fn is_secure(&self) -> bool {
        self.inner.key.id.verify_id(&self.inner.key.addr.ip())
    }
}

impl PartialEq for Node {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

impl From<NodeKey> for Node {
    fn from(key: NodeKey) -> Self {
        Self::new(key.id, key.addr)
    }
}

#[derive(Debug)]
struct InnerNode {
    key: NodeKey,
    /// The unique token of the node
    token: RwLock<TokenSecret>,
    /// The token to use for announcing a peer
    announce_token: Mutex<Option<NodeToken>>,
    /// The current state of the node
    state: Mutex<NodeState>,
    /// The last time we received a message from the node
    last_seen: Mutex<Instant>,
    /// The last time we indexed the node.
    last_indexed: Mutex<Option<Instant>>,
    /// The interval announced by the node for scraping info hashes.
    indexing_interval: Mutex<Option<Duration>>,
    /// The metrics of the node
    metrics: NodeMetrics,
}

impl InnerNode {
    async fn confirmed(&self) {
        self.update_last_seen().await;
        self.metrics.confirmed_queries.inc();
        self.update_state(NodeState::Good).await;
    }

    async fn failed(&self) {
        self.metrics.errors.inc();

        let last_seen = *self.last_seen.lock().await;
        let new_state = NodeState::calculate(Instant::now() - last_seen, &self.metrics);
        self.update_state(new_state).await;
    }

    async fn update_last_seen(&self) {
        let now = Instant::now();
        let mut last_seen = self.last_seen.lock().await;
        *last_seen = now;
    }

    async fn update_announce_token(&self, token: NodeToken) {
        let mut announce_token = self.announce_token.lock().await;
        *announce_token = Some(token);
    }

    async fn update_state(&self, new_state: NodeState) {
        let mut state = self.state.lock().await;
        *state = new_state;
    }
}

impl PartialEq for InnerNode {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}

/// The state of a node
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum NodeState {
    Good = 0,
    Questionable = 1,
    Bad = 2,
}

impl NodeState {
    /// Calculate the node state from the given metrics.
    ///
    /// A good node is a node has responded to one of our queries within the last 15 minutes.
    /// After 15 minutes of inactivity, a node becomes questionable.
    /// Nodes become bad when they fail to respond to multiple queries in a row.
    pub(crate) fn calculate(last_seen_since: Duration, metrics: &NodeMetrics) -> Self {
        let total_queries = metrics.confirmed_queries.total();
        let total_errors = metrics.errors.total();

        // if we've never had successful query response and X timeouts,
        // the node is considered bad
        if total_queries == 0 && total_errors as usize > BAD_NODE_AFTER_TIMEOUTS {
            return Self::Bad;
        }

        // if the error rate (5s avg success rate - 5s avg timeout rate) also exceeds the threshold,
        // the node is considered bad
        let error_rate = metrics
            .confirmed_queries
            .rate()
            .saturating_sub(metrics.errors.rate());
        if error_rate as usize > BAD_NODE_ERROR_RATE_THRESHOLD {
            return Self::Bad;
        }

        if last_seen_since < QUESTIONABLE_NODE_AFTER {
            return Self::Good;
        }

        Self::Questionable
    }
}

/// The token information of a node.
#[derive(Debug, Clone)]
struct TokenSecret {
    secret: TokenSecretValue,
    old_secret: TokenSecretValue,
    last_refreshed: Instant,
}

impl TokenSecret {
    fn new() -> Self {
        let mut random = rng();
        Self {
            secret: random.random(),
            old_secret: random.random(),
            last_refreshed: Instant::now(),
        }
    }

    fn verify(&self, token: &NodeToken, addr: &IpAddr) -> bool {
        Self::verify_with(self, token, addr, &self.secret)
            || Self::verify_with(self, token, addr, &self.old_secret)
    }

    fn generate(&self, addr: &IpAddr) -> NodeToken {
        let hash = Self::hash(&self.secret, addr);
        NodeToken::new(hash.as_slice())
    }

    /// Rotate the token secret.
    fn rotate(&mut self) {
        self.old_secret = self.secret;
        self.secret = rng().random();
        self.last_refreshed = Instant::now();
    }

    /// Verify if the token secret needs to be rotated.
    /// This is done every 5 minutes.
    fn needs_rotation(&self) -> bool {
        self.last_refreshed.elapsed() > TOKEN_SECRET_REFRESH
    }

    fn verify_with(&self, token: &NodeToken, addr: &IpAddr, secret: &TokenSecretValue) -> bool {
        let hash = Self::hash(secret, &addr);
        let validation_token = NodeToken::new(hash.as_slice());
        token == &validation_token
    }

    fn hash(secret: &TokenSecretValue, addr: &IpAddr) -> Vec<u8> {
        let mut hasher = Sha1::new();
        hasher.update(addr.to_string().as_bytes());
        hasher.update(secret);
        hasher.finalize().to_vec()
    }
}

impl TryFrom<&[u8]> for TokenSecret {
    type Error = Error;

    fn try_from(value: &[u8]) -> Result<Self> {
        if value.len() != TOKEN_SECRET_SIZE {
            return Err(Error::InvalidToken);
        }

        let secret: [u8; TOKEN_SECRET_SIZE] = value.try_into().map_err(|_| Error::InvalidToken)?;

        Ok(Self {
            secret,
            old_secret: secret,
            last_refreshed: Instant::now(),
        })
    }
}

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

    mod token {
        use super::*;

        #[test]
        fn test_verify() {
            let ip = IpAddr::V4(Ipv4Addr::new(190, 180, 170, 5));
            let mut token_secret = TokenSecret::new();
            let token = token_secret.generate(&ip);

            let result = token_secret.verify(&token, &ip);
            assert!(result, "expected the token to be valid");

            // rotate the secret
            token_secret.rotate();
            let result = token_secret.verify(&token, &ip);
            assert!(
                result,
                "expected the old secret token to be valid after rotation"
            );

            // rotate the secret a 2nd time
            token_secret.rotate();
            let result = token_secret.verify(&token, &ip);
            assert!(
                !result,
                "expected the old secret token to be invalid after 2nd rotation"
            );
        }

        #[test]
        fn test_generate() {
            let ip = IpAddr::V4(Ipv4Addr::new(120, 188, 12, 1));
            let token_secret = TokenSecret::new();

            let result = token_secret.generate(&ip);

            assert!(
                !result.0.iter().all(|e| *e == 0),
                "expected the token to be non-zero"
            );
        }

        #[test]
        fn test_from_byte_slice() {
            let token = "aoeusnthaoeusnthaoeu".as_bytes();

            let result =
                TokenSecret::try_from(token).expect("expected the token value to be valid");

            assert_eq!(
                result.secret,
                token[..token.len()],
                "expected the token secret to match the parsed value"
            );
            assert_eq!(
                result.old_secret,
                token[..token.len()],
                "expected the old secret to match the parsed value"
            );
        }
    }

    mod node_state {
        use super::*;
        use crate::metrics::Metric;

        #[test]
        fn test_calculate_good_state() {
            let metrics = NodeMetrics::new();
            metrics.confirmed_queries.inc();
            metrics.tick(Duration::from_secs(1));
            let result = NodeState::calculate(Duration::from_secs(3 * 60), &metrics);
            assert_eq!(NodeState::Good, result);

            let metrics = NodeMetrics::new();
            metrics.errors.inc_by(2);
            metrics.tick(Duration::from_secs(1));
            let result = NodeState::calculate(Duration::from_secs(10 * 60), &metrics);
            assert_eq!(NodeState::Good, result);
        }

        #[test]
        fn test_calculate_questionable_state() {
            let metrics = NodeMetrics::new();
            let result = NodeState::calculate(Duration::from_secs(15 * 60), &metrics);
            assert_eq!(NodeState::Questionable, result);

            let metrics = NodeMetrics::new();
            let result = NodeState::calculate(Duration::from_secs(16 * 60), &metrics);
            assert_eq!(NodeState::Questionable, result);
        }

        #[test]
        fn test_calculate_bad_state() {
            let metrics = NodeMetrics::new();
            metrics.errors.inc_by(6);
            metrics.tick(Duration::from_secs(1));
            let result = NodeState::calculate(Duration::from_secs(5 * 60), &metrics);
            assert_eq!(NodeState::Bad, result);
        }
    }

    mod node_from {
        use super::*;

        #[test]
        fn test_from_node_key() {
            let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 10081));
            let id = NodeId::from_ip(&addr.ip());
            let key = NodeKey { id, addr };

            let result = Node::from(key);

            assert_eq!(result.id(), &id);
        }
    }

    mod node_token {
        use super::*;
        use crate::bencode;

        #[test]
        fn test_serialize() {
            let expected_result = "10:LoremIpsum";
            let token = NodeToken("LoremIpsum".as_bytes().to_vec());

            let result =
                bencode::to_string(&token).expect("expected the token to have been serialized");

            assert_eq!(expected_result, result.as_str());
        }

        #[test]
        fn test_deserialize_str() {
            let value = "8:LoremIps";
            let expected_result = NodeToken("LoremIps".as_bytes().to_vec());

            let result: NodeToken =
                bencode::from_str(value).expect("expected the token to be valid");

            assert_eq!(expected_result, result);
        }

        #[test]
        fn test_deserialize_bytes() {
            let expected_result = NodeToken("Qwerty".as_bytes().to_vec());
            let bytes =
                bencode::to_bytes(&expected_result).expect("expected the token to be serialized");

            let result: NodeToken =
                bencode::from_bytes(bytes.as_slice()).expect("expected the token to be valid");

            assert_eq!(expected_result, result);
        }
    }
}