aria2-protocol 0.3.7

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
use super::super::{DhtEngine, DhtEngineConfig};
use crate::bittorrent::bencode::codec::BencodeValue;
use crate::bittorrent::dht::message::{DhtMessage, DhtMessageBuilder};
use crate::bittorrent::dht::modern::{MutableValue, StoredItem};
use crate::bittorrent::dht::node::DhtNode;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::time::Duration;
use tokio::net::UdpSocket;

#[tokio::test]
async fn test_krpc_loopback_messages_include_aria2_version() {
    let engine_id = [0x41; 20];
    let remote_id = [0x42; 20];
    let engine = DhtEngine::start(DhtEngineConfig {
        self_id: engine_id,
        listen_addr: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)),
        ..DhtEngineConfig::local()
    })
    .await
    .unwrap();

    let remote = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();
    let remote_addr = remote.local_addr().unwrap();
    let add_node_engine = engine.clone();
    let add_node = tokio::spawn(async move {
        add_node_engine.add_node(remote_addr).await;
    });

    let mut packet = [0u8; 2048];
    let (length, engine_addr) =
        tokio::time::timeout(Duration::from_secs(2), remote.recv_from(&mut packet))
            .await
            .expect("engine should send a ping to the loopback node")
            .unwrap();
    assert_aria2_dht_version(&packet[..length]);
    let query = DhtMessage::decode(&packet[..length]).unwrap();
    assert!(query.is_query());
    assert_eq!(
        query.q.as_ref().map(|method| method.0.as_str()),
        Some("ping")
    );

    let response = DhtMessageBuilder::ping_response(&query.t, &remote_id).encode();
    remote.send_to(&response, engine_addr).await.unwrap();
    add_node.await.unwrap();

    let client = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();
    let ping = DhtMessageBuilder::ping(0x1234_5678, &remote_id).encode();
    client.send_to(&ping, engine.local_addr()).await.unwrap();
    let (length, _) = tokio::time::timeout(Duration::from_secs(2), client.recv_from(&mut packet))
        .await
        .expect("engine should answer the loopback ping")
        .unwrap();
    assert_aria2_dht_version(&packet[..length]);
    let response = DhtMessage::decode(&packet[..length]).unwrap();
    assert!(response.is_response());

    engine.shutdown_async().await;
}

async fn assert_get_peers_returns_peers_and_nodes(use_ipv6: bool) {
    const INFO_HASH: [u8; 20] = [0x51; 20];
    let local_ip = if use_ipv6 {
        IpAddr::V6(Ipv6Addr::LOCALHOST)
    } else {
        IpAddr::V4(Ipv4Addr::LOCALHOST)
    };
    let engine = DhtEngine::start(DhtEngineConfig {
        self_id: [0x41; 20],
        listen_addr: Some(local_ip),
        ..DhtEngineConfig::local()
    })
    .await
    .expect("DHT engine should start");

    let routing_node = UdpSocket::bind(SocketAddr::new(local_ip, 0))
        .await
        .expect("routing node should bind");
    let routing_node_addr = routing_node.local_addr().unwrap();
    let add_node_engine = engine.clone();
    let add_node = tokio::spawn(async move { add_node_engine.add_node(routing_node_addr).await });
    let mut packet = [0u8; 2048];
    let (length, engine_addr) =
        tokio::time::timeout(Duration::from_secs(2), routing_node.recv_from(&mut packet))
            .await
            .expect("engine should ping its routing node")
            .expect("routing node should receive ping");
    let ping = DhtMessage::decode(&packet[..length]).expect("valid ping");
    let ping_response = DhtMessageBuilder::ping_response(&ping.t, &[0x42; 20]).encode();
    routing_node
        .send_to(&ping_response, engine_addr)
        .await
        .expect("routing node should answer ping");
    add_node.await.expect("node admission should complete");

    let client = UdpSocket::bind(SocketAddr::new(local_ip, 0))
        .await
        .expect("DHT client should bind");
    let first_response = exchange_dht_message(
        &client,
        engine.local_addr(),
        DhtMessageBuilder::get_peers(1, &[0x43; 20], &INFO_HASH),
    )
    .await;
    let token = first_response
        .r
        .as_ref()
        .and_then(|result| result.dict_get(b"token"))
        .and_then(BencodeValue::as_bytes)
        .expect("get_peers should return a token")
        .to_vec();

    let announce_response = exchange_dht_message(
        &client,
        engine.local_addr(),
        DhtMessageBuilder::announce_peer_with_token(2, &[0x43; 20], &INFO_HASH, 6881, &token),
    )
    .await;
    assert!(announce_response.is_response());

    let response = exchange_dht_message(
        &client,
        engine.local_addr(),
        DhtMessageBuilder::get_peers(3, &[0x43; 20], &INFO_HASH),
    )
    .await;
    let result = response.r.as_ref().expect("get_peers response result");
    let values = result
        .dict_get(b"values")
        .and_then(BencodeValue::as_list)
        .expect("known peer should be returned");
    assert_eq!(values.len(), 1);
    let peers = crate::bittorrent::dht::compact::extract_compact_peers_from_response(&response);
    assert_eq!(
        peers,
        vec![SocketAddr::new(client.local_addr().unwrap().ip(), 6881)]
    );
    let node_field = if use_ipv6 {
        b"nodes6".as_slice()
    } else {
        b"nodes"
    };
    let nodes = result
        .dict_get(node_field)
        .and_then(BencodeValue::as_bytes)
        .expect("known peers must not suppress closest DHT nodes");
    let node_record_length = if use_ipv6 { 38 } else { 26 };
    assert!(
        !nodes.is_empty() && nodes.len().is_multiple_of(node_record_length),
        "response should include compact routing nodes for its family, got {} bytes",
        nodes.len(),
    );

    engine.shutdown_async().await;
}

#[tokio::test]
async fn ipv4_get_peers_returns_known_peers_and_closest_nodes_together() {
    assert_get_peers_returns_peers_and_nodes(false).await;
}

#[tokio::test]
async fn ipv6_get_peers_returns_known_peers_and_closest_nodes_together() {
    assert_get_peers_returns_peers_and_nodes(true).await;
}

async fn exchange_dht_message(
    client: &UdpSocket,
    engine_addr: SocketAddr,
    query: DhtMessage,
) -> DhtMessage {
    client
        .send_to(&query.encode(), engine_addr)
        .await
        .expect("send DHT query");
    let mut packet = [0u8; 2048];
    let (length, source) =
        tokio::time::timeout(Duration::from_secs(2), client.recv_from(&mut packet))
            .await
            .expect("DHT response should arrive")
            .expect("receive DHT response");
    assert_eq!(source, engine_addr);
    DhtMessage::decode(&packet[..length]).expect("valid DHT response")
}

#[tokio::test]
async fn test_all_public_dht_builders_include_version_over_loopback_udp() {
    use crate::bittorrent::dht::modern::{
        MutableValue, SampleInfoHashesResponse, get_query, put_immutable_query, put_query,
        sample_infohashes_query, sample_infohashes_response,
    };

    let client = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();
    let server = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).await.unwrap();
    let node_id = [0x51; 20];
    let info_hash = [0x52; 20];
    let tx = [0x53, 0x54, 0x55, 0x56];
    let peer: SocketAddr = "127.0.0.1:6881".parse().unwrap();

    let requests = [
        DhtMessageBuilder::ping(1, &node_id),
        DhtMessageBuilder::find_node(2, &node_id, &info_hash),
        DhtMessageBuilder::get_peers(3, &node_id, &info_hash),
        DhtMessageBuilder::announce_peer_with_token(
            4,
            &node_id,
            &info_hash,
            6881,
            b"announce-token",
        ),
        sample_infohashes_query(5, &node_id, &info_hash),
        get_query(6, &node_id, &info_hash, Some(1)),
        put_query(
            7,
            &node_id,
            b"mutable-token",
            &MutableValue {
                public_key: [0x65; 32],
                signature: [0x66; 64],
                sequence: 1,
                salt: Some(b"udp".to_vec()),
                value: BencodeValue::Bytes(b"mutable value".to_vec()),
            },
            Some(0),
        ),
        put_immutable_query(
            8,
            &node_id,
            b"immutable-token",
            &BencodeValue::Bytes(b"immutable value".to_vec()),
        ),
    ];
    for request in requests {
        assert_message_roundtrips_with_aria2_version(&client, &server, &request).await;
    }

    let responses = [
        DhtMessageBuilder::ping_response(&tx, &node_id),
        DhtMessageBuilder::find_node_response(&tx, &node_id, &[0x61; 26]),
        DhtMessageBuilder::find_node_response6(&tx, &node_id, &[0x62; 38]),
        DhtMessageBuilder::get_peers_response_with_peers(&tx, &node_id, b"peer-token", &[peer]),
        DhtMessageBuilder::get_peers_response_with_nodes(&tx, &node_id, b"node-token", &[0x63; 26]),
        DhtMessageBuilder::get_peers_response_with_nodes6(
            &tx,
            &node_id,
            b"node-token",
            &[0x64; 38],
        ),
        DhtMessageBuilder::announce_peer_response(&tx, &node_id),
        DhtMessageBuilder::error_response(&tx, 203, "Protocol Error"),
        sample_infohashes_response(
            &tx,
            &node_id,
            &SampleInfoHashesResponse {
                interval: 900,
                num: 1,
                samples: vec![info_hash],
                nodes: vec![0x67; 26],
                nodes6: vec![0x68; 38],
            },
        ),
    ];
    for response in responses {
        assert_message_roundtrips_with_aria2_version(&server, &client, &response).await;
    }
}

async fn assert_message_roundtrips_with_aria2_version(
    sender: &UdpSocket,
    receiver: &UdpSocket,
    message: &DhtMessage,
) {
    let packet = message.encode();
    sender
        .send_to(&packet, receiver.local_addr().unwrap())
        .await
        .unwrap();

    let mut received = [0u8; 2048];
    let (length, source) =
        tokio::time::timeout(Duration::from_secs(1), receiver.recv_from(&mut received))
            .await
            .expect("loopback UDP packet should arrive")
            .unwrap();
    assert_eq!(source, sender.local_addr().unwrap());
    assert_aria2_dht_version(&received[..length]);

    let decoded = DhtMessage::decode(&received[..length]).unwrap();
    assert_eq!(decoded.is_query(), message.is_query());
    assert_eq!(decoded.is_response(), message.is_response());
    assert_eq!(decoded.is_error(), message.is_error());
}

fn assert_aria2_dht_version(packet: &[u8]) {
    let (message, consumed) = BencodeValue::decode(packet).unwrap();
    assert_eq!(consumed, packet.len());
    assert_eq!(
        message.dict_get(b"v").and_then(BencodeValue::as_bytes),
        Some(b"A2\x00\x03".as_slice()),
        "KRPC packet must carry the aria2 version field"
    );
}

#[tokio::test]
async fn test_bep44_udp_roundtrip_and_store_restart() {
    use crate::bittorrent::bencode::codec::BencodeValue;
    use ed25519_dalek::{Signer, SigningKey};

    let temp_dir = tempfile::tempdir().unwrap();
    let dht_path = temp_dir.path().join("dht.dat");
    let node_a_id = [0x11; 20];
    let node_b_id = [0x22; 20];
    let engine_a = DhtEngine::start(DhtEngineConfig {
        self_id: node_a_id,
        listen_addr: Some(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)),
        ..DhtEngineConfig::local()
    })
    .await
    .unwrap();
    let engine_b_config = DhtEngineConfig {
        self_id: node_b_id,
        dht_file_path: Some(dht_path.clone()),
        listen_addr: Some(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)),
        ..DhtEngineConfig::local()
    };
    let engine_b = DhtEngine::start(engine_b_config.clone()).await.unwrap();
    let b_addr = engine_b.context.task_context.socket.local_addr();
    engine_a
        .context
        .task_context
        .routing_table
        .write()
        .await
        .insert(DhtNode::new(node_b_id, b_addr));

    let immutable = BencodeValue::Bytes(b"udp immutable".to_vec());
    assert!(engine_a.put_immutable(&immutable).await.unwrap());
    let immutable_target = StoredItem::immutable_target(&immutable);
    assert!(matches!(
        engine_a.get_item(&immutable_target, None).await.unwrap(),
        Some(StoredItem::Immutable { .. })
    ));

    let key = SigningKey::from_bytes(&[3u8; 32]);
    let mut mutable = MutableValue {
        public_key: key.verifying_key().to_bytes(),
        signature: [0; 64],
        sequence: 1,
        salt: Some(b"udp".to_vec()),
        value: BencodeValue::Bytes(b"mutable value".to_vec()),
    };
    mutable.signature = key.sign(&mutable.signed_payload()).to_bytes();
    assert!(engine_a.put_mutable(&mutable, None).await.unwrap());
    let mutable_target = StoredItem::mutable_target(&mutable.public_key, mutable.salt.as_deref());
    assert!(
        engine_a
            .get_item(&mutable_target, None)
            .await
            .unwrap()
            .is_some()
    );

    let mut updated = mutable.clone();
    updated.sequence = 2;
    updated.value = BencodeValue::Bytes(b"updated value".to_vec());
    updated.signature = key.sign(&updated.signed_payload()).to_bytes();
    assert!(!engine_a.put_mutable(&updated, Some(0)).await.unwrap());
    assert!(engine_a.put_mutable(&updated, Some(1)).await.unwrap());

    engine_b.shutdown_async().await;
    let item_path = dht_path.with_extension("items");
    assert!(item_path.exists());
    let engine_b = DhtEngine::start(engine_b_config).await.unwrap();
    let b_addr = engine_b.context.task_context.socket.local_addr();
    engine_a
        .context
        .task_context
        .routing_table
        .write()
        .await
        .insert(DhtNode::new(node_b_id, b_addr));
    assert!(
        engine_a
            .get_item(&immutable_target, None)
            .await
            .unwrap()
            .is_some()
    );
    assert!(
        engine_a
            .get_item(&mutable_target, None)
            .await
            .unwrap()
            .is_some()
    );

    engine_b.shutdown_async().await;
    engine_a.shutdown_async().await;
}

#[tokio::test]
async fn test_bep51_udp_roundtrip_uses_peer_store_samples() {
    let node_a_id = [0x31; 20];
    let node_b_id = [0x32; 20];
    let engine_a = DhtEngine::start(DhtEngineConfig {
        self_id: node_a_id,
        listen_addr: Some(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)),
        ..DhtEngineConfig::local()
    })
    .await
    .unwrap();
    let engine_b = DhtEngine::start(DhtEngineConfig {
        self_id: node_b_id,
        listen_addr: Some(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)),
        ..DhtEngineConfig::local()
    })
    .await
    .unwrap();

    let sampled_info_hash = [0xA7; 20];
    engine_b
        .context
        .peer_storage
        .add_peer(sampled_info_hash, "127.0.0.1:6881".parse().unwrap());
    let b_addr = engine_b.context.task_context.socket.local_addr();
    engine_a
        .context
        .task_context
        .routing_table
        .write()
        .await
        .insert(DhtNode::new(node_b_id, b_addr));

    let response = engine_a
        .sample_infohashes(&[0xA6; 20])
        .await
        .unwrap()
        .expect("BEP 51 peer should return a sample response");
    assert_eq!(response.interval, 900);
    assert_eq!(response.num, 1);
    assert_eq!(response.samples, vec![sampled_info_hash]);

    engine_b.shutdown_async().await;
    engine_a.shutdown_async().await;
}

/// Read-only public-network smoke test for DNS bootstrap and KRPC lookup.
/// It is intentionally ignored because availability depends on the host
/// network and public DHT nodes; CI can opt in explicitly.
#[tokio::test]
#[ignore = "requires public DHT connectivity"]
async fn test_public_dht_find_peers_interoperability() {
    run_public_dht_find_peers_smoke(IpAddr::V4(Ipv4Addr::UNSPECIFIED)).await;
}

#[tokio::test]
#[ignore = "requires public IPv6 DHT connectivity"]
async fn test_public_dht_find_peers_interoperability_ipv6() {
    run_public_dht_find_peers_smoke(IpAddr::V6(Ipv6Addr::UNSPECIFIED)).await;
}

async fn run_public_dht_find_peers_smoke(listen_addr: IpAddr) {
    let family = match listen_addr {
        IpAddr::V4(_) => "IPv4",
        IpAddr::V6(_) => "IPv6",
    };
    let bootstrap_nodes = std::env::var("ARIA2_TEST_DHT_BOOTSTRAP_NODES")
        .ok()
        .map(|raw| {
            let nodes = raw
                .split(',')
                .map(str::trim)
                .filter(|endpoint| !endpoint.is_empty())
                .map(|endpoint| {
                    endpoint.parse::<SocketAddr>().unwrap_or_else(|error| {
                        panic!(
                            "invalid ARIA2_TEST_DHT_BOOTSTRAP_NODES endpoint {endpoint:?}: {error}"
                        )
                    })
                })
                .filter(|endpoint| endpoint.is_ipv6() == listen_addr.is_ipv6())
                .collect::<Vec<_>>();
            assert!(
                !nodes.is_empty(),
                "ARIA2_TEST_DHT_BOOTSTRAP_NODES has no endpoints for {family}"
            );
            nodes
        })
        .unwrap_or_default();
    let engine = DhtEngine::start(DhtEngineConfig {
        port: 0,
        listen_addr: Some(listen_addr),
        bootstrap_nodes,
        bootstrap_timeout: Duration::from_secs(15),
        ..DhtEngineConfig::default()
    })
    .await
    .unwrap_or_else(|error| {
        panic!("public {family} DHT engine should bind an ephemeral port: {error}")
    });

    engine
        .wait_until_ready(Duration::from_secs(30))
        .await
        .unwrap_or_else(|error| {
            panic!("public {family} DHT engine did not finish bootstrap setup: {error}")
        });
    let local_addr = engine.local_addr();
    let bootstrap_stats = engine.stats().await;
    assert!(
        bootstrap_stats.total_nodes > 0,
        "public {family} DHT bootstrap resolved no nodes: local_addr={local_addr}"
    );
    let good_nodes_before_lookup = engine
        .context
        .task_context
        .routing_table
        .read()
        .await
        .collect_good_nodes();
    let bootstrap_endpoints = engine
        .context
        .task_context
        .routing_table
        .read()
        .await
        .get_all_buckets()
        .into_iter()
        .flat_map(|bucket| bucket.nodes().iter().map(DhtNode::addr))
        .collect::<Vec<_>>();
    let result = tokio::time::timeout(Duration::from_secs(30), engine.find_peers(&[0x3Cu8; 20]))
        .await
        .unwrap_or_else(|_| panic!("public {family} DHT lookup timed out"))
        .unwrap_or_else(|error| panic!("public {family} DHT lookup failed: {error}"));
    let good_nodes_after_lookup = engine
        .context
        .task_context
        .routing_table
        .read()
        .await
        .collect_good_nodes();
    let stats = engine.stats().await;
    engine.shutdown_async().await;

    let received_valid_response = good_nodes_after_lookup.iter().any(|node| {
        good_nodes_before_lookup
            .iter()
            .find(|previous| previous.id() == node.id() && previous.addr() == node.addr())
            .is_none_or(|previous| node.last_seen() > previous.last_seen())
    });
    assert!(
        received_valid_response,
        "public {family} DHT lookup observed no valid node response: local_addr={local_addr}, queries_sent={}, bootstrap_nodes={}, bootstrap_endpoints={bootstrap_endpoints:?}, bootstrap_good_nodes={}, lookup_good_nodes_before={}, lookup_good_nodes_after={}, total_nodes={}, good_nodes={}, pending_transactions={}",
        result.nodes_contacted,
        bootstrap_stats.total_nodes,
        bootstrap_stats.good_nodes,
        good_nodes_before_lookup.len(),
        good_nodes_after_lookup.len(),
        stats.total_nodes,
        stats.good_nodes,
        stats.pending_transactions,
    );
}