1
//! Vanguard manager configuration
2

            
3
use std::time::Duration;
4

            
5
use tor_netdir::params::NetParameters;
6

            
7
use crate::VanguardMode;
8

            
9
/// The default L2 pool size.
10
const DEFAULT_L2_POOL_SIZE: usize = 4;
11

            
12
/// The default minimum lifetime of L2 guards.
13
const DEFAULT_L2_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600 * 24);
14

            
15
/// The default maximum lifetime of L2 guards.
16
const DEFAULT_L2_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 24 * 12);
17

            
18
/// The default L3 pool size.
19
const DEFAULT_L3_POOL_SIZE: usize = 8;
20

            
21
/// The default minimum lifetime of L3 guards.
22
const DEFAULT_L3_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600);
23

            
24
/// The default maximum lifetime of L3 guards.
25
const 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)]
37
pub 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

            
66
impl Default for VanguardParams {
67
2661
    fn default() -> Self {
68
2661
        Self {
69
2661
            vanguards_enabled: VanguardMode::Lite,
70
2661
            vanguards_hs_service: VanguardMode::Full,
71
2661
            l2_pool_size: DEFAULT_L2_POOL_SIZE,
72
2661
            l2_lifetime_min: DEFAULT_L2_GUARD_LIFETIME_MIN,
73
2661
            l2_lifetime_max: DEFAULT_L2_GUARD_LIFETIME_MAX,
74
2661
            l3_pool_size: DEFAULT_L3_POOL_SIZE,
75
2661
            l3_lifetime_min: DEFAULT_L3_GUARD_LIFETIME_MIN,
76
2661
            l3_lifetime_max: DEFAULT_L3_GUARD_LIFETIME_MAX,
77
2661
        }
78
2661
    }
79
}
80

            
81
impl TryFrom<&NetParameters> for VanguardParams {
82
    type Error = tor_units::Error;
83

            
84
2112
    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
4224
        fn lifetime_or_default(
90
4224
            min: Duration,
91
4224
            max: Duration,
92
4224
            default_min: Duration,
93
4224
            default_max: Duration,
94
4224
        ) -> (Duration, Duration) {
95
4224
            if min <= max {
96
4224
                (min, max)
97
            } else {
98
                (default_min, default_max)
99
            }
100
4224
        }
101

            
102
2112
        let (l2_lifetime_min, l2_lifetime_max) = lifetime_or_default(
103
2112
            p.guard_hs_l2_lifetime_min.try_into()?,
104
2112
            p.guard_hs_l2_lifetime_max.try_into()?,
105
            DEFAULT_L2_GUARD_LIFETIME_MIN,
106
            DEFAULT_L2_GUARD_LIFETIME_MAX,
107
        );
108

            
109
2112
        let (l3_lifetime_min, l3_lifetime_max) = lifetime_or_default(
110
2112
            p.guard_hs_l3_lifetime_min.try_into()?,
111
2112
            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
2112
            vanguards_enabled: VanguardMode::from_net_parameter(p.vanguards_enabled),
118
2112
            vanguards_hs_service: VanguardMode::from_net_parameter(p.vanguards_hs_service),
119
2112
            l2_pool_size: p.guard_hs_l2_number.try_into()?,
120
2112
            l2_lifetime_min,
121
2112
            l2_lifetime_max,
122
2112
            l3_pool_size: p.guard_hs_l3_number.try_into()?,
123
2112
            l3_lifetime_min,
124
2112
            l3_lifetime_max,
125
        })
126
2112
    }
127
}