aria2-protocol 0.3.9

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
use crate::bittorrent::bencode::codec::BencodeValue;

#[derive(Debug, Clone)]
pub enum DhtMessageType {
    Query,
    Response,
    Error,
}

#[derive(Debug, Clone)]
pub struct DhtQueryMethod(pub String);

impl DhtQueryMethod {
    pub const PING: &'static str = "ping";
    pub const FIND_NODE: &'static str = "find_node";
    pub const GET_PEERS: &'static str = "get_peers";
    pub const ANNOUNCE_PEER: &'static str = "announce_peer";
    pub const GET: &'static str = "get";
    pub const PUT: &'static str = "put";
    pub const SAMPLE_INFOHASHES: &'static str = "sample_infohashes";
}

#[derive(Debug, Clone)]
pub struct DhtMessage {
    pub t: Vec<u8>,
    pub y: DhtMessageType,
    pub q: Option<DhtQueryMethod>,
    pub a: Option<BencodeValue>,
    pub r: Option<BencodeValue>,
    pub e: Option<(i64, String)>,
}

impl DhtMessage {
    pub fn new_query(tx_id: u32, method: &str, args: BencodeValue) -> Self {
        Self {
            t: tx_id.to_be_bytes().to_vec(),
            y: DhtMessageType::Query,
            q: Some(DhtQueryMethod(method.to_string())),
            a: Some(args),
            r: None,
            e: None,
        }
    }

    pub fn new_response(tx_id: Vec<u8>, result: BencodeValue) -> Self {
        Self {
            t: tx_id,
            y: DhtMessageType::Response,
            q: None,
            a: None,
            r: Some(result),
            e: None,
        }
    }

    pub fn new_error(tx_id: Vec<u8>, code: i64, msg: &str) -> Self {
        Self {
            t: tx_id,
            y: DhtMessageType::Error,
            q: None,
            a: None,
            r: None,
            e: Some((code, msg.to_string())),
        }
    }

    pub fn encode(&self) -> Vec<u8> {
        use std::collections::BTreeMap;
        let mut dict = BTreeMap::new();

        dict.insert(b"t".to_vec(), BencodeValue::Bytes(self.t.clone()));
        dict.insert(b"v".to_vec(), BencodeValue::Bytes(b"A2\x00\x03".to_vec()));
        dict.insert(
            b"y".to_vec(),
            BencodeValue::Bytes(match self.y {
                DhtMessageType::Query => b"q".to_vec(),
                DhtMessageType::Response => b"r".to_vec(),
                DhtMessageType::Error => b"e".to_vec(),
            }),
        );

        match &self.y {
            DhtMessageType::Query => {
                if let Some(ref method) = self.q {
                    dict.insert(
                        b"q".to_vec(),
                        BencodeValue::Bytes(method.0.clone().into_bytes()),
                    );
                }
                if let Some(ref args) = self.a {
                    dict.insert(b"a".to_vec(), args.clone());
                }
            }
            DhtMessageType::Response => {
                if let Some(ref result) = self.r {
                    dict.insert(b"r".to_vec(), result.clone());
                }
            }
            DhtMessageType::Error => {
                if let Some((code, msg)) = &self.e {
                    dict.insert(
                        b"e".to_vec(),
                        BencodeValue::List(vec![
                            BencodeValue::Int(*code),
                            BencodeValue::Bytes(msg.clone().into_bytes()),
                        ]),
                    );
                }
            }
        }

        BencodeValue::Dict(dict).encode()
    }

    pub fn decode(data: &[u8]) -> Result<Self, String> {
        let (root, _) = BencodeValue::decode(data)?;

        let t = root
            .dict_get(b"t")
            .and_then(|v| v.as_bytes())
            .map(|b| b.to_vec())
            .ok_or("Missing 't' field")?;

        let y_bytes = root
            .dict_get(b"y")
            .and_then(|v| v.as_bytes())
            .ok_or("Missing 'y' field")?;

        let y = match y_bytes {
            b"q" => DhtMessageType::Query,
            b"r" => DhtMessageType::Response,
            b"e" => DhtMessageType::Error,
            _ => return Err(format!("Invalid 'y' value: {:?}", y_bytes)),
        };

        match y {
            DhtMessageType::Query => {
                let q_str = root.dict_get_str("q").ok_or("Missing 'q' field")?;
                let args = root.dict_get(b"a").cloned();
                Ok(Self {
                    t,
                    y,
                    q: Some(DhtQueryMethod(q_str.to_string())),
                    a: args,
                    r: None,
                    e: None,
                })
            }
            DhtMessageType::Response => {
                let result = root.dict_get(b"r").ok_or("Missing 'r' field")?;
                result.as_dict().ok_or("Invalid 'r' field")?;
                let node_id = result
                    .dict_get(b"id")
                    .and_then(BencodeValue::as_bytes)
                    .ok_or("Missing 'r.id' field")?;
                if node_id.len() != 20 {
                    return Err("Invalid 'r.id' field length".to_string());
                }
                Ok(Self {
                    t,
                    y,
                    q: None,
                    a: None,
                    r: Some(result.clone()),
                    e: None,
                })
            }
            DhtMessageType::Error => {
                let err_val = root
                    .dict_get(b"e")
                    .and_then(|v| v.as_list())
                    .ok_or("Missing 'e' field")?;
                if err_val.len() != 2 {
                    return Err("Invalid error format".to_string());
                }
                let code = err_val[0].as_int().ok_or("Invalid error code")?;
                let msg = err_val[1].as_bytes().ok_or("Invalid error message")?;
                Ok(Self {
                    t,
                    y,
                    q: None,
                    a: None,
                    r: None,
                    e: Some((code, String::from_utf8_lossy(msg).into_owned())),
                })
            }
        }
    }

    pub fn is_query(&self) -> bool {
        matches!(self.y, DhtMessageType::Query)
    }
    pub fn is_response(&self) -> bool {
        matches!(self.y, DhtMessageType::Response)
    }
    pub fn is_error(&self) -> bool {
        matches!(self.y, DhtMessageType::Error)
    }
}

/// Encode a `SocketAddr` into BEP 0005 compact peer format.
///
/// - IPv4: 6 bytes (4 bytes IP + 2 bytes port, big-endian)
/// - IPv6: 18 bytes (16 bytes IP + 2 bytes port, big-endian)
///
/// The output is directly consumable by
/// [`crate::bittorrent::dht::compact::extract_compact_peers_from_response`].
pub fn encode_compact_peer(addr: std::net::SocketAddr) -> Vec<u8> {
    match addr {
        std::net::SocketAddr::V4(v4) => {
            let mut buf = Vec::with_capacity(6);
            buf.extend_from_slice(&v4.ip().octets());
            buf.extend_from_slice(&v4.port().to_be_bytes());
            buf
        }
        std::net::SocketAddr::V6(v6) => {
            let mut buf = Vec::with_capacity(18);
            buf.extend_from_slice(&v6.ip().octets());
            buf.extend_from_slice(&v6.port().to_be_bytes());
            buf
        }
    }
}

pub struct DhtMessageBuilder;

impl DhtMessageBuilder {
    pub fn ping(transaction_id: u32, sender_id: &[u8; 20]) -> DhtMessage {
        let mut args_dict = std::collections::BTreeMap::new();
        args_dict.insert(b"id".to_vec(), BencodeValue::Bytes(sender_id.to_vec()));
        DhtMessage::new_query(
            transaction_id,
            DhtQueryMethod::PING,
            BencodeValue::Dict(args_dict),
        )
    }

    pub fn find_node(transaction_id: u32, sender_id: &[u8; 20], target: &[u8; 20]) -> DhtMessage {
        let mut args_dict = std::collections::BTreeMap::new();
        args_dict.insert(b"id".to_vec(), BencodeValue::Bytes(sender_id.to_vec()));
        args_dict.insert(b"target".to_vec(), BencodeValue::Bytes(target.to_vec()));
        DhtMessage::new_query(
            transaction_id,
            DhtQueryMethod::FIND_NODE,
            BencodeValue::Dict(args_dict),
        )
    }

    pub fn get_peers(
        transaction_id: u32,
        sender_id: &[u8; 20],
        info_hash: &[u8; 20],
    ) -> DhtMessage {
        let mut args_dict = std::collections::BTreeMap::new();
        args_dict.insert(b"id".to_vec(), BencodeValue::Bytes(sender_id.to_vec()));
        args_dict.insert(
            b"info_hash".to_vec(),
            BencodeValue::Bytes(info_hash.to_vec()),
        );
        DhtMessage::new_query(
            transaction_id,
            DhtQueryMethod::GET_PEERS,
            BencodeValue::Dict(args_dict),
        )
    }

    /// Build an `announce_peer` query using the opaque token returned by
    /// `get_peers`.
    pub fn announce_peer_with_token(
        transaction_id: u32,
        sender_id: &[u8; 20],
        info_hash: &[u8; 20],
        port: u16,
        token: &[u8],
    ) -> DhtMessage {
        let mut args_dict = std::collections::BTreeMap::new();
        args_dict.insert(b"id".to_vec(), BencodeValue::Bytes(sender_id.to_vec()));
        args_dict.insert(
            b"info_hash".to_vec(),
            BencodeValue::Bytes(info_hash.to_vec()),
        );
        args_dict.insert(b"port".to_vec(), BencodeValue::Int(port as i64));
        args_dict.insert(b"token".to_vec(), BencodeValue::Bytes(token.to_vec()));
        DhtMessage::new_query(
            transaction_id,
            DhtQueryMethod::ANNOUNCE_PEER,
            BencodeValue::Dict(args_dict),
        )
    }

    // ==================== Response Builders ====================

    /// Build a ping response: `{"t":tx,"y":"r","r":{"id":self_id}}`.
    ///
    /// The `tx` parameter is the transaction ID from the original ping query
    /// and is echoed back verbatim per BEP 0005.
    pub fn ping_response(tx: &[u8], self_id: &[u8; 20]) -> DhtMessage {
        let mut r_dict = std::collections::BTreeMap::new();
        r_dict.insert(b"id".to_vec(), BencodeValue::Bytes(self_id.to_vec()));
        DhtMessage::new_response(tx.to_vec(), BencodeValue::Dict(r_dict))
    }

    /// Build a find_node response:
    /// `{"t":tx,"y":"r","r":{"id":self_id,"nodes":compact_nodes}}`.
    ///
    /// `compact_nodes` is a concatenation of 26-byte compact node entries
    /// (20 bytes node ID + 6 bytes IPv4 compact addr) per BEP 0005.
    pub fn find_node_response(tx: &[u8], self_id: &[u8; 20], compact_nodes: &[u8]) -> DhtMessage {
        Self::find_node_response_with_nodes_field(tx, self_id, b"nodes", compact_nodes)
    }

    /// Build a find_node response carrying IPv6 compact nodes under `nodes6`.
    pub fn find_node_response6(tx: &[u8], self_id: &[u8; 20], compact_nodes: &[u8]) -> DhtMessage {
        Self::find_node_response_with_nodes_field(tx, self_id, b"nodes6", compact_nodes)
    }

    fn find_node_response_with_nodes_field(
        tx: &[u8],
        self_id: &[u8; 20],
        nodes_field: &[u8],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        let mut r_dict = std::collections::BTreeMap::new();
        r_dict.insert(b"id".to_vec(), BencodeValue::Bytes(self_id.to_vec()));
        r_dict.insert(
            nodes_field.to_vec(),
            BencodeValue::Bytes(compact_nodes.to_vec()),
        );
        DhtMessage::new_response(tx.to_vec(), BencodeValue::Dict(r_dict))
    }

    /// Build a get_peers response carrying known peers:
    /// `{"t":tx,"y":"r","r":{"id":self_id,"token":token,"values":[...]}}`.
    ///
    /// Each peer in `peers` is encoded via [`encode_compact_peer`]
    /// (6 bytes for IPv4, 18 bytes for IPv6).
    pub fn get_peers_response_with_peers(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        peers: &[std::net::SocketAddr],
    ) -> DhtMessage {
        let mut r_dict = std::collections::BTreeMap::new();
        r_dict.insert(b"id".to_vec(), BencodeValue::Bytes(self_id.to_vec()));
        r_dict.insert(b"token".to_vec(), BencodeValue::Bytes(token.to_vec()));
        let values: Vec<BencodeValue> = peers
            .iter()
            .map(|p| BencodeValue::Bytes(encode_compact_peer(*p)))
            .collect();
        r_dict.insert(b"values".to_vec(), BencodeValue::List(values));
        DhtMessage::new_response(tx.to_vec(), BencodeValue::Dict(r_dict))
    }

    /// Build a get_peers response carrying both peers and IPv4 closest nodes.
    pub fn get_peers_response_with_peers_and_nodes(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        peers: &[std::net::SocketAddr],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        Self::get_peers_response_with_peers_and_nodes_field(
            tx,
            self_id,
            token,
            peers,
            b"nodes",
            compact_nodes,
        )
    }

    /// Build a get_peers response carrying both peers and IPv6 closest nodes.
    pub fn get_peers_response_with_peers_and_nodes6(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        peers: &[std::net::SocketAddr],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        Self::get_peers_response_with_peers_and_nodes_field(
            tx,
            self_id,
            token,
            peers,
            b"nodes6",
            compact_nodes,
        )
    }

    fn get_peers_response_with_peers_and_nodes_field(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        peers: &[std::net::SocketAddr],
        nodes_field: &[u8],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        let mut response = Self::get_peers_response_with_peers(tx, self_id, token, peers);
        let Some(BencodeValue::Dict(result)) = response.r.as_mut() else {
            unreachable!("get_peers response builder always creates a dictionary")
        };
        result.insert(
            nodes_field.to_vec(),
            BencodeValue::Bytes(compact_nodes.to_vec()),
        );
        response
    }

    /// Build a get_peers response carrying closest nodes (no peers known):
    /// `{"t":tx,"y":"r","r":{"id":self_id,"token":token,"nodes":compact_nodes}}`.
    pub fn get_peers_response_with_nodes(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        Self::get_peers_response_with_nodes_field(tx, self_id, token, b"nodes", compact_nodes)
    }

    /// Build a get_peers response carrying closest IPv6 nodes under `nodes6`.
    pub fn get_peers_response_with_nodes6(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        Self::get_peers_response_with_nodes_field(tx, self_id, token, b"nodes6", compact_nodes)
    }

    fn get_peers_response_with_nodes_field(
        tx: &[u8],
        self_id: &[u8; 20],
        token: &[u8],
        nodes_field: &[u8],
        compact_nodes: &[u8],
    ) -> DhtMessage {
        let mut r_dict = std::collections::BTreeMap::new();
        r_dict.insert(b"id".to_vec(), BencodeValue::Bytes(self_id.to_vec()));
        r_dict.insert(b"token".to_vec(), BencodeValue::Bytes(token.to_vec()));
        r_dict.insert(
            nodes_field.to_vec(),
            BencodeValue::Bytes(compact_nodes.to_vec()),
        );
        DhtMessage::new_response(tx.to_vec(), BencodeValue::Dict(r_dict))
    }

    /// Build an announce_peer response: `{"t":tx,"y":"r","r":{"id":self_id}}`.
    pub fn announce_peer_response(tx: &[u8], self_id: &[u8; 20]) -> DhtMessage {
        let mut r_dict = std::collections::BTreeMap::new();
        r_dict.insert(b"id".to_vec(), BencodeValue::Bytes(self_id.to_vec()));
        DhtMessage::new_response(tx.to_vec(), BencodeValue::Dict(r_dict))
    }

    /// Build a DHT error response: `{"t":tx,"y":"e","e":[code,message]}`.
    pub fn error_response(tx: &[u8], code: i64, message: &str) -> DhtMessage {
        DhtMessage::new_error(tx.to_vec(), code, message)
    }
}

#[cfg(test)]
mod tests;