rustydht_lib/dht/dht_settings.rs
1/// Struct that represents configuration for DHT that, in general, does
2/// not change after the DHT is started.
3///
4/// You may need one of these to pass into [DHTBuilder](crate::dht::DHTBuilder).
5///
6/// Use [DHTSettings::default()](crate::dht::DHTSettings::default) to create an instance with the
7/// 'recommended' defaults (which can be customized). Or use [DHTSettingsBuilder](crate::dht::DHTSettingsBuilder)
8/// to construct a customized one. DHTSettings has the [non_exhaustive](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute)
9/// attribute and can't be constructed directly.
10#[derive(Clone)]
11#[non_exhaustive]
12pub struct DHTSettings {
13 /// Number of bytes for token secrets for get_peers responses
14 pub token_secret_size: usize,
15
16 /// Max number of peers to provide in response to a get_peers.
17 /// Shouldn't be much higher than this as the entire response packet needs to be less than 1500
18 pub max_peers_response: usize,
19
20 /// Max number of info hashes to provide in response to a sample_infohashes request
21 pub max_sample_response: usize,
22
23 /// How often we claim to rotate our sample_infohashes response
24 pub min_sample_interval_secs: i32,
25
26 /// We'll ping the "routers" at least this often (we may ping more frequently if needed)
27 pub router_ping_interval_secs: u64,
28
29 /// We'll ping previously-verified nodes at least this often to re-verify them
30 pub reverify_interval_secs: u64,
31
32 /// Verified nodes that we don't reverify within this amount of time are dropped
33 pub reverify_grace_period_secs: u64,
34
35 /// New nodes have this long to respond to a ping before we drop them
36 pub verify_grace_period_secs: u64,
37
38 /// When asked to provide peers, we'll only provide ones that announced within this time
39 pub get_peers_freshness_secs: u64,
40
41 /// We'll think about sending a find_nodes request at least this often.
42 /// If we have enough nodes already we might not do it.
43 pub find_nodes_interval_secs: u64,
44
45 /// We won't send a periodic find_nodes request if we have at least this many unverified nodes
46 pub find_nodes_skip_count: usize,
47
48 /// Max number of torrents to store peers for
49 pub max_torrents: usize,
50
51 /// Max number of peers per torrent to store
52 pub max_peers_per_torrent: usize,
53
54 /// We'll think about pinging and pruning nodes at this interval
55 pub ping_check_interval_secs: u64,
56
57 /// Outgoing requests may be pruned after this many seconds
58 pub outgoing_request_prune_secs: u64,
59
60 /// We'll think about pruning outgoing requests at this interval
61 pub outgoing_reqiest_check_interval_secs: u64,
62
63 /// If true, we will set the read only flag in outgoing requests to prevent
64 /// other nodes from adding us to their routing tables. This is useful if
65 /// we're behind a restrictive NAT/firewall and can't accept incoming
66 /// packets from IPs that we haven't sent anything to.
67 pub read_only: bool,
68
69 /// Vector of hostnames/ports that the DHT will use as DHT routers for
70 /// bootstrapping purposes.
71 ///
72 /// E.g., "router.example.org:6881"
73 pub routers: Vec<String>,
74
75 /// The maximum number of packets that will be accepted from an IP (during a [period](crate::dht::DHTSettings::throttle_period_secs))
76 /// before packets from that IP will be temporarily ignored.
77 pub throttle_packet_count: usize,
78
79 /// The number of seconds that the throttler keeps a packet count record for an IP. If the IP sends more
80 /// than [throttle_packet_count](crate::dht::DHTSettings::throttle_packet_count) packets during this period
81 /// then packets from it will be blocked.
82 pub throttle_period_secs: u64,
83
84 /// If the throttler blocks an IP, it will be blocked for at least this amount of time. Every time the blocked
85 /// IP sends another packet, the counter is reset again
86 /// (up to a limit of [throttle_max_tracking_secs](crate::dht::DHTSettings::throttle_max_tracking_secs)).
87 pub throttle_naughty_timeout_secs: u64,
88
89 /// The maximum amount of time that the throttler will keep any record, even if the IP on the
90 /// record is still sending traffic.
91 pub throttle_max_tracking_secs: u64,
92}
93
94/// Returns DHTSettings with a default set of options.
95impl Default for DHTSettings {
96 fn default() -> Self {
97 DHTSettings {
98 token_secret_size: 10,
99 max_peers_response: 128,
100 max_sample_response: 50,
101 min_sample_interval_secs: 10,
102 router_ping_interval_secs: 900,
103 reverify_interval_secs: 14 * 60,
104 reverify_grace_period_secs: 15 * 60,
105 verify_grace_period_secs: 60,
106 get_peers_freshness_secs: 15 * 60,
107 find_nodes_interval_secs: 33,
108 find_nodes_skip_count: 32,
109 max_torrents: 50,
110 max_peers_per_torrent: 100,
111 ping_check_interval_secs: 10,
112 outgoing_request_prune_secs: 30,
113 outgoing_reqiest_check_interval_secs: 30,
114 read_only: false,
115 routers: vec![
116 "router.bittorrent.com:6881".to_string(),
117 "router.utorrent.com:6881".to_string(),
118 "dht.transmissionbt.com:6881".to_string(),
119 ],
120 throttle_packet_count: 10,
121 throttle_period_secs: 6,
122 throttle_naughty_timeout_secs: 60,
123 throttle_max_tracking_secs: 86400,
124 }
125 }
126}
127
128#[derive(Clone, Default)]
129/// Builder for DHTSettings
130pub struct DHTSettingsBuilder {
131 settings: DHTSettings,
132}
133
134macro_rules! make_builder_method {
135 ($prop:ident, $prop_type:ty) => {
136 pub fn $prop(mut self, $prop: $prop_type) -> Self {
137 self.settings.$prop = $prop;
138 self
139 }
140 };
141}
142
143impl DHTSettingsBuilder {
144 pub fn new() -> DHTSettingsBuilder {
145 Self::default()
146 }
147
148 make_builder_method!(token_secret_size, usize);
149 make_builder_method!(max_peers_response, usize);
150 make_builder_method!(max_sample_response, usize);
151 make_builder_method!(min_sample_interval_secs, i32);
152 make_builder_method!(router_ping_interval_secs, u64);
153 make_builder_method!(reverify_interval_secs, u64);
154 make_builder_method!(reverify_grace_period_secs, u64);
155 make_builder_method!(verify_grace_period_secs, u64);
156 make_builder_method!(get_peers_freshness_secs, u64);
157 make_builder_method!(find_nodes_interval_secs, u64);
158 make_builder_method!(find_nodes_skip_count, usize);
159 make_builder_method!(max_torrents, usize);
160 make_builder_method!(max_peers_per_torrent, usize);
161 make_builder_method!(ping_check_interval_secs, u64);
162 make_builder_method!(outgoing_request_prune_secs, u64);
163 make_builder_method!(outgoing_reqiest_check_interval_secs, u64);
164 make_builder_method!(read_only, bool);
165 make_builder_method!(routers, Vec<String>);
166 make_builder_method!(throttle_packet_count, usize);
167 make_builder_method!(throttle_period_secs, u64);
168 make_builder_method!(throttle_naughty_timeout_secs, u64);
169 make_builder_method!(throttle_max_tracking_secs, u64);
170
171 pub fn build(self) -> DHTSettings {
172 self.settings
173 }
174}