tor_guardmgr/vanguards/
config.rs

1//! Vanguard manager configuration
2
3use std::time::Duration;
4
5use tor_netdir::params::NetParameters;
6
7use crate::VanguardMode;
8
9/// The default L2 pool size.
10const DEFAULT_L2_POOL_SIZE: usize = 4;
11
12/// The default minimum lifetime of L2 guards.
13const DEFAULT_L2_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600 * 24);
14
15/// The default maximum lifetime of L2 guards.
16const DEFAULT_L2_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 24 * 12);
17
18/// The default L3 pool size.
19const DEFAULT_L3_POOL_SIZE: usize = 8;
20
21/// The default minimum lifetime of L3 guards.
22const DEFAULT_L3_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600);
23
24/// The default maximum lifetime of L3 guards.
25const DEFAULT_L3_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 48);
26
27/// 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)]
40    vanguards_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)]
45    vanguards_hs_service: VanguardMode,
46    /// The number of guards in the L2 guardset
47    #[getter(as_copy)]
48    l2_pool_size: usize,
49    /// The minimum lifetime of L2 guards
50    #[getter(as_copy)]
51    l2_lifetime_min: Duration,
52    /// The maximum lifetime of L2 guards
53    #[getter(as_copy)]
54    l2_lifetime_max: Duration,
55    /// The number of guards in the L3 guardset
56    #[getter(as_copy)]
57    l3_pool_size: usize,
58    /// The minimum lifetime of L3 guards
59    #[getter(as_copy)]
60    l3_lifetime_min: Duration,
61    /// The maximum lifetime of L3 guards
62    #[getter(as_copy)]
63    l3_lifetime_max: Duration,
64}
65
66impl Default for VanguardParams {
67    fn default() -> Self {
68        Self {
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}
80
81impl TryFrom<&NetParameters> for VanguardParams {
82    type Error = tor_units::Error;
83
84    fn 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)`.
89        fn lifetime_or_default(
90            min: Duration,
91            max: Duration,
92            default_min: Duration,
93            default_max: Duration,
94        ) -> (Duration, Duration) {
95            if min <= max {
96                (min, max)
97            } else {
98                (default_min, default_max)
99            }
100        }
101
102        let (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        );
108
109        let (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        );
115
116        Ok(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}