tor_guardmgr/vanguards/
config.rs1use std::time::Duration;
4
5use tor_netdir::params::NetParameters;
6
7use crate::VanguardMode;
8
9const DEFAULT_L2_POOL_SIZE: usize = 4;
11
12const DEFAULT_L2_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600 * 24);
14
15const DEFAULT_L2_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 24 * 12);
17
18const DEFAULT_L3_POOL_SIZE: usize = 8;
20
21const DEFAULT_L3_GUARD_LIFETIME_MIN: Duration = Duration::from_secs(3600);
23
24const DEFAULT_L3_GUARD_LIFETIME_MAX: Duration = Duration::from_secs(3600 * 48);
26
27#[derive(Debug, Clone, amplify::Getters)]
37pub struct VanguardParams {
38 #[getter(as_copy)]
40 vanguards_enabled: VanguardMode,
41 #[getter(as_copy)]
45 vanguards_hs_service: VanguardMode,
46 #[getter(as_copy)]
48 l2_pool_size: usize,
49 #[getter(as_copy)]
51 l2_lifetime_min: Duration,
52 #[getter(as_copy)]
54 l2_lifetime_max: Duration,
55 #[getter(as_copy)]
57 l3_pool_size: usize,
58 #[getter(as_copy)]
60 l3_lifetime_min: Duration,
61 #[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 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}