tor_relay_selection/
lib.rs1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
47#![deny(clippy::string_slice)] mod config;
51mod restriction;
52mod selector;
53mod target_port;
54mod usage;
55
56pub use config::RelaySelectionConfig;
57pub use restriction::{RelayExclusion, RelayRestriction};
58pub use selector::{RelaySelector, SelectionInfo};
59pub use target_port::TargetPort;
60pub use usage::RelayUsage;
61
62pub trait LowLevelRelayPredicate {
68 fn low_level_predicate_permits_relay(&self, relay: &tor_netdir::Relay<'_>) -> bool;
70}
71
72#[cfg(test)]
74pub(crate) mod testing {
75 #![allow(clippy::bool_assert_comparison)]
77 #![allow(clippy::clone_on_copy)]
78 #![allow(clippy::dbg_macro)]
79 #![allow(clippy::mixed_attributes_style)]
80 #![allow(clippy::print_stderr)]
81 #![allow(clippy::print_stdout)]
82 #![allow(clippy::single_char_pattern)]
83 #![allow(clippy::unwrap_used)]
84 #![allow(clippy::unchecked_time_subtraction)]
85 #![allow(clippy::useless_vec)]
86 #![allow(clippy::needless_pass_by_value)]
87 #![allow(clippy::string_slice)] use crate::{LowLevelRelayPredicate, RelaySelectionConfig};
91 use std::collections::HashSet;
92 use std::sync::LazyLock;
93 use tor_netdir::{NetDir, Relay, SubnetConfig};
94 use tor_netdoc::types::relay_flags::RelayFlag;
95
96 pub(crate) fn split_netdir<'a, P: LowLevelRelayPredicate>(
104 netdir: &'a NetDir,
105 pred: &P,
106 ) -> (Vec<Relay<'a>>, Vec<Relay<'a>>) {
107 let (yes, no): (Vec<_>, Vec<_>) = netdir
108 .relays()
109 .partition(|r| pred.low_level_predicate_permits_relay(r));
110 assert!(!yes.is_empty());
111 assert!(!no.is_empty());
112 (yes, no)
113 }
114
115 pub(crate) fn cfg() -> RelaySelectionConfig<'static> {
117 static STABLE_PORTS: LazyLock<HashSet<u16>> = LazyLock::new(|| [22].into_iter().collect());
118 RelaySelectionConfig {
119 long_lived_ports: &STABLE_PORTS,
120 subnet_config: SubnetConfig::default(),
121 }
122 }
123
124 pub(crate) fn testnet() -> NetDir {
126 tor_netdir::testnet::construct_custom_netdir(|idx, node, _| {
127 if idx % 7 == 0 {
128 node.rs.clear_flags(RelayFlag::Fast);
129 }
130 if idx % 5 == 0 {
131 node.rs.clear_flags(RelayFlag::Stable);
132 };
133 })
134 .unwrap()
135 .unwrap_if_sufficient()
136 .unwrap()
137 }
138}