1//! Vanguard manager configuration
23use std::time::Duration;
45use tor_netdir::params::NetParameters;
67use crate::VanguardMode;
89/// The default L2 pool size.
10const DEFAULT_L2_POOL_SIZE: usize = 4;
1112/// The default minimum lifetime of L2 guards.
13const DEFAULT_L2_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600 * 24);
1415/// The default maximum lifetime of L2 guards.
16const DEFAULT_L2_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 24 * 12);
1718/// The default L3 pool size.
19const DEFAULT_L3_POOL_SIZE: usize = 8;
2021/// The default minimum lifetime of L3 guards.
22const DEFAULT_L3_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600);
2324/// The default maximum lifetime of L3 guards.
25const DEFAULT_L3_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 48);
2627/// A set of parameters, derived from the consensus document,
28/// controlling the behavior of a [`VanguardMgr`](crate::vanguards::VanguardMgr).
29///
30/// Note: these are not part of [`VanguardConfig`](crate::VanguardConfig),
31/// because like all Tor network parameters,
32/// they can be overridden via the `TorClientConfig::override_net_params`.
33//
34// TODO(#1382): vanguards_enabled and vanguards_hs_service are currently unused,
35// because the vanguard mode is read from the VanguardConfig.
36#[derive(Debug, Clone, amplify::Getters)]
37pub struct VanguardParams {
38/// The type of vanguards to use by default when building onion service circuits.
39#[getter(as_copy)]
40vanguards_enabled: VanguardMode,
41/// If higher than `vanguards-enabled`,
42 /// and we are running an onion service,
43 /// we use this level for all our onion service circuits.
44#[getter(as_copy)]
45vanguards_hs_service: VanguardMode,
46/// The number of guards in the L2 guardset
47#[getter(as_copy)]
48l2_pool_size: usize,
49/// The minimum lifetime of L2 guards
50#[getter(as_copy)]
51l2_lifetime_min: Duration,
52/// The maximum lifetime of L2 guards
53#[getter(as_copy)]
54l2_lifetime_max: Duration,
55/// The number of guards in the L3 guardset
56#[getter(as_copy)]
57l3_pool_size: usize,
58/// The minimum lifetime of L3 guards
59#[getter(as_copy)]
60l3_lifetime_min: Duration,
61/// The maximum lifetime of L3 guards
62#[getter(as_copy)]
63l3_lifetime_max: Duration,
64}
6566impl Default for VanguardParams {
67fn default() -> Self {
68Self {
69 vanguards_enabled: VanguardMode::Lite,
70 vanguards_hs_service: VanguardMode::Full,
71 l2_pool_size: DEFAULT_L2_POOL_SIZE,
72 l2_lifetime_min: DEFAULT_L2_GUARD_LIFETIME_MIN,
73 l2_lifetime_max: DEFAULT_L2_GUARD_LIFETIME_MAX,
74 l3_pool_size: DEFAULT_L3_POOL_SIZE,
75 l3_lifetime_min: DEFAULT_L3_GUARD_LIFETIME_MIN,
76 l3_lifetime_max: DEFAULT_L3_GUARD_LIFETIME_MAX,
77 }
78 }
79}
8081impl TryFrom<&NetParameters> for VanguardParams {
82type Error = tor_units::Error;
8384fn try_from(p: &NetParameters) -> Result<VanguardParams, Self::Error> {
85/// Return a pair of `(min, max)` values representing a closed interval.
86 ///
87 /// If `min <= max`, returns `(min, max)`.
88 /// Otherwise, returns `(default_min, default_max)`.
89fn lifetime_or_default(
90 min: Duration,
91 max: Duration,
92 default_min: Duration,
93 default_max: Duration,
94 ) -> (Duration, Duration) {
95if min <= max {
96 (min, max)
97 } else {
98 (default_min, default_max)
99 }
100 }
101102let (l2_lifetime_min, l2_lifetime_max) = lifetime_or_default(
103 p.guard_hs_l2_lifetime_min.try_into()?,
104 p.guard_hs_l2_lifetime_max.try_into()?,
105 DEFAULT_L2_GUARD_LIFETIME_MIN,
106 DEFAULT_L2_GUARD_LIFETIME_MAX,
107 );
108109let (l3_lifetime_min, l3_lifetime_max) = lifetime_or_default(
110 p.guard_hs_l3_lifetime_min.try_into()?,
111 p.guard_hs_l3_lifetime_max.try_into()?,
112 DEFAULT_L3_GUARD_LIFETIME_MIN,
113 DEFAULT_L3_GUARD_LIFETIME_MAX,
114 );
115116Ok(VanguardParams {
117 vanguards_enabled: VanguardMode::from_net_parameter(p.vanguards_enabled),
118 vanguards_hs_service: VanguardMode::from_net_parameter(p.vanguards_hs_service),
119 l2_pool_size: p.guard_hs_l2_number.try_into()?,
120 l2_lifetime_min,
121 l2_lifetime_max,
122 l3_pool_size: p.guard_hs_l3_number.try_into()?,
123 l3_lifetime_min,
124 l3_lifetime_max,
125 })
126 }
127}