dht_crawler/
types.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
4pub enum NetMode {
5    Ipv4Only,
6    Ipv6Only,
7    #[default]
8    DualStack,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct TorrentInfo {
13    pub info_hash: String,
14    pub magnet_link: String,
15    pub name: String,
16    pub total_size: u64,
17    pub files: Vec<FileInfo>,
18    pub piece_length: u64,
19    pub peers: Vec<String>,
20    pub timestamp: u64,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct FileInfo {
25    pub path: String,
26    pub size: u64,
27}
28
29impl TorrentInfo {
30    pub fn format_size(&self) -> String {
31        format_bytes(self.total_size)
32    }
33}
34
35impl FileInfo {
36    pub fn format_size(&self) -> String {
37        format_bytes(self.size)
38    }
39}
40
41fn format_bytes(bytes: u64) -> String {
42    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
43    let mut size = bytes as f64;
44    let mut unit_index = 0;
45
46    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
47        size /= 1024.0;
48        unit_index += 1;
49    }
50
51    format!("{:.2} {}", size, UNITS[unit_index])
52}
53
54#[derive(Debug, Clone)]
55pub struct DHTOptions {
56    pub port: u16,
57
58    pub auto_metadata: bool,
59
60    pub metadata_timeout: u64,
61
62    pub max_metadata_queue_size: usize,
63
64    pub max_metadata_worker_count: usize,
65
66    pub netmode: NetMode,
67
68    pub node_queue_capacity: usize,
69}
70
71impl Default for DHTOptions {
72    fn default() -> Self {
73        Self {
74            port: 6881,
75            auto_metadata: true,
76            metadata_timeout: 3,
77            max_metadata_queue_size: 100000,
78            max_metadata_worker_count: 1000,
79            netmode: NetMode::Ipv4Only,
80            node_queue_capacity: 100000,
81        }
82    }
83}