aria2-core 0.3.13

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
use super::*;

#[tokio::test]
async fn ftp_proxy_records_each_payload_chunk_for_timeout() {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    let first_chunk = vec![b'A'; 16 * 1024];
    let second_chunk = vec![b'B'; 16 * 1024];
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let server = tokio::spawn({
        let first_chunk = first_chunk.clone();
        let second_chunk = second_chunk.clone();
        async move {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut request = [0u8; 4096];
            let bytes_read = stream.read(&mut request).await.unwrap();
            assert!(bytes_read > 0, "FTP proxy fixture should receive a request");
            stream
                .write_all(
                    format!(
                        "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                        first_chunk.len() + second_chunk.len()
                    )
                    .as_bytes(),
                )
                .await
                .unwrap();
            stream.write_all(&first_chunk).await.unwrap();
            stream.flush().await.unwrap();
            tokio::time::sleep(Duration::from_millis(150)).await;
            stream.write_all(&second_chunk).await.unwrap();
            stream.shutdown().await.unwrap();
        }
    });

    let output_dir = tempfile::tempdir().unwrap();
    let url = "ftp://ftp.example.test/pub/proxy-timeout.bin".to_string();
    let options = DownloadOptions {
        ftp_proxy: Some(format!("http://{address}")),
        ..DownloadOptions::default()
    };
    let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
        GroupId::new(9002),
        vec![url.clone()],
        options,
    )));
    let mut command =
        FtpDownloadCommand::new_with_group(Arc::clone(&group), output_dir.path().to_str(), None)
            .unwrap();

    let command_task = tokio::spawn(async move { command.execute().await });
    tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            if group.recover().completed_length() >= first_chunk.len() as u64 {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("FTP proxy did not receive its first payload chunk");
    let first_activity = group.recover().last_network_activity();

    tokio::time::timeout(Duration::from_secs(5), command_task)
        .await
        .expect("FTP proxy command did not complete")
        .expect("FTP proxy command panicked")
        .expect("FTP proxy command failed");
    server.await.unwrap();

    assert!(
        group.recover().last_network_activity() > first_activity,
        "each non-empty FTP proxy payload chunk must refresh the inactivity clock"
    );
}

#[tokio::test]
async fn ftp_writer_open_failure_aborts_the_active_transfer() {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    use tokio::net::{TcpListener, TcpStream};

    let control_listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind FTP control fixture");
    let control_address = control_listener
        .local_addr()
        .expect("read FTP control address");
    let server = tokio::spawn(async move {
        let (stream, _) = control_listener
            .accept()
            .await
            .expect("accept FTP control connection");
        let mut control = BufReader::new(stream);
        control
            .get_mut()
            .write_all(b"220 FTP fixture ready\r\n")
            .await
            .expect("write FTP greeting");

        let mut commands = Vec::new();
        for (expected, response) in [
            ("ABOR", b"226 transfer aborted\r\n".as_slice()),
            ("QUIT", b"221 goodbye\r\n".as_slice()),
        ] {
            let mut command = String::new();
            tokio::time::timeout(Duration::from_secs(2), control.read_line(&mut command))
                .await
                .expect("FTP cleanup command should arrive")
                .expect("read FTP cleanup command");
            let command = command.trim_end().to_owned();
            assert_eq!(command, expected);
            commands.push(command);
            control
                .get_mut()
                .write_all(response)
                .await
                .expect("write FTP cleanup response");
        }
        commands
    });

    let data_listener = TcpListener::bind("127.0.0.1:0")
        .await
        .expect("bind FTP data fixture");
    let data_address = data_listener.local_addr().expect("read FTP data address");
    let data_client = TcpStream::connect(data_address)
        .await
        .expect("connect FTP data fixture");
    let (mut data_server, _) = data_listener
        .accept()
        .await
        .expect("accept FTP data connection");
    data_server
        .write_all(b"payload")
        .await
        .expect("write FTP payload");
    data_server.shutdown().await.expect("finish FTP payload");

    let output = tempfile::tempdir().expect("create output directory");
    let uri = format!("ftp://127.0.0.1:{}/remote.bin", control_address.port());
    let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
        GroupId::new(9_905),
        vec![uri],
        DownloadOptions::default(),
    )));
    group
        .recover_mut()
        .start()
        .expect("start FTP request group");
    let mut command = FtpDownloadCommand::new_with_group(
        Arc::clone(&group),
        output.path().to_str(),
        Some("remote.bin"),
    )
    .expect("construct FTP command");
    // A directory is a deterministic target that makes the lazy writer's
    // first payload open fail after RETR has already entered its data phase.
    command.output_path = output.path().to_path_buf();
    let control = RawFtpControl::connect_at(
        "127.0.0.1",
        control_address.port(),
        control_address,
        Arc::new(crate::network::OutboundNetworkPolicy::direct()),
        None,
    )
    .await
    .expect("connect FTP control session");

    let result = command
        .receive_data_transfer(super::super::transfer::FtpReceiveTransfer {
            ctrl: control,
            data_stream: aria2_protocol::ftp::tls::FtpDataStream::Plain(data_client),
            file_size: Some(7),
            in_memory_download: false,
            remote_modified_time: None,
            write_offset: 0,
            reuse_key: None,
        })
        .await;

    assert!(result.is_err(), "writing to a directory target must fail");
    assert_eq!(
        server.await.expect("FTP cleanup fixture should finish"),
        ["ABOR", "QUIT"]
    );
}

#[tokio::test]
async fn ftp_reuses_authenticated_control_session_across_downloads() {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    use tokio::net::TcpListener;

    let control_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let control_address = control_listener.local_addr().unwrap();
    let _proxy_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let proxy_address = _proxy_listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let (stream, _) = control_listener.accept().await.unwrap();
        let mut control = BufReader::new(stream);
        control
            .get_mut()
            .write_all(b"220 local FTP fixture\r\n")
            .await
            .unwrap();
        let mut data_listener = None;
        let mut transfer_count = 0;
        let mut login_count = 0;
        let mut command = String::new();
        while control.read_line(&mut command).await.unwrap() != 0 {
            let line = command.trim_end_matches(['\r', '\n']).to_owned();
            command.clear();
            match line
                .split_once(' ')
                .map_or((line.as_str(), ""), |(verb, arg)| (verb, arg))
            {
                ("USER", _) => {
                    login_count += 1;
                    control
                        .get_mut()
                        .write_all(b"331 password required\r\n")
                        .await
                        .unwrap();
                }
                ("PASS", _) => control
                    .get_mut()
                    .write_all(b"230 logged in\r\n")
                    .await
                    .unwrap(),
                ("TYPE", _) => control
                    .get_mut()
                    .write_all(b"200 type set\r\n")
                    .await
                    .unwrap(),
                ("PWD", _) => control.get_mut().write_all(b"257 \"/\"\r\n").await.unwrap(),
                ("CWD", _) => control
                    .get_mut()
                    .write_all(b"250 directory changed\r\n")
                    .await
                    .unwrap(),
                ("SIZE", _) => control.get_mut().write_all(b"213 4\r\n").await.unwrap(),
                ("REST", _) => control
                    .get_mut()
                    .write_all(b"350 restart accepted\r\n")
                    .await
                    .unwrap(),
                ("EPSV", _) => {
                    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
                    let port = listener.local_addr().unwrap().port();
                    data_listener = Some(listener);
                    control
                        .get_mut()
                        .write_all(
                            format!("229 Entering Extended Passive Mode (|||{port}|)\r\n")
                                .as_bytes(),
                        )
                        .await
                        .unwrap();
                }
                ("PASV", _) => {
                    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
                    let port = listener.local_addr().unwrap().port();
                    data_listener = Some(listener);
                    control
                        .get_mut()
                        .write_all(
                            format!(
                                "227 Entering Passive Mode (127,0,0,1,{},{})\r\n",
                                port / 256,
                                port % 256
                            )
                            .as_bytes(),
                        )
                        .await
                        .unwrap();
                }
                ("RETR", _) => {
                    control
                        .get_mut()
                        .write_all(b"150 opening data connection\r\n")
                        .await
                        .unwrap();
                    let listener = data_listener.take().expect("EPSV must precede RETR");
                    let (mut data, _) = listener.accept().await.unwrap();
                    data.write_all(b"data").await.unwrap();
                    data.shutdown().await.unwrap();
                    control
                        .get_mut()
                        .write_all(b"226 transfer complete\r\n")
                        .await
                        .unwrap();
                    transfer_count += 1;
                    if transfer_count == 2 {
                        break;
                    }
                }
                other => panic!("unexpected FTP command: {other:?}"),
            }
        }
        (login_count, transfer_count)
    });

    let pool = Arc::new(crate::ftp::connection_pool::FtpConnectionPool::new(4));
    let output_dir = tempfile::tempdir().unwrap();
    for (index, filename) in ["first.bin", "second.bin"].into_iter().enumerate() {
        let uri = format!("ftp://reuse-user:reuse-pass@{control_address}/{filename}");
        let options = DownloadOptions {
            allow_overwrite: true,
            ftp_proxy: Some(format!("http://{proxy_address}")),
            no_proxy: Some("127.0.0.1".to_string()),
            ..DownloadOptions::default()
        };
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(9100 + index as u64),
            vec![uri],
            options,
        )));
        let mut command = FtpDownloadCommand::new_with_group(
            Arc::clone(&group),
            output_dir.path().to_str(),
            None,
        )
        .unwrap();
        command.set_connection_pool(Arc::clone(&pool));
        tokio::time::timeout(Duration::from_secs(5), command.execute())
            .await
            .expect("FTP download should finish")
            .expect("FTP download should succeed");
        assert_eq!(
            std::fs::read(output_dir.path().join(filename)).unwrap(),
            b"data"
        );
        assert_eq!(pool.size().await, 1);
    }

    let (login_count, transfer_count) = tokio::time::timeout(Duration::from_secs(5), server)
        .await
        .expect("FTP fixture should finish")
        .expect("FTP fixture should not panic");
    assert_eq!(login_count, 1, "second download should reuse the login");
    assert_eq!(transfer_count, 2);
    let stats = pool.stats().await;
    assert_eq!(stats.connections_created, 1);
    assert_eq!(stats.connections_reused, 1);
}

#[tokio::test]
async fn ftp_reconnects_after_a_pooled_control_socket_goes_stale() {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    use tokio::net::TcpListener;

    let control_listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let control_address = control_listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let mut login_count = 0;
        for session_index in 0..2 {
            let (stream, _) =
                tokio::time::timeout(Duration::from_secs(5), control_listener.accept())
                    .await
                    .expect("FTP client should connect or recover from the stale session")
                    .unwrap();
            let mut control = BufReader::new(stream);
            control
                .get_mut()
                .write_all(b"220 local FTP fixture\r\n")
                .await
                .unwrap();
            let mut data_listener = None;
            let mut command = String::new();
            loop {
                if control.read_line(&mut command).await.unwrap() == 0 {
                    break;
                }
                let line = command.trim_end_matches(['\r', '\n']).to_owned();
                command.clear();
                match line
                    .split_once(' ')
                    .map_or((line.as_str(), ""), |(verb, arg)| (verb, arg))
                {
                    ("USER", _) => {
                        login_count += 1;
                        control
                            .get_mut()
                            .write_all(b"331 password required\r\n")
                            .await
                            .unwrap();
                    }
                    ("PASS", _) => {
                        control
                            .get_mut()
                            .write_all(b"230 logged in\r\n")
                            .await
                            .unwrap();
                    }
                    ("TYPE", _) => {
                        control
                            .get_mut()
                            .write_all(b"200 type set\r\n")
                            .await
                            .unwrap();
                    }
                    ("PWD", _) => {
                        control.get_mut().write_all(b"257 \"/\"\r\n").await.unwrap();
                    }
                    ("CWD", _) => {
                        control
                            .get_mut()
                            .write_all(b"250 directory changed\r\n")
                            .await
                            .unwrap();
                    }
                    ("SIZE", _) => {
                        control.get_mut().write_all(b"213 4\r\n").await.unwrap();
                    }
                    ("REST", _) => {
                        control
                            .get_mut()
                            .write_all(b"350 restart accepted\r\n")
                            .await
                            .unwrap();
                    }
                    ("EPSV", _) => {
                        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
                        let port = listener.local_addr().unwrap().port();
                        data_listener = Some(listener);
                        control
                            .get_mut()
                            .write_all(
                                format!("229 Entering Extended Passive Mode (|||{port}|)\r\n")
                                    .as_bytes(),
                            )
                            .await
                            .unwrap();
                    }
                    ("PASV", _) => {
                        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
                        let port = listener.local_addr().unwrap().port();
                        data_listener = Some(listener);
                        control
                            .get_mut()
                            .write_all(
                                format!(
                                    "227 Entering Passive Mode (127,0,0,1,{},{})\r\n",
                                    port / 256,
                                    port % 256
                                )
                                .as_bytes(),
                            )
                            .await
                            .unwrap();
                    }
                    ("RETR", _) => {
                        control
                            .get_mut()
                            .write_all(b"150 opening data connection\r\n")
                            .await
                            .unwrap();
                        let listener = data_listener.take().expect("EPSV must precede RETR");
                        let (mut data, _) = listener.accept().await.unwrap();
                        data.write_all(b"data").await.unwrap();
                        data.shutdown().await.unwrap();
                        control
                            .get_mut()
                            .write_all(b"226 transfer complete\r\n")
                            .await
                            .unwrap();
                        break;
                    }
                    other => panic!("unexpected FTP command: {other:?}"),
                }
            }
            // The first successful session is intentionally closed while idle.
            // The next task initially leases this stale entry, then must retry
            // over a fresh authenticated connection after its control read fails.
            drop(control);
            if session_index == 0 {
                tokio::time::sleep(Duration::from_millis(20)).await;
            }
        }
        login_count
    });

    let pool = Arc::new(crate::ftp::connection_pool::FtpConnectionPool::new(4));
    let output_dir = tempfile::tempdir().unwrap();
    for (index, filename) in ["first-stale.bin", "second-stale.bin"]
        .into_iter()
        .enumerate()
    {
        let uri = format!("ftp://stale-user:stale-pass@{control_address}/{filename}");
        let options = DownloadOptions {
            allow_overwrite: true,
            max_retries: 2,
            retry_wait: 0,
            ..DownloadOptions::default()
        };
        let group = Arc::new(std::sync::RwLock::new(RequestGroup::new(
            GroupId::new(9200 + index as u64),
            vec![uri],
            options,
        )));
        let mut command = FtpDownloadCommand::new_with_group(
            Arc::clone(&group),
            output_dir.path().to_str(),
            None,
        )
        .unwrap();
        command.set_connection_pool(Arc::clone(&pool));
        tokio::time::timeout(Duration::from_secs(5), command.execute())
            .await
            .expect("stale FTP session recovery should not hang")
            .expect("FTP retry should recover with a fresh control session");
        assert_eq!(
            std::fs::read(output_dir.path().join(filename)).unwrap(),
            b"data"
        );
    }

    assert_eq!(
        tokio::time::timeout(Duration::from_secs(5), server)
            .await
            .expect("FTP stale-session fixture should finish")
            .expect("FTP stale-session fixture should not panic"),
        2,
        "the replacement connection should authenticate again"
    );
    assert_eq!(pool.stats().await.connections_reused, 1);
}